summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2006-10-04 05:48:05 +0000
committerMartin v. Löwis <martin@v.loewis.de>2006-10-04 05:48:05 +0000
commit98041d7bf034fec109fb24a88fcf7b70fdade899 (patch)
treec85a00f5345dbee010cf19b672761aa9861b4b71 /Objects
parente346a7324da8678aa690eb697f1166b106c08df4 (diff)
downloadcpython-git-98041d7bf034fec109fb24a88fcf7b70fdade899.tar.gz
Fix integer negation and absolute value to not rely
on undefined behaviour of the C compiler anymore.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/intobject.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 1a7e2f7c86..579fe7dcbf 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -674,10 +674,9 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
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);
@@ -686,7 +685,7 @@ int_neg(PyIntObject *v)
}
return NULL;
}
- return PyInt_FromLong(x);
+ return PyInt_FromLong(-a);
}
static PyObject *