summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-06-30 11:45:53 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-06-30 11:45:53 (GMT)
commitcba8c10b5c262f41873ac877d25c242823ab668c (patch)
treef3036f5038379647fb4bff4aa4cd7715a3a27882
parentabe52d749c89a7c8860cdc191a940b8b6b314dab (diff)
downloadcpython-cba8c10b5c262f41873ac877d25c242823ab668c.zip
cpython-cba8c10b5c262f41873ac877d25c242823ab668c.tar.gz
cpython-cba8c10b5c262f41873ac877d25c242823ab668c.tar.bz2
Unparse infinite imaginary literals correctly. Add some more numeric tests.
-rw-r--r--Demo/parser/test_unparse.py14
-rw-r--r--Demo/parser/unparse.py11
2 files changed, 20 insertions, 5 deletions
diff --git a/Demo/parser/test_unparse.py b/Demo/parser/test_unparse.py
index 3a795e9..d457523 100644
--- a/Demo/parser/test_unparse.py
+++ b/Demo/parser/test_unparse.py
@@ -123,6 +123,8 @@ class UnparseTestCase(ASTTestCase):
def test_unary_parens(self):
self.check_roundtrip("(-1)**7")
+ self.check_roundtrip("(-1.)**8")
+ self.check_roundtrip("(-1j)**6")
self.check_roundtrip("not True or False")
self.check_roundtrip("True or not False")
@@ -132,6 +134,18 @@ class UnparseTestCase(ASTTestCase):
def test_huge_float(self):
self.check_roundtrip("1e1000")
self.check_roundtrip("-1e1000")
+ self.check_roundtrip("1e1000j")
+ self.check_roundtrip("-1e1000j")
+
+ def test_min_int(self):
+ self.check_roundtrip(str(-2**31))
+ self.check_roundtrip(str(-2**63))
+
+ def test_imaginary_literals(self):
+ self.check_roundtrip("7j")
+ self.check_roundtrip("-7j")
+ self.check_roundtrip("0j")
+ self.check_roundtrip("-0j")
def test_lambda_parentheses(self):
self.check_roundtrip("(lambda: int)()")
diff --git a/Demo/parser/unparse.py b/Demo/parser/unparse.py
index e1c2719..e96ef54 100644
--- a/Demo/parser/unparse.py
+++ b/Demo/parser/unparse.py
@@ -6,6 +6,10 @@ import tokenize
import io
import os
+# 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)
+
def interleave(inter, f, seq):
"""Call f on each item in seq, calling inter() in between.
"""
@@ -311,11 +315,8 @@ class Unparser:
self.write(t.id)
def _Num(self, t):
- if isinstance(t.n, float) and math.isinf(t.n):
- # Subsitute overflowing decimal literal for AST infinity
- self.write("1e" + repr(sys.float_info.max_10_exp + 1))
- else:
- self.write(repr(t.n))
+ # Substitute overflowing decimal literal for AST infinities.
+ self.write(repr(t.n).replace("inf", INFSTR))
def _List(self, t):
self.write("[")