This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bug parsing integers with zero padding
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: geoffreyspear, luisgf, mark.dickinson
Priority: normal Keywords:

Created on 2015-01-13 12:18 by luisgf, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg233928 - (view) Author: Luis G.F (luisgf) Date: 2015-01-13 12:18
Python 3.4 interpreter fail to parse a integer that has zero padding, whereas python 2.7 works properly.

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int(001)
1
>>> 

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int(001)
  File "<stdin>", line 1
    int(001)
          ^
SyntaxError: invalid token
>>>
msg233929 - (view) Author: Geoffrey Spear (geoffreyspear) * Date: 2015-01-13 12:22
This is not a bug, it's a deliberate change.

Python 2.x doesn't "work correctly"; what you have there is an octal literal, not a 0-padded base-10 integer. Try 008 or 011 and be surprised that python 2 is "broken" and you'll see why this syntax was removed.
msg233930 - (view) Author: Luis G.F (luisgf) Date: 2015-01-13 12:45
Thanks for the response, but in my case, 001 is not an octal literal, is a base-10 zero padded comming from the parsing of a ip string like 111.000.222.333 , where numbers are all integers in base-10.

The solution for parsing that seams to cast 000 as string and then using int('000', base=10).
msg233961 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-01-13 19:33
> is a base-10 zero padded comming from the parsing of a ip string

If you're parsing an ip string, how do you end up with a 000 *literal*?  The SyntaxError only applies to literals in Python code; it doesn't affect conversion from strings to integers.  So you don't need the "base=10" keyword: the following works in both Python 2 and Python 3.

>>> int("000")
0
>>> int("0019")
19
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67419
2015-01-13 19:33:04mark.dickinsonsetnosy: + mark.dickinson
messages: + msg233961
2015-01-13 13:13:37r.david.murraysetstatus: open -> closed
stage: resolved
2015-01-13 12:45:18luisgfsetresolution: not a bug
messages: + msg233930
2015-01-13 12:22:18geoffreyspearsetnosy: + geoffreyspear
messages: + msg233929
2015-01-13 12:18:29luisgfcreate