473,498 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Jan 27 '06 #1
2 1590
Alexander Schmolck <a.********@gmail.com> writes:
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
Jan 27 '06 #2
Alexander Schmolck wrote:
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.

Jan 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1636
by: Justin Johnson | last post by:
Hello, I was wondering if anyone has seen this behavior before for the process.py module available at http://starship.python.net/crew/tmick/. I've been using it quite well on windows 2000...
6
1510
by: John J. Lee | last post by:
I'm tearing my hair out at what seems like weird import behaviour I'm getting from Python's stdlib test script, regrtest.py (not for the first time: seem to have forgotten the resolution from last...
6
3802
by: zelzel.zsu | last post by:
I am a new learner of Python Programming Language. Now. I am reading a book. In the section relating to module, I see an example. the directory tree looks like below: root\ system1\...
9
2968
by: svenn.are | last post by:
Hi, has anybody thought of / already used graphviz to convert the output of trace.py into a graph? I looked at PyUMLGraph, but 1. PyUMLGraph does not successfully create a dot file, and 2. I...
1
2459
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
3
5779
by: Sandro Dentella | last post by:
Hi everybody, I'm trying to fix the packaging of a very simple package, but some problem show me that I have not well understood the whole mechanism The structure of my package (some debug...
1
3163
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
5
5229
by: TiNo | last post by:
Hi, I have installed python two days ago on a USB memory stick (I am on the move and have no laptop.) I am on windows computers, mostly XP, all the time. Now, after pluging it in to a...
9
2430
by: korean_dave | last post by:
From command Prompt, i type in a script, "tryme.py". This, instead, brings up PythonWin editor and Interactive Window. Path variable is "C:\Python24". (I need Python 2.4 installed, not 2.5) ...
0
2182
by: rajasankar | last post by:
Hi, I am using Jython based application and trying to use inspect.py in the python files. Here is my code import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): ...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.