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

Mark Dickinson report at bugs.python.org
Tue Oct 14 21:29:04 CEST 2008


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

One more minor deficiency in the patch: it gives incorrect results for 
very large integers.  For example, on a 32-bit build of the trunk:

>>> x = 1 << 2**31-1
>>> x <<= 2**31-1
>>> x.numbits()  # expect 4294967295
4294967295L
>>> x <<= 2
>>> x.numbits()  # expect 4294967297
4294967295L

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.

Alternatively, in case of OverflowError one could recompute numbits 
correctly, without overflow, by using Python longs instead of a C size_t;  
but this would mean adding little-used, and probably little-tested, extra 
code for what must be a very rare special case.  Probably not worth it.

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


More information about the Python-bugs-list mailing list