Py_BuildValue("I", ...) does not work

M

Martin Kulas

Hallo!

I have a problem with Py_BuildValue: I want to convert an unsigned int
to a PyObject *.
http://docs.python.org/api/arg-parsing.html says that I can use
"I" as a format string.
But it does not work :-\

Here is my simplified code:

$ cat -n mini.c
1 #include <Python.h>
2
3 static PyObject *
4 mini_foo(PyObject *self, PyObject *args)
5 {
6 /* should be 3735928495 not -559038801 */
7 unsigned int v = 0xdeadbeafL; /* byte representation */
8
9 return Py_BuildValue("I", v);
10 }
11
12 static PyMethodDef
13 mini_methods[] = {
14 { "foo", mini_foo, METH_NOARGS,
15 "bla." },
16 { NULL, NULL, 0, NULL }
17 };
18
19 PyMODINIT_FUNC
20 initmini(void)
21 {
22 Py_InitModule("mini", mini_methods);
23 }
$ cat -n setup.py
1 from distutils.core import setup, Extension
2
3 module1 = Extension('mini', sources = ['mini.c'])
4
5 setup(name = 'mini',
6 ext_modules = [ module1] )
7
$ python setup.py build
running build
running build_ext
building 'mini' extension
cc -pthread -fno-strict-aliasing -DNDEBUG -O2 -pipe
-DTHREAD_STACK_SIZE=0x20000 -fPIC -fPIC -I/usr/local/include/python2.4
-c mini.c -o build/temp.openbsd-3.9-i386-2.4/mini.o
cc -pthread -shared -fPIC -L/usr/obj/i386/python-2.4.2p0/Python-2.4.2
build/temp.openbsd-3.9-i386-2.4/mini.o -o
build/lib.openbsd-3.9-i386-2.4/mini.so

$ cd build/lib.openbsd-3.9-i386-2.4
$ ls
mini.so
$ python
Python 2.4.2 (#1, Mar 2 2006, 14:17:22)
[GCC 3.3.5 (propolice)] on openbsd3
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

Is Python's documentation wrong (I hope not)?
Or, have I missed anything?

Tanks in advance,
Martin
 
N

Nick Craig-Wood

Martin Kulas said:
I have a problem with Py_BuildValue: I want to convert an unsigned int
to a PyObject *.
http://docs.python.org/api/arg-parsing.html says that I can use
"I" as a format string.
But it does not work :-\

I tried your examples under linux.

It fails under python 2.3 in the same fashion as you noted, but works
under 2.4.

On my system (debian/testing) I have python 2.4.4c0 installed.

Handling of these sized integers > 0x80000000 changed from 2.3 to 2.4
so maybe this was a fixed bug?

I can't seem to find the point release changelogs though!

$ python2.3 setup.py build
$ cd build/lib.linux-i686-2.3/
$ python2.3
Python 2.3.5 (#2, Jun 13 2006, 23:12:55)
[GCC 4.1.2 20060613 (prerelease) (Debian 4.1.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last):


$ python2.4 setup.py build
$ cd build/lib.linux-i686-2.4
$ python2.4
Python 2.4.4c0 (#2, Jun 14 2006, 22:35:41)
[GCC 4.1.2 20060613 (prerelease) (Debian 4.1.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 
M

Martin Kulas

Nick said:
I tried your examples under linux.

It fails under python 2.3 in the same fashion as you noted, but works
under 2.4.

On my system (debian/testing) I have python 2.4.4c0 installed.

I was running Python 2.4.2 -- this was the problem!
After Installing 2.4.3 Py_BuildValue() eats "I"
as format string without any problems :)

Regards,
Martin
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top