Changes to Python's build process and to the C API include:
It's possible for Python code to obtain AST objects by using the
compile() built-in and specifying _ast.PyCF_ONLY_AST
as the value of the
flags parameter:
from _ast import PyCF_ONLY_AST ast = compile("""a=0 for i in range(10): a += i """, "<string>", 'exec', PyCF_ONLY_AST) assignment = ast.body[0] for_loop = ast.body[1]
No official documentation has been written for the AST code yet, but PEP 339 discusses the design. To start learning about the code, read the definition of the various AST nodes in Parser/Python.asdl. A Python script reads this file and generates a set of C structure definitions in Include/Python-ast.h. The PyParser_ASTFromString() and PyParser_ASTFromFile(), defined in Include/pythonrun.h, take Python source as input and return the root of an AST representing the contents. This AST can then be turned into a code object by PyAST_Compile(). For more information, read the source code, and then ask questions on python-dev.
The AST code was developed under Jeremy Hylton's management, and implemented by (in alphabetical order) Brett Cannon, Nick Coghlan, Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters, Armin Rigo, and Neil Schemenauer, plus the participants in a number of AST sprints at conferences such as PyCon.
"trunk:45355:45356M, Apr 13 2006, 07:42:19"
.
(Contributed by Barry Warsaw.)
range = PyObject_CallFunction((PyObject*) &PyRange_Type, "lll", start, stop, step);
See About this document... for information on suggesting changes.