[Python-bugs-list] [ python-Bugs-697591 ] string.atoi function causing TypeError
SourceForge.net
noreply@sourceforge.net
Tue, 11 Mar 2003 14:39:03 -0800
Bugs item #697591, was opened at 2003-03-04 17:51
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=697591&group_id=5470
Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Jim Murff (jmurff)
Assigned to: Raymond Hettinger (rhettinger)
Summary: string.atoi function causing TypeError
Initial Comment:
Discoved this when using Pmw with 2.3a1 and a2 found atoi
doesn't work properly. So, I wrote a small test:
------------------
import string
if __name__ == __main__:
num = atoi(test, 10)
------------------
Always get and exception from it and using Pmw that is the
same:
<...>
python2.3/string.py, line 220
return _int(s, base)
TypeError: int() can't convert non-string with explicit base
Seems to always pass a string like it asks for.
I can also reproduce by using the MenuBar.py and
MainMenuBar.py in Pmw/Pmw1.1/demos.
I fixed it by looking at python2.3/stringold.py. I took the guts of
atoi from there.
So it was:
---------------
def atoi(s, base=10) :
return _int(s, base)
It is now:
--------------
def atoi(*args) :
try:
s = arg[0]
except IndexError:
raise TypeError('function requires a least one argument: %d
given' % len(args))
return apply(_int, args)
Hope this helps. Not sure if it is best way to fix it but solved all our
errors using atoi for now.
Let me know if you need more info. - jmurff@pacbell.net
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-03-11 17:39
Message:
Logged In: YES
user_id=80475
Corvus, did you have any luck finding the offending lines in
PmW?
Can this bug be posted on PmW's SF site and closed here
or do you guys think something still needs to be fixed in
Python?
----------------------------------------------------------------------
Comment By: Jim Murff (jmurff)
Date: 2003-03-05 16:57
Message:
Logged In: YES
user_id=454042
We don't see the problem in Python 2.1. In fact initially we fell back to
2.1 but I don't want to stay there.
----------------------------------------------------------------------
Comment By: John Speno (corvus)
Date: 2003-03-05 15:43
Message:
Logged In: YES
user_id=2138
Looks like it's not a string.atoi problem, but a Pmw problem. I added the print >>sys.stderr, type(s), repr(s), str(s) and it shows this:
<type 'int'> 0 0
I'll try digging in Pmw to see if I can find the error.
I don't know if it's a Python 2.3 vs. 2.2 issue, or a Tcl/Tk AquaTk vs. X11 Tk issue. I see the problem with 2.3 and AquaTk but havn't tested other combos.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-03-05 14:40
Message:
Logged In: YES
user_id=80475
Values like 'str' are being properly flagged as invalid
literals. We need to find out the value and type that is
causing the error in Pmw. Can you insert a "print >>
sys.stderr, type(s), repr(s), str(s)" at the beginning of the
atoi() code.
The lines in the string.atoi() code have been around since
Py1.6, so if this is a new error, then it means that the
behavior of int(s, 10) has changed in subtle ways or that
the object s has changed in some way.
I would like to fix only the thing that caused the error and
avoid altering the string.atoi code which has been around
for so long.
The easiest way to track this one down is to simplify the
bug report to something like this:
string.atoi(s, 10)runs under Py2.2 but fails under Py2.3
when s is UserString with the value of " 10".
BTW, a simpler workaround is to change string.atoi to:
def atoi(s, *args) :
return _int(s, *args)
----------------------------------------------------------------------
Comment By: Jim Murff (jmurff)
Date: 2003-03-05 13:49
Message:
Logged In: YES
user_id=454042
I tried all kinds of values for the test variable...
'str' "str" sys.argv[0]. They all complained about invalid literal.
Pmw code showed other error. These used 'str' or a return from a method
as i recall. Yes i meant "string.atoi" -- sorry :)
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-03-04 21:18
Message:
Logged In: YES
user_id=80475
I'll take a look at this one.
Can you provide the value and type of the "test" variable
when the exception was raised?
Also, please confirm that the test code meant to
say: "num = string.atoi(test, 10) ".
----------------------------------------------------------------------
Comment By: John Speno (corvus)
Date: 2003-03-04 20:50
Message:
Logged In: YES
user_id=2138
I replaced the string.atoi() calls with calls to just int() in Pmw when I encountered this. I'm glad you filed this bug so I don't have to!
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=697591&group_id=5470