consistently use Py_TYPE, Py_REFCNT, and correct initializer macros (#3563)
This no-op change makes 2.7 more consistent with 3.x to ease comparison and backports.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 768a92a..5d6ce70 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -83,12 +83,12 @@
Py_ssize_t i;
assert(src != NULL);
- i = src->ob_size;
+ i = Py_SIZE(src);
if (i < 0)
i = -(i);
result = _PyLong_New(i);
if (result != NULL) {
- result->ob_size = src->ob_size;
+ Py_SIZE(result) = Py_SIZE(src);
while (--i >= 0)
result->ob_digit[i] = src->ob_digit[i];
}
@@ -129,7 +129,7 @@
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->ob_digit;
- v->ob_size = negative ? -ndigits : ndigits;
+ Py_SIZE(v) = negative ? -ndigits : ndigits;
t = abs_ival;
while (t) {
*p++ = (digit)(t & PyLong_MASK);
@@ -372,7 +372,7 @@
return -1;
}
v = (PyLongObject *)vv;
- i = v->ob_size;
+ i = Py_SIZE(v);
sign = 1;
x = 0;
if (i < 0) {
@@ -464,7 +464,7 @@
return (unsigned long) -1;
}
v = (PyLongObject *)vv;
- i = v->ob_size;
+ i = Py_SIZE(v);
sign = 1;
x = 0;
if (i < 0) {
@@ -951,7 +951,7 @@
PyObject *io;
if (PyInt_Check(vv))
return (PY_LONG_LONG)PyInt_AsLong(vv);
- if ((nb = vv->ob_type->tp_as_number) == NULL ||
+ if ((nb = Py_TYPE(vv)->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
@@ -1025,7 +1025,7 @@
return (unsigned long) -1;
}
v = (PyLongObject *)vv;
- i = v->ob_size;
+ i = Py_SIZE(v);
sign = 1;
x = 0;
if (i < 0) {
@@ -1068,7 +1068,7 @@
if (!PyLong_Check(vv)) {
PyNumberMethods *nb;
- nb = vv->ob_type->tp_as_number;
+ nb = Py_TYPE(vv)->tp_as_number;
if (nb == NULL || nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError,
"an integer is required");
@@ -1495,10 +1495,10 @@
*p = '\0';
if (addL)
*--p = 'L';
- if (a->ob_size < 0)
+ if (Py_SIZE(a) < 0)
sign = '-';
- if (a->ob_size == 0) {
+ if (Py_SIZE(a) == 0) {
*--p = '0';
}
else if ((base & (base - 1)) == 0) {
@@ -2063,10 +2063,10 @@
The quotient z has the sign of a*b;
the remainder r has the sign of a,
so a = b*z + r. */
- if ((a->ob_size < 0) != (b->ob_size < 0))
- z->ob_size = -(z->ob_size);
- if (a->ob_size < 0 && (*prem)->ob_size != 0)
- (*prem)->ob_size = -((*prem)->ob_size);
+ if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
+ Py_SIZE(z) = -(Py_SIZE(z));
+ if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
+ Py_SIZE(*prem) = -Py_SIZE(*prem);
*pdiv = z;
return 0;
}
@@ -2398,7 +2398,7 @@
/* This is designed so that Python ints and longs with the
same value hash to the same value, otherwise comparisons
of mapping keys will turn out weird */
- i = v->ob_size;
+ i = Py_SIZE(v);
sign = 1;
x = 0;
if (i < 0) {
@@ -2510,7 +2510,7 @@
}
assert(borrow == 0);
if (sign < 0)
- z->ob_size = -(z->ob_size);
+ Py_SIZE(z) = -(Py_SIZE(z));
return long_normalize(z);
}
@@ -2521,17 +2521,17 @@
CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
- if (a->ob_size < 0) {
- if (b->ob_size < 0) {
+ if (Py_SIZE(a) < 0) {
+ if (Py_SIZE(b) < 0) {
z = x_add(a, b);
- if (z != NULL && z->ob_size != 0)
- z->ob_size = -(z->ob_size);
+ if (z != NULL && Py_SIZE(z) != 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
}
else
z = x_sub(b, a);
}
else {
- if (b->ob_size < 0)
+ if (Py_SIZE(b) < 0)
z = x_sub(a, b);
else
z = x_add(a, b);
@@ -2548,16 +2548,16 @@
CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b);
- if (a->ob_size < 0) {
- if (b->ob_size < 0)
+ if (Py_SIZE(a) < 0) {
+ if (Py_SIZE(b) < 0)
z = x_sub(a, b);
else
z = x_add(a, b);
- if (z != NULL && z->ob_size != 0)
- z->ob_size = -(z->ob_size);
+ if (z != NULL && Py_SIZE(z) != 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
}
else {
- if (b->ob_size < 0)
+ if (Py_SIZE(b) < 0)
z = x_add(a, b);
else
z = x_sub(a, b);
@@ -2742,7 +2742,7 @@
/* If a is small compared to b, splitting on b gives a degenerate
* case with ah==0, and Karatsuba may be (even much) less efficient
* than "grade school" then. However, we can still win, by viewing
- * b as a string of "big digits", each of width a->ob_size. That
+ * b as a string of "big digits", each of width Py_SIZE(a). That
* leads to a sequence of balanced calls to k_mul.
*/
if (2 * asize <= bsize)
@@ -2910,7 +2910,7 @@
/* b has at least twice the digits of a, and a is big enough that Karatsuba
* would pay off *if* the inputs had balanced sizes. View b as a sequence
- * of slices, each with a->ob_size digits, and multiply the slices by a,
+ * of slices, each with Py_SIZE(a) digits, and multiply the slices by a,
* one at a time. This gives k_mul balanced inputs to work with, and is
* also cache-friendly (we compute one double-width slice of the result
* at a time, then move on, never backtracking except for the helpful
@@ -2982,8 +2982,8 @@
z = k_mul(a, b);
/* Negate if exactly one of the inputs is negative. */
- if (((a->ob_size ^ b->ob_size) < 0) && z)
- z->ob_size = -(z->ob_size);
+ if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
+ Py_SIZE(z) = -(Py_SIZE(z));
Py_DECREF(a);
Py_DECREF(b);
return (PyObject *)z;
@@ -3464,7 +3464,7 @@
Py_DECREF(c);
c = temp;
temp = NULL;
- c->ob_size = - c->ob_size;
+ Py_SIZE(c) = - Py_SIZE(c);
}
/* if modulus == 1:
@@ -3608,21 +3608,21 @@
long_neg(PyLongObject *v)
{
PyLongObject *z;
- if (v->ob_size == 0 && PyLong_CheckExact(v)) {
+ if (Py_SIZE(v) == 0 && PyLong_CheckExact(v)) {
/* -0 == 0 */
Py_INCREF(v);
return (PyObject *) v;
}
z = (PyLongObject *)_PyLong_Copy(v);
if (z != NULL)
- z->ob_size = -(v->ob_size);
+ Py_SIZE(z) = -(Py_SIZE(v));
return (PyObject *)z;
}
static PyObject *
long_abs(PyLongObject *v)
{
- if (v->ob_size < 0)
+ if (Py_SIZE(v) < 0)
return long_neg(v);
else
return long_long((PyObject *)v);
@@ -3725,15 +3725,15 @@
wordshift = shiftby / PyLong_SHIFT;
remshift = shiftby - wordshift * PyLong_SHIFT;
- oldsize = ABS(a->ob_size);
+ oldsize = ABS(Py_SIZE(a));
newsize = oldsize + wordshift;
if (remshift)
++newsize;
z = _PyLong_New(newsize);
if (z == NULL)
goto out;
- if (a->ob_size < 0)
- z->ob_size = -(z->ob_size);
+ if (Py_SIZE(a) < 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
for (i = 0; i < wordshift; i++)
z->ob_digit[i] = 0;
accum = 0;
@@ -4142,7 +4142,7 @@
{
Py_ssize_t res;
- res = v->ob_type->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit);
+ res = Py_TYPE(v)->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit);
return PyInt_FromSsize_t(res);
}
@@ -4314,8 +4314,7 @@
};
PyTypeObject PyLong_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0, /* ob_size */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
"long", /* tp_name */
offsetof(PyLongObject, ob_digit), /* tp_basicsize */
sizeof(digit), /* tp_itemsize */