472,986 Members | 3,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

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 3557
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter | last post by:
L.S. I am developing a PHP-login script (on Lycos Tripod) that uses Session to pass on variables. Below is the entire (stripped) structure that I use. It opens a page where you can Set and Read...
3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
4
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work...
7
by: Tom | last post by:
Hi Is this a conditional ? what is the structure of the statement? ch Tom
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
10
by: Antoine | last post by:
I can't work out what is causing this problem. Can anyone suggest what the typical causes beyond the obvious might be? Could you get it with datasets? Maybe I should run in debug mode and test...
5
by: Jason | last post by:
I've got a small form (400 X 310) and I want it to start up in the lower right corner of the screen. Is there a way I can easily do this? Thanks
1
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. ...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.