classification
Title: Inconsistent Exception for int() conversion
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, exarkun, ezio.melotti, flox, mark.dickinson
Priority: normal Keywords:

Created on 2010-01-15 19:35 by flox, last changed 2012-07-21 14:57 by flox. This issue is now closed.

Messages (5)
msg97839 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-15 19:35
On Python 3:

>>> int('\0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character '\x00' in position 0: invalid decimal Unicode string

>>> int('\01')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x01'

>>> int('\x80')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: unexpected code byte

>>> int('\xc0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 0: unexpected end of data


On Python 2, it raises ValueError (except for '\0').
msg97841 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-15 20:26
The null byte gives UnicodeEncodeError for other conversions too.

Python 3:
 int('\0'), float('\0'), complex('\0')

Python 2:
 int(u'\0'), long(u'\0'), float(u'\0'), complex(u'0')


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\x00' in position 1: invalid decimal Unicode string
msg97842 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-01-15 20:28
Loosely related to issue4221.
msg146594 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-10-29 02:55
On 3.2 it is fixed (I didn't find the related changeset).

Not backported to 2.7.
msg166037 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2012-07-21 14:57
The behavior seems acceptable in 2.7 too.

>>> int('\0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('\01')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x01'


>>> int(u'\0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\x00' in position 0: invalid decimal Unicode string
>>> int(u'\01')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x01'
History
Date User Action Args
2012-07-21 14:57:57floxsetstatus: open -> closed
resolution: fixed
messages: + msg166037

stage: test needed -> resolved
2011-10-29 02:55:10floxsetmessages: + msg146594
versions: - Python 3.1, Python 3.2
2010-08-12 18:10:40floxsetnosy: + belopolsky
stage: test needed
type: behavior

versions: + Python 3.1, Python 2.7
2010-08-12 18:09:27floxlinkissue9578 superseder
2010-08-12 17:55:06ezio.melottisetnosy: + ezio.melotti
2010-01-20 12:44:32mark.dickinsonsetnosy: + mark.dickinson
2010-01-15 20:28:10exarkunsetnosy: + exarkun
messages: + msg97842
2010-01-15 20:26:22floxsetmessages: + msg97841
2010-01-15 19:35:21floxcreate