Python/C: unsigned Py_BuildValue, PyInt_FromLong

M

Mike C. Fletcher

You're likely looking for:

PyLong_FromUnsignedLong
http://www.python.org/doc/2.3.3/api/longObjects.html

Unsigned ints can be longer than Python 32-bit signed integers, so
either do a check for the high-bit being set and use
PyLong_FromUnsignedLong for that case and PyInt_FromLong otherwise, or
just always use PyLong_FromUnsignedLong and eat the slight performance
penalty of having longs everywhere.

HTH,
Mike

Christian said:
Hi comp.lang.python readers,

are there routines to create a Python integer from unsigned C-integers?

I only could find Py_BuildValue[1] and PyInt_FromLong[2] which both only use
signed c-integers. Is there another way I overlooked?

[1]: http://www.python.org/doc/2.3.3/api/arg-parsing.html
ParseValue knows "k" for unsigned long, BuildValue doesn't.

[2]: http://www.python.org/doc/2.3.3/api/intObjects.html
PyInt_FromLong only uses a signed integer.

Chris

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
C

Christian Vogel

Mike said:
PyLong_FromUnsignedLong
http://www.python.org/doc/2.3.3/api/longObjects.html
Unsigned ints can be longer than Python 32-bit signed integers, so
either do a check for the high-bit being set and use
PyLong_FromUnsignedLong for that case and PyInt_FromLong otherwise, or
just always use PyLong_FromUnsignedLong and eat the slight performance
penalty of having longs everywhere.

Yes, now that you mention it, it's clear to me. Thanks for your help!

Chris
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top