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

Martin Kulas coolazNOSPAM at web.de
Mon Aug 21 05:44:01 EDT 2006


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





More information about the Python-list mailing list