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

Parse error for Long octal literals in ast27 #26

Closed

Description

@ddfisher

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.

Activity

  1. ddfisher commented on Jan 27, 2017

    @ddfisher
    CollaboratorAuthor

    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.
  2. gvanrossum commented on Jan 27, 2017

    @gvanrossum
    Member

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

  3. ddfisher commented on Jan 27, 2017

    @ddfisher
    CollaboratorAuthor

    That's a great idea. Thanks!

  4. added a commit that references this issue on Jan 27, 2017
    f72d7f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions