[Pyrex] converting python objects to 'unsigned'

Bob Ippolito bob at redivi.com
Wed Mar 23 22:30:16 CET 2005


On Mar 23, 2005, at 3:13 PM, Phil Frost wrote:

> I have a method as such:
>
> def foo(unsigned i):
>     pass
>
> Looking at the generated C, Pyrex is using "i" for unsigned in
> PyArg_ParseTupleAndKeywords. This has two undesirable effects:
>
> foo(-16)
> # raises no exception, and makes i = 4294967280
>
> foo(4026531840L)
> # raises OverflowError, although 4026531840 is well within the range of
> # an unsigned.
>
> Looking at the Python C API, there doesn't seem to be a good solution.
> Ideas?

Did you read <http://docs.python.org/api/intObjects.html> ?

unsigned long PyInt_AsUnsignedLongMask(PyObject *io)

Will first attempt to cast the object to a PyIntObject or  
PyLongObject, if it is not already one, and then return its  value as 
unsigned long. This function does not check for overflow.  New in 
version 2.3.

-bob




More information about the Pyrex mailing list