Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Parse error for Long octal literals in ast27 #26

Closed
ddfisher opened this issue Jan 21, 2017 · 3 comments
Closed

Parse error for Long octal literals in ast27 #26

ddfisher opened this issue Jan 21, 2017 · 3 comments

Comments

@ddfisher
Copy link
Collaborator

01L is a perfectly valid Python 2 long literal, but for some reason ast27 refuses to parse it and instead gives ValueError: invalid literal for int() with base 0: '01'. 01 and 1L both work fine.

@ddfisher
Copy link
Collaborator Author

011111111111111111111111 also fails.

Here's why: Numbers are parsed in the parsenumber function in ast27/Python/ast.c. In two different circumstances, parsenumber calls PyLong_FromString to do the parsing:

  1. If the number ends with an 'l' or an 'L', we strip the final character (as Python 3 doesn't allow it) and pass it to PyLong_FromString.
  2. If the number is too large to be parsed by strtol (or there's some other error when we try parsing it with strtol), we pass it to PyLong_FromString.
    Unfortunately, Python 3 doesn't support the 01 octal syntax; it has to be 0o1 instead. Hence the error.

@gvanrossum
Copy link
Member

You can pre-scan the number to determine the base and pass a base of 8 to PyLong_FromString.

@ddfisher
Copy link
Collaborator Author

That's a great idea. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants