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

Fix parser and AST: fill lineno and col_offset when compiling AST from Python objects #69741

Closed
vstinner opened this issue Nov 5, 2015 · 7 comments

Comments

@vstinner
Copy link
Member

vstinner commented Nov 5, 2015

BPO 25555
Nosy @vstinner, @benjaminp, @1st1, @matrixise
Files
  • ast.patch
  • 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 2015-11-09.21:14:10.182>
    created_at = <Date 2015-11-05.09:42:04.793>
    labels = []
    title = 'Fix parser and AST: fill lineno and col_offset when compiling AST from Python objects'
    updated_at = <Date 2015-11-09.21:14:10.181>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2015-11-09.21:14:10.181>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-11-09.21:14:10.182>
    closer = 'vstinner'
    components = []
    creation = <Date 2015-11-05.09:42:04.793>
    creator = 'vstinner'
    dependencies = []
    files = ['40949']
    hgrepos = []
    issue_num = 25555
    keywords = ['patch']
    message_count = 7.0
    messages = ['254093', '254155', '254202', '254203', '254204', '254403', '254409']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'benjamin.peterson', 'python-dev', 'yselivanov', 'matrixise']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue25555'
    versions = ['Python 3.5', 'Python 3.6']

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 5, 2015

    I wrote an AST optimizer which build AST from source code using ast.parse(), modify AST in place, and then compile the AST to bytecode.

    The problem is that the lineno and col_offset attributes of the "arg" AST node are uninitialized when AST is rebuild from Python objects: compile(ast_tree, ...). As a consequence, the compilation may fail because lineno or col_offset values are invalid.

    Attached patch enhances Parser/asdl_c.py to take "arg" attributes in account. It also updates the generated files Include/Python-ast.h and Python/Python-ast.c, and fix usage of the arg() function in Python/ast.c.

    It looks like only the "arg" AST node had attributes which were initialized (other AST nodes are handled with a different code path which already filled attributes).

    See Parser/Python.asdl for the definition of the "arg" AST node.

    Note: Python 2.7 is not affected. In Python 2, function arguments are simple expressions, they don't have a dedicated type with lineno and col_offset attributes. "arg" was introduced in Python 3 with the PEP-3107 (function annotations).

    @matrixise
    Copy link
    Member

    Hi Victor,

    1. Result of the compilation -> success
    2. I have checked your patch, I think there is no problem.
    3. How can I test it? currently, everything is fine.

    For me, this patch is ok and working, but I need the point 3.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 6, 2015

    New changeset 54d4290f0ec6 by Victor Stinner in branch 'default':
    Issue bpo-25555: Fix parser and AST: fill lineno and col_offset of "arg" node when
    https://hg.python.org/cpython/rev/54d4290f0ec6

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 6, 2015

    1. How can I test it? currently, everything is fine.

    Sorry, I don't know how to test it.

    To write my fix, I had to modify the compile() builtin function to be able to compile Python to AST, and then AST to Python. But without this fix I don't know how to test.

    I tried to revert the AST patch on my FAT Python to try to find which part of the code fails without the fix, but FAT Python doesn't compile anymore. I reverted the revert, and it still doesn't work!?

    For this bug, sorry, you have to trust me :-(

    I applied the fix to Python 3.6. I will wait a bit for buildbots before backporting to 3.5.

    @matrixise
    Copy link
    Member

    I trust you, I have reviewed the source code and it seems to be right.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 9, 2015

    New changeset 2d8988f64cea by Victor Stinner in branch '3.5':
    Issue bpo-25555: Fix parser and AST: fill lineno and col_offset of "arg" node when
    https://hg.python.org/cpython/rev/2d8988f64cea

    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 9, 2015

    I backported the fix to Python 3.5. Since I'm not convinced that the bug can be triggered without modifying CPython (the bug only impacts FAT Python which installs a hook on the AST compiler), I don't want to backport it to Python 2.7 to avoid the risk of regression.

    @vstinner vstinner closed this as completed Nov 9, 2015
    @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
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants