This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Easier creation of _ast nodes
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, loewis
Priority: normal Keywords: patch

Created on 2008-03-29 13:38 by georg.brandl, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ast-attrs.diff georg.brandl, 2008-03-29 13:38
ast-constructor-v2.diff georg.brandl, 2008-03-29 18:31
Messages (6)
msg64691 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-29 13:38
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?
msg64693 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-29 13:40
On second thought, restricting node attributes may prevent custom AST
processing tools from adding useful information themselves...
msg64700 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-03-29 14:49
> 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?
msg64715 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-29 18:31
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.
msg64722 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-03-29 20:03
Looks fine to me. It might be reasonable to further restrict the
constructor to either 0 or len(_fields) arguments.
msg64744 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-30 07:01
Committed in r62049.
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46757
2008-03-30 07:01:54georg.brandlsetstatus: open -> closed
messages: + msg64744
2008-03-29 20:03:12loewissetassignee: loewis -> georg.brandl
resolution: accepted
messages: + msg64722
2008-03-29 18:31:45georg.brandlsetfiles: + ast-constructor-v2.diff
messages: + msg64715
title: Restrict attributes of _ast nodes -> Easier creation of _ast nodes
2008-03-29 14:49:17loewissetmessages: + msg64700
2008-03-29 13:40:35georg.brandlsetmessages: + msg64693
2008-03-29 13:38:09georg.brandlcreate