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

ast.dump() with incomplete node #82131

Closed
serhiy-storchaka opened this issue Aug 26, 2019 · 4 comments
Closed

ast.dump() with incomplete node #82131

serhiy-storchaka opened this issue Aug 26, 2019 · 4 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 37950
Nosy @serhiy-storchaka, @miss-islington
PRs
  • bpo-37950: Fix ast.dump() when call with incompletely initialized node. #15510
  • [3.7] bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510) #15581
  • [3.8] bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510) #15582
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2019-08-29.08:12:59.783>
    created_at = <Date 2019-08-26.07:35:54.550>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'ast.dump() with incomplete node'
    updated_at = <Date 2019-08-29.08:12:59.782>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-08-29.08:12:59.782>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2019-08-29.08:12:59.783>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2019-08-26.07:35:54.550>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37950
    keywords = ['patch']
    message_count = 4.0
    messages = ['350506', '350732', '350740', '350745']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'miss-islington']
    pr_nums = ['15510', '15581', '15582']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37950'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    There are several issues in ast.dump() with incompletely initialized node. Some fields and attributes of AST nodes are optional, but creating an AST node without them leads ast.dump() to fail or to produce incorrect result.

    1. With annotate_fields=False ast.dump() outputs subnodes as positional arguments of the node constructor.
    >>> ast.dump(ast.Raise(exc=ast.Name(id='a', ctx=ast.Load()), cause=ast.Name(id='b', ctx=ast.Load())), annotate_fields=False)
    "Raise(Name('a', Load()), Name('b', Load()))"

    But if miss the optional exc field it outputs incorrect output:

    >>> ast.dump(ast.Raise(cause=ast.Name(id='a', ctx=ast.Load())), annotate_fields=False)
    "Raise(Name('a', Load()))"

    which is not distinguished from the case when you pass only the exc field and miss the cause field (both are optional):

    >>> ast.dump(ast.Raise(exc=ast.Name(id='a', ctx=ast.Load())), annotate_fields=False)
    "Raise(Name('a', Load()))"
    1. The documentation of ast.dump() says that its result with annotate_fields=True is impossible to evaluate, but this is not true, because keyword arguments are supported by AST node constructors.

    2. Attributes end_lineno and end_col_offset are optional, but if you miss them ast.dump() with include_attributes=True will fail:

    >>> ast.dump(ast.Raise(lineno=3, col_offset=4), include_attributes=True)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/serhiy/py/cpython3.8/Lib/ast.py", line 126, in dump
        return _format(node)
      File "/home/serhiy/py/cpython3.8/Lib/ast.py", line 118, in _format
        rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
      File "/home/serhiy/py/cpython3.8/Lib/ast.py", line 118, in <genexpr>
        rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
    AttributeError: 'Raise' object has no attribute 'end_lineno'
    1. Even if you specify all attributes, the output looks weird if you do not specify any field (note a space after "("):
    >>> ast.dump(ast.Raise(lineno=3, col_offset=4, end_lineno=3, end_col_offset=24), include_attributes=True)
    'Raise( lineno=3, col_offset=4, end_lineno=3, end_col_offset=24)'

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes labels Aug 26, 2019
    @serhiy-storchaka serhiy-storchaka self-assigned this Aug 26, 2019
    @serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 26, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset e64f948 by Serhiy Storchaka in branch 'master':
    bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510)
    e64f948

    @miss-islington
    Copy link
    Contributor

    New changeset d3d2650 by Miss Islington (bot) in branch '3.7':
    bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510)
    d3d2650

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 097eae5 by Serhiy Storchaka in branch '3.8':
    [3.8] bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510) (GH-15582)
    097eae5

    @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
    3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants