[issue5175] negative PyLong -> C unsigned integral, TypeError or OverflowError?

Mark Dickinson report at bugs.python.org
Sun Feb 8 13:45:41 CET 2009


Mark Dickinson <dickinsm at gmail.com> added the comment:

This also affects 3.1.

Note that the current behaviour (or rather, its effects in 
PyLong_AsUnsignedLongLong) is as documented.  In

http://docs.python.org/dev/c-api/long.html

it says for PyLong_AsUnsignedLongLong:

"Return a C unsigned long long from a Python long integer. If pylong 
cannot be represented as an unsigned long long, an OverflowError will be 
raised if the value is positive, or a TypeError will be raised if the 
value is negative."

...which suggests that the choice of TypeError was intentional.  It 
still seems wrong to me, though: the argument has the correct type, but 
an illegal value, so ValueError or (for consistency with the other 
methods) OverflowError would seem more appropriate.

If this change is made, then test_struct needs fixing: the change 
affects the 'Q' struct format.  I don't think the change in struct 
behaviour is serious, since there's very little consistency between 
different struct types at the moment:  for negative ints, 'Q' raises an 
error, 'L' and 'I' give a DeprecationWarning, and 'H' raises a 
struct.error.

----------
priority:  -> normal
versions: +Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5175>
_______________________________________


More information about the Python-bugs-list mailing list