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

__future__ imports fail when compiling from python ast #58586

Closed
talljosh mannequin opened this issue Mar 21, 2012 · 6 comments
Closed

__future__ imports fail when compiling from python ast #58586

talljosh mannequin opened this issue Mar 21, 2012 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-bug An unexpected behavior, bug, or error

Comments

@talljosh
Copy link
Mannequin

talljosh mannequin commented Mar 21, 2012

BPO 14378
Nosy @brettcannon, @birkenfeld, @ncoghlan, @benjaminp, @bitdancer
Files
  • futureimport.py: Python code demonstrating the issue
  • 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-03-22.12:58:03.865>
    created_at = <Date 2012-03-21.06:19:41.889>
    labels = ['interpreter-core', 'type-bug', 'release-blocker']
    title = '__future__ imports fail when compiling from python ast'
    updated_at = <Date 2012-03-22.12:58:47.207>
    user = 'https://bugs.python.org/talljosh'

    bugs.python.org fields:

    activity = <Date 2012-03-22.12:58:47.207>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-03-22.12:58:03.865>
    closer = 'python-dev'
    components = ['Interpreter Core']
    creation = <Date 2012-03-21.06:19:41.889>
    creator = 'talljosh'
    dependencies = []
    files = ['24981']
    hgrepos = []
    issue_num = 14378
    keywords = []
    message_count = 6.0
    messages = ['156477', '156478', '156548', '156554', '156555', '156556']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'ncoghlan', 'benjamin.peterson', 'r.david.murray', 'python-dev', 'talljosh']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue14378'
    versions = ['Python 2.7', 'Python 3.2']

    @talljosh
    Copy link
    Mannequin Author

    talljosh mannequin commented Mar 21, 2012

    GOAL
    I am trying to compile an AST which contains an ImportFrom node which performs a __future__ import. The ImportFrom node in question is the first node within the AST's body (as it should be, because __future__ imports must occur at the beginning of modules).

    ISSUE
    Compiling the AST fails with the error "SyntaxError: from __future__ imports must occur at the beginning of the file".

    ANALYSIS
    The future_parse() function in future.c looks for __future__ imports and identifies them based on the condition (ds->v.ImportFrom.module == future) where future is constructed using PyString_InternFromString("__future__"). That is, the module attribute of the ImportFrom node is compared by identity with the interned string "__future__".

    The AST which I was compiling used the string "__future__" after extracting that string from a much longer string of user input, and as a result, the string was not interned.

    The attached file futureimport.py is a simple script demonstrating this issue. I have confirmed that the issue occurs in Python 2.7.2 and 3.2.2.

    @talljosh talljosh mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 21, 2012
    @talljosh
    Copy link
    Mannequin Author

    talljosh mannequin commented Mar 21, 2012

    Incidentally, the workaround that I'm using for the time being is to run the following code before attempting to compile root_node.

    for node in ast.walk(root_node):
        if isinstance(node, ast.ImportFrom) and node.module == '__future__':
            node.module = '__future__'

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 22, 2012

    New changeset f57cbcefde34 by Benjamin Peterson in branch '3.2':
    check by equality for __future__ not identity (closes bpo-14378)
    http://hg.python.org/cpython/rev/f57cbcefde34

    New changeset 9d793be3b4eb by Benjamin Peterson in branch 'default':
    merge 3.2 (bpo-14378)
    http://hg.python.org/cpython/rev/9d793be3b4eb

    @python-dev python-dev mannequin closed this as completed Mar 22, 2012
    @bitdancer
    Copy link
    Member

    After this commit the buildbots are dying randomly with segfaults.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 22, 2012

    New changeset 1729ec440bb6 by Benjamin Peterson in branch '2.7':
    check by equality for __future__ not identity (closes bpo-14378)
    http://hg.python.org/cpython/rev/1729ec440bb6

    @python-dev python-dev mannequin closed this as completed Mar 22, 2012
    @benjaminp
    Copy link
    Contributor

    1b467efb9b27

    @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) release-blocker type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants