Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e64f948e
Commit
e64f948e
authored
Aug 29, 2019
by
Serhiy Storchaka
Committed by
GitHub
Aug 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37950: Fix ast.dump() when call with incompletely initialized node. (GH-15510)
parent
b235a1b4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
19 deletions
+59
-19
Doc/library/ast.rst
Doc/library/ast.rst
+5
-4
Lib/ast.py
Lib/ast.py
+24
-15
Lib/test/test_ast.py
Lib/test/test_ast.py
+29
-0
Misc/NEWS.d/next/Library/2019-08-26-10-45-51.bpo-37950.-K1IKT.rst
...S.d/next/Library/2019-08-26-10-45-51.bpo-37950.-K1IKT.rst
+1
-0
No files found.
Doc/library/ast.rst
View file @
e64f948e
...
...
@@ -322,11 +322,12 @@ and classes for traversing abstract syntax trees:
.. function:: dump(node, annotate_fields=True, include_attributes=False)
Return a formatted dump of the tree in *node*. This is mainly useful for
debugging purposes. The returned string will show the names and the values
for fields. This makes the code impossible to evaluate, so if evaluation is
wanted *annotate_fields* must be set to ``False``. Attributes such as line
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
numbers and column offsets are not dumped by default. If this is wanted,
*include_attributes* can be set to
``True``
.
*include_attributes* can be set to
true
.
.. seealso::
...
...
Lib/ast.py
View file @
e64f948e
...
...
@@ -98,26 +98,35 @@ def literal_eval(node_or_string):
def
dump
(
node
,
annotate_fields
=
True
,
include_attributes
=
False
):
"""
Return a formatted dump of the tree in *node*. This is mainly useful for
debugging purposes. The returned string will show the names and the values
for fields. This makes the code impossible to evaluate, so if evaluation is
wanted *annotate_fields* must be set to False. Attributes such as line
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
numbers and column offsets are not dumped by default. If this is wanted,
*include_attributes* can be set to T
rue.
include_attributes can be set to t
rue.
"""
def
_format
(
node
):
if
isinstance
(
node
,
AST
):
fields
=
[(
a
,
_format
(
b
))
for
a
,
b
in
iter_fields
(
node
)]
rv
=
'%s(%s'
%
(
node
.
__class__
.
__name__
,
', '
.
join
(
(
'%s=%s'
%
field
for
field
in
fields
)
if
annotate_fields
else
(
b
for
a
,
b
in
fields
)
))
args
=
[]
keywords
=
annotate_fields
for
field
in
node
.
_fields
:
try
:
value
=
getattr
(
node
,
field
)
except
AttributeError
:
keywords
=
True
else
:
if
keywords
:
args
.
append
(
'%s=%s'
%
(
field
,
_format
(
value
)))
else
:
args
.
append
(
_format
(
value
))
if
include_attributes
and
node
.
_attributes
:
rv
+=
fields
and
', '
or
' '
rv
+=
', '
.
join
(
'%s=%s'
%
(
a
,
_format
(
getattr
(
node
,
a
)))
for
a
in
node
.
_attributes
)
return
rv
+
')'
for
a
in
node
.
_attributes
:
try
:
args
.
append
(
'%s=%s'
%
(
a
,
_format
(
getattr
(
node
,
a
))))
except
AttributeError
:
pass
return
'%s(%s)'
%
(
node
.
__class__
.
__name__
,
', '
.
join
(
args
))
elif
isinstance
(
node
,
list
):
return
'[%s]'
%
', '
.
join
(
_format
(
x
)
for
x
in
node
)
return
repr
(
node
)
...
...
Lib/test/test_ast.py
View file @
e64f948e
...
...
@@ -645,6 +645,35 @@ class ASTHelpers_Test(unittest.TestCase):
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])"
)
def
test_dump_incomplete
(
self
):
node
=
ast
.
Raise
(
lineno
=
3
,
col_offset
=
4
)
self
.
assertEqual
(
ast
.
dump
(
node
),
"Raise()"
)
self
.
assertEqual
(
ast
.
dump
(
node
,
include_attributes
=
True
),
"Raise(lineno=3, col_offset=4)"
)
node
=
ast
.
Raise
(
exc
=
ast
.
Name
(
id
=
'e'
,
ctx
=
ast
.
Load
()),
lineno
=
3
,
col_offset
=
4
)
self
.
assertEqual
(
ast
.
dump
(
node
),
"Raise(exc=Name(id='e', ctx=Load()))"
)
self
.
assertEqual
(
ast
.
dump
(
node
,
annotate_fields
=
False
),
"Raise(Name('e', Load()))"
)
self
.
assertEqual
(
ast
.
dump
(
node
,
include_attributes
=
True
),
"Raise(exc=Name(id='e', ctx=Load()), lineno=3, col_offset=4)"
)
self
.
assertEqual
(
ast
.
dump
(
node
,
annotate_fields
=
False
,
include_attributes
=
True
),
"Raise(Name('e', Load()), lineno=3, col_offset=4)"
)
node
=
ast
.
Raise
(
cause
=
ast
.
Name
(
id
=
'e'
,
ctx
=
ast
.
Load
()))
self
.
assertEqual
(
ast
.
dump
(
node
),
"Raise(cause=Name(id='e', ctx=Load()))"
)
self
.
assertEqual
(
ast
.
dump
(
node
,
annotate_fields
=
False
),
"Raise(cause=Name('e', Load()))"
)
def
test_copy_location
(
self
):
src
=
ast
.
parse
(
'1 + 1'
,
mode
=
'eval'
)
src
.
body
.
right
=
ast
.
copy_location
(
ast
.
Num
(
2
),
src
.
body
.
right
)
...
...
Misc/NEWS.d/next/Library/2019-08-26-10-45-51.bpo-37950.-K1IKT.rst
0 → 100644
View file @
e64f948e
Fix :func:`ast.dump` when call with incompletely initialized node.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment