summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorKodi Arfer <Kodiologist@users.noreply.github.com>2021-03-18 17:36:06 (GMT)
committerGitHub <noreply@github.com>2021-03-18 17:36:06 (GMT)
commit08ff4369afca84587b1c82034af4e9f64caddbf2 (patch)
treedc3651017ab316e451c13f84a6686d20faf7441e /Lib/ast.py
parenteec8e61992fb654d4cf58de4d727c18622b8303e (diff)
downloadcpython-08ff4369afca84587b1c82034af4e9f64caddbf2.zip
cpython-08ff4369afca84587b1c82034af4e9f64caddbf2.tar.gz
cpython-08ff4369afca84587b1c82034af4e9f64caddbf2.tar.bz2
bpo-43521: Allow ast.unparse with empty sets and NaN (GH-24897)
Automerge-Triggered-By: GH:pablogsal
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index 03ae2ec..e46ab43 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -1199,8 +1199,13 @@ class _Unparser(NodeVisitor):
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,10 +1278,13 @@ class _Unparser(NodeVisitor):
self.traverse(node.orelse)
def visit_Set(self, node):
- if not node.elts:
- raise ValueError("Set node should have at least one item")
- with self.delimit("{", "}"):
- self.interleave(lambda: self.write(", "), self.traverse, node.elts)
+ 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('{*()}')
def visit_Dict(self, node):
def write_key_value_pair(k, v):