classification
Title: int('\0') gives wrong error message
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: `int()`, `float()`, etc think python strings are null-terminated
View: 16741
Assigned To: Nosy List: Kurt.Rose, eryksun, vstinner
Priority: normal Keywords:

Created on 2014-05-20 23:38 by Kurt.Rose, last changed 2014-05-23 20:39 by terry.reedy. This issue is now closed.

Messages (3)
msg218859 - (view) Author: Kurt Rose (Kurt.Rose) Date: 2014-05-20 23:38
int() ignores everything after a null byte when reporting an error message.

Here you can see an example of how this manifests, and why could be a problem.

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> int('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
>>> int('\0a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> int('abc\0def')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
msg218860 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-21 01:19
It looks like the issue was already fixed in Python 3:

Python 3.5.0a0 (default:61d9aa8be445, May 20 2014, 16:03:51) 
>>> int('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
>>> int('\0a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x00a'
>>> int('abc\x00def')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc\x00def'

And the last one:

>>> int('\0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\x00'
msg218862 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2014-05-21 01:32
See issue 16741. This is fixed in 3.3, but 2.7 intentionally keeps the old behavior.
History
Date User Action Args
2014-05-23 20:39:13terry.reedysetstatus: open -> closed
type: behavior
superseder: `int()`, `float()`, etc think python strings are null-terminated
resolution: duplicate
stage: resolved
2014-05-21 01:32:37eryksunsetnosy: + eryksun
messages: + msg218862
2014-05-21 01:19:45vstinnersetnosy: + vstinner
messages: + msg218860
2014-05-20 23:38:26Kurt.Rosecreate