Re: Bug!?

Guido van Rossum (guido@cwi.nl)
Tue, 04 Aug 1992 10:59:14 +0200

Peter Dobcsanyi writes:
>I accidentally found the following strange behavior:
[...]
> x = string.atoi('08')
[...]
>SyntaxError: invalid syntax

Yeah, string.atoi() internally calls eval(), which interprets this as
an illegal octal number. According to the documentation string.atoi()
is not supposed to handle octal numbers, so I'll fix it. (Would this
break anybody's code?)

Here is what I believe is a fixed version of string.atoi():

# Convert string to integer
atoi_error = 'non-numeric argument to string.atoi'
def atoi(str):
sign = ''
s = str
if s[:1] in '+-':
sign = s[0]
s = s[1:]
if not s: raise atoi_error, str
while s[0] == '0' and len(s) > 1: s = s[1:]
for c in s:
if c not in digits: raise atoi_error, str
return eval(sign + s)

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Are all your pets called Eric?"