cross-platform c questions

C

chewie54

Hi All,

When writing a python c extension for needs to be compiled for
Windows, Linux, and the Mac,
what cross-platform differences need to be accounted for? Are there
functions in the python api to deal with the differences? For
example, byte ordering, how is that controlled?


Thanks in advance,
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

When writing a python c extension for needs to be compiled for
Windows, Linux, and the Mac,
what cross-platform differences need to be accounted for?#

From a Python point of view, it's primarily the difference in the
size of types. For example, long may vary across platforms, and
so changes the result value range for PyInt_AsLong.

Another issue is the mechanism to export things from a shared
library; on Windows, you have to use __declspec(export).

Yet another issue the question what basic functions are available
in the system (e.g. whether strdup is available)
Are there
functions in the python api to deal with the differences? For
example, byte ordering, how is that controlled?

Not functions, no, but macros and typedefs. For example, there is
now a typedef Py_ssize_t that you should use if you measure the
number of bytes (or, more generally, things in a collection).

For the __declspec(export) thing, there are the PyAPI_FUNC and
PyMODINIT_FUNC macros.

For detecting platform-specific details, autoconf is used,
which defines things like HAVE_STRDUP. autoconf also defines
WORDS_BIGENDIAN if the system uses the bigendian byte order.

Regards,
Martin
 
G

Gabriel Genellina

Not functions, no, but macros and typedefs. For example, there is
now a typedef Py_ssize_t that you should use if you measure the
number of bytes (or, more generally, things in a collection).

Is there any rules/criteria to decide when to use Py_ssize_t, int, or
long? I've seen them somewhat mixed and don't know when exactly to use
Py_ssize_t.
 
?

=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=

Is there any rules/criteria to decide when to use Py_ssize_t, int, or
long? I've seen them somewhat mixed and don't know when exactly to use
Py_ssize_t.

You should use Py_ssize_t when you are counting things, and when there
is no small limit (e.g. 66536) to the maximum number of things you can
have. More precisely, you should use it if the maximum number of things
you could possibly have correlates to the available address space.

Regards,
Martin
 
G

Gabriel Genellina

You should use Py_ssize_t when you are counting things, and when there
is no small limit (e.g. 66536) to the maximum number of things you can
have. More precisely, you should use it if the maximum number of things
you could possibly have correlates to the available address space.

It's perfectly clear now, thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top