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

Easier creation of _ast nodes #46757

Closed
birkenfeld opened this issue Mar 29, 2008 · 6 comments
Closed

Easier creation of _ast nodes #46757

birkenfeld opened this issue Mar 29, 2008 · 6 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@birkenfeld
Copy link
Member

BPO 2505
Nosy @loewis, @birkenfeld
Files
  • ast-attrs.diff
  • ast-constructor-v2.diff
  • 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/birkenfeld'
    closed_at = <Date 2008-03-30.07:01:54.812>
    created_at = <Date 2008-03-29.13:38:09.563>
    labels = ['extension-modules', 'type-bug']
    title = 'Easier creation of _ast nodes'
    updated_at = <Date 2008-03-30.07:01:54.811>
    user = 'https://github.com/birkenfeld'

    bugs.python.org fields:

    activity = <Date 2008-03-30.07:01:54.811>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2008-03-30.07:01:54.812>
    closer = 'georg.brandl'
    components = ['Extension Modules']
    creation = <Date 2008-03-29.13:38:09.563>
    creator = 'georg.brandl'
    dependencies = []
    files = ['9887', '9893']
    hgrepos = []
    issue_num = 2505
    keywords = ['patch']
    message_count = 6.0
    messages = ['64691', '64693', '64700', '64715', '64722', '64744']
    nosy_count = 2.0
    nosy_names = ['loewis', 'georg.brandl']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue2505'
    versions = ['Python 2.6', 'Python 3.0']

    @birkenfeld
    Copy link
    Member Author

    This patch adds two things to the _ast module:

    • Nodes can be initialized with keyword arguments:
      m = _ast.Module(body=[...])

    • Only attributes that are in _fields or _attributes can be set on nodes.

    Martin, what do you think?

    @birkenfeld birkenfeld added extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error labels Mar 29, 2008
    @birkenfeld
    Copy link
    Member Author

    On second thought, restricting node attributes may prevent custom AST
    processing tools from adding useful information themselves...

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Mar 29, 2008

    On second thought, restricting node attributes may prevent custom AST
    processing tools from adding useful information themselves...

    Indeed. If anything is to be checked, it should be whether the child
    nodes or attributes have the right types. However, that should rather
    be done in a recursive check function on _mod, which then would also
    check whether all necessary fields have been set. Or, such checks
    could be delayed until actual compilation of the tree is attempted
    (i.e. conversion to the C AST structures).

    As for being able to pass constructor arguments: I'd expect them
    to be positional arguments, not keyword arguments - anybody creating
    AST nodes would normally have Python.asdl open, no?

    @birkenfeld
    Copy link
    Member Author

    Okay, I'm dropping the attribute restriction part.

    Attaching new patch that allows creating nodes with fields (not
    attributes) as positional arguments, and setting all keyword arguments
    as attributes on self.

    @birkenfeld birkenfeld changed the title Restrict attributes of _ast nodes Easier creation of _ast nodes Mar 29, 2008
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Mar 29, 2008

    Looks fine to me. It might be reasonable to further restrict the
    constructor to either 0 or len(_fields) arguments.

    @loewis loewis mannequin assigned birkenfeld and unassigned loewis Mar 29, 2008
    @birkenfeld
    Copy link
    Member Author

    Committed in r62049.

    @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
    extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant