473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to avoid overflow errors

I thought that overflow errors would be a thing of the past now that
Python automatically converts ints to longs as needed. Unfortunately,
that is not the case.
>>class MyInt(int):
.... pass
....
>>MyInt(sys.maxint)
2147483647
>>MyInt(sys.maxint+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
How do I subclass int and/or long so that my class also auto-converts
only when needed?

--
Steven.
Sep 15 '07 #1
8 4748
On 9/14/07, Steven D'Aprano <st***@remove-this-cybersource.com.auwrote:
I thought that overflow errors would be a thing of the past now that
Python automatically converts ints to longs as needed. Unfortunately,
that is not the case.
>class MyInt(int):
... pass
...
>MyInt(sys.maxint)
2147483647
>MyInt(sys.maxint+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int

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
Thanks to your mail, only now I see how important this change
(int/liong unification) really is!

How do I subclass int and/or long so that my class also auto-converts
only when needed?
What about just subclassing long - is this not an option?

Steven.
--
http://mail.python.org/mailman/listinfo/python-list

--
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
Sep 15 '07 #2
"Eduardo O. Padoan" <ed************@gmail.comwrites:
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()
Sep 15 '07 #3
Steven D'Aprano wrote:
I thought that overflow errors would be a thing of the past now that
Python automatically converts ints to longs as needed. Unfortunately,
that is not the case.
>>>class MyInt(int):
... pass
...
>>>MyInt(sys.maxint)
2147483647
>>>MyInt(sys.maxint+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
How do I subclass int and/or long so that my class also auto-converts
only when needed?
Use __new__.

pyimport sys
py>
pyclass MyLong(long):
.... pass
....
pyclass MyInt(int):
.... def __new__(cls, *args, **kwargs):
.... try:
.... return int.__new__(cls, *args, **kwargs)
.... except OverflowError:
.... return MyLong(*args, **kwargs)
....
pyMyInt(sys.maxint**2)
4611686014132420609L
pytype(_)
<class '__main__.MyLong'>
James
Sep 15 '07 #4
On 14 Sep 2007 18:08:00 -0700, Paul Rubin
<"http://phr.cx"@nospam.invalidwrote:
"Eduardo O. Padoan" <ed************@gmail.comwrites:
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.

--
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
Sep 15 '07 #5
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.invalidwrote:
>"Eduardo O. Padoan" <ed************@gmail.comwrites:
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
Sep 15 '07 #6
On Fri, 14 Sep 2007 21:43:38 -0300, Eduardo O. Padoan wrote:
>How do I subclass int and/or long so that my class also auto-converts
only when needed?

What about just subclassing long - is this not an option?
Of course it's an option. As it turned out, that was the easiest way for
me to proceed. I was a little concerned that the overhead of using longs
might have been excessive, but for values close to zero (and by close I
mean < millions) there's no significant time difference between
arithmetic with ints and longints.
--
Steven.
Sep 15 '07 #7
On Fri, 14 Sep 2007 18:19:45 -0700, James Stroud wrote:
>How do I subclass int and/or long so that my class also auto-converts
only when needed?

Use __new__.
The disadvantage of that is that your example code requires me to
duplicate my methods in the long version and the int version. It's easy
enough to work around that (class factory function) but it just seems all
rather untidy...
--
Steven.
Sep 15 '07 #8
On 9/15/07, Carl Banks <pa************@gmail.comwrote:
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.invalidwrote:
"Eduardo O. Padoan" <ed************@gmail.comwrites:
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.
No one is denying that by now :)
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.
Agreed. I was lookind at itertoolsmodule.c, and even for my very
limited C knowlegment, it does not seem to be too hard.
Anyway: http://bugs.python.org/issue1165

Carl Banks
--
http://mail.python.org/mailman/listinfo/python-list

--
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
Sep 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Could someone please give me a hand? I have created a navigation bar for a Web site I assist with. Everything works fine if I use the files while offline, i.e., the files that I created are...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
5
by: Tom Szabo | last post by:
Hi, Is it possible to have only vertical scrolling enabled on a DIV? style="BORDER:1px ; OVERFLOW: auto; WIDTH:799px; HEIGHT:249px;position: absolute; top:0; left:0; " will have both...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
1
by: Chua Wen Ching | last post by:
Hi there. Sorry for the late reply. Thanks for Justin, Jon Skeet, Jack Hanebach and Ann Marinas replies. Actually i am testing a code from Swarm.Net by Mike Gold. I rewrote the same code...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
7
by: semut | last post by:
Given that the string is of null-terminated type. What could be the possible causes (by experience) the string to have no null character (\0) and cause buffer overflow later. I know it is quite...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
4
by: szimek | last post by:
Hi, I've already posted an email with this problem, but this time I think I got a bit more info. The app I'm currently working on works like this: when user clicks on a clickable element, it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.