[issue3439] create a numbits() method for int and long types

STINNER Victor report at bugs.python.org
Tue Nov 4 19:16:00 CET 2008

STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> It would be nicer if the OverflowError from _PyLong_NumBits 
> were propagated, so that the second case raises OverflowError
> instead of giving an incorrect result

Why not, but I prefer your second proposition: return a long integer. 
Attached patch implements this solution.

>>> x=1<<(2**31-1)
>>> n=x.numbits(); n, n.numbits()
(2147483648L, 32L)
>>> x<<=(2**31-1)
>>> n=x.numbits(); n, n.numbits()
(4294967295L, 32L)
>>> x<<=1
>>> n=x.numbits(); n, n.numbits()
(4294967296L, 33L) # yeah!

With my patch, there are two functions:
 - _PyLong_NumBits(long)->size_t: may overflow
 - long_numbits(long)->long: don't raise overflow error, but may raise 
other errors like memory error

Added file: http://bugs.python.org/file11939/numbits-2.diff

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list