diff Python/compile.c @ 42704:092f02cf018f legacy-trunk

Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code object's co_consts tuple; add a test to show that the previous behavior (where these two constants were "collapsed" into one) causes serious malfunctioning.
author Alex Martelli <aleaxit@gmail.com>
date Wed, 22 Aug 2007 21:14:17 +0000
parents 90487a887be1
children 1b420e6e00eb
line wrap: on
line diff
--- a/Python/compile.c	Wed Aug 22 19:05:21 2007 +0000
+++ b/Python/compile.c	Wed Aug 22 21:14:17 2007 +0000
@@ -907,7 +907,20 @@
 	Py_ssize_t arg;
 
 	/* necessary to make sure types aren't coerced (e.g., int and long) */
-	t = PyTuple_Pack(2, o, o->ob_type);
+        /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
+        if (PyFloat_Check(o)) {
+            double d = PyFloat_AS_DOUBLE(o);
+            unsigned char* p = (unsigned char*) &d;
+            /* all we need is to make the tuple different in either the 0.0
+             * or -0.0 case from all others, just to avoid the "coercion".
+             */
+            if (*p==0 && p[sizeof(double)-1]==0)
+                t = PyTuple_Pack(3, o, o->ob_type, Py_None);
+            else
+	        t = PyTuple_Pack(2, o, o->ob_type);
+        } else {
+	    t = PyTuple_Pack(2, o, o->ob_type);
+        }
 	if (t == NULL)
 	    return -1;