Commit 820d6ac9 authored by Martin v. Löwis's avatar Martin v. Löwis

Fix integer negation and absolute value to not rely

on undefined behaviour of the C compiler anymore.
Will backport to 2.5 and 2.4.
parent 82271f13
...@@ -116,6 +116,7 @@ class BuiltinTest(unittest.TestCase): ...@@ -116,6 +116,7 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual(abs(0), 0) self.assertEqual(abs(0), 0)
self.assertEqual(abs(1234), 1234) self.assertEqual(abs(1234), 1234)
self.assertEqual(abs(-1234), 1234) self.assertEqual(abs(-1234), 1234)
self.assertTrue(abs(-sys.maxint-1) > 0)
# float # float
self.assertEqual(abs(0.0), 0.0) self.assertEqual(abs(0.0), 0.0)
self.assertEqual(abs(3.14), 3.14) self.assertEqual(abs(3.14), 3.14)
......
...@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1? ...@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
Core and builtins 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 - Bug #1566800: make sure that EnvironmentError can be called with any
number of arguments, as was the case in Python 2.4. number of arguments, as was the case in Python 2.4.
......
...@@ -754,10 +754,9 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) ...@@ -754,10 +754,9 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
static PyObject * static PyObject *
int_neg(PyIntObject *v) int_neg(PyIntObject *v)
{ {
register long a, x; register long a;
a = v->ob_ival; a = v->ob_ival;
x = -a; if (a < 0 && (unsigned long)a == 0-(unsigned long)a) {
if (a < 0 && x < 0) {
PyObject *o = PyLong_FromLong(a); PyObject *o = PyLong_FromLong(a);
if (o != NULL) { if (o != NULL) {
PyObject *result = PyNumber_Negative(o); PyObject *result = PyNumber_Negative(o);
...@@ -766,7 +765,7 @@ int_neg(PyIntObject *v) ...@@ -766,7 +765,7 @@ int_neg(PyIntObject *v)
} }
return NULL; return NULL;
} }
return PyInt_FromLong(x); return PyInt_FromLong(-a);
} }
static PyObject * static PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment