[Python-checkins] r82393 - in python/branches/py3k/Demo/parser: test_unparse.py unparse.py

mark.dickinson python-checkins at python.org
Wed Jun 30 13:45:53 CEST 2010


Author: mark.dickinson
Date: Wed Jun 30 13:45:53 2010
New Revision: 82393

Log:
Unparse infinite imaginary literals correctly.  Add some more numeric tests.

Modified:
   python/branches/py3k/Demo/parser/test_unparse.py
   python/branches/py3k/Demo/parser/unparse.py

Modified: python/branches/py3k/Demo/parser/test_unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/test_unparse.py	(original)
+++ python/branches/py3k/Demo/parser/test_unparse.py	Wed Jun 30 13:45:53 2010
@@ -123,6 +123,8 @@
 
     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 @@
     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)()")

Modified: python/branches/py3k/Demo/parser/unparse.py
==============================================================================
--- python/branches/py3k/Demo/parser/unparse.py	(original)
+++ python/branches/py3k/Demo/parser/unparse.py	Wed Jun 30 13:45:53 2010
@@ -6,6 +6,10 @@
 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 @@
         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("[")


More information about the Python-checkins mailing list