py-ext: casting pointers to ints on 32bit and 64bit systems

A

Alexander Schmolck

what's the best approach to write C(++)-extension code that has to create a
python int from a C pointer and vice versa so that it works smoothly on 32 bit
and 64 platforms (on which sizeof(int) != sizeof(*void)) equally work (under
unix,mac&windows and with gcc, vc and borland)?

Currently the relevant code (in mlabraw.cpp available from
<http://mlabwrap.sf.net>) looks like somthing like this:

/* C++ -> py */
Engine *ep;
[...]
return Py_BuildValue("i", int(ep))

and this:

/* py -> C++ */
int lHandle;
[...]
PyArg_ParseTuple(args, "is:get", &lHandle, &lName)

I've suggested modifications along the following lines to a user who had
problems building the module on a 64 bit system

/* C++ -> py */
Engine *ep;
[...]
return Py_BuildValue("l", long(ep))

/* py -> C++ */
long lHandle;
[...]
PyArg_ParseTuple(args, "ls:get", &lHandle, &lName)

This apparently works, so I'm now looking for the best way to adapt the source
code so that it will (ideally) compile out of the box on both 32 bit and 64
bit systems.

I guess the right type to use would be intptr_t (rather than #define something
to be int or long according to platform), but I don't know how well that is
supported by various C++ compilers. Also, is there something better than

#ifdef _A_64_BIT_PLATFORM
PyArg_ParseTuple(args, "ls:get", &lHandle, &lName)
#else
PyArg_ParseTuple(args, "is:get", &lHandle, &lName)
#endif
?

and how would I best go about #define'ing _A_64_BIT_PLATFORM?

thanks,

'as
 
T

Thomas Heller

Alexander Schmolck said:
what's the best approach to write C(++)-extension code that has to
create a python int from a C pointer and vice versa so that it works
smoothly on 32 bit and 64 platforms (on which sizeof(int) !=
sizeof(*void)) equally work (under unix,mac&windows and with gcc, vc
and borland)?

PyLong_FromVoidPtr and PyLong_AsVoidPtr.

Thomas
 
D

David Rushby

Alexander said:
what's the best approach to write C(++)-extension code that has to create a
python int from a C pointer and vice versa so that it works smoothly on 32 bit
and 64 platforms (on which sizeof(int) != sizeof(*void)) equally work (under
unix,mac&windows and with gcc, vc and borland)?

Do you really need to represent the pointers as integers, or do you
just need to pass them through Python and back to C? If the latter,
how about using PyCObject_FromVoidPtr/PyCObject_AsVoidPtr
(http://docs.python.org/api/cObjects.html )? PyCObject is probably
aimed squarely at the task you're doing (passing an opaque handle to a
C object from C into Python and back to C).
I've suggested modifications along the following lines to a user who had
problems building the module on a 64 bit system

/* C++ -> py */
Engine *ep;
[...]
return Py_BuildValue("l", long(ep))

/* py -> C++ */
long lHandle;
[...]
PyArg_ParseTuple(args, "ls:get", &lHandle, &lName)

I haven't actually done any Win64 programming, but I've read that on
that platform, sizeof(long) == 4, but sizeof(void *) == 8, so the C++
code above won't work on that platform. BTW, Visual C++ 7 and 8 have a
helpful /Wp64 switch that'll emit Win64 portability warnings.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top