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
|
|
|
"""
|
|
|
|
ast
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The `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 and allows modifications of it.
|
|
|
|
|
|
|
|
An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
|
|
|
|
a flag to the `compile()` builtin function or by using the `parse()`
|
|
|
|
function from this module. The result will be a tree of objects whose
|
|
|
|
classes all inherit from `ast.AST`.
|
|
|
|
|
|
|
|
A modified abstract syntax tree can be compiled into a Python code object
|
|
|
|
using the built-in `compile()` function.
|
|
|
|
|
|
|
|
Additionally various helper functions are provided that make working with
|
|
|
|
the trees simpler. The main intention of the helper functions and this
|
|
|
|
module in general is to provide an easy to use interface for libraries
|
|
|
|
that work tightly with the python syntax (template engines for example).
|
|
|
|
|
|
|
|
|
|
|
|
:copyright: Copyright 2008 by Armin Ronacher.
|
|
|
|
:license: Python License.
|
|
|
|
"""
|
2019-11-24 19:02:40 -04:00
|
|
|
import sys
|
2023-04-24 02:03:49 -03:00
|
|
|
import re
|
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
|
|
|
from _ast import *
|
2019-12-23 12:11:00 -04:00
|
|
|
from contextlib import contextmanager, nullcontext
|
2021-04-21 14:20:44 -03:00
|
|
|
from enum import IntEnum, auto, _simple_enum
|
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-03-07 16:38:08 -04:00
|
|
|
def parse(source, filename='<unknown>', mode='exec', *,
|
2023-08-21 13:31:30 -03:00
|
|
|
type_comments=False, feature_version=None, optimize=-1):
|
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, filename, mode, PyCF_ONLY_AST).
|
2019-01-31 07:40:27 -04:00
|
|
|
Pass type_comments=True to get back type comments where the syntax allows.
|
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
|
|
|
flags = PyCF_ONLY_AST
|
2023-08-21 13:31:30 -03:00
|
|
|
if optimize > 0:
|
|
|
|
flags |= PyCF_OPTIMIZED_AST
|
2019-01-31 07:40:27 -04:00
|
|
|
if type_comments:
|
|
|
|
flags |= PyCF_TYPE_COMMENTS
|
2022-08-29 11:05:24 -03:00
|
|
|
if feature_version is None:
|
|
|
|
feature_version = -1
|
|
|
|
elif isinstance(feature_version, tuple):
|
2019-06-11 21:23:12 -03:00
|
|
|
major, minor = feature_version # Should be a 2-tuple.
|
2022-07-26 06:43:09 -03:00
|
|
|
if major != 3:
|
|
|
|
raise ValueError(f"Unsupported major version: {major}")
|
2019-06-11 21:23:12 -03:00
|
|
|
feature_version = minor
|
|
|
|
# Else it should be an int giving the minor version for 3.x.
|
2019-03-07 16:38:08 -04:00
|
|
|
return compile(source, filename, mode, flags,
|
2023-08-21 13:31:30 -03:00
|
|
|
_feature_version=feature_version, optimize=optimize)
|
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
|
|
|
|
|
|
|
|
|
|
|
def literal_eval(node_or_string):
|
|
|
|
"""
|
2022-10-01 21:55:40 -03:00
|
|
|
Evaluate an expression node or a string containing only a Python
|
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
|
|
|
expression. The string or node provided may only consist of the following
|
2011-04-17 14:10:27 -03:00
|
|
|
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
|
|
|
|
sets, booleans, and None.
|
2022-10-01 21:55:40 -03:00
|
|
|
|
|
|
|
Caution: A complex expression can overflow the C stack and cause a crash.
|
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
|
|
|
"""
|
|
|
|
if isinstance(node_or_string, str):
|
2020-10-03 21:46:44 -03:00
|
|
|
node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
|
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
|
|
|
if isinstance(node_or_string, Expression):
|
|
|
|
node_or_string = node_or_string.body
|
2020-05-05 16:40:56 -03:00
|
|
|
def _raise_malformed_node(node):
|
2020-12-25 13:04:31 -04:00
|
|
|
msg = "malformed node or string"
|
|
|
|
if lno := getattr(node, 'lineno', None):
|
|
|
|
msg += f' on line {lno}'
|
|
|
|
raise ValueError(msg + f': {node!r}')
|
2018-01-04 05:15:39 -04:00
|
|
|
def _convert_num(node):
|
2020-05-05 16:40:56 -03:00
|
|
|
if not isinstance(node, Constant) or type(node.value) not in (int, float, complex):
|
|
|
|
_raise_malformed_node(node)
|
|
|
|
return node.value
|
2018-01-04 05:15:39 -04:00
|
|
|
def _convert_signed_num(node):
|
|
|
|
if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
|
|
|
|
operand = _convert_num(node.operand)
|
|
|
|
if isinstance(node.op, UAdd):
|
|
|
|
return + operand
|
|
|
|
else:
|
|
|
|
return - operand
|
|
|
|
return _convert_num(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
|
|
|
def _convert(node):
|
2016-01-25 19:40:57 -04:00
|
|
|
if isinstance(node, Constant):
|
|
|
|
return node.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
|
|
|
elif isinstance(node, Tuple):
|
|
|
|
return tuple(map(_convert, node.elts))
|
|
|
|
elif isinstance(node, List):
|
|
|
|
return list(map(_convert, node.elts))
|
2010-07-11 06:41:21 -03:00
|
|
|
elif isinstance(node, Set):
|
|
|
|
return set(map(_convert, node.elts))
|
2020-01-03 01:21:18 -04:00
|
|
|
elif (isinstance(node, Call) and isinstance(node.func, Name) and
|
|
|
|
node.func.id == 'set' and node.args == node.keywords == []):
|
|
|
|
return set()
|
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
|
|
|
elif isinstance(node, Dict):
|
2020-05-05 16:40:56 -03:00
|
|
|
if len(node.keys) != len(node.values):
|
|
|
|
_raise_malformed_node(node)
|
2018-01-04 05:15:39 -04:00
|
|
|
return dict(zip(map(_convert, node.keys),
|
|
|
|
map(_convert, node.values)))
|
2016-01-25 19:40:57 -04:00
|
|
|
elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
|
2018-01-04 05:15:39 -04:00
|
|
|
left = _convert_signed_num(node.left)
|
|
|
|
right = _convert_num(node.right)
|
|
|
|
if isinstance(left, (int, float)) and isinstance(right, complex):
|
2016-01-25 19:40:57 -04:00
|
|
|
if isinstance(node.op, Add):
|
|
|
|
return left + right
|
|
|
|
else:
|
|
|
|
return left - right
|
2018-01-04 05:15:39 -04:00
|
|
|
return _convert_signed_num(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
|
|
|
return _convert(node_or_string)
|
|
|
|
|
|
|
|
|
2019-09-09 13:33:13 -03:00
|
|
|
def 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
|
|
|
"""
|
2019-08-29 03:30:23 -03:00
|
|
|
Return a formatted dump of the tree in node. This is mainly useful for
|
|
|
|
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-09-09 13:33:13 -03:00
|
|
|
include_attributes can be set to true. If indent is a non-negative
|
|
|
|
integer or string, then the tree will be pretty-printed with that indent
|
|
|
|
level. None (the default) selects the single line representation.
|
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-09-09 13:33:13 -03:00
|
|
|
def _format(node, level=0):
|
|
|
|
if indent is not None:
|
|
|
|
level += 1
|
|
|
|
prefix = '\n' + indent * level
|
|
|
|
sep = ',\n' + indent * level
|
|
|
|
else:
|
|
|
|
prefix = ''
|
|
|
|
sep = ', '
|
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
|
|
|
if isinstance(node, AST):
|
2020-03-09 19:07:47 -03:00
|
|
|
cls = type(node)
|
2019-08-29 03:30:23 -03:00
|
|
|
args = []
|
2019-09-09 13:33:13 -03:00
|
|
|
allsimple = True
|
2019-08-29 03:30:23 -03:00
|
|
|
keywords = annotate_fields
|
2020-03-09 19:07:47 -03:00
|
|
|
for name in node._fields:
|
2019-08-29 03:30:23 -03:00
|
|
|
try:
|
2020-03-09 19:07:47 -03:00
|
|
|
value = getattr(node, name)
|
2019-08-29 03:30:23 -03:00
|
|
|
except AttributeError:
|
|
|
|
keywords = True
|
2020-03-09 19:07:47 -03:00
|
|
|
continue
|
|
|
|
if value is None and getattr(cls, name, ...) is None:
|
|
|
|
keywords = True
|
|
|
|
continue
|
|
|
|
value, simple = _format(value, level)
|
|
|
|
allsimple = allsimple and simple
|
|
|
|
if keywords:
|
|
|
|
args.append('%s=%s' % (name, value))
|
2019-08-29 03:30:23 -03:00
|
|
|
else:
|
2020-03-09 19:07:47 -03:00
|
|
|
args.append(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
|
|
|
if include_attributes and node._attributes:
|
2020-03-09 19:07:47 -03:00
|
|
|
for name in node._attributes:
|
2019-08-29 03:30:23 -03:00
|
|
|
try:
|
2020-03-09 19:07:47 -03:00
|
|
|
value = getattr(node, name)
|
2019-08-29 03:30:23 -03:00
|
|
|
except AttributeError:
|
2020-03-09 19:07:47 -03:00
|
|
|
continue
|
|
|
|
if value is None and getattr(cls, name, ...) is None:
|
|
|
|
continue
|
|
|
|
value, simple = _format(value, level)
|
|
|
|
allsimple = allsimple and simple
|
|
|
|
args.append('%s=%s' % (name, value))
|
2019-09-09 13:33:13 -03:00
|
|
|
if allsimple and len(args) <= 3:
|
|
|
|
return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args
|
|
|
|
return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False
|
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
|
|
|
elif isinstance(node, list):
|
2019-09-09 13:33:13 -03:00
|
|
|
if not node:
|
|
|
|
return '[]', True
|
|
|
|
return '[%s%s]' % (prefix, sep.join(_format(x, level)[0] for x in node)), False
|
|
|
|
return repr(node), 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
|
|
|
if not isinstance(node, AST):
|
|
|
|
raise TypeError('expected AST, got %r' % node.__class__.__name__)
|
2019-09-09 13:33:13 -03:00
|
|
|
if indent is not None and not isinstance(indent, str):
|
|
|
|
indent = ' ' * indent
|
|
|
|
return _format(node)[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
|
|
|
|
|
|
|
|
|
|
|
def 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 (`lineno`, `col_offset`, `end_lineno`, and `end_col_offset`
|
|
|
|
attributes) 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
|
|
|
"""
|
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
|
|
|
for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
|
2020-03-09 19:07:47 -03:00
|
|
|
if attr in old_node._attributes and attr in new_node._attributes:
|
|
|
|
value = getattr(old_node, attr, None)
|
2020-08-05 10:32:32 -03:00
|
|
|
# end_lineno and end_col_offset are optional attributes, and they
|
|
|
|
# should be copied whether the value is None or not.
|
|
|
|
if value is not None or (
|
|
|
|
hasattr(old_node, attr) and attr.startswith("end_")
|
|
|
|
):
|
2020-03-09 19:07:47 -03:00
|
|
|
setattr(new_node, attr, 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
|
|
|
return new_node
|
|
|
|
|
|
|
|
|
|
|
|
def fix_missing_locations(node):
|
|
|
|
"""
|
|
|
|
When you compile a node tree with compile(), the compiler expects lineno and
|
|
|
|
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*.
|
|
|
|
"""
|
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
|
|
|
def _fix(node, lineno, col_offset, 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
|
|
|
if 'lineno' in node._attributes:
|
|
|
|
if not hasattr(node, 'lineno'):
|
|
|
|
node.lineno = lineno
|
|
|
|
else:
|
|
|
|
lineno = node.lineno
|
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
|
|
|
if 'end_lineno' in node._attributes:
|
2020-03-09 19:07:47 -03:00
|
|
|
if getattr(node, 'end_lineno', None) is None:
|
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
|
|
|
node.end_lineno = end_lineno
|
|
|
|
else:
|
|
|
|
end_lineno = node.end_lineno
|
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
|
|
|
if 'col_offset' in node._attributes:
|
|
|
|
if not hasattr(node, 'col_offset'):
|
|
|
|
node.col_offset = col_offset
|
|
|
|
else:
|
|
|
|
col_offset = node.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
|
|
|
if 'end_col_offset' in node._attributes:
|
2020-03-09 19:07:47 -03:00
|
|
|
if getattr(node, 'end_col_offset', None) is None:
|
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
|
|
|
node.end_col_offset = end_col_offset
|
|
|
|
else:
|
|
|
|
end_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
|
|
|
for child in iter_child_nodes(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
|
|
|
_fix(child, lineno, col_offset, end_lineno, end_col_offset)
|
|
|
|
_fix(node, 1, 0, 1, 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
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
def 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
|
|
|
"""
|
|
|
|
for child in walk(node):
|
2022-11-22 06:41:14 -04:00
|
|
|
# TypeIgnore is a special case where lineno is not an attribute
|
|
|
|
# but rather a field of the node itself.
|
|
|
|
if isinstance(child, TypeIgnore):
|
|
|
|
child.lineno = getattr(child, 'lineno', 0) + n
|
|
|
|
continue
|
|
|
|
|
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
|
|
|
if 'lineno' in child._attributes:
|
|
|
|
child.lineno = getattr(child, 'lineno', 0) + n
|
2020-08-05 10:32:32 -03:00
|
|
|
if (
|
|
|
|
"end_lineno" in child._attributes
|
|
|
|
and (end_lineno := getattr(child, "end_lineno", 0)) is not None
|
|
|
|
):
|
|
|
|
child.end_lineno = end_lineno + n
|
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 node
|
|
|
|
|
|
|
|
|
|
|
|
def iter_fields(node):
|
|
|
|
"""
|
|
|
|
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
|
|
|
|
that is present on *node*.
|
|
|
|
"""
|
|
|
|
for field in node._fields:
|
|
|
|
try:
|
|
|
|
yield field, getattr(node, field)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def 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.
|
|
|
|
"""
|
|
|
|
for name, field in iter_fields(node):
|
|
|
|
if isinstance(field, AST):
|
|
|
|
yield field
|
|
|
|
elif isinstance(field, list):
|
|
|
|
for item in field:
|
|
|
|
if isinstance(item, AST):
|
|
|
|
yield item
|
|
|
|
|
|
|
|
|
|
|
|
def get_docstring(node, clean=True):
|
|
|
|
"""
|
|
|
|
Return the docstring for the given node or None if no docstring can
|
|
|
|
be found. If the node provided does not have docstrings a TypeError
|
|
|
|
will be raised.
|
2017-02-24 02:44:19 -04:00
|
|
|
|
|
|
|
If *clean* is `True`, all tabs are expanded to spaces and any whitespace
|
|
|
|
that can be uniformly removed from the second line onwards is removed.
|
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
|
|
|
"""
|
2015-07-23 02:54:35 -03:00
|
|
|
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
|
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
|
|
|
raise TypeError("%r can't have docstrings" % node.__class__.__name__)
|
2018-06-15 05:05:15 -03:00
|
|
|
if not(node.body and isinstance(node.body[0], Expr)):
|
2018-05-29 06:04:55 -03:00
|
|
|
return None
|
|
|
|
node = node.body[0].value
|
2023-05-06 13:48:07 -03:00
|
|
|
if isinstance(node, Constant) and isinstance(node.value, str):
|
2018-05-29 06:04:55 -03:00
|
|
|
text = node.value
|
|
|
|
else:
|
|
|
|
return None
|
2018-06-15 05:05:15 -03:00
|
|
|
if clean:
|
2016-01-25 19:40:57 -04:00
|
|
|
import inspect
|
|
|
|
text = inspect.cleandoc(text)
|
|
|
|
return text
|
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
|
|
|
|
|
|
|
|
2023-04-24 02:03:49 -03:00
|
|
|
_line_pattern = re.compile(r"(.*?(?:\r\n|\n|\r|$))")
|
|
|
|
def _splitlines_no_ff(source, maxlines=None):
|
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
|
|
|
"""Split a string into lines ignoring form feed and other chars.
|
|
|
|
|
|
|
|
This mimics how the Python parser splits source code.
|
|
|
|
"""
|
|
|
|
lines = []
|
2023-04-24 02:03:49 -03:00
|
|
|
for lineno, match in enumerate(_line_pattern.finditer(source), 1):
|
|
|
|
if maxlines is not None and lineno > maxlines:
|
|
|
|
break
|
|
|
|
lines.append(match[0])
|
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
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def _pad_whitespace(source):
|
2020-02-13 14:32:09 -04:00
|
|
|
r"""Replace all chars except '\f\t' in a line with spaces."""
|
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
|
|
|
result = ''
|
|
|
|
for c in source:
|
|
|
|
if c in '\f\t':
|
|
|
|
result += c
|
|
|
|
else:
|
|
|
|
result += ' '
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_source_segment(source, node, *, padded=False):
|
|
|
|
"""Get source code segment of the *source* that generated *node*.
|
|
|
|
|
|
|
|
If some location information (`lineno`, `end_lineno`, `col_offset`,
|
|
|
|
or `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.
|
|
|
|
"""
|
|
|
|
try:
|
2020-05-18 15:14:12 -03:00
|
|
|
if node.end_lineno is None or node.end_col_offset is None:
|
|
|
|
return None
|
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
|
|
|
lineno = node.lineno - 1
|
|
|
|
end_lineno = node.end_lineno - 1
|
|
|
|
col_offset = node.col_offset
|
|
|
|
end_col_offset = node.end_col_offset
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
2023-04-24 02:03:49 -03:00
|
|
|
lines = _splitlines_no_ff(source, maxlines=end_lineno+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
|
|
|
if end_lineno == lineno:
|
|
|
|
return lines[lineno].encode()[col_offset:end_col_offset].decode()
|
|
|
|
|
|
|
|
if padded:
|
|
|
|
padding = _pad_whitespace(lines[lineno].encode()[:col_offset].decode())
|
|
|
|
else:
|
|
|
|
padding = ''
|
|
|
|
|
|
|
|
first = padding + lines[lineno].encode()[col_offset:].decode()
|
|
|
|
last = lines[end_lineno].encode()[:end_col_offset].decode()
|
|
|
|
lines = lines[lineno+1:end_lineno]
|
|
|
|
|
|
|
|
lines.insert(0, first)
|
|
|
|
lines.append(last)
|
|
|
|
return ''.join(lines)
|
|
|
|
|
|
|
|
|
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
|
|
|
def 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
|
|
|
"""
|
|
|
|
from collections import deque
|
|
|
|
todo = deque([node])
|
|
|
|
while todo:
|
|
|
|
node = todo.popleft()
|
|
|
|
todo.extend(iter_child_nodes(node))
|
|
|
|
yield node
|
|
|
|
|
|
|
|
|
|
|
|
class NodeVisitor(object):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
which is forwarded by the `visit` method.
|
|
|
|
|
|
|
|
This class is meant to be subclassed, with the subclass adding visitor
|
|
|
|
methods.
|
|
|
|
|
|
|
|
Per default the visitor functions for the nodes are ``'visit_'`` +
|
|
|
|
class name of the node. So a `TryFinally` node visit function would
|
|
|
|
be `visit_TryFinally`. This behavior can be changed by overriding
|
|
|
|
the `visit` method. If no visitor function exists for a node
|
|
|
|
(return value `None`) the `generic_visit` visitor is used instead.
|
|
|
|
|
|
|
|
Don't use the `NodeVisitor` if you want to apply changes to nodes during
|
|
|
|
traversing. For this a special visitor exists (`NodeTransformer`) that
|
|
|
|
allows modifications.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def visit(self, node):
|
|
|
|
"""Visit a node."""
|
|
|
|
method = 'visit_' + node.__class__.__name__
|
|
|
|
visitor = getattr(self, method, self.generic_visit)
|
|
|
|
return visitor(node)
|
|
|
|
|
|
|
|
def generic_visit(self, node):
|
|
|
|
"""Called if no explicit visitor function exists for a node."""
|
|
|
|
for field, value in iter_fields(node):
|
|
|
|
if isinstance(value, list):
|
|
|
|
for item in value:
|
|
|
|
if isinstance(item, AST):
|
|
|
|
self.visit(item)
|
|
|
|
elif isinstance(value, AST):
|
|
|
|
self.visit(value)
|
|
|
|
|
2019-08-26 04:13:19 -03:00
|
|
|
def visit_Constant(self, node):
|
|
|
|
value = node.value
|
|
|
|
type_name = _const_node_type_names.get(type(value))
|
|
|
|
if type_name is None:
|
|
|
|
for cls, name in _const_node_type_names.items():
|
|
|
|
if isinstance(value, cls):
|
|
|
|
type_name = name
|
|
|
|
break
|
|
|
|
if type_name is not None:
|
|
|
|
method = 'visit_' + type_name
|
|
|
|
try:
|
|
|
|
visitor = getattr(self, method)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
import warnings
|
|
|
|
warnings.warn(f"{method} is deprecated; add visit_Constant",
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
return visitor(node)
|
|
|
|
return self.generic_visit(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
|
|
|
|
|
|
|
class NodeTransformer(NodeVisitor):
|
|
|
|
"""
|
|
|
|
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
|
|
|
|
allows modification of nodes.
|
|
|
|
|
|
|
|
The `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.
|
|
|
|
|
|
|
|
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-03-11 21:48:19 -03: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.
|
|
|
|
|
|
|
|
Usually you use the transformer like this::
|
|
|
|
|
|
|
|
node = YourTransformer().visit(node)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def generic_visit(self, node):
|
|
|
|
for field, old_value in iter_fields(node):
|
|
|
|
if isinstance(old_value, list):
|
|
|
|
new_values = []
|
|
|
|
for value in old_value:
|
|
|
|
if isinstance(value, AST):
|
|
|
|
value = self.visit(value)
|
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
elif not isinstance(value, AST):
|
|
|
|
new_values.extend(value)
|
|
|
|
continue
|
|
|
|
new_values.append(value)
|
|
|
|
old_value[:] = new_values
|
|
|
|
elif isinstance(old_value, AST):
|
|
|
|
new_node = self.visit(old_value)
|
|
|
|
if new_node is None:
|
|
|
|
delattr(node, field)
|
|
|
|
else:
|
|
|
|
setattr(node, field, new_node)
|
|
|
|
return node
|
2018-09-27 11:42:37 -03:00
|
|
|
|
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
_DEPRECATED_VALUE_ALIAS_MESSAGE = (
|
|
|
|
"{name} is deprecated and will be removed in Python {remove}; use value instead"
|
|
|
|
)
|
|
|
|
_DEPRECATED_CLASS_MESSAGE = (
|
|
|
|
"{name} is deprecated and will be removed in Python {remove}; "
|
|
|
|
"use ast.Constant instead"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-15 13:03:34 -03:00
|
|
|
# If the ast module is loaded more than once, only add deprecated methods once
|
|
|
|
if not hasattr(Constant, 'n'):
|
|
|
|
# The following code is for backward compatibility.
|
|
|
|
# It will be removed in future.
|
2018-09-27 11:42:37 -03:00
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
def _n_getter(self):
|
|
|
|
"""Deprecated. Use value instead."""
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
"Attribute n", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def _n_setter(self, value):
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
"Attribute n", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def _s_getter(self):
|
2020-09-15 13:03:34 -03:00
|
|
|
"""Deprecated. Use value instead."""
|
2023-05-06 13:48:07 -03:00
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
"Attribute s", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
2020-09-15 13:03:34 -03:00
|
|
|
return self.value
|
2018-09-27 11:42:37 -03:00
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
def _s_setter(self, value):
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
"Attribute s", message=_DEPRECATED_VALUE_ALIAS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
2020-09-15 13:03:34 -03:00
|
|
|
self.value = value
|
2018-09-27 11:42:37 -03:00
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
Constant.n = property(_n_getter, _n_setter)
|
|
|
|
Constant.s = property(_s_getter, _s_setter)
|
2018-09-27 11:42:37 -03:00
|
|
|
|
|
|
|
class _ABC(type):
|
|
|
|
|
2020-03-22 15:33:34 -03:00
|
|
|
def __init__(cls, *args):
|
|
|
|
cls.__doc__ = """Deprecated AST node class. Use ast.Constant instead"""
|
|
|
|
|
2018-09-27 11:42:37 -03:00
|
|
|
def __instancecheck__(cls, inst):
|
2023-05-06 13:48:07 -03:00
|
|
|
if cls in _const_types:
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
f"ast.{cls.__qualname__}",
|
|
|
|
message=_DEPRECATED_CLASS_MESSAGE,
|
|
|
|
remove=(3, 14)
|
|
|
|
)
|
2018-09-27 11:42:37 -03:00
|
|
|
if not isinstance(inst, Constant):
|
|
|
|
return False
|
|
|
|
if cls in _const_types:
|
|
|
|
try:
|
|
|
|
value = inst.value
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
else:
|
2019-01-18 15:30:28 -04:00
|
|
|
return (
|
|
|
|
isinstance(value, _const_types[cls]) and
|
|
|
|
not isinstance(value, _const_types_not.get(cls, ()))
|
|
|
|
)
|
2018-09-27 11:42:37 -03:00
|
|
|
return type.__instancecheck__(cls, inst)
|
|
|
|
|
|
|
|
def _new(cls, *args, **kwargs):
|
2020-05-24 18:12:57 -03:00
|
|
|
for key in kwargs:
|
|
|
|
if key not in cls._fields:
|
|
|
|
# arbitrary keyword arguments are accepted
|
|
|
|
continue
|
|
|
|
pos = cls._fields.index(key)
|
|
|
|
if pos < len(args):
|
|
|
|
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
|
2018-09-27 11:42:37 -03:00
|
|
|
if cls in _const_types:
|
2023-05-06 13:48:07 -03:00
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
f"ast.{cls.__qualname__}", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
2018-09-27 11:42:37 -03:00
|
|
|
return Constant(*args, **kwargs)
|
|
|
|
return Constant.__new__(cls, *args, **kwargs)
|
|
|
|
|
|
|
|
class Num(Constant, metaclass=_ABC):
|
|
|
|
_fields = ('n',)
|
|
|
|
__new__ = _new
|
|
|
|
|
|
|
|
class Str(Constant, metaclass=_ABC):
|
|
|
|
_fields = ('s',)
|
|
|
|
__new__ = _new
|
|
|
|
|
|
|
|
class Bytes(Constant, metaclass=_ABC):
|
|
|
|
_fields = ('s',)
|
|
|
|
__new__ = _new
|
|
|
|
|
|
|
|
class NameConstant(Constant, metaclass=_ABC):
|
|
|
|
__new__ = _new
|
|
|
|
|
|
|
|
class Ellipsis(Constant, metaclass=_ABC):
|
|
|
|
_fields = ()
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
2023-05-06 13:48:07 -03:00
|
|
|
if cls is _ast_Ellipsis:
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
"ast.Ellipsis", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
2018-09-27 11:42:37 -03:00
|
|
|
return Constant(..., *args, **kwargs)
|
|
|
|
return Constant.__new__(cls, *args, **kwargs)
|
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
# Keep another reference to Ellipsis in the global namespace
|
|
|
|
# so it can be referenced in Ellipsis.__new__
|
|
|
|
# (The original "Ellipsis" name is removed from the global namespace later on)
|
|
|
|
_ast_Ellipsis = Ellipsis
|
|
|
|
|
2018-09-27 11:42:37 -03:00
|
|
|
_const_types = {
|
|
|
|
Num: (int, float, complex),
|
|
|
|
Str: (str,),
|
|
|
|
Bytes: (bytes,),
|
|
|
|
NameConstant: (type(None), bool),
|
|
|
|
Ellipsis: (type(...),),
|
|
|
|
}
|
2019-01-18 15:30:28 -04:00
|
|
|
_const_types_not = {
|
|
|
|
Num: (bool,),
|
|
|
|
}
|
2020-03-10 13:52:34 -03:00
|
|
|
|
2019-08-26 04:13:19 -03:00
|
|
|
_const_node_type_names = {
|
|
|
|
bool: 'NameConstant', # should be before int
|
|
|
|
type(None): 'NameConstant',
|
|
|
|
int: 'Num',
|
|
|
|
float: 'Num',
|
|
|
|
complex: 'Num',
|
|
|
|
str: 'Str',
|
|
|
|
bytes: 'Bytes',
|
|
|
|
type(...): 'Ellipsis',
|
|
|
|
}
|
2019-09-09 17:36:13 -03:00
|
|
|
|
2020-03-22 15:33:34 -03:00
|
|
|
class slice(AST):
|
|
|
|
"""Deprecated AST node class."""
|
|
|
|
|
|
|
|
class Index(slice):
|
|
|
|
"""Deprecated AST node class. Use the index value directly instead."""
|
2020-03-10 13:52:34 -03:00
|
|
|
def __new__(cls, value, **kwargs):
|
|
|
|
return value
|
|
|
|
|
2020-03-22 15:33:34 -03:00
|
|
|
class ExtSlice(slice):
|
|
|
|
"""Deprecated AST node class. Use ast.Tuple instead."""
|
2020-03-10 13:52:34 -03:00
|
|
|
def __new__(cls, dims=(), **kwargs):
|
|
|
|
return Tuple(list(dims), Load(), **kwargs)
|
|
|
|
|
2020-09-15 13:03:34 -03:00
|
|
|
# If the ast module is loaded more than once, only add deprecated methods once
|
|
|
|
if not hasattr(Tuple, 'dims'):
|
|
|
|
# The following code is for backward compatibility.
|
|
|
|
# It will be removed in future.
|
2020-03-10 13:52:34 -03:00
|
|
|
|
2020-09-15 13:03:34 -03:00
|
|
|
def _dims_getter(self):
|
|
|
|
"""Deprecated. Use elts instead."""
|
|
|
|
return self.elts
|
2020-03-10 13:52:34 -03:00
|
|
|
|
2020-09-15 13:03:34 -03:00
|
|
|
def _dims_setter(self, value):
|
|
|
|
self.elts = value
|
|
|
|
|
|
|
|
Tuple.dims = property(_dims_getter, _dims_setter)
|
2020-03-10 13:52:34 -03:00
|
|
|
|
2020-03-22 15:33:34 -03:00
|
|
|
class Suite(mod):
|
|
|
|
"""Deprecated AST node class. Unused in Python 3."""
|
|
|
|
|
|
|
|
class AugLoad(expr_context):
|
|
|
|
"""Deprecated AST node class. Unused in Python 3."""
|
|
|
|
|
|
|
|
class AugStore(expr_context):
|
|
|
|
"""Deprecated AST node class. Unused in Python 3."""
|
|
|
|
|
|
|
|
class Param(expr_context):
|
|
|
|
"""Deprecated AST node class. Unused in Python 3."""
|
|
|
|
|
2020-03-10 13:52:34 -03:00
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
# Large float and imaginary literals get turned into infinities in the AST.
|
|
|
|
# We unparse those infinities to INFSTR.
|
|
|
|
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
|
|
|
|
|
2021-04-21 14:20:44 -03:00
|
|
|
@_simple_enum(IntEnum)
|
|
|
|
class _Precedence:
|
2020-03-01 16:12:17 -04:00
|
|
|
"""Precedence table that originated from python grammar."""
|
|
|
|
|
2021-05-16 10:33:22 -03:00
|
|
|
NAMED_EXPR = auto() # <target> := <expr1>
|
|
|
|
TUPLE = auto() # <expr1>, <expr2>
|
2020-03-01 16:12:17 -04:00
|
|
|
YIELD = auto() # 'yield', 'yield from'
|
|
|
|
TEST = auto() # 'if'-'else', 'lambda'
|
|
|
|
OR = auto() # 'or'
|
|
|
|
AND = auto() # 'and'
|
|
|
|
NOT = auto() # 'not'
|
|
|
|
CMP = auto() # '<', '>', '==', '>=', '<=', '!=',
|
|
|
|
# 'in', 'not in', 'is', 'is not'
|
|
|
|
EXPR = auto()
|
|
|
|
BOR = EXPR # '|'
|
|
|
|
BXOR = auto() # '^'
|
|
|
|
BAND = auto() # '&'
|
|
|
|
SHIFT = auto() # '<<', '>>'
|
|
|
|
ARITH = auto() # '+', '-'
|
|
|
|
TERM = auto() # '*', '@', '/', '%', '//'
|
|
|
|
FACTOR = auto() # unary '+', '-', '~'
|
|
|
|
POWER = auto() # '**'
|
|
|
|
AWAIT = auto() # 'await'
|
|
|
|
ATOM = auto()
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
try:
|
|
|
|
return self.__class__(self + 1)
|
|
|
|
except ValueError:
|
|
|
|
return self
|
|
|
|
|
2020-11-20 17:16:42 -04:00
|
|
|
|
|
|
|
_SINGLE_QUOTES = ("'", '"')
|
|
|
|
_MULTI_QUOTES = ('"""', "'''")
|
|
|
|
_ALL_QUOTES = (*_SINGLE_QUOTES, *_MULTI_QUOTES)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
class _Unparser(NodeVisitor):
|
|
|
|
"""Methods in this class recursively traverse an AST and
|
|
|
|
output source code for the abstract syntax; original formatting
|
|
|
|
is disregarded."""
|
|
|
|
|
2024-01-16 15:24:25 -04:00
|
|
|
def __init__(self):
|
2019-11-24 19:02:40 -04:00
|
|
|
self._source = []
|
2020-03-01 16:12:17 -04:00
|
|
|
self._precedences = {}
|
2020-05-16 20:04:12 -03:00
|
|
|
self._type_ignores = {}
|
2019-11-24 19:02:40 -04:00
|
|
|
self._indent = 0
|
2021-12-14 12:48:15 -04:00
|
|
|
self._in_try_star = False
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def interleave(self, inter, f, seq):
|
|
|
|
"""Call f on each item in seq, calling inter() in between."""
|
|
|
|
seq = iter(seq)
|
|
|
|
try:
|
|
|
|
f(next(seq))
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for x in seq:
|
|
|
|
inter()
|
|
|
|
f(x)
|
|
|
|
|
2020-03-09 17:27:03 -03:00
|
|
|
def items_view(self, traverser, items):
|
|
|
|
"""Traverse and separate the given *items* with a comma and append it to
|
|
|
|
the buffer. If *items* is a single item sequence, a trailing comma
|
|
|
|
will be added."""
|
|
|
|
if len(items) == 1:
|
|
|
|
traverser(items[0])
|
|
|
|
self.write(",")
|
|
|
|
else:
|
|
|
|
self.interleave(lambda: self.write(", "), traverser, items)
|
|
|
|
|
2020-05-03 14:11:51 -03:00
|
|
|
def maybe_newline(self):
|
|
|
|
"""Adds a newline if it isn't the start of generated source"""
|
|
|
|
if self._source:
|
|
|
|
self.write("\n")
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def fill(self, text=""):
|
|
|
|
"""Indent a piece of text and append it, according to the current
|
|
|
|
indentation level"""
|
2020-05-03 14:11:51 -03:00
|
|
|
self.maybe_newline()
|
|
|
|
self.write(" " * self._indent + text)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2021-05-15 09:55:53 -03:00
|
|
|
def write(self, *text):
|
|
|
|
"""Add new source parts"""
|
|
|
|
self._source.extend(text)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2021-05-08 20:32:04 -03:00
|
|
|
@contextmanager
|
|
|
|
def buffered(self, buffer = None):
|
|
|
|
if buffer is None:
|
|
|
|
buffer = []
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2021-05-08 20:32:04 -03:00
|
|
|
original_source = self._source
|
|
|
|
self._source = buffer
|
|
|
|
yield buffer
|
|
|
|
self._source = original_source
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2019-12-23 12:42:48 -04:00
|
|
|
@contextmanager
|
2020-05-16 20:04:12 -03:00
|
|
|
def block(self, *, extra = None):
|
2019-11-24 19:02:40 -04:00
|
|
|
"""A context manager for preparing the source for blocks. It adds
|
|
|
|
the character':', increases the indentation on enter and decreases
|
2020-05-16 20:04:12 -03:00
|
|
|
the indentation on exit. If *extra* is given, it will be directly
|
|
|
|
appended after the colon character.
|
|
|
|
"""
|
2019-12-23 12:42:48 -04:00
|
|
|
self.write(":")
|
2020-05-16 20:04:12 -03:00
|
|
|
if extra:
|
|
|
|
self.write(extra)
|
2019-12-23 12:42:48 -04:00
|
|
|
self._indent += 1
|
|
|
|
yield
|
|
|
|
self._indent -= 1
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2019-12-23 12:11:00 -04:00
|
|
|
@contextmanager
|
|
|
|
def delimit(self, start, end):
|
|
|
|
"""A context manager for preparing the source for expressions. It adds
|
|
|
|
*start* to the buffer and enters, after exit it adds *end*."""
|
|
|
|
|
|
|
|
self.write(start)
|
|
|
|
yield
|
|
|
|
self.write(end)
|
|
|
|
|
|
|
|
def delimit_if(self, start, end, condition):
|
|
|
|
if condition:
|
|
|
|
return self.delimit(start, end)
|
|
|
|
else:
|
|
|
|
return nullcontext()
|
|
|
|
|
2020-03-01 16:12:17 -04:00
|
|
|
def require_parens(self, precedence, node):
|
|
|
|
"""Shortcut to adding precedence related parens"""
|
|
|
|
return self.delimit_if("(", ")", self.get_precedence(node) > precedence)
|
|
|
|
|
|
|
|
def get_precedence(self, node):
|
|
|
|
return self._precedences.get(node, _Precedence.TEST)
|
|
|
|
|
|
|
|
def set_precedence(self, precedence, *nodes):
|
|
|
|
for node in nodes:
|
|
|
|
self._precedences[node] = precedence
|
|
|
|
|
2020-03-02 14:59:01 -04:00
|
|
|
def get_raw_docstring(self, node):
|
|
|
|
"""If a docstring node is found in the body of the *node* parameter,
|
|
|
|
return that docstring node, None otherwise.
|
|
|
|
|
|
|
|
Logic mirrored from ``_PyAST_GetDocString``."""
|
|
|
|
if not isinstance(
|
|
|
|
node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)
|
|
|
|
) or len(node.body) < 1:
|
|
|
|
return None
|
|
|
|
node = node.body[0]
|
|
|
|
if not isinstance(node, Expr):
|
|
|
|
return None
|
|
|
|
node = node.value
|
|
|
|
if isinstance(node, Constant) and isinstance(node.value, str):
|
|
|
|
return node
|
|
|
|
|
2020-05-16 20:04:12 -03:00
|
|
|
def get_type_comment(self, node):
|
|
|
|
comment = self._type_ignores.get(node.lineno) or node.type_comment
|
|
|
|
if comment is not None:
|
|
|
|
return f" # type: {comment}"
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def traverse(self, node):
|
|
|
|
if isinstance(node, list):
|
|
|
|
for item in node:
|
|
|
|
self.traverse(item)
|
|
|
|
else:
|
|
|
|
super().visit(node)
|
|
|
|
|
2021-04-29 02:58:44 -03:00
|
|
|
# Note: as visit() resets the output text, do NOT rely on
|
|
|
|
# NodeVisitor.generic_visit to handle any nodes (as it calls back in to
|
|
|
|
# the subclass visit() method, which resets self._source to an empty list)
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit(self, node):
|
|
|
|
"""Outputs a source code string that, if converted back to an ast
|
|
|
|
(using ast.parse) will generate an AST equivalent to *node*"""
|
|
|
|
self._source = []
|
|
|
|
self.traverse(node)
|
|
|
|
return "".join(self._source)
|
|
|
|
|
2020-03-02 14:59:01 -04:00
|
|
|
def _write_docstring_and_traverse_body(self, node):
|
|
|
|
if (docstring := self.get_raw_docstring(node)):
|
|
|
|
self._write_docstring(docstring)
|
|
|
|
self.traverse(node.body[1:])
|
|
|
|
else:
|
|
|
|
self.traverse(node.body)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_Module(self, node):
|
2020-05-16 20:04:12 -03:00
|
|
|
self._type_ignores = {
|
|
|
|
ignore.lineno: f"ignore{ignore.tag}"
|
|
|
|
for ignore in node.type_ignores
|
|
|
|
}
|
2020-03-02 14:59:01 -04:00
|
|
|
self._write_docstring_and_traverse_body(node)
|
2020-05-16 20:04:12 -03:00
|
|
|
self._type_ignores.clear()
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2020-03-15 16:56:57 -03:00
|
|
|
def visit_FunctionType(self, node):
|
|
|
|
with self.delimit("(", ")"):
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "), self.traverse, node.argtypes
|
|
|
|
)
|
|
|
|
|
|
|
|
self.write(" -> ")
|
|
|
|
self.traverse(node.returns)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_Expr(self, node):
|
|
|
|
self.fill()
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.YIELD, node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_NamedExpr(self, node):
|
2021-05-16 10:33:22 -03:00
|
|
|
with self.require_parens(_Precedence.NAMED_EXPR, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.target, node.value)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.target)
|
|
|
|
self.write(" := ")
|
|
|
|
self.traverse(node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Import(self, node):
|
|
|
|
self.fill("import ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.names)
|
|
|
|
|
|
|
|
def visit_ImportFrom(self, node):
|
|
|
|
self.fill("from ")
|
2022-09-05 14:14:50 -03:00
|
|
|
self.write("." * (node.level or 0))
|
2019-11-24 19:02:40 -04:00
|
|
|
if node.module:
|
|
|
|
self.write(node.module)
|
|
|
|
self.write(" import ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.names)
|
|
|
|
|
|
|
|
def visit_Assign(self, node):
|
|
|
|
self.fill()
|
|
|
|
for target in node.targets:
|
2021-05-16 10:33:22 -03:00
|
|
|
self.set_precedence(_Precedence.TUPLE, target)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(target)
|
|
|
|
self.write(" = ")
|
|
|
|
self.traverse(node.value)
|
2020-05-16 20:04:12 -03:00
|
|
|
if type_comment := self.get_type_comment(node):
|
|
|
|
self.write(type_comment)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_AugAssign(self, node):
|
|
|
|
self.fill()
|
|
|
|
self.traverse(node.target)
|
|
|
|
self.write(" " + self.binop[node.op.__class__.__name__] + "= ")
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_AnnAssign(self, node):
|
|
|
|
self.fill()
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit_if("(", ")", not node.simple and isinstance(node.target, Name)):
|
|
|
|
self.traverse(node.target)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.annotation)
|
|
|
|
if node.value:
|
|
|
|
self.write(" = ")
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_Return(self, node):
|
|
|
|
self.fill("return")
|
|
|
|
if node.value:
|
|
|
|
self.write(" ")
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_Pass(self, node):
|
|
|
|
self.fill("pass")
|
|
|
|
|
|
|
|
def visit_Break(self, node):
|
|
|
|
self.fill("break")
|
|
|
|
|
|
|
|
def visit_Continue(self, node):
|
|
|
|
self.fill("continue")
|
|
|
|
|
|
|
|
def visit_Delete(self, node):
|
|
|
|
self.fill("del ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.targets)
|
|
|
|
|
|
|
|
def visit_Assert(self, node):
|
|
|
|
self.fill("assert ")
|
|
|
|
self.traverse(node.test)
|
|
|
|
if node.msg:
|
|
|
|
self.write(", ")
|
|
|
|
self.traverse(node.msg)
|
|
|
|
|
|
|
|
def visit_Global(self, node):
|
|
|
|
self.fill("global ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.write, node.names)
|
|
|
|
|
|
|
|
def visit_Nonlocal(self, node):
|
|
|
|
self.fill("nonlocal ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.write, node.names)
|
|
|
|
|
|
|
|
def visit_Await(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.AWAIT, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
self.write("await")
|
|
|
|
if node.value:
|
|
|
|
self.write(" ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.value)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Yield(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.YIELD, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
self.write("yield")
|
|
|
|
if node.value:
|
|
|
|
self.write(" ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.value)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_YieldFrom(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.YIELD, node):
|
2020-01-02 14:20:04 -04:00
|
|
|
self.write("yield from ")
|
|
|
|
if not node.value:
|
|
|
|
raise ValueError("Node can't be used without a value attribute.")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.value)
|
2020-01-02 14:20:04 -04:00
|
|
|
self.traverse(node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Raise(self, node):
|
|
|
|
self.fill("raise")
|
|
|
|
if not node.exc:
|
|
|
|
if node.cause:
|
|
|
|
raise ValueError(f"Node can't use cause without an exception.")
|
|
|
|
return
|
|
|
|
self.write(" ")
|
|
|
|
self.traverse(node.exc)
|
|
|
|
if node.cause:
|
|
|
|
self.write(" from ")
|
|
|
|
self.traverse(node.cause)
|
|
|
|
|
2021-12-14 12:48:15 -04:00
|
|
|
def do_visit_try(self, node):
|
2019-11-24 19:02:40 -04:00
|
|
|
self.fill("try")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
for ex in node.handlers:
|
|
|
|
self.traverse(ex)
|
|
|
|
if node.orelse:
|
|
|
|
self.fill("else")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.orelse)
|
|
|
|
if node.finalbody:
|
|
|
|
self.fill("finally")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.finalbody)
|
|
|
|
|
2021-12-14 12:48:15 -04:00
|
|
|
def visit_Try(self, node):
|
|
|
|
prev_in_try_star = self._in_try_star
|
|
|
|
try:
|
|
|
|
self._in_try_star = False
|
|
|
|
self.do_visit_try(node)
|
|
|
|
finally:
|
|
|
|
self._in_try_star = prev_in_try_star
|
|
|
|
|
|
|
|
def visit_TryStar(self, node):
|
|
|
|
prev_in_try_star = self._in_try_star
|
|
|
|
try:
|
|
|
|
self._in_try_star = True
|
|
|
|
self.do_visit_try(node)
|
|
|
|
finally:
|
|
|
|
self._in_try_star = prev_in_try_star
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_ExceptHandler(self, node):
|
2021-12-14 12:48:15 -04:00
|
|
|
self.fill("except*" if self._in_try_star else "except")
|
2019-11-24 19:02:40 -04:00
|
|
|
if node.type:
|
|
|
|
self.write(" ")
|
|
|
|
self.traverse(node.type)
|
|
|
|
if node.name:
|
|
|
|
self.write(" as ")
|
|
|
|
self.write(node.name)
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
|
|
|
|
def visit_ClassDef(self, node):
|
2020-05-03 14:11:51 -03:00
|
|
|
self.maybe_newline()
|
2019-11-24 19:02:40 -04:00
|
|
|
for deco in node.decorator_list:
|
|
|
|
self.fill("@")
|
|
|
|
self.traverse(deco)
|
|
|
|
self.fill("class " + node.name)
|
2023-06-16 13:31:23 -03:00
|
|
|
if hasattr(node, "type_params"):
|
|
|
|
self._type_params_helper(node.type_params)
|
2020-05-16 18:53:25 -03:00
|
|
|
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
|
2019-12-23 12:11:00 -04:00
|
|
|
comma = False
|
|
|
|
for e in node.bases:
|
|
|
|
if comma:
|
|
|
|
self.write(", ")
|
|
|
|
else:
|
|
|
|
comma = True
|
|
|
|
self.traverse(e)
|
|
|
|
for e in node.keywords:
|
|
|
|
if comma:
|
|
|
|
self.write(", ")
|
|
|
|
else:
|
|
|
|
comma = True
|
|
|
|
self.traverse(e)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
with self.block():
|
2020-03-02 14:59:01 -04:00
|
|
|
self._write_docstring_and_traverse_body(node)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_FunctionDef(self, node):
|
2020-03-02 14:59:01 -04:00
|
|
|
self._function_helper(node, "def")
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_AsyncFunctionDef(self, node):
|
2020-03-02 14:59:01 -04:00
|
|
|
self._function_helper(node, "async def")
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2020-03-02 14:59:01 -04:00
|
|
|
def _function_helper(self, node, fill_suffix):
|
2020-05-03 14:11:51 -03:00
|
|
|
self.maybe_newline()
|
2019-11-24 19:02:40 -04:00
|
|
|
for deco in node.decorator_list:
|
|
|
|
self.fill("@")
|
|
|
|
self.traverse(deco)
|
2019-12-23 12:11:00 -04:00
|
|
|
def_str = fill_suffix + " " + node.name
|
2019-11-24 19:02:40 -04:00
|
|
|
self.fill(def_str)
|
2023-06-16 13:31:23 -03:00
|
|
|
if hasattr(node, "type_params"):
|
|
|
|
self._type_params_helper(node.type_params)
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("(", ")"):
|
|
|
|
self.traverse(node.args)
|
2019-11-24 19:02:40 -04:00
|
|
|
if node.returns:
|
|
|
|
self.write(" -> ")
|
|
|
|
self.traverse(node.returns)
|
2020-05-16 20:04:12 -03:00
|
|
|
with self.block(extra=self.get_type_comment(node)):
|
2020-03-02 14:59:01 -04:00
|
|
|
self._write_docstring_and_traverse_body(node)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2023-05-22 01:25:09 -03:00
|
|
|
def _type_params_helper(self, type_params):
|
|
|
|
if type_params is not None and len(type_params) > 0:
|
2023-05-16 00:36:23 -03:00
|
|
|
with self.delimit("[", "]"):
|
2023-05-22 01:25:09 -03:00
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, type_params)
|
2023-05-16 00:36:23 -03:00
|
|
|
|
|
|
|
def visit_TypeVar(self, node):
|
|
|
|
self.write(node.name)
|
|
|
|
if node.bound:
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.bound)
|
|
|
|
|
|
|
|
def visit_TypeVarTuple(self, node):
|
|
|
|
self.write("*" + node.name)
|
|
|
|
|
|
|
|
def visit_ParamSpec(self, node):
|
|
|
|
self.write("**" + node.name)
|
|
|
|
|
|
|
|
def visit_TypeAlias(self, node):
|
|
|
|
self.fill("type ")
|
|
|
|
self.traverse(node.name)
|
2023-05-22 01:25:09 -03:00
|
|
|
self._type_params_helper(node.type_params)
|
2023-05-16 00:36:23 -03:00
|
|
|
self.write(" = ")
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_For(self, node):
|
2020-03-02 14:59:01 -04:00
|
|
|
self._for_helper("for ", node)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_AsyncFor(self, node):
|
2020-03-02 14:59:01 -04:00
|
|
|
self._for_helper("async for ", node)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2020-03-02 14:59:01 -04:00
|
|
|
def _for_helper(self, fill, node):
|
2019-11-24 19:02:40 -04:00
|
|
|
self.fill(fill)
|
2021-05-16 10:33:22 -03:00
|
|
|
self.set_precedence(_Precedence.TUPLE, node.target)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.target)
|
|
|
|
self.write(" in ")
|
|
|
|
self.traverse(node.iter)
|
2020-05-16 20:04:12 -03:00
|
|
|
with self.block(extra=self.get_type_comment(node)):
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.body)
|
|
|
|
if node.orelse:
|
|
|
|
self.fill("else")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.orelse)
|
|
|
|
|
|
|
|
def visit_If(self, node):
|
|
|
|
self.fill("if ")
|
|
|
|
self.traverse(node.test)
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
# collapse nested ifs into equivalent elifs.
|
|
|
|
while node.orelse and len(node.orelse) == 1 and isinstance(node.orelse[0], If):
|
|
|
|
node = node.orelse[0]
|
|
|
|
self.fill("elif ")
|
|
|
|
self.traverse(node.test)
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
# final else
|
|
|
|
if node.orelse:
|
|
|
|
self.fill("else")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.orelse)
|
|
|
|
|
|
|
|
def visit_While(self, node):
|
|
|
|
self.fill("while ")
|
|
|
|
self.traverse(node.test)
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
if node.orelse:
|
|
|
|
self.fill("else")
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.orelse)
|
|
|
|
|
|
|
|
def visit_With(self, node):
|
|
|
|
self.fill("with ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.items)
|
2020-05-16 20:04:12 -03:00
|
|
|
with self.block(extra=self.get_type_comment(node)):
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.body)
|
|
|
|
|
|
|
|
def visit_AsyncWith(self, node):
|
|
|
|
self.fill("async with ")
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.items)
|
2020-05-16 20:04:12 -03:00
|
|
|
with self.block(extra=self.get_type_comment(node)):
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.body)
|
|
|
|
|
2020-11-20 17:16:42 -04:00
|
|
|
def _str_literal_helper(
|
|
|
|
self, string, *, quote_types=_ALL_QUOTES, escape_special_whitespace=False
|
|
|
|
):
|
|
|
|
"""Helper for writing string literals, minimizing escapes.
|
|
|
|
Returns the tuple (string literal to write, possible quote types).
|
|
|
|
"""
|
|
|
|
def escape_char(c):
|
|
|
|
# \n and \t are non-printable, but we only escape them if
|
|
|
|
# escape_special_whitespace is True
|
|
|
|
if not escape_special_whitespace and c in "\n\t":
|
|
|
|
return c
|
|
|
|
# Always escape backslashes and other non-printable characters
|
|
|
|
if c == "\\" or not c.isprintable():
|
|
|
|
return c.encode("unicode_escape").decode("ascii")
|
|
|
|
return c
|
|
|
|
|
|
|
|
escaped_string = "".join(map(escape_char, string))
|
|
|
|
possible_quotes = quote_types
|
|
|
|
if "\n" in escaped_string:
|
|
|
|
possible_quotes = [q for q in possible_quotes if q in _MULTI_QUOTES]
|
|
|
|
possible_quotes = [q for q in possible_quotes if q not in escaped_string]
|
|
|
|
if not possible_quotes:
|
|
|
|
# If there aren't any possible_quotes, fallback to using repr
|
|
|
|
# on the original string. Try to use a quote from quote_types,
|
|
|
|
# e.g., so that we use triple quotes for docstrings.
|
|
|
|
string = repr(string)
|
|
|
|
quote = next((q for q in quote_types if string[0] in q), string[0])
|
|
|
|
return string[1:-1], [quote]
|
|
|
|
if escaped_string:
|
|
|
|
# Sort so that we prefer '''"''' over """\""""
|
|
|
|
possible_quotes.sort(key=lambda q: q[0] == escaped_string[-1])
|
|
|
|
# If we're using triple quotes and we'd need to escape a final
|
|
|
|
# quote, escape it
|
|
|
|
if possible_quotes[0][0] == escaped_string[-1]:
|
|
|
|
assert len(possible_quotes[0]) == 3
|
|
|
|
escaped_string = escaped_string[:-1] + "\\" + escaped_string[-1]
|
|
|
|
return escaped_string, possible_quotes
|
|
|
|
|
|
|
|
def _write_str_avoiding_backslashes(self, string, *, quote_types=_ALL_QUOTES):
|
|
|
|
"""Write string literal value with a best effort attempt to avoid backslashes."""
|
|
|
|
string, quote_types = self._str_literal_helper(string, quote_types=quote_types)
|
|
|
|
quote_type = quote_types[0]
|
|
|
|
self.write(f"{quote_type}{string}{quote_type}")
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_JoinedStr(self, node):
|
|
|
|
self.write("f")
|
2023-09-05 17:01:23 -03:00
|
|
|
|
2021-05-08 20:32:04 -03:00
|
|
|
fstring_parts = []
|
2020-11-20 17:16:42 -04:00
|
|
|
for value in node.values:
|
2021-05-08 20:32:04 -03:00
|
|
|
with self.buffered() as buffer:
|
|
|
|
self._write_fstring_inner(value)
|
|
|
|
fstring_parts.append(
|
|
|
|
("".join(buffer), isinstance(value, Constant))
|
|
|
|
)
|
|
|
|
|
|
|
|
new_fstring_parts = []
|
|
|
|
quote_types = list(_ALL_QUOTES)
|
2023-09-18 10:56:19 -03:00
|
|
|
fallback_to_repr = False
|
2021-05-08 20:32:04 -03:00
|
|
|
for value, is_constant in fstring_parts:
|
2023-09-05 17:01:23 -03:00
|
|
|
if is_constant:
|
2023-09-18 10:56:19 -03:00
|
|
|
value, new_quote_types = self._str_literal_helper(
|
2023-09-05 17:01:23 -03:00
|
|
|
value,
|
|
|
|
quote_types=quote_types,
|
|
|
|
escape_special_whitespace=True,
|
|
|
|
)
|
2023-09-18 10:56:19 -03:00
|
|
|
if set(new_quote_types).isdisjoint(quote_types):
|
|
|
|
fallback_to_repr = True
|
|
|
|
break
|
|
|
|
quote_types = new_quote_types
|
2023-09-05 17:01:23 -03:00
|
|
|
elif "\n" in value:
|
|
|
|
quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
|
2023-09-18 10:56:19 -03:00
|
|
|
assert quote_types
|
2021-05-08 20:32:04 -03:00
|
|
|
new_fstring_parts.append(value)
|
|
|
|
|
2023-09-18 10:56:19 -03:00
|
|
|
if fallback_to_repr:
|
|
|
|
# If we weren't able to find a quote type that works for all parts
|
|
|
|
# of the JoinedStr, fallback to using repr and triple single quotes.
|
|
|
|
quote_types = ["'''"]
|
|
|
|
new_fstring_parts.clear()
|
|
|
|
for value, is_constant in fstring_parts:
|
|
|
|
if is_constant:
|
|
|
|
value = repr('"' + value) # force repr to use single quotes
|
|
|
|
expected_prefix = "'\""
|
|
|
|
assert value.startswith(expected_prefix), repr(value)
|
|
|
|
value = value[len(expected_prefix):-1]
|
|
|
|
new_fstring_parts.append(value)
|
|
|
|
|
2021-05-08 20:32:04 -03:00
|
|
|
value = "".join(new_fstring_parts)
|
2020-11-20 17:16:42 -04:00
|
|
|
quote_type = quote_types[0]
|
|
|
|
self.write(f"{quote_type}{value}{quote_type}")
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2024-02-21 06:24:08 -04:00
|
|
|
def _write_fstring_inner(self, node, is_format_spec=False):
|
2021-05-08 20:32:04 -03:00
|
|
|
if isinstance(node, JoinedStr):
|
|
|
|
# for both the f-string itself, and format_spec
|
|
|
|
for value in node.values:
|
2024-02-21 06:24:08 -04:00
|
|
|
self._write_fstring_inner(value, is_format_spec=is_format_spec)
|
2021-05-08 20:32:04 -03:00
|
|
|
elif isinstance(node, Constant) and isinstance(node.value, str):
|
|
|
|
value = node.value.replace("{", "{{").replace("}", "}}")
|
2024-02-21 06:24:08 -04:00
|
|
|
|
|
|
|
if is_format_spec:
|
|
|
|
value = value.replace("\\", "\\\\")
|
|
|
|
value = value.replace("'", "\\'")
|
|
|
|
value = value.replace('"', '\\"')
|
2023-10-05 10:26:44 -03:00
|
|
|
value = value.replace("\n", "\\n")
|
2021-05-08 20:32:04 -03:00
|
|
|
self.write(value)
|
|
|
|
elif isinstance(node, FormattedValue):
|
|
|
|
self.visit_FormattedValue(node)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unexpected node inside JoinedStr, {node!r}")
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2021-05-08 20:32:04 -03:00
|
|
|
def visit_FormattedValue(self, node):
|
|
|
|
def unparse_inner(inner):
|
2023-09-05 17:01:23 -03:00
|
|
|
unparser = type(self)()
|
2021-05-08 20:32:04 -03:00
|
|
|
unparser.set_precedence(_Precedence.TEST.next(), inner)
|
|
|
|
return unparser.visit(inner)
|
|
|
|
|
|
|
|
with self.delimit("{", "}"):
|
|
|
|
expr = unparse_inner(node.value)
|
|
|
|
if expr.startswith("{"):
|
|
|
|
# Separate pair of opening brackets as "{ {"
|
|
|
|
self.write(" ")
|
|
|
|
self.write(expr)
|
|
|
|
if node.conversion != -1:
|
|
|
|
self.write(f"!{chr(node.conversion)}")
|
|
|
|
if node.format_spec:
|
|
|
|
self.write(":")
|
2024-02-21 06:24:08 -04:00
|
|
|
self._write_fstring_inner(node.format_spec, is_format_spec=True)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Name(self, node):
|
|
|
|
self.write(node.id)
|
|
|
|
|
2020-03-02 14:59:01 -04:00
|
|
|
def _write_docstring(self, node):
|
|
|
|
self.fill()
|
|
|
|
if node.kind == "u":
|
|
|
|
self.write("u")
|
2020-11-20 17:16:42 -04:00
|
|
|
self._write_str_avoiding_backslashes(node.value, quote_types=_MULTI_QUOTES)
|
2020-03-02 14:59:01 -04:00
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def _write_constant(self, value):
|
|
|
|
if isinstance(value, (float, complex)):
|
2021-03-18 14:36:06 -03:00
|
|
|
# Substitute overflowing decimal literal for AST infinities,
|
|
|
|
# and inf - inf for NaNs.
|
|
|
|
self.write(
|
|
|
|
repr(value)
|
|
|
|
.replace("inf", _INFSTR)
|
|
|
|
.replace("nan", f"({_INFSTR}-{_INFSTR})")
|
|
|
|
)
|
2019-11-24 19:02:40 -04:00
|
|
|
else:
|
|
|
|
self.write(repr(value))
|
|
|
|
|
|
|
|
def visit_Constant(self, node):
|
|
|
|
value = node.value
|
|
|
|
if isinstance(value, tuple):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("(", ")"):
|
2020-03-09 17:27:03 -03:00
|
|
|
self.items_view(self._write_constant, value)
|
2019-11-24 19:02:40 -04:00
|
|
|
elif value is ...:
|
|
|
|
self.write("...")
|
|
|
|
else:
|
|
|
|
if node.kind == "u":
|
|
|
|
self.write("u")
|
|
|
|
self._write_constant(node.value)
|
|
|
|
|
|
|
|
def visit_List(self, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("[", "]"):
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_ListComp(self, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("[", "]"):
|
|
|
|
self.traverse(node.elt)
|
|
|
|
for gen in node.generators:
|
|
|
|
self.traverse(gen)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_GeneratorExp(self, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("(", ")"):
|
|
|
|
self.traverse(node.elt)
|
|
|
|
for gen in node.generators:
|
|
|
|
self.traverse(gen)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_SetComp(self, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("{", "}"):
|
|
|
|
self.traverse(node.elt)
|
|
|
|
for gen in node.generators:
|
|
|
|
self.traverse(gen)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_DictComp(self, node):
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("{", "}"):
|
|
|
|
self.traverse(node.key)
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.value)
|
|
|
|
for gen in node.generators:
|
|
|
|
self.traverse(gen)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_comprehension(self, node):
|
|
|
|
if node.is_async:
|
|
|
|
self.write(" async for ")
|
|
|
|
else:
|
|
|
|
self.write(" for ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.TUPLE, node.target)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.target)
|
|
|
|
self.write(" in ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.TEST.next(), node.iter, *node.ifs)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.iter)
|
|
|
|
for if_clause in node.ifs:
|
|
|
|
self.write(" if ")
|
|
|
|
self.traverse(if_clause)
|
|
|
|
|
|
|
|
def visit_IfExp(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.TEST, node):
|
|
|
|
self.set_precedence(_Precedence.TEST.next(), node.body, node.test)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.body)
|
|
|
|
self.write(" if ")
|
|
|
|
self.traverse(node.test)
|
|
|
|
self.write(" else ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.TEST, node.orelse)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.orelse)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Set(self, node):
|
2021-03-18 14:36:06 -03:00
|
|
|
if node.elts:
|
|
|
|
with self.delimit("{", "}"):
|
|
|
|
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
|
|
|
|
else:
|
|
|
|
# `{}` would be interpreted as a dictionary literal, and
|
|
|
|
# `set` might be shadowed. Thus:
|
|
|
|
self.write('{*()}')
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Dict(self, node):
|
|
|
|
def write_key_value_pair(k, v):
|
|
|
|
self.traverse(k)
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(v)
|
|
|
|
|
|
|
|
def write_item(item):
|
|
|
|
k, v = item
|
|
|
|
if k is None:
|
|
|
|
# for dictionary unpacking operator in dicts {**{'y': 2}}
|
|
|
|
# see PEP 448 for details
|
|
|
|
self.write("**")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.EXPR, v)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(v)
|
|
|
|
else:
|
|
|
|
write_key_value_pair(k, v)
|
|
|
|
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("{", "}"):
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "), write_item, zip(node.keys, node.values)
|
|
|
|
)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Tuple(self, node):
|
2022-05-16 09:38:06 -03:00
|
|
|
with self.delimit_if(
|
|
|
|
"(",
|
|
|
|
")",
|
|
|
|
len(node.elts) == 0 or self.get_precedence(node) > _Precedence.TUPLE
|
|
|
|
):
|
2020-03-09 17:27:03 -03:00
|
|
|
self.items_view(self.traverse, node.elts)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
unop = {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
|
2020-03-01 16:12:17 -04:00
|
|
|
unop_precedence = {
|
|
|
|
"not": _Precedence.NOT,
|
2020-05-16 18:46:11 -03:00
|
|
|
"~": _Precedence.FACTOR,
|
2020-03-01 16:12:17 -04:00
|
|
|
"+": _Precedence.FACTOR,
|
2020-05-16 18:46:11 -03:00
|
|
|
"-": _Precedence.FACTOR,
|
2020-03-01 16:12:17 -04:00
|
|
|
}
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_UnaryOp(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
operator = self.unop[node.op.__class__.__name__]
|
|
|
|
operator_precedence = self.unop_precedence[operator]
|
|
|
|
with self.require_parens(operator_precedence, node):
|
|
|
|
self.write(operator)
|
2021-06-12 23:47:44 -03:00
|
|
|
# factor prefixes (+, -, ~) shouldn't be separated
|
2020-05-16 18:46:11 -03:00
|
|
|
# from the value they belong, (e.g: +1 instead of + 1)
|
|
|
|
if operator_precedence is not _Precedence.FACTOR:
|
|
|
|
self.write(" ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(operator_precedence, node.operand)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.operand)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
binop = {
|
|
|
|
"Add": "+",
|
|
|
|
"Sub": "-",
|
|
|
|
"Mult": "*",
|
|
|
|
"MatMult": "@",
|
|
|
|
"Div": "/",
|
|
|
|
"Mod": "%",
|
|
|
|
"LShift": "<<",
|
|
|
|
"RShift": ">>",
|
|
|
|
"BitOr": "|",
|
|
|
|
"BitXor": "^",
|
|
|
|
"BitAnd": "&",
|
|
|
|
"FloorDiv": "//",
|
|
|
|
"Pow": "**",
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:12:17 -04:00
|
|
|
binop_precedence = {
|
|
|
|
"+": _Precedence.ARITH,
|
|
|
|
"-": _Precedence.ARITH,
|
|
|
|
"*": _Precedence.TERM,
|
|
|
|
"@": _Precedence.TERM,
|
|
|
|
"/": _Precedence.TERM,
|
|
|
|
"%": _Precedence.TERM,
|
|
|
|
"<<": _Precedence.SHIFT,
|
|
|
|
">>": _Precedence.SHIFT,
|
|
|
|
"|": _Precedence.BOR,
|
|
|
|
"^": _Precedence.BXOR,
|
|
|
|
"&": _Precedence.BAND,
|
|
|
|
"//": _Precedence.TERM,
|
|
|
|
"**": _Precedence.POWER,
|
|
|
|
}
|
|
|
|
|
|
|
|
binop_rassoc = frozenset(("**",))
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_BinOp(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
operator = self.binop[node.op.__class__.__name__]
|
|
|
|
operator_precedence = self.binop_precedence[operator]
|
|
|
|
with self.require_parens(operator_precedence, node):
|
|
|
|
if operator in self.binop_rassoc:
|
|
|
|
left_precedence = operator_precedence.next()
|
|
|
|
right_precedence = operator_precedence
|
|
|
|
else:
|
|
|
|
left_precedence = operator_precedence
|
|
|
|
right_precedence = operator_precedence.next()
|
|
|
|
|
|
|
|
self.set_precedence(left_precedence, node.left)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.left)
|
2020-03-01 16:12:17 -04:00
|
|
|
self.write(f" {operator} ")
|
|
|
|
self.set_precedence(right_precedence, node.right)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.right)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
cmpops = {
|
|
|
|
"Eq": "==",
|
|
|
|
"NotEq": "!=",
|
|
|
|
"Lt": "<",
|
|
|
|
"LtE": "<=",
|
|
|
|
"Gt": ">",
|
|
|
|
"GtE": ">=",
|
|
|
|
"Is": "is",
|
|
|
|
"IsNot": "is not",
|
|
|
|
"In": "in",
|
|
|
|
"NotIn": "not in",
|
|
|
|
}
|
|
|
|
|
|
|
|
def visit_Compare(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.CMP, node):
|
|
|
|
self.set_precedence(_Precedence.CMP.next(), node.left, *node.comparators)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.left)
|
|
|
|
for o, e in zip(node.ops, node.comparators):
|
|
|
|
self.write(" " + self.cmpops[o.__class__.__name__] + " ")
|
|
|
|
self.traverse(e)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
2019-12-23 12:11:00 -04:00
|
|
|
boolops = {"And": "and", "Or": "or"}
|
2020-03-01 16:12:17 -04:00
|
|
|
boolop_precedence = {"and": _Precedence.AND, "or": _Precedence.OR}
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_BoolOp(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
operator = self.boolops[node.op.__class__.__name__]
|
|
|
|
operator_precedence = self.boolop_precedence[operator]
|
|
|
|
|
|
|
|
def increasing_level_traverse(node):
|
|
|
|
nonlocal operator_precedence
|
|
|
|
operator_precedence = operator_precedence.next()
|
|
|
|
self.set_precedence(operator_precedence, node)
|
|
|
|
self.traverse(node)
|
|
|
|
|
|
|
|
with self.require_parens(operator_precedence, node):
|
|
|
|
s = f" {operator} "
|
|
|
|
self.interleave(lambda: self.write(s), increasing_level_traverse, node.values)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Attribute(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.value)
|
|
|
|
# Special case: 3.__abs__() is a syntax error, so if node.value
|
|
|
|
# is an integer literal then we need to either parenthesize
|
|
|
|
# it or add an extra space to get 3 .__abs__().
|
|
|
|
if isinstance(node.value, Constant) and isinstance(node.value.value, int):
|
|
|
|
self.write(" ")
|
|
|
|
self.write(".")
|
|
|
|
self.write(node.attr)
|
|
|
|
|
|
|
|
def visit_Call(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.func)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.func)
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("(", ")"):
|
|
|
|
comma = False
|
|
|
|
for e in node.args:
|
|
|
|
if comma:
|
|
|
|
self.write(", ")
|
|
|
|
else:
|
|
|
|
comma = True
|
|
|
|
self.traverse(e)
|
|
|
|
for e in node.keywords:
|
|
|
|
if comma:
|
|
|
|
self.write(", ")
|
|
|
|
else:
|
|
|
|
comma = True
|
|
|
|
self.traverse(e)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Subscript(self, node):
|
2022-03-26 13:55:35 -03:00
|
|
|
def is_non_empty_tuple(slice_value):
|
2020-05-18 17:48:49 -03:00
|
|
|
return (
|
|
|
|
isinstance(slice_value, Tuple)
|
|
|
|
and slice_value.elts
|
|
|
|
)
|
|
|
|
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.ATOM, node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.value)
|
2019-12-23 12:11:00 -04:00
|
|
|
with self.delimit("[", "]"):
|
2022-03-26 13:55:35 -03:00
|
|
|
if is_non_empty_tuple(node.slice):
|
|
|
|
# parentheses can be omitted if the tuple isn't empty
|
2020-03-10 13:52:34 -03:00
|
|
|
self.items_view(self.traverse, node.slice.elts)
|
2020-03-07 11:25:32 -04:00
|
|
|
else:
|
|
|
|
self.traverse(node.slice)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_Starred(self, node):
|
|
|
|
self.write("*")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.EXPR, node.value)
|
2019-11-24 19:02:40 -04:00
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_Ellipsis(self, node):
|
|
|
|
self.write("...")
|
|
|
|
|
|
|
|
def visit_Slice(self, node):
|
|
|
|
if node.lower:
|
|
|
|
self.traverse(node.lower)
|
|
|
|
self.write(":")
|
|
|
|
if node.upper:
|
|
|
|
self.traverse(node.upper)
|
|
|
|
if node.step:
|
|
|
|
self.write(":")
|
|
|
|
self.traverse(node.step)
|
|
|
|
|
2021-02-26 18:51:55 -04:00
|
|
|
def visit_Match(self, node):
|
|
|
|
self.fill("match ")
|
|
|
|
self.traverse(node.subject)
|
|
|
|
with self.block():
|
|
|
|
for case in node.cases:
|
|
|
|
self.traverse(case)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def visit_arg(self, node):
|
|
|
|
self.write(node.arg)
|
|
|
|
if node.annotation:
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.annotation)
|
|
|
|
|
|
|
|
def visit_arguments(self, node):
|
|
|
|
first = True
|
|
|
|
# normal arguments
|
|
|
|
all_args = node.posonlyargs + node.args
|
|
|
|
defaults = [None] * (len(all_args) - len(node.defaults)) + node.defaults
|
|
|
|
for index, elements in enumerate(zip(all_args, defaults), 1):
|
|
|
|
a, d = elements
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
self.write(", ")
|
|
|
|
self.traverse(a)
|
|
|
|
if d:
|
|
|
|
self.write("=")
|
|
|
|
self.traverse(d)
|
|
|
|
if index == len(node.posonlyargs):
|
|
|
|
self.write(", /")
|
|
|
|
|
|
|
|
# varargs, or bare '*' if no varargs but keyword-only arguments present
|
|
|
|
if node.vararg or node.kwonlyargs:
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
self.write(", ")
|
|
|
|
self.write("*")
|
|
|
|
if node.vararg:
|
|
|
|
self.write(node.vararg.arg)
|
|
|
|
if node.vararg.annotation:
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.vararg.annotation)
|
|
|
|
|
|
|
|
# keyword-only arguments
|
|
|
|
if node.kwonlyargs:
|
|
|
|
for a, d in zip(node.kwonlyargs, node.kw_defaults):
|
2019-12-16 08:26:58 -04:00
|
|
|
self.write(", ")
|
|
|
|
self.traverse(a)
|
2019-11-24 19:02:40 -04:00
|
|
|
if d:
|
|
|
|
self.write("=")
|
|
|
|
self.traverse(d)
|
|
|
|
|
|
|
|
# kwargs
|
|
|
|
if node.kwarg:
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
self.write(", ")
|
|
|
|
self.write("**" + node.kwarg.arg)
|
|
|
|
if node.kwarg.annotation:
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(node.kwarg.annotation)
|
|
|
|
|
|
|
|
def visit_keyword(self, node):
|
|
|
|
if node.arg is None:
|
|
|
|
self.write("**")
|
|
|
|
else:
|
|
|
|
self.write(node.arg)
|
|
|
|
self.write("=")
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_Lambda(self, node):
|
2020-03-01 16:12:17 -04:00
|
|
|
with self.require_parens(_Precedence.TEST, node):
|
2021-05-15 09:55:53 -03:00
|
|
|
self.write("lambda")
|
|
|
|
with self.buffered() as buffer:
|
|
|
|
self.traverse(node.args)
|
|
|
|
if buffer:
|
|
|
|
self.write(" ", *buffer)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.write(": ")
|
2020-03-01 16:12:17 -04:00
|
|
|
self.set_precedence(_Precedence.TEST, node.body)
|
2019-12-23 12:11:00 -04:00
|
|
|
self.traverse(node.body)
|
2019-11-24 19:02:40 -04:00
|
|
|
|
|
|
|
def visit_alias(self, node):
|
|
|
|
self.write(node.name)
|
|
|
|
if node.asname:
|
|
|
|
self.write(" as " + node.asname)
|
|
|
|
|
|
|
|
def visit_withitem(self, node):
|
|
|
|
self.traverse(node.context_expr)
|
|
|
|
if node.optional_vars:
|
|
|
|
self.write(" as ")
|
|
|
|
self.traverse(node.optional_vars)
|
|
|
|
|
2021-02-26 18:51:55 -04:00
|
|
|
def visit_match_case(self, node):
|
|
|
|
self.fill("case ")
|
|
|
|
self.traverse(node.pattern)
|
|
|
|
if node.guard:
|
|
|
|
self.write(" if ")
|
|
|
|
self.traverse(node.guard)
|
|
|
|
with self.block():
|
|
|
|
self.traverse(node.body)
|
|
|
|
|
2021-04-29 02:58:44 -03:00
|
|
|
def visit_MatchValue(self, node):
|
|
|
|
self.traverse(node.value)
|
|
|
|
|
|
|
|
def visit_MatchSingleton(self, node):
|
|
|
|
self._write_constant(node.value)
|
|
|
|
|
|
|
|
def visit_MatchSequence(self, node):
|
|
|
|
with self.delimit("[", "]"):
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "), self.traverse, node.patterns
|
|
|
|
)
|
|
|
|
|
|
|
|
def visit_MatchStar(self, node):
|
|
|
|
name = node.name
|
|
|
|
if name is None:
|
|
|
|
name = "_"
|
|
|
|
self.write(f"*{name}")
|
|
|
|
|
|
|
|
def visit_MatchMapping(self, node):
|
|
|
|
def write_key_pattern_pair(pair):
|
|
|
|
k, p = pair
|
|
|
|
self.traverse(k)
|
|
|
|
self.write(": ")
|
|
|
|
self.traverse(p)
|
|
|
|
|
|
|
|
with self.delimit("{", "}"):
|
|
|
|
keys = node.keys
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "),
|
|
|
|
write_key_pattern_pair,
|
|
|
|
zip(keys, node.patterns, strict=True),
|
|
|
|
)
|
|
|
|
rest = node.rest
|
|
|
|
if rest is not None:
|
|
|
|
if keys:
|
|
|
|
self.write(", ")
|
|
|
|
self.write(f"**{rest}")
|
|
|
|
|
|
|
|
def visit_MatchClass(self, node):
|
|
|
|
self.set_precedence(_Precedence.ATOM, node.cls)
|
|
|
|
self.traverse(node.cls)
|
|
|
|
with self.delimit("(", ")"):
|
|
|
|
patterns = node.patterns
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "), self.traverse, patterns
|
|
|
|
)
|
|
|
|
attrs = node.kwd_attrs
|
|
|
|
if attrs:
|
|
|
|
def write_attr_pattern(pair):
|
|
|
|
attr, pattern = pair
|
|
|
|
self.write(f"{attr}=")
|
|
|
|
self.traverse(pattern)
|
|
|
|
|
|
|
|
if patterns:
|
|
|
|
self.write(", ")
|
|
|
|
self.interleave(
|
|
|
|
lambda: self.write(", "),
|
|
|
|
write_attr_pattern,
|
|
|
|
zip(attrs, node.kwd_patterns, strict=True),
|
|
|
|
)
|
|
|
|
|
2021-02-26 18:51:55 -04:00
|
|
|
def visit_MatchAs(self, node):
|
2021-04-29 02:58:44 -03:00
|
|
|
name = node.name
|
|
|
|
pattern = node.pattern
|
|
|
|
if name is None:
|
|
|
|
self.write("_")
|
|
|
|
elif pattern is None:
|
|
|
|
self.write(node.name)
|
|
|
|
else:
|
|
|
|
with self.require_parens(_Precedence.TEST, node):
|
|
|
|
self.set_precedence(_Precedence.BOR, node.pattern)
|
|
|
|
self.traverse(node.pattern)
|
|
|
|
self.write(f" as {node.name}")
|
2021-02-26 18:51:55 -04:00
|
|
|
|
|
|
|
def visit_MatchOr(self, node):
|
|
|
|
with self.require_parens(_Precedence.BOR, node):
|
|
|
|
self.set_precedence(_Precedence.BOR.next(), *node.patterns)
|
|
|
|
self.interleave(lambda: self.write(" | "), self.traverse, node.patterns)
|
|
|
|
|
2019-11-24 19:02:40 -04:00
|
|
|
def unparse(ast_obj):
|
|
|
|
unparser = _Unparser()
|
|
|
|
return unparser.visit(ast_obj)
|
|
|
|
|
2019-09-09 17:36:13 -03:00
|
|
|
|
2023-05-06 13:48:07 -03:00
|
|
|
_deprecated_globals = {
|
|
|
|
name: globals().pop(name)
|
|
|
|
for name in ('Num', 'Str', 'Bytes', 'NameConstant', 'Ellipsis')
|
|
|
|
}
|
|
|
|
|
|
|
|
def __getattr__(name):
|
|
|
|
if name in _deprecated_globals:
|
|
|
|
globals()[name] = value = _deprecated_globals[name]
|
|
|
|
import warnings
|
|
|
|
warnings._deprecated(
|
|
|
|
f"ast.{name}", message=_DEPRECATED_CLASS_MESSAGE, remove=(3, 14)
|
|
|
|
)
|
|
|
|
return value
|
|
|
|
raise AttributeError(f"module 'ast' has no attribute '{name}'")
|
|
|
|
|
|
|
|
|
2019-09-09 17:36:13 -03:00
|
|
|
def main():
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(prog='python -m ast')
|
2024-01-10 09:07:19 -04:00
|
|
|
parser.add_argument('infile', nargs='?', default='-',
|
2019-09-09 17:36:13 -03:00
|
|
|
help='the file to parse; defaults to stdin')
|
|
|
|
parser.add_argument('-m', '--mode', default='exec',
|
|
|
|
choices=('exec', 'single', 'eval', 'func_type'),
|
|
|
|
help='specify what kind of code must be parsed')
|
2019-12-16 14:23:27 -04:00
|
|
|
parser.add_argument('--no-type-comments', default=True, action='store_false',
|
|
|
|
help="don't add information about type comments")
|
2019-09-09 17:36:13 -03:00
|
|
|
parser.add_argument('-a', '--include-attributes', action='store_true',
|
|
|
|
help='include attributes such as line numbers and '
|
|
|
|
'column offsets')
|
2019-12-16 14:23:27 -04:00
|
|
|
parser.add_argument('-i', '--indent', type=int, default=3,
|
|
|
|
help='indentation of nodes (number of spaces)')
|
2019-09-09 17:36:13 -03:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-01-10 09:07:19 -04:00
|
|
|
if args.infile == '-':
|
|
|
|
name = '<stdin>'
|
|
|
|
source = sys.stdin.buffer.read()
|
|
|
|
else:
|
|
|
|
name = args.infile
|
|
|
|
with open(args.infile, 'rb') as infile:
|
|
|
|
source = infile.read()
|
|
|
|
tree = parse(source, name, args.mode, type_comments=args.no_type_comments)
|
2019-12-16 14:23:27 -04:00
|
|
|
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
|
2019-09-09 17:36:13 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|