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

M.-A. Lemburg mal at egenix.com
Sun Apr 9 22:27:34 CEST 2006


Guido van Rossum wrote:

Go ahead and fix it. This was probably never changed since 1990 or so... Do expect some code brakage where people rely on the old behavior. :-(

--Guido On 4/9/06, Thomas Wouters <thomas at python.org> wrote: 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 "", line 1, in ? ValueError: invalid literal for int():

int("34\n\n\n5") Traceback (most recent call last): File "", 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 "", 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 "", 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 "", line 1, in ? LookupError: unknown encoding: decimal

This part I can explain: the internal decimal codec isn't made public through the codec registry since it only supports encoding.

The encoder converts a Unicode decimal strings to plain ASCII decimals.

The error message looks like a standard codec error message because the raise_encode_exception() API is used.

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!


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

-- --Guido van Rossum (home page: http://www.python.org/~guido/)


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/mal%40egenix.com

-- Marc-Andre Lemburg eGenix.com

Professional Python Services directly from the Source (#1, Apr 09 2006)

Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/


::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::



More information about the Python-Dev mailing list