Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenVMS
Python
cpython
Commits
12860de8f2ed
Commit
12860de8f2ed
authored
4 years ago
by
Kodi Arfer
Browse files
Options
Downloads
Patches
Plain Diff
bpo-43521: Allow ast.unparse with empty sets and NaN (GH-24897)
Automerge-Triggered-By: GH:pablogsal
parent
88b70bb52304
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Lib/ast.py
+14
-6
14 additions, 6 deletions
Lib/ast.py
Lib/test/test_unparse.py
+12
-3
12 additions, 3 deletions
Lib/test/test_unparse.py
Misc/NEWS.d/next/Library/2021-03-16-16-05-02.bpo-43521.mRT6fh.rst
+1
-0
1 addition, 0 deletions
...S.d/next/Library/2021-03-16-16-05-02.bpo-43521.mRT6fh.rst
with
27 additions
and
9 deletions
Lib/ast.py
+
14
−
6
View file @
12860de8
...
...
@@ -1199,8 +1199,13 @@
def
_write_constant
(
self
,
value
):
if
isinstance
(
value
,
(
float
,
complex
)):
# Substitute overflowing decimal literal for AST infinities.
self
.
write
(
repr
(
value
).
replace
(
"
inf
"
,
_INFSTR
))
# Substitute overflowing decimal literal for AST infinities,
# and inf - inf for NaNs.
self
.
write
(
repr
(
value
)
.
replace
(
"
inf
"
,
_INFSTR
)
.
replace
(
"
nan
"
,
f
"
(
{
_INFSTR
}
-
{
_INFSTR
}
)
"
)
)
elif
self
.
_avoid_backslashes
and
isinstance
(
value
,
str
):
self
.
_write_str_avoiding_backslashes
(
value
)
else
:
...
...
@@ -1273,7 +1278,6 @@
self
.
traverse
(
node
.
orelse
)
def
visit_Set
(
self
,
node
):
if
not
node
.
elts
:
raise
ValueError
(
"
Set node should have at least one item
"
)
if
node
.
elts
:
with
self
.
delimit
(
"
{
"
,
"
}
"
):
self
.
interleave
(
lambda
:
self
.
write
(
"
,
"
),
self
.
traverse
,
node
.
elts
)
...
...
@@ -1278,5 +1282,9 @@
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
(
'
{*()}
'
)
def
visit_Dict
(
self
,
node
):
def
write_key_value_pair
(
k
,
v
):
...
...
This diff is collapsed.
Click to expand it.
Lib/test/test_unparse.py
+
12
−
3
View file @
12860de8
...
...
@@ -199,6 +199,12 @@
self
.
check_ast_roundtrip
(
"
1e1000j
"
)
self
.
check_ast_roundtrip
(
"
-1e1000j
"
)
def
test_nan
(
self
):
self
.
assertASTEqual
(
ast
.
parse
(
ast
.
unparse
(
ast
.
Constant
(
value
=
float
(
'
nan
'
)))),
ast
.
parse
(
'
1e1000 - 1e1000
'
)
)
def
test_min_int
(
self
):
self
.
check_ast_roundtrip
(
str
(
-
(
2
**
31
)))
self
.
check_ast_roundtrip
(
str
(
-
(
2
**
63
)))
...
...
@@ -252,6 +258,12 @@
def
test_set_literal
(
self
):
self
.
check_ast_roundtrip
(
"
{
'
a
'
,
'
b
'
,
'
c
'
}
"
)
def
test_empty_set
(
self
):
self
.
assertASTEqual
(
ast
.
parse
(
ast
.
unparse
(
ast
.
Set
(
elts
=
[]))),
ast
.
parse
(
'
{*()}
'
)
)
def
test_set_comprehension
(
self
):
self
.
check_ast_roundtrip
(
"
{x for x in range(5)}
"
)
...
...
@@ -326,9 +338,6 @@
def
test_invalid_fstring_backslash
(
self
):
self
.
check_invalid
(
ast
.
FormattedValue
(
value
=
ast
.
Constant
(
value
=
"
\\\\
"
)))
def
test_invalid_set
(
self
):
self
.
check_invalid
(
ast
.
Set
(
elts
=
[]))
def
test_invalid_yield_from
(
self
):
self
.
check_invalid
(
ast
.
YieldFrom
(
value
=
None
))
...
...
This diff is collapsed.
Click to expand it.
Misc/NEWS.d/next/Library/2021-03-16-16-05-02.bpo-43521.mRT6fh.rst
0 → 100644
+
1
−
0
View file @
12860de8
``ast.unparse`` can now render NaNs and empty sets.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment