[Python-Dev] int()'s ValueError behaviour

Thomas Wouters thomas at python.org
Sun Apr 9 15:30:44 CEST 2006


Someone on IRC (who refuses to report bugs on sourceforge, so I guess he
wants to remain anonymous) came with this very amusing bug: int(), when
raising ValueError, doesn't quote (or repr(), rather) its arguments:

>>> int("")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int():
>>> int("34\n\n\n5")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 34


5
>>>

Unicode behaviour also isn't always consistent:
>>> int(u'\u0100')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'decimal' codec can't encode character u'\u0100' in
position 0: invalid decimal Unicode string
>>> int(u'\u09ec', 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 6

And trying to use the 'decimal' codec directly:
>>> u'6'.encode('decimal')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
LookupError: unknown encoding: decimal

I'm not sure if the latter problems are fixable, but the former should be
fixed by passing the argument to ValueError through repr(), I think. It's
also been suggested (by the reporter, and I agree) that the actual base
should be in the errormessage too. Is there some reason not to do this that
I've overlooked?

--
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060409/9b123b2e/attachment.htm 


More information about the Python-Dev mailing list