469,289 Members | 2,127 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

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

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.
>>import mini
mini.foo()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
SystemError: bad format char passed to Py_BuildValue
>>>
Is Python's documentation wrong (I hope not)?
Or, have I missed anything?

Tanks in advance,
Martin
Aug 21 '06 #1
2 3055
Martin Kulas <co**********@web.dewrote:
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.
>>import mini
mini.foo()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
SystemError: bad format char passed to Py_BuildValue
>>>

$ 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.
>>import mini
mini.foo()
3735928495L
>>>
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 21 '06 #2
Nick Craig-Wood wrote:
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

Aug 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Peter | last post: by
4 posts views Thread by Das | last post: by
7 posts views Thread by Tom | last post: by
5 posts views Thread by Jason | last post: by
11 posts views Thread by Jim | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.