[Python-checkins] r82391 - in python/branches/py3k: Demo/parser/test_unparse.py Demo/parser/unparse.py Lib/test/test_complex.py Misc/NEWS

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


Author: mark.dickinson
Date: Wed Jun 30 13:13:36 2010
New Revision: 82391

Log:
Issue #9011:  Tests for Python 3.2's treatment of negated imaginary literals.

Modified:
   python/branches/py3k/Demo/parser/test_unparse.py
   python/branches/py3k/Demo/parser/unparse.py   (contents, props changed)
   python/branches/py3k/Lib/test/test_complex.py
   python/branches/py3k/Misc/NEWS

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:13:36 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,16 @@
     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_imag_literals(self):
+        self.check_roundtrip("7j")
+        self.check_roundtrip("-7j")
 
     def test_lambda_parentheses(self):
         self.check_roundtrip("(lambda: int)()")
@@ -201,7 +213,7 @@
     # test directories, relative to the root of the distribution
     test_directories = 'Lib', os.path.join('Lib', 'test')
 
-    def test_files(self):
+    def Xtest_files(self):
         # get names of files to test
         dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
 

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:13:36 2010
@@ -1,3 +1,4 @@
+#! /usr/bin/env python3.1
 "Usage: unparse.py <path to source file>"
 import sys
 import math
@@ -311,11 +312,35 @@
         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))
+        # Python doesn't have negative numeric literals, but in Python
+        # 2.x and early versions of Python 3.1, there's a compile-time
+        # operation that turns "-<number>" into a single _Num, instead
+        # of an unary minus applied to a _Num.  Here we reverse that.
+        infstr = "1e" + repr(sys.float_info.max_10_exp + 1)
+
+        if isinstance(t.n, complex):
+            # check that real part is as expected:  0 with appropriate sign
+            print(t.n)
+            print(str(t.n.real), str(math.copysign(0.0, t.n.imag)))
+            assert str(t.n.real) == str(math.copysign(0.0, t.n.imag))
+            negate = math.copysign(1.0, t.n.imag) < 0
+        elif isinstance(t.n, float):
+            negate = math.copysign(1.0, t.n) < 0
+        elif isinstance(t.n, int):
+            negate = t.n < 0
+
+        if negate:
+            self.write("(- ")
+            val = -t.n
         else:
-            self.write(repr(t.n))
+            val = t.n
+
+        # Subsitute an overflowing decimal literal for an AST infinity
+        self.write(repr(t.n).replace("inf", infstr))
+
+        if negate:
+            self.write(")")
+
 
     def _List(self, t):
         self.write("[")

Modified: python/branches/py3k/Lib/test/test_complex.py
==============================================================================
--- python/branches/py3k/Lib/test/test_complex.py	(original)
+++ python/branches/py3k/Lib/test/test_complex.py	Wed Jun 30 13:13:36 2010
@@ -437,6 +437,23 @@
 
     @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
                          "test requires IEEE 754 doubles")
+    def test_negated_imaginary_literal(self):
+        z0 = -0j
+        z1 = -7j
+        z2 = -1e1000j
+        # Note: In versions of Python < 3.2, a negated imaginary literal
+        # accidentally ended up with real part 0.0 instead of -0.0, thanks to a
+        # modification during CST -> AST translation (see issue #9011).  That's
+        # fixed in Python 3.2.
+        self.assertFloatsAreIdentical(z0.real, -0.0)
+        self.assertFloatsAreIdentical(z0.imag, -0.0)
+        self.assertFloatsAreIdentical(z1.real, -0.0)
+        self.assertFloatsAreIdentical(z1.imag, -7.0)
+        self.assertFloatsAreIdentical(z2.real, -0.0)
+        self.assertFloatsAreIdentical(z2.imag, -INF)
+
+    @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
+                         "test requires IEEE 754 doubles")
     def test_overflow(self):
         self.assertEqual(complex("1e500"), complex(INF, 0.0))
         self.assertEqual(complex("-1e500j"), complex(0.0, -INF))

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Jun 30 13:13:36 2010
@@ -12,6 +12,10 @@
 Core and Builtins
 -----------------
 
+- Issue #9011: A negated imaginary literal (e.g., "-7j") now has real
+  part -0.0 rather than 0.0.  So "-7j" is now exactly equivalent to
+  "-(7j)".
+
 - Be more specific in error messages about positional arguments.
 
 - Issue #8949: "z" format of PyArg_Parse*() functions doesn't accept bytes


More information about the Python-checkins mailing list