Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent exception from int is confusing #48471

Closed
exarkun mannequin opened this issue Oct 28, 2008 · 8 comments
Closed

inconsistent exception from int is confusing #48471

exarkun mannequin opened this issue Oct 28, 2008 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@exarkun
Copy link
Mannequin

exarkun mannequin commented Oct 28, 2008

BPO 4221
Nosy @birkenfeld, @terryjreedy, @vstinner, @ezio-melotti, @florentx

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-07-21.15:01:06.265>
created_at = <Date 2008-10-28.17:11:57.175>
labels = ['interpreter-core', 'type-bug']
title = 'inconsistent exception from int is confusing'
updated_at = <Date 2012-07-21.15:01:06.264>
user = 'https://bugs.python.org/exarkun'

bugs.python.org fields:

activity = <Date 2012-07-21.15:01:06.264>
actor = 'flox'
assignee = 'none'
closed = True
closed_date = <Date 2012-07-21.15:01:06.265>
closer = 'flox'
components = ['Interpreter Core']
creation = <Date 2008-10-28.17:11:57.175>
creator = 'exarkun'
dependencies = []
files = []
hgrepos = []
issue_num = 4221
keywords = []
message_count = 8.0
messages = ['75290', '75294', '75439', '75442', '97843', '98103', '146595', '166038']
nosy_count = 6.0
nosy_names = ['georg.brandl', 'terry.reedy', 'exarkun', 'vstinner', 'ezio.melotti', 'flox']
pr_nums = []
priority = 'low'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4221'
versions = ['Python 2.7']

@exarkun
Copy link
Mannequin Author

exarkun mannequin commented Oct 28, 2008

exarkun@charm:~$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int('\0', 256)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 256: '\x00'
>>> int('x', 256)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: int() base must be >= 2 and <= 36
>>> 

The former is misleading. \x00 is a perfectly valid byte if the base is
256. The real problem, that base 256 isn't supported, is obscured. It
would be much better for the latter case's message to be used in the
former case.

@exarkun exarkun mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 28, 2008
@birkenfeld
Copy link
Member

Since it is not defined which bytes are used as digits for bases > 36,
\x00 is not a "valid byte".

In any case, the problem here is that the base check is done after the
"no NULL" check. Perhaps the error should explicitly mention that no
null bytes are allowed.

@terryjreedy
Copy link
Member

You tested on 2.5.2 but marked this for 2.6.  3.0 gives
>>> int('\0')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int('\0')
UnicodeEncodeError: 'decimal' codec can't encode character '\x00' in
position 0: invalid decimal Unicode string

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

>>> int('\1',256)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    int('\1',256)
ValueError: int() arg 2 must be >= 2 and <= 36

The 3.0 doc on string inputs
"A string must be a base-radix integer literal optionally preceded by
‘+’ or ‘-‘ (with no space in between) and optionally surrounded by
whitespace. A base-n literal consists of the digits 0 to n-1, with ‘a’
to ‘z’ (or ‘A’ to ‘Z’) having values 10 to 35."

in much clearer than the 2.6 doc
"If the argument is a string, it must contain a possibly signed decimal
number representable as a Python integer, possibly embedded in whitespace."

Even so, I do not see any inconsistency.

@exarkun
Copy link
Mannequin Author

exarkun mannequin commented Nov 1, 2008

2.6 exhibits the same behavior as 2.5 in this case. 3.0 exhibits
similar behavior, but with a slightly different exception in the NUL
case. The examples included showing the Python 3 behavior don't cover
the same cases as the examples I included in my initial comment.

@florentx
Copy link
Mannequin

florentx mannequin commented Jan 15, 2010

Python 3 gives same confusing error:

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

>>> int(b'x', 999)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: int() arg 2 must be >= 2 and <= 36

@vstinner
Copy link
Member

See also issue bpo-7710.

@florentx
Copy link
Mannequin

florentx mannequin commented Oct 29, 2011

No more bug with Python 3.2.

On 2.7, we still experience the behavior described in msg75290.

@florentx
Copy link
Mannequin

florentx mannequin commented Jul 21, 2012

Proposed as won't fix for the 2.x series.

@florentx florentx mannequin closed this as completed Jul 21, 2012
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants