Index: Include/longintrepr.h =================================================================== --- Include/longintrepr.h (révision 70465) +++ Include/longintrepr.h (copie de travail) @@ -62,6 +62,20 @@ /* Return a copy of src. */ PyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src); +/* Sign of a PyLong: + * - negative value if x is negative + * - zero is value is zero + * - positive value if x is positive + * + * Eg. "if (PyLong_SIGN(x) < 0) ..." */ +#define PyLong_SIGN(x) (Py_SIZE(x)) + +/* True if x equals 0 */ +#define PyLong_EQUALS_ZERO(x) (PyLong_SIGN(x) == 0) + +/* Number of digits (in base PyLong_BASE) */ +#define PyLong_NDIGITS(x) ((Py_SIZE(x) < 0)? -Py_SIZE(x) : Py_SIZE(x)) + #ifdef __cplusplus } #endif Index: Objects/longobject.c =================================================================== --- Objects/longobject.c (révision 70465) +++ Objects/longobject.c (copie de travail) @@ -44,13 +44,13 @@ static PyLongObject * long_normalize(register PyLongObject *v) { - Py_ssize_t j = ABS(Py_SIZE(v)); + Py_ssize_t j = PyLong_NDIGITS(v); Py_ssize_t i = j; while (i > 0 && v->ob_digit[i-1] == 0) --i; if (i != j) - Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; + Py_SIZE(v) = (PyLong_SIGN(v) < 0) ? -(i) : i; return v; } @@ -394,7 +394,7 @@ assert(v != NULL); assert(PyLong_Check(v)); - return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); + return PyLong_EQUALS_ZERO(v) ? 0 : (PyLong_SIGN(v) < 0 ? -1 : 1); } size_t @@ -406,7 +406,7 @@ assert(v != NULL); assert(PyLong_Check(v)); - ndigits = ABS(Py_SIZE(v)); + ndigits = PyLong_NDIGITS(v); assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); if (ndigits > 0) { digit msd = v->ob_digit[ndigits - 1]; @@ -558,7 +558,7 @@ assert(v != NULL && PyLong_Check(v)); - if (Py_SIZE(v) < 0) { + if (PyLong_SIGN(v) < 0) { ndigits = -(Py_SIZE(v)); if (!is_signed) { PyErr_SetString(PyExc_OverflowError, @@ -1130,7 +1130,7 @@ static PyLongObject * mul1(PyLongObject *a, digit n) { - Py_ssize_t size_a = ABS(Py_SIZE(a)); + Py_ssize_t size_a = PyLong_NDIGITS(a); PyLongObject *z = _PyLong_New(size_a+1); twodigits carry = 0; Py_ssize_t i; @@ -1176,7 +1176,7 @@ static PyLongObject * divrem1(PyLongObject *a, digit n, digit *prem) { - const Py_ssize_t size = ABS(Py_SIZE(a)); + const Py_ssize_t size = PyLong_NDIGITS(a); PyLongObject *z; assert(n > 0 && n <= PyLong_MASK); @@ -1208,7 +1208,7 @@ return NULL; } assert(base >= 2 && base <= 36); - size_a = ABS(Py_SIZE(a)); + size_a = PyLong_NDIGITS(a); /* Compute a rough upper bound for the length of the string */ i = base; @@ -1760,7 +1760,7 @@ long_divrem(PyLongObject *a, PyLongObject *b, PyLongObject **pdiv, PyLongObject **prem) { - Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); + Py_ssize_t size_a = PyLong_NDIGITS(a), size_b = PyLong_NDIGITS(b); PyLongObject *z; if (size_b == 0) { @@ -1812,7 +1812,7 @@ static PyLongObject * x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) { - Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1)); + Py_ssize_t size_v = PyLong_NDIGITS(v1), size_w = PyLong_NDIGITS(w1); digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1)); PyLongObject *v = mul1(v1, d); PyLongObject *w = mul1(w1, d); @@ -1827,9 +1827,9 @@ assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */ - assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */ + assert(size_w == PyLong_NDIGITS(w)); /* That's how d was calculated */ - size_v = ABS(Py_SIZE(v)); + size_v = PyLong_NDIGITS(v); k = size_v - size_w; a = _PyLong_New(k + 1); @@ -1933,13 +1933,13 @@ Py_ssize_t sign; if (Py_SIZE(a) != Py_SIZE(b)) { - if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0) + if (PyLong_EQUALS_ZERO(a) && PyLong_EQUALS_ZERO(b)) sign = 0; else sign = Py_SIZE(a) - Py_SIZE(b); } else { - Py_ssize_t i = ABS(Py_SIZE(a)); + Py_ssize_t i = PyLong_NDIGITS(a); while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) ; if (i < 0) @@ -1995,7 +1995,7 @@ static PyLongObject * x_add(PyLongObject *a, PyLongObject *b) { - Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); + Py_ssize_t size_a = PyLong_NDIGITS(a), size_b = PyLong_NDIGITS(b); PyLongObject *z; Py_ssize_t i; digit carry = 0; @@ -2029,7 +2029,7 @@ static PyLongObject * x_sub(PyLongObject *a, PyLongObject *b) { - Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); + Py_ssize_t size_a = PyLong_NDIGITS(a), size_b = PyLong_NDIGITS(b); PyLongObject *z; Py_ssize_t i; int sign = 1; @@ -2139,8 +2139,8 @@ x_mul(PyLongObject *a, PyLongObject *b) { PyLongObject *z; - Py_ssize_t size_a = ABS(Py_SIZE(a)); - Py_ssize_t size_b = ABS(Py_SIZE(b)); + Py_ssize_t size_a = PyLong_NDIGITS(a); + Py_ssize_t size_b = PyLong_NDIGITS(b); Py_ssize_t i; z = _PyLong_New(size_a + size_b); @@ -2231,7 +2231,7 @@ { PyLongObject *hi, *lo; Py_ssize_t size_lo, size_hi; - const Py_ssize_t size_n = ABS(Py_SIZE(n)); + const Py_ssize_t size_n = PyLong_NDIGITS(n); size_lo = MIN(size_n, size); size_hi = size_n - size_lo; @@ -2260,8 +2260,8 @@ static PyLongObject * k_mul(PyLongObject *a, PyLongObject *b) { - Py_ssize_t asize = ABS(Py_SIZE(a)); - Py_ssize_t bsize = ABS(Py_SIZE(b)); + Py_ssize_t asize = PyLong_NDIGITS(a); + Py_ssize_t bsize = PyLong_NDIGITS(b); PyLongObject *ah = NULL; PyLongObject *al = NULL; PyLongObject *bh = NULL; @@ -2481,8 +2481,8 @@ static PyLongObject * k_lopsided_mul(PyLongObject *a, PyLongObject *b) { - const Py_ssize_t asize = ABS(Py_SIZE(a)); - Py_ssize_t bsize = ABS(Py_SIZE(b)); + const Py_ssize_t asize = PyLong_NDIGITS(a); + Py_ssize_t bsize = PyLong_NDIGITS(b); Py_ssize_t nbdone; /* # of b digits already multiplied */ PyLongObject *ret; PyLongObject *bslice = NULL; @@ -2769,7 +2769,7 @@ return Py_NotImplemented; } - if (Py_SIZE(b) < 0) { /* if exponent is negative */ + if (PyLong_SIGN(b) < 0) { /* if exponent is negative */ if (c) { PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " "cannot be negative when 3rd argument specified"); @@ -2788,7 +2788,7 @@ if (c) { /* if modulus == 0: raise ValueError() */ - if (Py_SIZE(c) == 0) { + if (PyLong_EQUALS_ZERO(c)) { PyErr_SetString(PyExc_ValueError, "pow() 3rd argument cannot be 0"); goto Error; @@ -2797,7 +2797,7 @@ /* if modulus < 0: negativeOutput = True modulus = -modulus */ - if (Py_SIZE(c) < 0) { + if (PyLong_SIGN(c) < 0) { negativeOutput = 1; temp = (PyLongObject *)_PyLong_Copy(c); if (temp == NULL) @@ -2818,7 +2818,7 @@ /* if base < 0: base = base % modulus Having the base positive just makes things easier. */ - if (Py_SIZE(a) < 0) { + if (PyLong_SIGN(a) < 0) { if (l_divmod(a, c, NULL, &temp) < 0) goto Error; Py_DECREF(a); @@ -2892,7 +2892,7 @@ } } - if (negativeOutput && (Py_SIZE(z) != 0)) { + if (negativeOutput && !PyLong_EQUALS_ZERO(z)) { temp = (PyLongObject *)long_sub(z, c); if (temp == NULL) goto Error; @@ -2964,7 +2964,7 @@ static int long_nonzero(PyLongObject *v) { - return ABS(Py_SIZE(v)) != 0; + return !PyLong_EQUALS_ZERO(v); } static PyObject * @@ -2978,7 +2978,7 @@ CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); - if (Py_SIZE(a) < 0) { + if (PyLong_SIGN(a) < 0) { /* Right shifting negative numbers is harder */ PyLongObject *a1, *a2; a1 = (PyLongObject *) long_invert(a); @@ -3002,7 +3002,7 @@ goto rshift_error; } wordshift = shiftby / PyLong_SHIFT; - newsize = ABS(Py_SIZE(a)) - wordshift; + newsize = PyLong_NDIGITS(a) - wordshift; if (newsize <= 0) { z = _PyLong_New(0); Py_DECREF(a); @@ -3016,7 +3016,7 @@ z = _PyLong_New(newsize); if (z == NULL) goto rshift_error; - if (Py_SIZE(a) < 0) + if (PyLong_SIGN(a) < 0) Py_SIZE(z) = -(Py_SIZE(z)); for (i = 0, j = wordshift; i < newsize; i++, j++) { z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; @@ -3104,7 +3104,7 @@ digit diga, digb; PyObject *v; - if (Py_SIZE(a) < 0) { + if (PyLong_SIGN(a) < 0) { a = (PyLongObject *) long_invert(a); if (a == NULL) return NULL; @@ -3114,7 +3114,7 @@ Py_INCREF(a); maska = 0; } - if (Py_SIZE(b) < 0) { + if (PyLong_SIGN(b) < 0) { b = (PyLongObject *) long_invert(b); if (b == NULL) { Py_DECREF(a); @@ -3433,7 +3433,7 @@ { Py_ssize_t res; - res = v->ob_type->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit); + res = v->ob_type->tp_basicsize + PyLong_NDIGITS(v)*sizeof(digit); return PyInt_FromSsize_t(res); } @@ -3452,7 +3452,7 @@ assert(v != NULL); assert(PyLong_Check(v)); - ndigits = ABS(Py_SIZE(v)); + ndigits = PyLong_NDIGITS(v); if (ndigits == 0) return PyInt_FromLong(0); Index: Modules/mathmodule.c =================================================================== --- Modules/mathmodule.c (révision 70465) +++ Modules/mathmodule.c (copie de travail) @@ -688,7 +688,7 @@ exp = PyLong_AsLong(oexp); if (exp == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { - if (Py_SIZE(oexp) < 0) { + if (PyLong_SIGN(oexp) < 0) { exp = LONG_MIN; } else {