[Python-checkins] r71869 - in python/trunk: Lib/test/test_complex.py Objects/complexobject.c

mark.dickinson python-checkins at python.org
Sat Apr 25 11:47:00 CEST 2009


Author: mark.dickinson
Date: Sat Apr 25 11:47:00 2009
New Revision: 71869

Log:
Fix typo in complex parsing code;  expand tests.


Modified:
   python/trunk/Lib/test/test_complex.py
   python/trunk/Objects/complexobject.c

Modified: python/trunk/Lib/test/test_complex.py
==============================================================================
--- python/trunk/Lib/test/test_complex.py	(original)
+++ python/trunk/Lib/test/test_complex.py	Sat Apr 25 11:47:00 2009
@@ -432,10 +432,11 @@
     @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
                          "test requires IEEE 754 doubles")
     def test_repr_roundtrip(self):
-        # complex(repr(z)) should recover z exactly, even for complex numbers
-        # involving an infinity, nan, or negative zero
-        vals = [0.0, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN]
+        vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN]
         vals += [-v for v in vals]
+
+        # complex(repr(z)) should recover z exactly, even for complex
+        # numbers involving an infinity, nan, or negative zero
         for x in vals:
             for y in vals:
                 z = complex(x, y)
@@ -443,6 +444,21 @@
                 self.assertFloatsAreIdentical(z.real, roundtrip.real)
                 self.assertFloatsAreIdentical(z.imag, roundtrip.imag)
 
+        # if we predefine some constants, then eval(repr(z)) should
+        # also work, except that it might change the sign of zeros
+        inf, nan = float('inf'), float('nan')
+        infj, nanj = complex(0.0, inf), complex(0.0, nan)
+        for x in vals:
+            for y in vals:
+                z = complex(x, y)
+                roundtrip = eval(repr(z))
+                # adding 0.0 has no effect beside changing -0.0 to 0.0
+                self.assertFloatsAreIdentical(0.0 + z.real,
+                                              0.0 + roundtrip.real)
+                self.assertFloatsAreIdentical(0.0 + z.imag,
+                                              0.0 + roundtrip.imag)
+
+
 
 def test_main():
     test_support.run_unittest(ComplexTest)

Modified: python/trunk/Objects/complexobject.c
==============================================================================
--- python/trunk/Objects/complexobject.c	(original)
+++ python/trunk/Objects/complexobject.c	Sat Apr 25 11:47:00 2009
@@ -962,7 +962,7 @@
 			y = PyOS_ascii_strtod(s, &end);
 			if (end == s && errno == ENOMEM)
 				return PyErr_NoMemory();
-			if (errno == ERANGE && fabs(z) >= 1.0)
+			if (errno == ERANGE && fabs(y) >= 1.0)
 				goto overflow;
 			if (end != s)
 				/* <float><signed-float>j */


More information about the Python-checkins mailing list