2010-10-27 00:06:43 -03:00
|
|
|
:mod:`ast` --- Abstract Syntax Trees
|
|
|
|
====================================
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
.. module:: ast
|
|
|
|
:synopsis: Abstract Syntax Tree classes and manipulation.
|
|
|
|
|
|
|
|
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
|
|
|
|
.. sectionauthor:: Georg Brandl <georg@python.org>
|
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
.. testsetup::
|
|
|
|
|
|
|
|
import ast
|
|
|
|
|
2011-01-09 23:26:08 -04:00
|
|
|
**Source code:** :source:`Lib/ast.py`
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2011-01-10 15:54:11 -04:00
|
|
|
--------------
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
The :mod:`ast` module helps Python applications to process trees of the Python
|
|
|
|
abstract syntax grammar. The abstract syntax itself might change with each
|
|
|
|
Python release; this module helps to find out programmatically what the current
|
|
|
|
grammar looks like.
|
|
|
|
|
2008-11-08 13:05:00 -04:00
|
|
|
An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
|
2009-07-26 11:54:51 -03:00
|
|
|
a flag to the :func:`compile` built-in function, or using the :func:`parse`
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
helper provided in this module. The result will be a tree of objects whose
|
2008-11-08 13:05:00 -04:00
|
|
|
classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
|
|
|
|
compiled into a Python code object using the built-in :func:`compile` function.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
.. _abstract-grammar:
|
|
|
|
|
|
|
|
Abstract Grammar
|
|
|
|
----------------
|
|
|
|
|
|
|
|
The abstract grammar is currently defined as follows:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../Parser/Python.asdl
|
2020-05-07 17:57:26 -03:00
|
|
|
:language: asdl
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
Node classes
|
|
|
|
------------
|
|
|
|
|
|
|
|
.. class:: AST
|
|
|
|
|
|
|
|
This is the base of all AST node classes. The actual node classes are
|
|
|
|
derived from the :file:`Parser/Python.asdl` file, which is reproduced
|
|
|
|
:ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
|
|
|
|
module and re-exported in :mod:`ast`.
|
|
|
|
|
|
|
|
There is one class defined for each left-hand side symbol in the abstract
|
|
|
|
grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
|
|
|
|
there is one class defined for each constructor on the right-hand side; these
|
|
|
|
classes inherit from the classes for the left-hand side trees. For example,
|
|
|
|
:class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
|
|
|
|
with alternatives (aka "sums"), the left-hand side class is abstract: only
|
|
|
|
instances of specific constructor nodes are ever created.
|
|
|
|
|
2018-10-28 08:41:26 -03:00
|
|
|
.. index:: single: ? (question mark); in AST grammar
|
|
|
|
.. index:: single: * (asterisk); in AST grammar
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
.. attribute:: _fields
|
|
|
|
|
|
|
|
Each concrete class has an attribute :attr:`_fields` which gives the names
|
|
|
|
of all child nodes.
|
|
|
|
|
|
|
|
Each instance of a concrete class has one attribute for each child node,
|
|
|
|
of the type as defined in the grammar. For example, :class:`ast.BinOp`
|
|
|
|
instances have an attribute :attr:`left` of type :class:`ast.expr`.
|
|
|
|
|
|
|
|
If these attributes are marked as optional in the grammar (using a
|
|
|
|
question mark), the value might be ``None``. If the attributes can have
|
|
|
|
zero-or-more values (marked with an asterisk), the values are represented
|
|
|
|
as Python lists. All possible attributes must be present and have valid
|
|
|
|
values when compiling an AST with :func:`compile`.
|
|
|
|
|
|
|
|
.. attribute:: lineno
|
|
|
|
col_offset
|
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points:
* It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`.
* I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient.
* Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear.
* For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in
```python
class C:
pass
pass
```
the end line and end column for the class definition is (2, 8).
* For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node.
* I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing.
An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
2019-01-22 07:18:22 -04:00
|
|
|
end_lineno
|
|
|
|
end_col_offset
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
|
2020-11-03 17:28:42 -04:00
|
|
|
:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`, and
|
|
|
|
:attr:`end_col_offset` attributes. The :attr:`lineno` and :attr:`end_lineno`
|
|
|
|
are the first and last line numbers of source text span (1-indexed so the
|
|
|
|
first line is line 1) and the :attr:`col_offset` and :attr:`end_col_offset`
|
|
|
|
are the corresponding UTF-8 byte offsets of the first and last tokens that
|
|
|
|
generated the node. The UTF-8 offset is recorded because the parser uses
|
|
|
|
UTF-8 internally.
|
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points:
* It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`.
* I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient.
* Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear.
* For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in
```python
class C:
pass
pass
```
the end line and end column for the class definition is (2, 8).
* For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node.
* I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing.
An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
2019-01-22 07:18:22 -04:00
|
|
|
|
|
|
|
Note that the end positions are not required by the compiler and are
|
|
|
|
therefore optional. The end offset is *after* the last symbol, for example
|
|
|
|
one can get the source segment of a one-line expression node using
|
|
|
|
``source_line[node.col_offset : node.end_col_offset]``.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
The constructor of a class :class:`ast.T` parses its arguments as follows:
|
|
|
|
|
|
|
|
* If there are positional arguments, there must be as many as there are items
|
|
|
|
in :attr:`T._fields`; they will be assigned as attributes of these names.
|
|
|
|
* If there are keyword arguments, they will set the attributes of the same
|
|
|
|
names to the given values.
|
|
|
|
|
|
|
|
For example, to create and populate an :class:`ast.UnaryOp` node, you could
|
|
|
|
use ::
|
|
|
|
|
|
|
|
node = ast.UnaryOp()
|
|
|
|
node.op = ast.USub()
|
2018-09-27 11:42:37 -03:00
|
|
|
node.operand = ast.Constant()
|
|
|
|
node.operand.value = 5
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
node.operand.lineno = 0
|
|
|
|
node.operand.col_offset = 0
|
|
|
|
node.lineno = 0
|
|
|
|
node.col_offset = 0
|
|
|
|
|
|
|
|
or the more compact ::
|
|
|
|
|
2018-09-27 11:42:37 -03:00
|
|
|
node = ast.UnaryOp(ast.USub(), ast.Constant(5, lineno=0, col_offset=0),
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
lineno=0, col_offset=0)
|
|
|
|
|
2020-02-17 05:03:00 -04:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
|
|
|
Class :class:`ast.Constant` is now used for all constants.
|
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
|
|
|
|
Simple indices are represented by their value, extended slices are
|
|
|
|
represented as tuples.
|
|
|
|
|
2018-09-27 11:42:37 -03:00
|
|
|
.. deprecated:: 3.8
|
|
|
|
|
2020-02-17 05:03:00 -04:00
|
|
|
Old classes :class:`ast.Num`, :class:`ast.Str`, :class:`ast.Bytes`,
|
2018-09-27 11:42:37 -03:00
|
|
|
:class:`ast.NameConstant` and :class:`ast.Ellipsis` are still available,
|
2020-03-10 13:52:34 -03:00
|
|
|
but they will be removed in future Python releases. In the meantime,
|
2020-02-17 05:03:00 -04:00
|
|
|
instantiating them will return an instance of a different class.
|
2018-09-27 11:42:37 -03:00
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
.. deprecated:: 3.9
|
|
|
|
|
|
|
|
Old classes :class:`ast.Index` and :class:`ast.ExtSlice` are still
|
|
|
|
available, but they will be removed in future Python releases.
|
|
|
|
In the meantime, instantiating them will return an instance of
|
|
|
|
a different class.
|
|
|
|
|
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
Literals
|
|
|
|
^^^^^^^^
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
.. class:: Constant(value)
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
A constant value. The ``value`` attribute of the ``Constant`` literal contains the
|
|
|
|
Python object it represents. The values represented can be simple types
|
|
|
|
such as a number, string or ``None``, but also immutable container types
|
|
|
|
(tuples and frozensets) if all of their elements are constant.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2020-03-01 23:14:06 -04:00
|
|
|
.. doctest::
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
|
|
|
|
Expression(
|
2020-03-09 19:07:47 -03:00
|
|
|
body=Constant(value=123))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: FormattedValue(value, conversion, format_spec)
|
|
|
|
|
|
|
|
Node representing a single formatting field in an f-string. If the string
|
|
|
|
contains a single formatting field and nothing else the node can be
|
|
|
|
isolated otherwise it appears in :class:`JoinedStr`.
|
|
|
|
|
|
|
|
* ``value`` is any expression node (such as a literal, a variable, or a
|
|
|
|
function call).
|
|
|
|
* ``conversion`` is an integer:
|
|
|
|
|
|
|
|
* -1: no formatting
|
|
|
|
* 115: ``!s`` string formatting
|
|
|
|
* 114: ``!r`` repr formatting
|
|
|
|
* 97: ``!a`` ascii formatting
|
|
|
|
|
|
|
|
* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
|
|
|
|
of the value, or ``None`` if no format was specified. Both
|
|
|
|
``conversion`` and ``format_spec`` can be set at the same time.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: JoinedStr(values)
|
|
|
|
|
|
|
|
An f-string, comprising a series of :class:`FormattedValue` and :class:`Constant`
|
|
|
|
nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=JoinedStr(
|
|
|
|
values=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value='sin('),
|
2020-03-07 14:22:58 -04:00
|
|
|
FormattedValue(
|
|
|
|
value=Name(id='a', ctx=Load()),
|
2020-03-09 19:07:47 -03:00
|
|
|
conversion=-1),
|
|
|
|
Constant(value=') is '),
|
2020-03-07 14:22:58 -04:00
|
|
|
FormattedValue(
|
|
|
|
value=Call(
|
|
|
|
func=Name(id='sin', ctx=Load()),
|
|
|
|
args=[
|
|
|
|
Name(id='a', ctx=Load())],
|
|
|
|
keywords=[]),
|
|
|
|
conversion=-1,
|
|
|
|
format_spec=JoinedStr(
|
|
|
|
values=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value='.3')]))]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: List(elts, ctx)
|
|
|
|
Tuple(elts, ctx)
|
|
|
|
|
|
|
|
A list or tuple. ``elts`` holds a list of nodes representing the elements.
|
|
|
|
``ctx`` is :class:`Store` if the container is an assignment target (i.e.
|
|
|
|
``(x,y)=something``), and :class:`Load` otherwise.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=List(
|
|
|
|
elts=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=1),
|
|
|
|
Constant(value=2),
|
|
|
|
Constant(value=3)],
|
2020-03-07 14:22:58 -04:00
|
|
|
ctx=Load()))
|
|
|
|
>>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Tuple(
|
|
|
|
elts=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=1),
|
|
|
|
Constant(value=2),
|
|
|
|
Constant(value=3)],
|
2020-03-07 14:22:58 -04:00
|
|
|
ctx=Load()))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Set(elts)
|
|
|
|
|
|
|
|
A set. ``elts`` holds a list of nodes representing the set's elements.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Set(
|
|
|
|
elts=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=1),
|
|
|
|
Constant(value=2),
|
|
|
|
Constant(value=3)]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Dict(keys, values)
|
|
|
|
|
|
|
|
A dictionary. ``keys`` and ``values`` hold lists of nodes representing the
|
|
|
|
keys and the values respectively, in matching order (what would be returned
|
|
|
|
when calling :code:`dictionary.keys()` and :code:`dictionary.values()`).
|
|
|
|
|
|
|
|
When doing dictionary unpacking using dictionary literals the expression to be
|
|
|
|
expanded goes in the ``values`` list, with a ``None`` at the corresponding
|
|
|
|
position in ``keys``.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Dict(
|
|
|
|
keys=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value='a'),
|
2020-03-07 14:22:58 -04:00
|
|
|
None],
|
|
|
|
values=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=1),
|
2020-03-07 14:22:58 -04:00
|
|
|
Name(id='d', ctx=Load())]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
Variables
|
|
|
|
^^^^^^^^^
|
|
|
|
|
|
|
|
.. class:: Name(id, ctx)
|
|
|
|
|
|
|
|
A variable name. ``id`` holds the name as a string, and ``ctx`` is one of
|
|
|
|
the following types.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Load()
|
|
|
|
Store()
|
|
|
|
Del()
|
|
|
|
|
|
|
|
Variable references can be used to load the value of a variable, to assign
|
|
|
|
a new value to it, or to delete it. Variable references are given a context
|
|
|
|
to distinguish these cases.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse('a'), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=Name(id='a', ctx=Load()))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse('a = 1'), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Assign(
|
|
|
|
targets=[
|
|
|
|
Name(id='a', ctx=Store())],
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=1))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse('del a'), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Delete(
|
|
|
|
targets=[
|
|
|
|
Name(id='a', ctx=Del())])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Starred(value, ctx)
|
|
|
|
|
|
|
|
A ``*var`` variable reference. ``value`` holds the variable, typically a
|
|
|
|
:class:`Name` node. This type must be used when building a :class:`Call`
|
|
|
|
node with ``*args``.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse('a, *b = it'), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Assign(
|
|
|
|
targets=[
|
|
|
|
Tuple(
|
|
|
|
elts=[
|
|
|
|
Name(id='a', ctx=Store()),
|
|
|
|
Starred(
|
|
|
|
value=Name(id='b', ctx=Store()),
|
|
|
|
ctx=Store())],
|
|
|
|
ctx=Store())],
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Name(id='it', ctx=Load()))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
Expressions
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. class:: Expr(value)
|
|
|
|
|
|
|
|
When an expression, such as a function call, appears as a statement by itself
|
|
|
|
with its return value not used or stored, it is wrapped in this container.
|
|
|
|
``value`` holds one of the other nodes in this section, a :class:`Constant`, a
|
|
|
|
:class:`Name`, a :class:`Lambda`, a :class:`Yield` or :class:`YieldFrom` node.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse('-a'), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=UnaryOp(
|
|
|
|
op=USub(),
|
|
|
|
operand=Name(id='a', ctx=Load())))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: UnaryOp(op, operand)
|
|
|
|
|
|
|
|
A unary operation. ``op`` is the operator, and ``operand`` any expression
|
|
|
|
node.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: UAdd
|
|
|
|
USub
|
|
|
|
Not
|
|
|
|
Invert
|
|
|
|
|
|
|
|
Unary operator tokens. :class:`Not` is the ``not`` keyword, :class:`Invert`
|
|
|
|
is the ``~`` operator.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=UnaryOp(
|
|
|
|
op=Not(),
|
|
|
|
operand=Name(id='x', ctx=Load())))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: BinOp(left, op, right)
|
|
|
|
|
|
|
|
A binary operation (like addition or division). ``op`` is the operator, and
|
|
|
|
``left`` and ``right`` are any expression nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=BinOp(
|
|
|
|
left=Name(id='x', ctx=Load()),
|
|
|
|
op=Add(),
|
|
|
|
right=Name(id='y', ctx=Load())))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Add
|
|
|
|
Sub
|
|
|
|
Mult
|
|
|
|
Div
|
|
|
|
FloorDiv
|
|
|
|
Mod
|
|
|
|
Pow
|
|
|
|
LShift
|
|
|
|
RShift
|
|
|
|
BitOr
|
|
|
|
BitXor
|
|
|
|
BitAnd
|
|
|
|
MatMult
|
|
|
|
|
|
|
|
Binary operator tokens.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: BoolOp(op, values)
|
|
|
|
|
|
|
|
A boolean operation, 'or' or 'and'. ``op`` is :class:`Or` or :class:`And`.
|
|
|
|
``values`` are the values involved. Consecutive operations with the same
|
|
|
|
operator, such as ``a or b or c``, are collapsed into one node with several
|
|
|
|
values.
|
|
|
|
|
|
|
|
This doesn't include ``not``, which is a :class:`UnaryOp`.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=BoolOp(
|
|
|
|
op=Or(),
|
|
|
|
values=[
|
|
|
|
Name(id='x', ctx=Load()),
|
|
|
|
Name(id='y', ctx=Load())]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: And
|
|
|
|
Or
|
|
|
|
|
|
|
|
Boolean operator tokens.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Compare(left, ops, comparators)
|
|
|
|
|
|
|
|
A comparison of two or more values. ``left`` is the first value in the
|
|
|
|
comparison, ``ops`` the list of operators, and ``comparators`` the list
|
|
|
|
of values after the first element in the comparison.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Compare(
|
2020-03-09 19:07:47 -03:00
|
|
|
left=Constant(value=1),
|
2020-03-07 14:22:58 -04:00
|
|
|
ops=[
|
|
|
|
LtE(),
|
|
|
|
Lt()],
|
|
|
|
comparators=[
|
|
|
|
Name(id='a', ctx=Load()),
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=10)]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Eq
|
|
|
|
NotEq
|
|
|
|
Lt
|
|
|
|
LtE
|
|
|
|
Gt
|
|
|
|
GtE
|
|
|
|
Is
|
|
|
|
IsNot
|
|
|
|
In
|
|
|
|
NotIn
|
|
|
|
|
|
|
|
Comparison operator tokens.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Call(func, args, keywords, starargs, kwargs)
|
|
|
|
|
|
|
|
A function call. ``func`` is the function, which will often be a
|
|
|
|
:class:`Name` or :class:`Attribute` object. Of the arguments:
|
|
|
|
|
|
|
|
* ``args`` holds a list of the arguments passed by position.
|
|
|
|
* ``keywords`` holds a list of :class:`keyword` objects representing
|
|
|
|
arguments passed by keyword.
|
|
|
|
|
|
|
|
When creating a ``Call`` node, ``args`` and ``keywords`` are required, but
|
|
|
|
they can be empty lists. ``starargs`` and ``kwargs`` are optional.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Call(
|
|
|
|
func=Name(id='func', ctx=Load()),
|
|
|
|
args=[
|
|
|
|
Name(id='a', ctx=Load()),
|
|
|
|
Starred(
|
|
|
|
value=Name(id='d', ctx=Load()),
|
|
|
|
ctx=Load())],
|
|
|
|
keywords=[
|
|
|
|
keyword(
|
|
|
|
arg='b',
|
|
|
|
value=Name(id='c', ctx=Load())),
|
|
|
|
keyword(
|
|
|
|
value=Name(id='e', ctx=Load()))]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: keyword(arg, value)
|
|
|
|
|
|
|
|
A keyword argument to a function call or class definition. ``arg`` is a raw
|
|
|
|
string of the parameter name, ``value`` is a node to pass in.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: IfExp(test, body, orelse)
|
|
|
|
|
|
|
|
An expression such as ``a if b else c``. Each field holds a single node, so
|
|
|
|
in the following example, all three are :class:`Name` nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=IfExp(
|
|
|
|
test=Name(id='b', ctx=Load()),
|
|
|
|
body=Name(id='a', ctx=Load()),
|
|
|
|
orelse=Name(id='c', ctx=Load())))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Attribute(value, attr, ctx)
|
|
|
|
|
|
|
|
Attribute access, e.g. ``d.keys``. ``value`` is a node, typically a
|
|
|
|
:class:`Name`. ``attr`` is a bare string giving the name of the attribute,
|
|
|
|
and ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del` according to how
|
|
|
|
the attribute is acted on.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Attribute(
|
|
|
|
value=Name(id='snake', ctx=Load()),
|
|
|
|
attr='colour',
|
|
|
|
ctx=Load()))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: NamedExpr(target, value)
|
|
|
|
|
|
|
|
A named expression. This AST node is produced by the assignment expressions
|
|
|
|
operator (also known as the walrus operator). As opposed to the :class:`Assign`
|
|
|
|
node in which the first argument can be multiple nodes, in this case both
|
|
|
|
``target`` and ``value`` must be single nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=NamedExpr(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=4)))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
Subscripting
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: Subscript(value, slice, ctx)
|
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
A subscript, such as ``l[1]``. ``value`` is the subscripted object
|
|
|
|
(usually sequence or mapping). ``slice`` is an index, slice or key.
|
|
|
|
It can be a :class:`Tuple` and contain a :class:`Slice`.
|
|
|
|
``ctx`` is :class:`Load`, :class:`Store` or :class:`Del`
|
2020-03-01 23:14:06 -04:00
|
|
|
according to the action performed with the subscript.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
>>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
|
2020-03-07 14:22:58 -04:00
|
|
|
Expression(
|
|
|
|
body=Subscript(
|
|
|
|
value=Name(id='l', ctx=Load()),
|
2020-03-10 13:52:34 -03:00
|
|
|
slice=Tuple(
|
|
|
|
elts=[
|
|
|
|
Slice(
|
|
|
|
lower=Constant(value=1),
|
|
|
|
upper=Constant(value=2)),
|
|
|
|
Constant(value=3)],
|
|
|
|
ctx=Load()),
|
2020-03-07 14:22:58 -04:00
|
|
|
ctx=Load()))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: Slice(lower, upper, step)
|
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
Regular slicing (on the form ``lower:upper`` or ``lower:upper:step``).
|
|
|
|
Can occur only inside the *slice* field of :class:`Subscript`, either
|
|
|
|
directly or as an element of :class:`Tuple`.
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=Subscript(
|
|
|
|
value=Name(id='l', ctx=Load()),
|
|
|
|
slice=Slice(
|
2020-03-09 19:07:47 -03:00
|
|
|
lower=Constant(value=1),
|
|
|
|
upper=Constant(value=2)),
|
2020-03-07 14:22:58 -04:00
|
|
|
ctx=Load()))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
Comprehensions
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: ListComp(elt, generators)
|
|
|
|
SetComp(elt, generators)
|
|
|
|
GeneratorExp(elt, generators)
|
|
|
|
DictComp(key, value, generators)
|
|
|
|
|
|
|
|
List and set comprehensions, generator expressions, and dictionary
|
|
|
|
comprehensions. ``elt`` (or ``key`` and ``value``) is a single node
|
|
|
|
representing the part that will be evaluated for each item.
|
|
|
|
|
|
|
|
``generators`` is a list of :class:`comprehension` nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=ListComp(
|
|
|
|
elt=Name(id='x', ctx=Load()),
|
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
|
|
|
iter=Name(id='numbers', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=0)]))
|
|
|
|
>>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=DictComp(
|
|
|
|
key=Name(id='x', ctx=Load()),
|
|
|
|
value=BinOp(
|
|
|
|
left=Name(id='x', ctx=Load()),
|
|
|
|
op=Pow(),
|
2020-03-09 19:07:47 -03:00
|
|
|
right=Constant(value=2)),
|
2020-03-07 14:22:58 -04:00
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
|
|
|
iter=Name(id='numbers', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=0)]))
|
|
|
|
>>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4))
|
|
|
|
Expression(
|
|
|
|
body=SetComp(
|
|
|
|
elt=Name(id='x', ctx=Load()),
|
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
|
|
|
iter=Name(id='numbers', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=0)]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: comprehension(target, iter, ifs, is_async)
|
|
|
|
|
|
|
|
One ``for`` clause in a comprehension. ``target`` is the reference to use for
|
|
|
|
each element - typically a :class:`Name` or :class:`Tuple` node. ``iter``
|
|
|
|
is the object to iterate over. ``ifs`` is a list of test expressions: each
|
|
|
|
``for`` clause can have multiple ``ifs``.
|
|
|
|
|
|
|
|
``is_async`` indicates a comprehension is asynchronous (using an
|
|
|
|
``async for`` instead of ``for``). The value is an integer (0 or 1).
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('[ord(c) for line in file for c in line]', mode='eval'),
|
2020-03-01 23:14:06 -04:00
|
|
|
... indent=4)) # Multiple comprehensions in one.
|
|
|
|
Expression(
|
|
|
|
body=ListComp(
|
|
|
|
elt=Call(
|
|
|
|
func=Name(id='ord', ctx=Load()),
|
|
|
|
args=[
|
|
|
|
Name(id='c', ctx=Load())],
|
|
|
|
keywords=[]),
|
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='line', ctx=Store()),
|
|
|
|
iter=Name(id='file', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=0),
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='c', ctx=Store()),
|
|
|
|
iter=Name(id='line', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=0)]))
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('(n**2 for n in it if n>5 if n<10)', mode='eval'),
|
2020-03-01 23:14:06 -04:00
|
|
|
... indent=4)) # generator comprehension
|
|
|
|
Expression(
|
|
|
|
body=GeneratorExp(
|
|
|
|
elt=BinOp(
|
|
|
|
left=Name(id='n', ctx=Load()),
|
|
|
|
op=Pow(),
|
2020-03-09 19:07:47 -03:00
|
|
|
right=Constant(value=2)),
|
2020-03-01 23:14:06 -04:00
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='n', ctx=Store()),
|
|
|
|
iter=Name(id='it', ctx=Load()),
|
|
|
|
ifs=[
|
|
|
|
Compare(
|
|
|
|
left=Name(id='n', ctx=Load()),
|
|
|
|
ops=[
|
|
|
|
Gt()],
|
|
|
|
comparators=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=5)]),
|
2020-03-01 23:14:06 -04:00
|
|
|
Compare(
|
|
|
|
left=Name(id='n', ctx=Load()),
|
|
|
|
ops=[
|
|
|
|
Lt()],
|
|
|
|
comparators=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=10)])],
|
2020-03-01 23:14:06 -04:00
|
|
|
is_async=0)]))
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
|
2020-03-01 23:14:06 -04:00
|
|
|
... indent=4)) # Async comprehension
|
2020-03-07 14:22:58 -04:00
|
|
|
Expression(
|
|
|
|
body=ListComp(
|
|
|
|
elt=Name(id='i', ctx=Load()),
|
|
|
|
generators=[
|
|
|
|
comprehension(
|
|
|
|
target=Name(id='i', ctx=Store()),
|
|
|
|
iter=Name(id='soc', ctx=Load()),
|
|
|
|
ifs=[],
|
|
|
|
is_async=1)]))
|
2020-03-01 23:14:06 -04:00
|
|
|
|
|
|
|
Statements
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
|
|
|
.. class:: Assign(targets, value, type_comment)
|
|
|
|
|
|
|
|
An assignment. ``targets`` is a list of nodes, and ``value`` is a single node.
|
|
|
|
|
|
|
|
Multiple nodes in ``targets`` represents assigning the same value to each.
|
|
|
|
Unpacking is represented by putting a :class:`Tuple` or :class:`List`
|
|
|
|
within ``targets``.
|
|
|
|
|
|
|
|
.. attribute:: type_comment
|
|
|
|
|
|
|
|
``type_comment`` is an optional string with the type annotation as a comment.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('a = b = 1'), indent=4)) # Multiple assignment
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Assign(
|
|
|
|
targets=[
|
|
|
|
Name(id='a', ctx=Store()),
|
|
|
|
Name(id='b', ctx=Store())],
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=1))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Assign(
|
|
|
|
targets=[
|
|
|
|
Tuple(
|
|
|
|
elts=[
|
|
|
|
Name(id='a', ctx=Store()),
|
|
|
|
Name(id='b', ctx=Store())],
|
|
|
|
ctx=Store())],
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Name(id='c', ctx=Load()))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: AnnAssign(target, annotation, value, simple)
|
|
|
|
|
|
|
|
An assignment with a type annotation. ``target`` is a single node and can
|
|
|
|
be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`.
|
|
|
|
``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name`
|
|
|
|
node. ``value`` is a single optional node. ``simple`` is a boolean integer
|
|
|
|
set to True for a :class:`Name` node in ``target`` that do not appear in
|
|
|
|
between parenthesis and are hence pure names and not expressions.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('c: int'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AnnAssign(
|
|
|
|
target=Name(id='c', ctx=Store()),
|
|
|
|
annotation=Name(id='int', ctx=Load()),
|
|
|
|
simple=1)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('(a): int = 1'), indent=4)) # Annotation with parenthesis
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AnnAssign(
|
|
|
|
target=Name(id='a', ctx=Store()),
|
|
|
|
annotation=Name(id='int', ctx=Load()),
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=1),
|
2020-03-01 23:14:06 -04:00
|
|
|
simple=0)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('a.b: int'), indent=4)) # Attribute annotation
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AnnAssign(
|
|
|
|
target=Attribute(
|
|
|
|
value=Name(id='a', ctx=Load()),
|
|
|
|
attr='b',
|
|
|
|
ctx=Store()),
|
|
|
|
annotation=Name(id='int', ctx=Load()),
|
|
|
|
simple=0)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('a[1]: int'), indent=4)) # Subscript annotation
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AnnAssign(
|
|
|
|
target=Subscript(
|
|
|
|
value=Name(id='a', ctx=Load()),
|
2020-03-10 13:52:34 -03:00
|
|
|
slice=Constant(value=1),
|
2020-03-01 23:14:06 -04:00
|
|
|
ctx=Store()),
|
|
|
|
annotation=Name(id='int', ctx=Load()),
|
|
|
|
simple=0)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: AugAssign(target, op, value)
|
|
|
|
|
|
|
|
Augmented assignment, such as ``a += 1``. In the following example,
|
|
|
|
``target`` is a :class:`Name` node for ``x`` (with the :class:`Store`
|
|
|
|
context), ``op`` is :class:`Add`, and ``value`` is a :class:`Constant` with
|
|
|
|
value for 1.
|
|
|
|
|
|
|
|
The ``target`` attribute connot be of class :class:`Tuple` or :class:`List`,
|
|
|
|
unlike the targets of :class:`Assign`.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('x += 2'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AugAssign(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
|
|
|
op=Add(),
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=2))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Raise(exc, cause)
|
|
|
|
|
|
|
|
A ``raise`` statement. ``exc`` is the exception object to be raised, normally a
|
|
|
|
:class:`Call` or :class:`Name`, or ``None`` for a standalone ``raise``.
|
|
|
|
``cause`` is the optional part for ``y`` in ``raise x from y``.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('raise x from y'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Raise(
|
|
|
|
exc=Name(id='x', ctx=Load()),
|
|
|
|
cause=Name(id='y', ctx=Load()))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Assert(test, msg)
|
|
|
|
|
|
|
|
An assertion. ``test`` holds the condition, such as a :class:`Compare` node.
|
|
|
|
``msg`` holds the failure message.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('assert x,y'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Assert(
|
|
|
|
test=Name(id='x', ctx=Load()),
|
|
|
|
msg=Name(id='y', ctx=Load()))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Delete(targets)
|
|
|
|
|
|
|
|
Represents a ``del`` statement. ``targets`` is a list of nodes, such as
|
|
|
|
:class:`Name`, :class:`Attribute` or :class:`Subscript` nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('del x,y,z'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Delete(
|
|
|
|
targets=[
|
|
|
|
Name(id='x', ctx=Del()),
|
|
|
|
Name(id='y', ctx=Del()),
|
|
|
|
Name(id='z', ctx=Del())])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Pass()
|
|
|
|
|
|
|
|
A ``pass`` statement.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('pass'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Pass()],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
Other statements which are only applicable inside functions or loops are
|
|
|
|
described in other sections.
|
|
|
|
|
|
|
|
Imports
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
.. class:: Import(names)
|
|
|
|
|
|
|
|
An import statement. ``names`` is a list of :class:`alias` nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('import x,y,z'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Import(
|
|
|
|
names=[
|
2020-03-09 19:07:47 -03:00
|
|
|
alias(name='x'),
|
|
|
|
alias(name='y'),
|
|
|
|
alias(name='z')])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: ImportFrom(module, names, level)
|
|
|
|
|
|
|
|
Represents ``from x import y``. ``module`` is a raw string of the 'from' name,
|
|
|
|
without any leading dots, or ``None`` for statements such as ``from . import foo``.
|
|
|
|
``level`` is an integer holding the level of the relative import (0 means
|
|
|
|
absolute import).
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('from y import x,y,z'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
ImportFrom(
|
|
|
|
module='y',
|
|
|
|
names=[
|
2020-03-09 19:07:47 -03:00
|
|
|
alias(name='x'),
|
|
|
|
alias(name='y'),
|
|
|
|
alias(name='z')],
|
2020-03-01 23:14:06 -04:00
|
|
|
level=0)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: alias(name, asname)
|
|
|
|
|
|
|
|
Both parameters are raw strings of the names. ``asname`` can be ``None`` if
|
|
|
|
the regular name is to be used.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('from ..foo.bar import a as b, c'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
ImportFrom(
|
|
|
|
module='foo.bar',
|
|
|
|
names=[
|
|
|
|
alias(name='a', asname='b'),
|
2020-03-09 19:07:47 -03:00
|
|
|
alias(name='c')],
|
2020-03-01 23:14:06 -04:00
|
|
|
level=2)],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
Control flow
|
|
|
|
^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Optional clauses such as ``else`` are stored as an empty list if they're
|
|
|
|
not present.
|
|
|
|
|
|
|
|
.. class:: If(test, body, orelse)
|
|
|
|
|
|
|
|
An ``if`` statement. ``test`` holds a single node, such as a :class:`Compare`
|
|
|
|
node. ``body`` and ``orelse`` each hold a list of nodes.
|
|
|
|
|
|
|
|
``elif`` clauses don't have a special representation in the AST, but rather
|
|
|
|
appear as extra :class:`If` nodes within the ``orelse`` section of the
|
|
|
|
previous one.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""
|
|
|
|
... if x:
|
|
|
|
... ...
|
|
|
|
... elif y:
|
|
|
|
... ...
|
|
|
|
... else:
|
|
|
|
... ...
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
If(
|
|
|
|
test=Name(id='x', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
orelse=[
|
|
|
|
If(
|
|
|
|
test=Name(id='y', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
orelse=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))])])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: For(target, iter, body, orelse, type_comment)
|
|
|
|
|
|
|
|
A ``for`` loop. ``target`` holds the variable(s) the loop assigns to, as a
|
|
|
|
single :class:`Name`, :class:`Tuple` or :class:`List` node. ``iter`` holds
|
|
|
|
the item to be looped over, again as a single node. ``body`` and ``orelse``
|
|
|
|
contain lists of nodes to execute. Those in ``orelse`` are executed if the
|
|
|
|
loop finishes normally, rather than via a ``break`` statement.
|
|
|
|
|
|
|
|
.. attribute:: type_comment
|
|
|
|
|
|
|
|
``type_comment`` is an optional string with the type annotation as a comment.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""
|
|
|
|
... for x in y:
|
|
|
|
... ...
|
|
|
|
... else:
|
|
|
|
... ...
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
For(
|
|
|
|
target=Name(id='x', ctx=Store()),
|
|
|
|
iter=Name(id='y', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
orelse=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: While(test, body, orelse)
|
|
|
|
|
|
|
|
A ``while`` loop. ``test`` holds the condition, such as a :class:`Compare`
|
|
|
|
node.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>> print(ast.dump(ast.parse("""
|
|
|
|
... while x:
|
|
|
|
... ...
|
|
|
|
... else:
|
|
|
|
... ...
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
While(
|
|
|
|
test=Name(id='x', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
orelse=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Break
|
|
|
|
Continue
|
|
|
|
|
|
|
|
The ``break`` and ``continue`` statements.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... for a in b:
|
|
|
|
... if a > 5:
|
|
|
|
... break
|
|
|
|
... else:
|
|
|
|
... continue
|
|
|
|
...
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
For(
|
|
|
|
target=Name(id='a', ctx=Store()),
|
|
|
|
iter=Name(id='b', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
If(
|
|
|
|
test=Compare(
|
|
|
|
left=Name(id='a', ctx=Load()),
|
|
|
|
ops=[
|
|
|
|
Gt()],
|
|
|
|
comparators=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=5)]),
|
2020-03-01 23:14:06 -04:00
|
|
|
body=[
|
|
|
|
Break()],
|
|
|
|
orelse=[
|
|
|
|
Continue()])],
|
2020-03-09 19:07:47 -03:00
|
|
|
orelse=[])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Try(body, handlers, orelse, finalbody)
|
|
|
|
|
|
|
|
``try`` blocks. All attributes are list of nodes to execute, except for
|
|
|
|
``handlers``, which is a list of :class:`ExceptHandler` nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""
|
|
|
|
... try:
|
|
|
|
... ...
|
|
|
|
... except Exception:
|
|
|
|
... ...
|
|
|
|
... except OtherException as e:
|
|
|
|
... ...
|
|
|
|
... else:
|
|
|
|
... ...
|
|
|
|
... finally:
|
|
|
|
... ...
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Try(
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
handlers=[
|
|
|
|
ExceptHandler(
|
|
|
|
type=Name(id='Exception', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))]),
|
2020-03-01 23:14:06 -04:00
|
|
|
ExceptHandler(
|
|
|
|
type=Name(id='OtherException', ctx=Load()),
|
|
|
|
name='e',
|
|
|
|
body=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))])],
|
2020-03-01 23:14:06 -04:00
|
|
|
orelse=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))],
|
2020-03-01 23:14:06 -04:00
|
|
|
finalbody=[
|
|
|
|
Expr(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=Ellipsis))])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: ExceptHandler(type, name, body)
|
|
|
|
|
|
|
|
A single ``except`` clause. ``type`` is the exception type it will match,
|
|
|
|
typically a :class:`Name` node (or ``None`` for a catch-all ``except:`` clause).
|
|
|
|
``name`` is a raw string for the name to hold the exception, or ``None`` if
|
|
|
|
the clause doesn't have ``as foo``. ``body`` is a list of nodes.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... try:
|
|
|
|
... a + 1
|
|
|
|
... except TypeError:
|
|
|
|
... pass
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Try(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=BinOp(
|
|
|
|
left=Name(id='a', ctx=Load()),
|
|
|
|
op=Add(),
|
2020-03-09 19:07:47 -03:00
|
|
|
right=Constant(value=1)))],
|
2020-03-01 23:14:06 -04:00
|
|
|
handlers=[
|
|
|
|
ExceptHandler(
|
|
|
|
type=Name(id='TypeError', ctx=Load()),
|
|
|
|
body=[
|
|
|
|
Pass()])],
|
|
|
|
orelse=[],
|
|
|
|
finalbody=[])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: With(items, body, type_comment)
|
|
|
|
|
|
|
|
A ``with`` block. ``items`` is a list of :class:`withitem` nodes representing
|
|
|
|
the context managers, and ``body`` is the indented block inside the context.
|
|
|
|
|
|
|
|
.. attribute:: type_comment
|
|
|
|
|
|
|
|
``type_comment`` is an optional string with the type annotation as a comment.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: withitem(context_expr, optional_vars)
|
|
|
|
|
|
|
|
A single context manager in a ``with`` block. ``context_expr`` is the context
|
|
|
|
manager, often a :class:`Call` node. ``optional_vars`` is a :class:`Name`,
|
|
|
|
:class:`Tuple` or :class:`List` for the ``as foo`` part, or ``None`` if that
|
|
|
|
isn't used.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... with a as b, c as d:
|
|
|
|
... something(b, d)
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
With(
|
|
|
|
items=[
|
|
|
|
withitem(
|
|
|
|
context_expr=Name(id='a', ctx=Load()),
|
|
|
|
optional_vars=Name(id='b', ctx=Store())),
|
|
|
|
withitem(
|
|
|
|
context_expr=Name(id='c', ctx=Load()),
|
|
|
|
optional_vars=Name(id='d', ctx=Store()))],
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=Call(
|
|
|
|
func=Name(id='something', ctx=Load()),
|
|
|
|
args=[
|
|
|
|
Name(id='b', ctx=Load()),
|
|
|
|
Name(id='d', ctx=Load())],
|
2020-03-09 19:07:47 -03:00
|
|
|
keywords=[]))])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
Function and class definitions
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. class:: FunctionDef(name, args, body, decorator_list, returns, type_comment)
|
|
|
|
|
|
|
|
A function definition.
|
|
|
|
|
|
|
|
* ``name`` is a raw string of the function name.
|
|
|
|
* ``args`` is a :class:`arguments` node.
|
|
|
|
* ``body`` is the list of nodes inside the function.
|
|
|
|
* ``decorator_list`` is the list of decorators to be applied, stored outermost
|
|
|
|
first (i.e. the first in the list will be applied last).
|
|
|
|
* ``returns`` is the return annotation.
|
|
|
|
|
|
|
|
.. attribute:: type_comment
|
|
|
|
|
|
|
|
``type_comment`` is an optional string with the type annotation as a comment.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Lambda(args, body)
|
|
|
|
|
|
|
|
``lambda`` is a minimal function definition that can be used inside an
|
|
|
|
expression. Unlike :class:`FunctionDef`, ``body`` holds a single node.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=Lambda(
|
|
|
|
args=arguments(
|
|
|
|
posonlyargs=[],
|
|
|
|
args=[
|
2020-03-09 19:07:47 -03:00
|
|
|
arg(arg='x'),
|
|
|
|
arg(arg='y')],
|
2020-03-01 23:14:06 -04:00
|
|
|
kwonlyargs=[],
|
|
|
|
kw_defaults=[],
|
|
|
|
defaults=[]),
|
2020-03-09 19:07:47 -03:00
|
|
|
body=Constant(value=Ellipsis)))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults)
|
|
|
|
|
|
|
|
The arguments for a function.
|
|
|
|
|
|
|
|
* ``posonlyargs``, ``args`` and ``kwonlyargs`` are lists of :class:`arg` nodes.
|
|
|
|
* ``vararg`` and ``kwarg`` are single :class:`arg` nodes, referring to the
|
|
|
|
``*args, **kwargs`` parameters.
|
|
|
|
* ``kw_defaults`` is a list of default values for keyword-only arguments. If
|
|
|
|
one is ``None``, the corresponding argument is required.
|
|
|
|
* ``defaults`` is a list of default values for arguments that can be passed
|
|
|
|
positionally. If there are fewer defaults, they correspond to the last n
|
|
|
|
arguments.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: arg(arg, annotation, type_comment)
|
|
|
|
|
|
|
|
A single argument in a list. ``arg`` is a raw string of the argument
|
|
|
|
name, ``annotation`` is its annotation, such as a :class:`Str` or
|
|
|
|
:class:`Name` node.
|
|
|
|
|
|
|
|
.. attribute:: type_comment
|
|
|
|
|
|
|
|
``type_comment`` is an optional string with the type annotation as a comment
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... @decorator1
|
|
|
|
... @decorator2
|
|
|
|
... def f(a: 'annotation', b=1, c=2, *d, e, f=3, **g) -> 'return annotation':
|
|
|
|
... pass
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
FunctionDef(
|
|
|
|
name='f',
|
|
|
|
args=arguments(
|
|
|
|
posonlyargs=[],
|
|
|
|
args=[
|
|
|
|
arg(
|
|
|
|
arg='a',
|
2020-03-09 19:07:47 -03:00
|
|
|
annotation=Constant(value='annotation')),
|
|
|
|
arg(arg='b'),
|
|
|
|
arg(arg='c')],
|
|
|
|
vararg=arg(arg='d'),
|
2020-03-01 23:14:06 -04:00
|
|
|
kwonlyargs=[
|
2020-03-09 19:07:47 -03:00
|
|
|
arg(arg='e'),
|
|
|
|
arg(arg='f')],
|
2020-03-01 23:14:06 -04:00
|
|
|
kw_defaults=[
|
|
|
|
None,
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=3)],
|
|
|
|
kwarg=arg(arg='g'),
|
2020-03-01 23:14:06 -04:00
|
|
|
defaults=[
|
2020-03-09 19:07:47 -03:00
|
|
|
Constant(value=1),
|
|
|
|
Constant(value=2)]),
|
2020-03-01 23:14:06 -04:00
|
|
|
body=[
|
|
|
|
Pass()],
|
|
|
|
decorator_list=[
|
|
|
|
Name(id='decorator1', ctx=Load()),
|
|
|
|
Name(id='decorator2', ctx=Load())],
|
2020-03-09 19:07:47 -03:00
|
|
|
returns=Constant(value='return annotation'))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Return(value)
|
|
|
|
|
|
|
|
A ``return`` statement.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('return 4'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Return(
|
2020-03-09 19:07:47 -03:00
|
|
|
value=Constant(value=4))],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Yield(value)
|
|
|
|
YieldFrom(value)
|
|
|
|
|
|
|
|
A ``yield`` or ``yield from`` expression. Because these are expressions, they
|
|
|
|
must be wrapped in a :class:`Expr` node if the value sent back is not used.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('yield x'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=Yield(
|
|
|
|
value=Name(id='x', ctx=Load())))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('yield from x'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=YieldFrom(
|
|
|
|
value=Name(id='x', ctx=Load())))],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Global(names)
|
|
|
|
Nonlocal(names)
|
|
|
|
|
|
|
|
``global`` and ``nonlocal`` statements. ``names`` is a list of raw strings.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('global x,y,z'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Global(
|
|
|
|
names=[
|
|
|
|
'x',
|
|
|
|
'y',
|
|
|
|
'z'])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
2020-03-07 14:22:58 -04:00
|
|
|
>>> print(ast.dump(ast.parse('nonlocal x,y,z'), indent=4))
|
2020-03-01 23:14:06 -04:00
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Nonlocal(
|
|
|
|
names=[
|
|
|
|
'x',
|
|
|
|
'y',
|
|
|
|
'z'])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: ClassDef(name, bases, keywords, starargs, kwargs, body, decorator_list)
|
|
|
|
|
|
|
|
A class definition.
|
|
|
|
|
|
|
|
* ``name`` is a raw string for the class name
|
|
|
|
* ``bases`` is a list of nodes for explicitly specified base classes.
|
|
|
|
* ``keywords`` is a list of :class:`keyword` nodes, principally for 'metaclass'.
|
|
|
|
Other keywords will be passed to the metaclass, as per `PEP-3115
|
|
|
|
<http://www.python.org/dev/peps/pep-3115/>`_.
|
|
|
|
* ``starargs`` and ``kwargs`` are each a single node, as in a function call.
|
|
|
|
starargs will be expanded to join the list of base classes, and kwargs will
|
|
|
|
be passed to the metaclass.
|
|
|
|
* ``body`` is a list of nodes representing the code within the class
|
|
|
|
definition.
|
|
|
|
* ``decorator_list`` is a list of nodes, as in :class:`FunctionDef`.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... @decorator1
|
|
|
|
... @decorator2
|
|
|
|
... class Foo(base1, base2, metaclass=meta):
|
|
|
|
... pass
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
ClassDef(
|
|
|
|
name='Foo',
|
|
|
|
bases=[
|
|
|
|
Name(id='base1', ctx=Load()),
|
|
|
|
Name(id='base2', ctx=Load())],
|
|
|
|
keywords=[
|
|
|
|
keyword(
|
|
|
|
arg='metaclass',
|
|
|
|
value=Name(id='meta', ctx=Load()))],
|
|
|
|
body=[
|
|
|
|
Pass()],
|
|
|
|
decorator_list=[
|
|
|
|
Name(id='decorator1', ctx=Load()),
|
|
|
|
Name(id='decorator2', ctx=Load())])],
|
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
Async and await
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. class:: AsyncFunctionDef(name, args, body, decorator_list, returns, type_comment)
|
|
|
|
|
|
|
|
An ``async def`` function definition. Has the same fields as
|
|
|
|
:class:`FunctionDef`.
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: Await(value)
|
|
|
|
|
|
|
|
An ``await`` expression. ``value`` is what it waits for.
|
|
|
|
Only valid in the body of an :class:`AsyncFunctionDef`.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> print(ast.dump(ast.parse("""\
|
|
|
|
... async def f():
|
|
|
|
... await other_func()
|
|
|
|
... """), indent=4))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
AsyncFunctionDef(
|
|
|
|
name='f',
|
|
|
|
args=arguments(
|
|
|
|
posonlyargs=[],
|
|
|
|
args=[],
|
|
|
|
kwonlyargs=[],
|
|
|
|
kw_defaults=[],
|
|
|
|
defaults=[]),
|
|
|
|
body=[
|
|
|
|
Expr(
|
|
|
|
value=Await(
|
|
|
|
value=Call(
|
|
|
|
func=Name(id='other_func', ctx=Load()),
|
|
|
|
args=[],
|
|
|
|
keywords=[])))],
|
2020-03-09 19:07:47 -03:00
|
|
|
decorator_list=[])],
|
2020-03-01 23:14:06 -04:00
|
|
|
type_ignores=[])
|
|
|
|
|
|
|
|
|
|
|
|
.. class:: AsyncFor(target, iter, body, orelse, type_comment)
|
|
|
|
AsyncWith(items, body, type_comment)
|
|
|
|
|
|
|
|
``async for`` loops and ``async with`` context managers. They have the same
|
|
|
|
fields as :class:`For` and :class:`With`, respectively. Only valid in the
|
|
|
|
body of an :class:`AsyncFunctionDef`.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2020-10-22 13:02:43 -03:00
|
|
|
.. note::
|
|
|
|
When a string is parsed by :func:`ast.parse`, operator nodes (subclasses
|
|
|
|
of :class:`ast.operator`, :class:`ast.unaryop`, :class:`ast.cmpop`,
|
|
|
|
:class:`ast.boolop` and :class:`ast.expr_context`) on the returned tree
|
|
|
|
will be singletons. Changes to one will be reflected in all other
|
|
|
|
occurrences of the same value (e.g. :class:`ast.Add`).
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
:mod:`ast` Helpers
|
|
|
|
------------------
|
|
|
|
|
2015-11-13 21:07:43 -04:00
|
|
|
Apart from the node classes, the :mod:`ast` module defines these utility functions
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
and classes for traversing abstract syntax trees:
|
|
|
|
|
2019-06-11 21:23:12 -03:00
|
|
|
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2011-01-24 17:36:03 -04:00
|
|
|
Parse the source into an AST node. Equivalent to ``compile(source,
|
2008-11-08 13:05:00 -04:00
|
|
|
filename, mode, ast.PyCF_ONLY_AST)``.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
If ``type_comments=True`` is given, the parser is modified to check
|
|
|
|
and return type comments as specified by :pep:`484` and :pep:`526`.
|
|
|
|
This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the
|
|
|
|
flags passed to :func:`compile()`. This will report syntax errors
|
|
|
|
for misplaced type comments. Without this flag, type comments will
|
|
|
|
be ignored, and the ``type_comment`` field on selected AST nodes
|
|
|
|
will always be ``None``. In addition, the locations of ``# type:
|
|
|
|
ignore`` comments will be returned as the ``type_ignores``
|
|
|
|
attribute of :class:`Module` (otherwise it is always an empty list).
|
|
|
|
|
|
|
|
In addition, if ``mode`` is ``'func_type'``, the input syntax is
|
|
|
|
modified to correspond to :pep:`484` "signature type comments",
|
|
|
|
e.g. ``(str, int) -> List[str]``.
|
|
|
|
|
2019-06-11 21:23:12 -03:00
|
|
|
Also, setting ``feature_version`` to a tuple ``(major, minor)``
|
|
|
|
will attempt to parse using that Python version's grammar.
|
|
|
|
Currently ``major`` must equal to ``3``. For example, setting
|
|
|
|
``feature_version=(3, 4)`` will allow the use of ``async`` and
|
|
|
|
``await`` as variable names. The lowest supported version is
|
|
|
|
``(3, 4)``; the highest is ``sys.version_info[0:2]``.
|
2019-03-07 16:38:08 -04:00
|
|
|
|
2018-03-09 16:03:22 -04:00
|
|
|
.. warning::
|
|
|
|
It is possible to crash the Python interpreter with a
|
|
|
|
sufficiently large/complex string due to stack depth limitations
|
|
|
|
in Python's AST compiler.
|
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
.. versionchanged:: 3.8
|
2019-03-07 16:38:08 -04:00
|
|
|
Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
|
2019-01-31 07:40:27 -04:00
|
|
|
|
2009-01-03 17:18:54 -04:00
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
.. function:: unparse(ast_obj)
|
|
|
|
|
|
|
|
Unparse an :class:`ast.AST` object and generate a string with code
|
|
|
|
that would produce an equivalent :class:`ast.AST` object if parsed
|
|
|
|
back with :func:`ast.parse`.
|
|
|
|
|
|
|
|
.. warning::
|
2019-12-28 18:16:02 -04:00
|
|
|
The produced code string will not necessarily be equal to the original
|
2020-06-27 22:11:43 -03:00
|
|
|
code that generated the :class:`ast.AST` object (without any compiler
|
|
|
|
optimizations, such as constant tuples/frozensets).
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
Trying to unparse a highly complex expression would result with
|
|
|
|
:exc:`RecursionError`.
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
.. function:: literal_eval(node_or_string)
|
|
|
|
|
2014-11-05 15:20:28 -04:00
|
|
|
Safely evaluate an expression node or a string containing a Python literal or
|
|
|
|
container display. The string or node provided may only consist of the
|
|
|
|
following Python literal structures: strings, bytes, numbers, tuples, lists,
|
|
|
|
dicts, sets, booleans, and ``None``.
|
|
|
|
|
|
|
|
This can be used for safely evaluating strings containing Python values from
|
|
|
|
untrusted sources without the need to parse the values oneself. It is not
|
|
|
|
capable of evaluating arbitrarily complex expressions, for example involving
|
|
|
|
operators or indexing.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
2018-03-09 16:03:22 -04:00
|
|
|
.. warning::
|
|
|
|
It is possible to crash the Python interpreter with a
|
|
|
|
sufficiently large/complex string due to stack depth limitations
|
|
|
|
in Python's AST compiler.
|
|
|
|
|
2010-07-11 06:41:21 -03:00
|
|
|
.. versionchanged:: 3.2
|
2010-07-13 03:38:10 -03:00
|
|
|
Now allows bytes and set literals.
|
2010-07-11 06:41:21 -03:00
|
|
|
|
2020-01-03 01:21:18 -04:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
Now supports creating empty sets with ``'set()'``.
|
|
|
|
|
2020-10-03 21:46:44 -03:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
For string inputs, leading spaces and tabs are now stripped.
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64089 | armin.ronacher | 2008-06-10 22:37:02 +0200 (mar., 10 juin 2008) | 3 lines
Fix a formatting error in the ast documentation.
........
r64098 | raymond.hettinger | 2008-06-11 02:25:29 +0200 (mer., 11 juin 2008) | 6 lines
Mini-PEP: Simplifying numbers.py
* Convert binary methods in Integral to mixin methods
* Remove three-arg __pow__ as a required method
* Make __int__ the root method instead of __long__.
........
r64100 | raymond.hettinger | 2008-06-11 02:28:51 +0200 (mer., 11 juin 2008) | 1 line
Update numbers doc for the Integral simplification.
........
r64101 | raymond.hettinger | 2008-06-11 02:44:47 +0200 (mer., 11 juin 2008) | 3 lines
Handle the case with zero arguments.
........
r64102 | benjamin.peterson | 2008-06-11 03:31:28 +0200 (mer., 11 juin 2008) | 4 lines
convert test_struct to a unittest thanks to Giampaolo Rodola
I had to disable one test because it was functioning incorrectly, see #1530559
I also removed the debugging prints
........
r64113 | thomas.heller | 2008-06-11 09:10:43 +0200 (mer., 11 juin 2008) | 2 lines
Fix markup.
Document the new 'offset' parameter for the 'ctypes.byref' function.
........
r64115 | raymond.hettinger | 2008-06-11 12:30:54 +0200 (mer., 11 juin 2008) | 1 line
Multi-arg form for set.difference() and set.difference_update().
........
r64116 | raymond.hettinger | 2008-06-11 14:06:49 +0200 (mer., 11 juin 2008) | 1 line
Issue 3051: Let heapq work with either __lt__ or __le__.
........
r64118 | raymond.hettinger | 2008-06-11 14:39:09 +0200 (mer., 11 juin 2008) | 1 line
Optimize previous checkin for heapq.
........
r64120 | raymond.hettinger | 2008-06-11 15:14:50 +0200 (mer., 11 juin 2008) | 1 line
Add test for heapq using both __lt__ and __le__.
........
r64132 | gregory.p.smith | 2008-06-11 20:00:52 +0200 (mer., 11 juin 2008) | 3 lines
Correct an incorrect comment about our #include of stddef.h.
(see Doug Evans' comment on python-dev 2008-06-10)
........
r64342 | guido.van.rossum | 2008-06-17 19:38:02 +0200 (mar., 17 juin 2008) | 3 lines
Roll back Raymond's -r64098 while we think of something better.
(See issue 3056 -- we're close to a resolution but need unittests.)
........
2008-06-17 17:36:03 -03:00
|
|
|
.. function:: get_docstring(node, clean=True)
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
Return the docstring of the given *node* (which must be a
|
2017-02-22 11:31:59 -04:00
|
|
|
:class:`FunctionDef`, :class:`AsyncFunctionDef`, :class:`ClassDef`,
|
|
|
|
or :class:`Module` node), or ``None`` if it has no docstring.
|
|
|
|
If *clean* is true, clean up the docstring's indentation with
|
|
|
|
:func:`inspect.cleandoc`.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
:class:`AsyncFunctionDef` is now supported.
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points:
* It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`.
* I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient.
* Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear.
* For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in
```python
class C:
pass
pass
```
the end line and end column for the class definition is (2, 8).
* For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node.
* I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing.
An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
2019-01-22 07:18:22 -04:00
|
|
|
.. function:: get_source_segment(source, node, *, padded=False)
|
|
|
|
|
|
|
|
Get source code segment of the *source* that generated *node*.
|
|
|
|
If some location information (:attr:`lineno`, :attr:`end_lineno`,
|
|
|
|
:attr:`col_offset`, or :attr:`end_col_offset`) is missing, return ``None``.
|
|
|
|
|
|
|
|
If *padded* is ``True``, the first line of a multi-line statement will
|
|
|
|
be padded with spaces to match its original position.
|
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
.. function:: fix_missing_locations(node)
|
|
|
|
|
|
|
|
When you compile a node tree with :func:`compile`, the compiler expects
|
|
|
|
:attr:`lineno` and :attr:`col_offset` attributes for every node that supports
|
|
|
|
them. This is rather tedious to fill in for generated nodes, so this helper
|
|
|
|
adds these attributes recursively where not already set, by setting them to
|
|
|
|
the values of the parent node. It works recursively starting at *node*.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: increment_lineno(node, n=1)
|
|
|
|
|
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points:
* It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`.
* I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient.
* Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear.
* For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in
```python
class C:
pass
pass
```
the end line and end column for the class definition is (2, 8).
* For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node.
* I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing.
An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
2019-01-22 07:18:22 -04:00
|
|
|
Increment the line number and end line number of each node in the tree
|
|
|
|
starting at *node* by *n*. This is useful to "move code" to a different
|
|
|
|
location in a file.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: copy_location(new_node, old_node)
|
|
|
|
|
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points:
* It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`.
* I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient.
* Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear.
* For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in
```python
class C:
pass
pass
```
the end line and end column for the class definition is (2, 8).
* For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node.
* I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing.
An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
2019-01-22 07:18:22 -04:00
|
|
|
Copy source location (:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`,
|
|
|
|
and :attr:`end_col_offset`) from *old_node* to *new_node* if possible,
|
|
|
|
and return *new_node*.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: iter_fields(node)
|
|
|
|
|
|
|
|
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
|
|
|
|
that is present on *node*.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: iter_child_nodes(node)
|
|
|
|
|
|
|
|
Yield all direct child nodes of *node*, that is, all fields that are nodes
|
|
|
|
and all items of fields that are lists of nodes.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: walk(node)
|
|
|
|
|
2011-01-09 03:38:51 -04:00
|
|
|
Recursively yield all descendant nodes in the tree starting at *node*
|
|
|
|
(including *node* itself), in no specified order. This is useful if you only
|
|
|
|
want to modify nodes in place and don't care about the context.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. class:: NodeVisitor()
|
|
|
|
|
|
|
|
A node visitor base class that walks the abstract syntax tree and calls a
|
|
|
|
visitor function for every node found. This function may return a value
|
2009-01-03 17:17:04 -04:00
|
|
|
which is forwarded by the :meth:`visit` method.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
This class is meant to be subclassed, with the subclass adding visitor
|
|
|
|
methods.
|
|
|
|
|
|
|
|
.. method:: visit(node)
|
|
|
|
|
|
|
|
Visit a node. The default implementation calls the method called
|
|
|
|
:samp:`self.visit_{classname}` where *classname* is the name of the node
|
|
|
|
class, or :meth:`generic_visit` if that method doesn't exist.
|
|
|
|
|
|
|
|
.. method:: generic_visit(node)
|
|
|
|
|
|
|
|
This visitor calls :meth:`visit` on all children of the node.
|
2009-01-03 17:18:54 -04:00
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
Note that child nodes of nodes that have a custom visitor method won't be
|
|
|
|
visited unless the visitor calls :meth:`generic_visit` or visits them
|
|
|
|
itself.
|
|
|
|
|
|
|
|
Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
|
|
|
|
during traversal. For this a special visitor exists
|
|
|
|
(:class:`NodeTransformer`) that allows modifications.
|
|
|
|
|
2019-08-26 04:13:19 -03:00
|
|
|
.. deprecated:: 3.8
|
|
|
|
|
|
|
|
Methods :meth:`visit_Num`, :meth:`visit_Str`, :meth:`visit_Bytes`,
|
|
|
|
:meth:`visit_NameConstant` and :meth:`visit_Ellipsis` are deprecated
|
|
|
|
now and will not be called in future Python versions. Add the
|
|
|
|
:meth:`visit_Constant` method to handle all constant nodes.
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
.. class:: NodeTransformer()
|
|
|
|
|
|
|
|
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
|
|
|
|
allows modification of nodes.
|
|
|
|
|
2009-01-03 17:17:04 -04:00
|
|
|
The :class:`NodeTransformer` will walk the AST and use the return value of
|
|
|
|
the visitor methods to replace or remove the old node. If the return value
|
|
|
|
of the visitor method is ``None``, the node will be removed from its
|
|
|
|
location, otherwise it is replaced with the return value. The return value
|
|
|
|
may be the original node in which case no replacement takes place.
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
Here is an example transformer that rewrites all occurrences of name lookups
|
|
|
|
(``foo``) to ``data['foo']``::
|
|
|
|
|
|
|
|
class RewriteName(NodeTransformer):
|
|
|
|
|
|
|
|
def visit_Name(self, node):
|
2020-01-12 16:38:53 -04:00
|
|
|
return Subscript(
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
value=Name(id='data', ctx=Load()),
|
2020-03-10 13:52:34 -03:00
|
|
|
slice=Constant(value=node.id),
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
ctx=node.ctx
|
2020-03-11 21:48:19 -03:00
|
|
|
)
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
Keep in mind that if the node you're operating on has child nodes you must
|
|
|
|
either transform the child nodes yourself or call the :meth:`generic_visit`
|
|
|
|
method for the node first.
|
|
|
|
|
|
|
|
For nodes that were part of a collection of statements (that applies to all
|
|
|
|
statement nodes), the visitor may also return a list of nodes rather than
|
|
|
|
just a single node.
|
|
|
|
|
2020-01-12 16:38:53 -04:00
|
|
|
If :class:`NodeTransformer` introduces new nodes (that weren't part of
|
|
|
|
original tree) without giving them location information (such as
|
|
|
|
:attr:`lineno`), :func:`fix_missing_locations` should be called with
|
|
|
|
the new sub-tree to recalculate the location information::
|
|
|
|
|
|
|
|
tree = ast.parse('foo', mode='eval')
|
|
|
|
new_tree = fix_missing_locations(RewriteName().visit(tree))
|
|
|
|
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
Usually you use the transformer like this::
|
|
|
|
|
|
|
|
node = YourTransformer().visit(node)
|
|
|
|
|
|
|
|
|
2019-09-09 13:33:13 -03:00
|
|
|
.. function:: dump(node, annotate_fields=True, include_attributes=False, *, indent=None)
|
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63829 | mark.summerfield | 2008-05-31 15:05:34 +0200 (Sat, 31 May 2008) | 4 lines
Added a note to [] that special forms & special chars lose their meaning
and backrefs can't be used inside []
........
r63830 | georg.brandl | 2008-05-31 16:40:09 +0200 (Sat, 31 May 2008) | 2 lines
#3010: clarification about stdin/use_rawinput.
........
r63831 | georg.brandl | 2008-05-31 16:45:55 +0200 (Sat, 31 May 2008) | 2 lines
#3005: add explaining sentence to easydialogs docs.
........
r63858 | georg.brandl | 2008-06-01 18:41:31 +0200 (Sun, 01 Jun 2008) | 2 lines
Add plain text make target.
........
r63865 | georg.brandl | 2008-06-01 21:24:36 +0200 (Sun, 01 Jun 2008) | 2 lines
Spaces vs. tabs.
........
r63879 | gregory.p.smith | 2008-06-02 00:57:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Make the _H #define's match the header file names. Fix comments to
mention the correct type names.
........
r63882 | gregory.p.smith | 2008-06-02 01:48:47 +0200 (Mon, 02 Jun 2008) | 3 lines
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
........
r63948 | alexandre.vassalotti | 2008-06-04 22:41:44 +0200 (Wed, 04 Jun 2008) | 2 lines
Fixed complex.__getnewargs__() to not emit another complex object.
........
r63970 | andrew.kuchling | 2008-06-06 01:33:54 +0200 (Fri, 06 Jun 2008) | 1 line
Document 'utc' parameter
........
r63971 | andrew.kuchling | 2008-06-06 01:35:31 +0200 (Fri, 06 Jun 2008) | 1 line
Add various items
........
r63972 | andrew.kuchling | 2008-06-06 01:35:48 +0200 (Fri, 06 Jun 2008) | 1 line
Grammar fix
........
r63976 | georg.brandl | 2008-06-06 09:34:50 +0200 (Fri, 06 Jun 2008) | 2 lines
Markup fix.
........
r63989 | thomas.heller | 2008-06-06 20:42:11 +0200 (Fri, 06 Jun 2008) | 2 lines
Add a reminder for the maintainer of whatsnew.
........
r64014 | georg.brandl | 2008-06-07 17:59:10 +0200 (Sat, 07 Jun 2008) | 3 lines
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
to ease standalone use of the algorithm.
........
r64015 | georg.brandl | 2008-06-07 18:04:01 +0200 (Sat, 07 Jun 2008) | 2 lines
Revert unwanted changes.
........
r64021 | georg.brandl | 2008-06-07 20:16:12 +0200 (Sat, 07 Jun 2008) | 2 lines
X-ref to numbers module.
........
r64022 | georg.brandl | 2008-06-07 20:17:37 +0200 (Sat, 07 Jun 2008) | 3 lines
Document the "st" API, to avoid confusion with the "new" AST.
Add a note about using the new AST module.
........
r64063 | martin.v.loewis | 2008-06-10 07:03:35 +0200 (Tue, 10 Jun 2008) | 2 lines
Add Gregor Lingl.
........
r64064 | georg.brandl | 2008-06-10 09:45:28 +0200 (Tue, 10 Jun 2008) | 2 lines
Add the "ast" module, containing helpers to ease use of the "_ast" classes.
........
r64065 | raymond.hettinger | 2008-06-10 09:57:15 +0200 (Tue, 10 Jun 2008) | 1 line
Add Arnaud for his efforts on multi-arg set operations.
........
r64067 | georg.brandl | 2008-06-10 14:46:39 +0200 (Tue, 10 Jun 2008) | 2 lines
#2536: fix itertools.permutations and itertools.combinations docstrings.
........
2008-06-10 13:37:50 -03:00
|
|
|
|
|
|
|
Return a formatted dump of the tree in *node*. This is mainly useful for
|
2019-08-29 03:30:23 -03:00
|
|
|
debugging purposes. If *annotate_fields* is true (by default),
|
|
|
|
the returned string will show the names and the values for fields.
|
|
|
|
If *annotate_fields* is false, the result string will be more compact by
|
|
|
|
omitting unambiguous field names. Attributes such as line
|
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64434 | andrew.kuchling | 2008-06-20 18:13:58 -0500 (Fri, 20 Jun 2008) | 1 line
Remove request for e-mail; it's unlikely these classes will be saved
........
r64435 | andrew.kuchling | 2008-06-20 18:14:32 -0500 (Fri, 20 Jun 2008) | 1 line
Grammar fixes
........
r64440 | andrew.kuchling | 2008-06-21 08:29:12 -0500 (Sat, 21 Jun 2008) | 1 line
Docstring typo
........
r64441 | andrew.kuchling | 2008-06-21 08:47:20 -0500 (Sat, 21 Jun 2008) | 1 line
Use repr() for bad input strings; this makes the empty string or binary characters more visible
........
r64442 | andrew.kuchling | 2008-06-21 08:48:38 -0500 (Sat, 21 Jun 2008) | 1 line
Docstring correction
........
r64443 | georg.brandl | 2008-06-21 09:26:19 -0500 (Sat, 21 Jun 2008) | 2 lines
Documentation fix.
........
r64445 | facundo.batista | 2008-06-21 12:30:06 -0500 (Sat, 21 Jun 2008) | 3 lines
Reviewed and updated the documentation. Fixes #3017.
........
r64447 | facundo.batista | 2008-06-21 13:58:04 -0500 (Sat, 21 Jun 2008) | 6 lines
Now a from submitted via POST that also has a query string
will contain both FieldStorage and MiniFieldStorage items.
Fixes #1817.
........
r64448 | facundo.batista | 2008-06-21 14:48:19 -0500 (Sat, 21 Jun 2008) | 5 lines
In the deprecated functions I added an alert to review
specially a section of the subprocess documentation
that helps with the replacing of those functionss.
........
r64450 | georg.brandl | 2008-06-22 04:05:29 -0500 (Sun, 22 Jun 2008) | 2 lines
Turn section references into proper cross-references.
........
r64452 | facundo.batista | 2008-06-22 08:36:20 -0500 (Sun, 22 Jun 2008) | 5 lines
Issue #2722. Now the char buffer to support the path string has
not fixed length, it mallocs memory if needed. As a result, we
don't have a maximum for the getcwd() method.
........
r64455 | facundo.batista | 2008-06-22 10:27:10 -0500 (Sun, 22 Jun 2008) | 4 lines
Issue 3164. Small fix to don't repeat a comparation
without necessity.
........
r64461 | georg.brandl | 2008-06-22 13:11:52 -0500 (Sun, 22 Jun 2008) | 2 lines
#3085: Fix syntax error.
........
r64464 | georg.brandl | 2008-06-22 13:31:54 -0500 (Sun, 22 Jun 2008) | 2 lines
Expand docstrings of sqlite3 functions.
........
r64466 | georg.brandl | 2008-06-22 14:07:59 -0500 (Sun, 22 Jun 2008) | 2 lines
Write out "phi" consistently.
........
r64468 | facundo.batista | 2008-06-22 14:35:24 -0500 (Sun, 22 Jun 2008) | 4 lines
Just returning nothing instead of rising TestSkipped, because
it makes the test fail in the trunk.loewis-sun buildbot.
........
2008-07-02 14:30:14 -03:00
|
|
|
numbers and column offsets are not dumped by default. If this is wanted,
|
2019-08-29 03:30:23 -03:00
|
|
|
*include_attributes* can be set to true.
|
2016-01-07 01:26:53 -04:00
|
|
|
|
2019-09-09 13:33:13 -03:00
|
|
|
If *indent* is a non-negative integer or string, then the tree will be
|
|
|
|
pretty-printed with that indent level. An indent level
|
|
|
|
of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
|
|
|
|
selects the single line representation. Using a positive integer indent
|
|
|
|
indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
|
|
|
|
that string is used to indent each level.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.9
|
|
|
|
Added the *indent* option.
|
|
|
|
|
|
|
|
|
2020-10-18 22:14:11 -03:00
|
|
|
.. _ast-compiler-flags:
|
|
|
|
|
|
|
|
Compiler Flags
|
|
|
|
--------------
|
|
|
|
|
|
|
|
The following flags may be passed to :func:`compile` in order to change
|
|
|
|
effects on the compilation of a program:
|
|
|
|
|
|
|
|
.. data:: PyCF_ALLOW_TOP_LEVEL_AWAIT
|
|
|
|
|
|
|
|
Enables support for top-level ``await``, ``async for``, ``async with``
|
|
|
|
and async comprehensions.
|
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
|
|
|
.. data:: PyCF_ONLY_AST
|
|
|
|
|
|
|
|
Generates and returns an abstract syntax tree instead of returning a
|
|
|
|
compiled code object.
|
|
|
|
|
|
|
|
.. data:: PyCF_TYPE_COMMENTS
|
|
|
|
|
|
|
|
Enables support for :pep:`484` and :pep:`526` style type comments
|
|
|
|
(``# type: <type>``, ``# type: ignore <stuff>``).
|
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
|
|
|
|
|
2019-09-09 17:36:13 -03:00
|
|
|
.. _ast-cli:
|
|
|
|
|
|
|
|
Command-Line Usage
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
|
|
|
The :mod:`ast` module can be executed as a script from the command line.
|
|
|
|
It is as simple as:
|
|
|
|
|
|
|
|
.. code-block:: sh
|
|
|
|
|
|
|
|
python -m ast [-m <mode>] [-a] [infile]
|
|
|
|
|
|
|
|
The following options are accepted:
|
|
|
|
|
|
|
|
.. program:: ast
|
|
|
|
|
|
|
|
.. cmdoption:: -h, --help
|
|
|
|
|
|
|
|
Show the help message and exit.
|
|
|
|
|
|
|
|
.. cmdoption:: -m <mode>
|
|
|
|
--mode <mode>
|
|
|
|
|
|
|
|
Specify what kind of code must be compiled, like the *mode* argument
|
|
|
|
in :func:`parse`.
|
|
|
|
|
2019-12-16 14:23:27 -04:00
|
|
|
.. cmdoption:: --no-type-comments
|
|
|
|
|
|
|
|
Don't parse type comments.
|
|
|
|
|
2019-09-09 17:36:13 -03:00
|
|
|
.. cmdoption:: -a, --include-attributes
|
|
|
|
|
|
|
|
Include attributes such as line numbers and column offsets.
|
|
|
|
|
2019-12-16 14:23:27 -04:00
|
|
|
.. cmdoption:: -i <indent>
|
|
|
|
--indent <indent>
|
|
|
|
|
|
|
|
Indentation of nodes in AST (number of spaces).
|
|
|
|
|
2019-09-09 17:36:13 -03:00
|
|
|
If :file:`infile` is specified its contents are parsed to AST and dumped
|
|
|
|
to stdout. Otherwise, the content is read from stdin.
|
|
|
|
|
|
|
|
|
2016-01-07 01:26:53 -04:00
|
|
|
.. seealso::
|
|
|
|
|
2020-08-11 11:07:49 -03:00
|
|
|
`Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external
|
|
|
|
documentation resource, has good details on working with Python ASTs.
|
|
|
|
|
|
|
|
`ASTTokens <https://asttokens.readthedocs.io/en/latest/user-guide.html>`_
|
|
|
|
annotates Python ASTs with the positions of tokens and text in the source
|
|
|
|
code that generated them. This is helpful for tools that make source code
|
|
|
|
transformations.
|
|
|
|
|
|
|
|
`leoAst.py <http://leoeditor.com/appendices.html#leoast-py>`_ unifies the
|
|
|
|
token-based and parse-tree-based views of python programs by inserting
|
|
|
|
two-way links between tokens and ast nodes.
|
|
|
|
|
|
|
|
`LibCST <https://libcst.readthedocs.io/>`_ parses code as a Concrete Syntax
|
|
|
|
Tree that looks like an ast tree and keeps all formatting details. It's
|
|
|
|
useful for building automated refactoring (codemod) applications and
|
|
|
|
linters.
|
|
|
|
|
|
|
|
`Parso <https://parso.readthedocs.io>`_ is a Python parser that supports
|
|
|
|
error recovery and round-trip parsing for different Python versions (in
|
|
|
|
multiple Python versions). Parso is also able to list multiple syntax errors
|
2020-10-03 21:46:44 -03:00
|
|
|
in your python file.
|