How to avoid overflow errors
Carl Banks
pavlovevidence at gmail.com
Sat Sep 15 00:54:09 EDT 2007
On Fri, 14 Sep 2007 22:59:13 -0300, Eduardo O. Padoan wrote:
> On 14 Sep 2007 18:08:00 -0700, Paul Rubin
> <"http://phr.cx"@nospam.invalid> wrote:
>> "Eduardo O. Padoan" <eduardo.padoan at gmail.com> writes:
>> > Not totally unrelated, but in Py3k, as it seems, overflows are really
>> > things of the past:
>> >
>> >
>> > Python 3.0a1 (py3k:58061, Sep 9 2007, 13:18:37) [GCC 4.1.3 20070831
>> > (prerelease) (Ubuntu 4.1.2-16ubuntu1)] on linux2 Type "help",
>> > "copyright", "credits" or "license" for more information.
>> > >>> class MyInt(int):
>> > ... pass
>> > ...
>> > >>> import sys
>> > >>> MyInt(sys.maxint)
>> > 2147483647
>> > >>> MyInt(sys.maxint+1)
>> > 2147483648
>>
>> I'd be interested in knowing what happens in 3.0a1 with
>>
>> a = itertools.count(sys.maxint)
>> print a.next()
>> print a.next()
>
>
>>>> print(next(a))
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> OverflowError: cannot count beyond PY_SSIZE_T_MAX
>
> Hum, you've got me there. it is the same as in 2.x. Maybe the message
> should be less crypt, at least - nothing that googling for
> PY_SSIZE_T_MAX cant help.
This should demonstrate that OverflowError will not disappear entirely
even in Python 3.
Even if we were to get rid of all OverflowErrors resulting from integer
operations in Python, there are going to be some C extensions--including
some in the Python standard library--that will store 32-bit integers.
Modules such as array and struct will still raise it, and probably other
modules were a long integer sometimes doesn't make sense (socket, for
instance). itertools.count() should work for arbitrary integers, though.
Carl Banks
More information about the Python-list
mailing list