[Python-checkins] r52131 - in python/branches/release25-maint: Lib/test/test_builtin.py Misc/NEWS Objects/intobject.c

martin.v.loewis python-checkins at python.org
Wed Oct 4 07:47:48 CEST 2006


Author: martin.v.loewis
Date: Wed Oct  4 07:47:47 2006
New Revision: 52131

Modified:
   python/branches/release25-maint/Lib/test/test_builtin.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Objects/intobject.c
Log:
Fix integer negation and absolute value to not rely
on undefined behaviour of the C compiler anymore.


Modified: python/branches/release25-maint/Lib/test/test_builtin.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_builtin.py	(original)
+++ python/branches/release25-maint/Lib/test/test_builtin.py	Wed Oct  4 07:47:47 2006
@@ -116,6 +116,7 @@
         self.assertEqual(abs(0), 0)
         self.assertEqual(abs(1234), 1234)
         self.assertEqual(abs(-1234), 1234)
+        self.assertTrue(abs(-sys.maxint-1) > 0)
         # float
         self.assertEqual(abs(0.0), 0.0)
         self.assertEqual(abs(3.14), 3.14)

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Wed Oct  4 07:47:47 2006
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Integer negation and absolute value were fixed to not rely
+  on undefined behaviour of the C compiler anymore.
+
 - Bug #1566800: make sure that EnvironmentError can be called with any
   number of arguments, as was the case in Python 2.4.
 

Modified: python/branches/release25-maint/Objects/intobject.c
==============================================================================
--- python/branches/release25-maint/Objects/intobject.c	(original)
+++ python/branches/release25-maint/Objects/intobject.c	Wed Oct  4 07:47:47 2006
@@ -760,10 +760,9 @@
 static PyObject *
 int_neg(PyIntObject *v)
 {
-	register long a, x;
+	register long a;
 	a = v->ob_ival;
-	x = -a;
-	if (a < 0 && x < 0) {
+	if (a < 0 && (unsigned long)a == 0-(unsigned long)a) {
 		PyObject *o = PyLong_FromLong(a);
 		if (o != NULL) {
 			PyObject *result = PyNumber_Negative(o);
@@ -772,7 +771,7 @@
 		}
 		return NULL;
 	}
-	return PyInt_FromLong(x);
+	return PyInt_FromLong(-a);
 }
 
 static PyObject *


More information about the Python-checkins mailing list