[Python-checkins] consistently use Py_TYPE, Py_REFCNT, and correct initializer macros (#3563)

Benjamin Peterson webhook-mailer at python.org
Thu Sep 14 00:20:31 EDT 2017


https://github.com/python/cpython/commit/a72d15c97f9837f6c9c6bd476a62175c942cc588
commit: a72d15c97f9837f6c9c6bd476a62175c942cc588
branch: 2.7
author: Benjamin Peterson <benjamin at python.org>
committer: GitHub <noreply at github.com>
date: 2017-09-13T21:20:29-07:00
summary:

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.

files:
M Include/frameobject.h
M Include/intobject.h
M Modules/_ctypes/ctypes.h
M Modules/_elementtree.c
M Modules/_json.c
M Modules/_lsprof.c
M Modules/_sqlite/connection.c
M Modules/_sqlite/cursor.c
M Modules/_sre.c
M Modules/_testcapimodule.c
M Modules/arraymodule.c
M Modules/datetimemodule.c
M Objects/bufferobject.c
M Objects/classobject.c
M Objects/exceptions.c
M Objects/fileobject.c
M Objects/floatobject.c
M Objects/intobject.c
M Objects/longobject.c
M Objects/rangeobject.c
M Objects/setobject.c
M Objects/weakrefobject.c

diff --git a/Include/frameobject.h b/Include/frameobject.h
index 17e7679ac87..843908159dd 100644
--- a/Include/frameobject.h
+++ b/Include/frameobject.h
@@ -54,7 +54,7 @@ typedef struct _frame {
 
 PyAPI_DATA(PyTypeObject) PyFrame_Type;
 
-#define PyFrame_Check(op) ((op)->ob_type == &PyFrame_Type)
+#define PyFrame_Check(op) (Py_TYPE(op) == &PyFrame_Type)
 #define PyFrame_IsRestricted(f) \
 	((f)->f_builtins != (f)->f_tstate->interp->builtins)
 
diff --git a/Include/intobject.h b/Include/intobject.h
index 252eea9fd92..59d061629e2 100644
--- a/Include/intobject.h
+++ b/Include/intobject.h
@@ -28,8 +28,8 @@ typedef struct {
 PyAPI_DATA(PyTypeObject) PyInt_Type;
 
 #define PyInt_Check(op) \
-		 PyType_FastSubclass((op)->ob_type, Py_TPFLAGS_INT_SUBCLASS)
-#define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type)
+		 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_INT_SUBCLASS)
+#define PyInt_CheckExact(op) (Py_TYPE(op) == &PyInt_Type)
 
 PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
 #ifdef Py_USING_UNICODE
diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h
index b21fe92b044..12b56c4342f 100644
--- a/Modules/_ctypes/ctypes.h
+++ b/Modules/_ctypes/ctypes.h
@@ -108,7 +108,7 @@ typedef struct {
     ffi_type *atypes[1];
 } CThunkObject;
 extern PyTypeObject PyCThunk_Type;
-#define CThunk_CheckExact(v)        ((v)->ob_type == &PyCThunk_Type)
+#define CThunk_CheckExact(v)        (Py_TYPE(v) == &PyCThunk_Type)
 
 typedef struct {
     /* First part identical to tagCDataObject */
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 929616f3e2e..b52402f3811 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1661,8 +1661,8 @@ static PyMappingMethods element_as_mapping = {
 };
 
 statichere PyTypeObject Element_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0, "Element", sizeof(ElementObject), 0,
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "Element", sizeof(ElementObject), 0,
     /* methods */
     (destructor)element_dealloc, /* tp_dealloc */
     0, /* tp_print */
@@ -2031,8 +2031,8 @@ treebuilder_getattr(TreeBuilderObject* self, char* name)
 }
 
 statichere PyTypeObject TreeBuilder_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0, "TreeBuilder", sizeof(TreeBuilderObject), 0,
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "TreeBuilder", sizeof(TreeBuilderObject), 0,
     /* methods */
     (destructor)treebuilder_dealloc, /* tp_dealloc */
     0, /* tp_print */
@@ -2897,8 +2897,8 @@ xmlparser_getattr(XMLParserObject* self, char* name)
 }
 
 statichere PyTypeObject XMLParser_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0, "XMLParser", sizeof(XMLParserObject), 0,
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "XMLParser", sizeof(XMLParserObject), 0,
     /* methods */
     (destructor)xmlparser_dealloc, /* tp_dealloc */
     0, /* tp_print */
diff --git a/Modules/_json.c b/Modules/_json.c
index be1e0796960..39ec467b093 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1767,8 +1767,7 @@ PyDoc_STRVAR(scanner_doc, "JSON scanner object");
 
 static
 PyTypeObject PyScannerType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                    /* tp_internal */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "_json.Scanner",       /* tp_name */
     sizeof(PyScannerObject), /* tp_basicsize */
     0,                    /* tp_itemsize */
@@ -2344,8 +2343,7 @@ PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable")
 
 static
 PyTypeObject PyEncoderType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                    /* tp_internal */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "_json.Encoder",       /* tp_name */
     sizeof(PyEncoderObject), /* tp_basicsize */
     0,                    /* tp_itemsize */
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index 049c94db393..6090c7dd236 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -817,8 +817,7 @@ Profiler(custom_timer=None, time_unit=None, subcalls=True, builtins=True)\n\
 ");
 
 statichere PyTypeObject PyProfiler_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                      /* ob_size */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "_lsprof.Profiler",                     /* tp_name */
     sizeof(ProfilerObject),                 /* tp_basicsize */
     0,                                      /* tp_itemsize */
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 237d6e419a9..439542e5139 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -293,7 +293,7 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
     Py_XDECREF(self->statements);
     Py_XDECREF(self->cursors);
 
-    self->ob_type->tp_free((PyObject*)self);
+    Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 /*
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index db360040e44..ad607d93861 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -135,7 +135,7 @@ static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
         PyObject_ClearWeakRefs((PyObject*)self);
     }
 
-    self->ob_type->tp_free((PyObject*)self);
+    Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 PyObject* _pysqlite_get_converter(PyObject* key)
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 6fd3affb09a..94b3d5089ce 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -2712,8 +2712,8 @@ static PyMemberDef pattern_members[] = {
 };
 
 statichere PyTypeObject Pattern_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0, "_" SRE_MODULE ".SRE_Pattern",
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_" SRE_MODULE ".SRE_Pattern",
     sizeof(PatternObject), sizeof(SRE_CODE),
     (destructor)pattern_dealloc, /*tp_dealloc*/
     0,                                  /* tp_print */
@@ -3952,8 +3952,8 @@ static PyMemberDef scanner_members[] = {
 };
 
 statichere PyTypeObject Scanner_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0, "_" SRE_MODULE ".SRE_Scanner",
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_" SRE_MODULE ".SRE_Scanner",
     sizeof(ScannerObject), 0,
     (destructor)scanner_dealloc, /*tp_dealloc*/
     0,				/* tp_print */
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index ecc473cf4c1..7691b5188ff 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -187,8 +187,7 @@ test_dict_iteration(PyObject* self)
  *   PyType_Ready if it hasn't already been called
  */
 static PyTypeObject _HashInheritanceTester_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                          /* Number of items for varobject */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "hashinheritancetester",            /* Name of this type */
     sizeof(PyObject),           /* Basic object size */
     0,                          /* Item size for varobject */
@@ -315,8 +314,7 @@ static PyBufferProcs memoryviewtester_as_buffer = {
 };
 
 static PyTypeObject _MemoryViewTester_Type = {
-    PyObject_HEAD_INIT(NULL)
-    0,                          /* Number of items for varobject */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "memoryviewtester",         /* Name of this type */
     sizeof(PyObject),           /* Basic object size */
     0,                          /* Item size for varobject */
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index f6f597a046c..5bd3a42f009 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1292,9 +1292,9 @@ array_tofile(arrayobject *self, PyObject *f)
         PyErr_SetString(PyExc_TypeError, "arg must be open file");
         return NULL;
     }
-    if (self->ob_size > 0) {
+    if (Py_SIZE(self) > 0) {
         if (fwrite(self->ob_item, self->ob_descr->itemsize,
-                   self->ob_size, fp) != (size_t)self->ob_size) {
+                   Py_SIZE(self), fp) != (size_t)Py_SIZE(self)) {
             PyErr_SetFromErrno(PyExc_IOError);
             clearerr(fp);
             return NULL;
@@ -1348,7 +1348,7 @@ array_fromlist(arrayobject *self, PyObject *list)
             if ((*self->ob_descr->setitem)(self,
                             Py_SIZE(self) - n + i, v) != 0) {
                 Py_SIZE(self) -= n;
-                if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {
+                if (itemsize && (Py_SIZE(self) > PY_SSIZE_T_MAX / itemsize)) {
                     return PyErr_NoMemory();
                 }
                 PyMem_RESIZE(item, char,
@@ -1444,7 +1444,7 @@ values,as if it had been read from a file using the fromfile() method).");
 static PyObject *
 array_tostring(arrayobject *self, PyObject *unused)
 {
-    if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
+    if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
         return PyString_FromStringAndSize(self->ob_item,
                             Py_SIZE(self) * self->ob_descr->itemsize);
     } else {
@@ -2289,8 +2289,8 @@ initarray(void)
 {
     PyObject *m;
 
-    Arraytype.ob_type = &PyType_Type;
-    PyArrayIter_Type.ob_type = &PyType_Type;
+    Py_TYPE(&Arraytype) = &PyType_Type;
+    Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
     m = Py_InitModule3("array", a_methods, module_doc);
     if (m == NULL)
         return;
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 45736376d14..34b8ed41b8d 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -3039,8 +3039,7 @@ static char tzinfo_doc[] =
 PyDoc_STR("Abstract base class for time zone info objects.");
 
 statichere PyTypeObject PyDateTime_TZInfoType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                          /* ob_size */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "datetime.tzinfo",                          /* tp_name */
     sizeof(PyDateTime_TZInfo),                  /* tp_basicsize */
     0,                                          /* tp_itemsize */
@@ -3564,8 +3563,7 @@ static PyNumberMethods time_as_number = {
 };
 
 statichere PyTypeObject PyDateTime_TimeType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                          /* ob_size */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "datetime.time",                            /* tp_name */
     sizeof(PyDateTime_Time),                    /* tp_basicsize */
     0,                                          /* tp_itemsize */
@@ -4692,8 +4690,7 @@ static PyNumberMethods datetime_as_number = {
 };
 
 statichere PyTypeObject PyDateTime_DateTimeType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                          /* ob_size */
+    PyVarObject_HEAD_INIT(NULL, 0)
     "datetime.datetime",                        /* tp_name */
     sizeof(PyDateTime_DateTime),                /* tp_basicsize */
     0,                                          /* tp_itemsize */
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index 65857bf0b39..d2297f306f4 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -34,7 +34,7 @@ get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size,
     else {
         Py_ssize_t count, offset;
         readbufferproc proc = 0;
-        PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer;
+        PyBufferProcs *bp = Py_TYPE(self->b_base)->tp_as_buffer;
         if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) {
             PyErr_SetString(PyExc_TypeError,
                 "single-segment buffer object expected");
@@ -47,7 +47,7 @@ get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size,
             (buffer_type == ANY_BUFFER))
             proc = (readbufferproc)bp->bf_getwritebuffer;
         else if (buffer_type == CHAR_BUFFER) {
-            if (!PyType_HasFeature(self->ob_type,
+            if (!PyType_HasFeature(Py_TYPE(self),
                         Py_TPFLAGS_HAVE_GETCHARBUFFER)) {
             PyErr_SetString(PyExc_TypeError,
                 "Py_TPFLAGS_HAVE_GETCHARBUFFER needed");
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 738e613c836..5b645787cbf 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -88,9 +88,9 @@ PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
             base = PyTuple_GET_ITEM(bases, i);
             if (!PyClass_Check(base)) {
                 if (PyCallable_Check(
-                    (PyObject *) base->ob_type))
+                        (PyObject *) Py_TYPE(base)))
                     return PyObject_CallFunctionObjArgs(
-                        (PyObject *) base->ob_type,
+                        (PyObject *) Py_TYPE(base),
                         name, bases, dict, NULL);
                 PyErr_SetString(PyExc_TypeError,
                     "PyClass_New: base must be a class");
@@ -265,7 +265,7 @@ class_getattr(register PyClassObject *op, PyObject *name)
                      PyString_AS_STRING(op->cl_name), sname);
         return NULL;
     }
-    f = TP_DESCR_GET(v->ob_type);
+    f = TP_DESCR_GET(Py_TYPE(v));
     if (f == NULL)
         Py_INCREF(v);
     else
@@ -442,8 +442,7 @@ class_traverse(PyClassObject *o, visitproc visit, void *arg)
 }
 
 PyTypeObject PyClass_Type = {
-    PyObject_HEAD_INIT(&PyType_Type)
-    0,
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "classobj",
     sizeof(PyClassObject),
     0,
@@ -639,9 +638,9 @@ instance_dealloc(register PyInstanceObject *inst)
         PyObject_ClearWeakRefs((PyObject *) inst);
 
     /* Temporarily resurrect the object. */
-    assert(inst->ob_type == &PyInstance_Type);
-    assert(inst->ob_refcnt == 0);
-    inst->ob_refcnt = 1;
+    assert(Py_TYPE(inst) == &PyInstance_Type);
+    assert(Py_REFCNT(inst) == 0);
+    Py_REFCNT(inst) = 1;
 
     /* Save the current exception, if any. */
     PyErr_Fetch(&error_type, &error_value, &error_traceback);
@@ -665,8 +664,8 @@ instance_dealloc(register PyInstanceObject *inst)
     /* Undo the temporary resurrection; can't use DECREF here, it would
      * cause a recursive call.
      */
-    assert(inst->ob_refcnt > 0);
-    if (--inst->ob_refcnt == 0) {
+    assert(Py_REFCNT(inst) > 0);
+    if (--Py_REFCNT(inst) == 0) {
 
         /* New weakrefs could be created during the finalizer call.
             If this occurs, clear them out without calling their
@@ -682,12 +681,12 @@ instance_dealloc(register PyInstanceObject *inst)
         PyObject_GC_Del(inst);
     }
     else {
-        Py_ssize_t refcnt = inst->ob_refcnt;
+        Py_ssize_t refcnt = Py_REFCNT(inst);
         /* __del__ resurrected it!  Make it look like the original
          * Py_DECREF never happened.
          */
         _Py_NewReference((PyObject *)inst);
-        inst->ob_refcnt = refcnt;
+        Py_REFCNT(inst) = refcnt;
         _PyObject_GC_TRACK(inst);
         /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
          * we need to undo that. */
@@ -699,8 +698,8 @@ instance_dealloc(register PyInstanceObject *inst)
          * undone.
          */
 #ifdef COUNT_ALLOCS
-        --inst->ob_type->tp_frees;
-        --inst->ob_type->tp_allocs;
+        --Py_TYPE(inst)->tp_frees;
+        --Py_TYPE(inst)->tp_allocs;
 #endif
     }
 }
@@ -756,7 +755,7 @@ instance_getattr2(register PyInstanceObject *inst, PyObject *name)
     v = class_lookup(inst->in_class, name, &klass);
     if (v != NULL) {
         Py_INCREF(v);
-        f = TP_DESCR_GET(v->ob_type);
+        f = TP_DESCR_GET(Py_TYPE(v));
         if (f != NULL) {
             PyObject *w = f(v, (PyObject *)inst,
                             (PyObject *)(inst->in_class));
@@ -1012,7 +1011,7 @@ instance_hash(PyInstanceObject *inst)
         return -1;
     if (PyInt_Check(res) || PyLong_Check(res))
         /* This already converts a -1 result to -2. */
-        outcome = res->ob_type->tp_hash(res);
+        outcome = Py_TYPE(res)->tp_hash(res);
     else {
         PyErr_SetString(PyExc_TypeError,
                         "__hash__() should return an int");
@@ -1500,7 +1499,7 @@ half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
     }
     v1 = PyTuple_GetItem(coerced, 0);
     w = PyTuple_GetItem(coerced, 1);
-    if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
+    if (Py_TYPE(v1) == Py_TYPE(v) && PyInstance_Check(v)) {
         /* prevent recursion if __coerce__ returns self as the first
          * argument */
         result = generic_binary_op(v1, w, opname);
@@ -2077,7 +2076,7 @@ instance_getiter(PyInstanceObject *self)
             PyErr_Format(PyExc_TypeError,
                          "__iter__ returned non-iterator "
                          "of type '%.100s'",
-                         res->ob_type->tp_name);
+                         Py_TYPE(res)->tp_name);
             Py_DECREF(res);
             res = NULL;
         }
@@ -2201,8 +2200,7 @@ static PyNumberMethods instance_as_number = {
 };
 
 PyTypeObject PyInstance_Type = {
-    PyObject_HEAD_INIT(&PyType_Type)
-    0,
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "instance",
     sizeof(PyInstanceObject),
     0,
@@ -2321,7 +2319,7 @@ static PyObject *
 instancemethod_getattro(PyObject *obj, PyObject *name)
 {
     PyMethodObject *im = (PyMethodObject *)obj;
-    PyTypeObject *tp = obj->ob_type;
+    PyTypeObject *tp = Py_TYPE(obj);
     PyObject *descr = NULL;
 
     if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) {
@@ -2333,9 +2331,9 @@ instancemethod_getattro(PyObject *obj, PyObject *name)
     }
 
     if (descr != NULL) {
-        descrgetfunc f = TP_DESCR_GET(descr->ob_type);
+        descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
         if (f != NULL)
-            return f(descr, obj, (PyObject *)obj->ob_type);
+            return f(descr, obj, (PyObject *)Py_TYPE(obj));
         else {
             Py_INCREF(descr);
             return descr;
@@ -2538,7 +2536,7 @@ getinstclassname(PyObject *inst, char *buf, int bufsize)
     if (klass == NULL) {
         /* This function cannot return an exception */
         PyErr_Clear();
-        klass = (PyObject *)(inst->ob_type);
+        klass = (PyObject *)Py_TYPE(inst);
         Py_INCREF(klass);
     }
     getclassname(klass, buf, bufsize);
@@ -2631,8 +2629,7 @@ instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
 }
 
 PyTypeObject PyMethod_Type = {
-    PyObject_HEAD_INIT(&PyType_Type)
-    0,
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "instancemethod",
     sizeof(PyMethodObject),
     0,
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 1929777197f..224d1ba08af 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -368,8 +368,7 @@ static PyGetSetDef BaseException_getset[] = {
 
 
 static PyTypeObject _PyExc_BaseException = {
-    PyObject_HEAD_INIT(NULL)
-    0,                          /*ob_size*/
+    PyVarObject_HEAD_INIT(NULL, 0)
     EXC_MODULE_NAME "BaseException", /*tp_name*/
     sizeof(PyBaseExceptionObject), /*tp_basicsize*/
     0,                          /*tp_itemsize*/
@@ -419,8 +418,7 @@ PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
  */
 #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
 static PyTypeObject _PyExc_ ## EXCNAME = { \
-    PyObject_HEAD_INIT(NULL) \
-    0, \
+    PyVarObject_HEAD_INIT(NULL, 0) \
     EXC_MODULE_NAME # EXCNAME, \
     sizeof(PyBaseExceptionObject), \
     0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
@@ -435,8 +433,7 @@ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
 
 #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
 static PyTypeObject _PyExc_ ## EXCNAME = { \
-    PyObject_HEAD_INIT(NULL) \
-    0, \
+    PyVarObject_HEAD_INIT(NULL, 0) \
     EXC_MODULE_NAME # EXCNAME, \
     sizeof(Py ## EXCSTORE ## Object), \
     0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
@@ -451,8 +448,7 @@ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
 
 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
 static PyTypeObject _PyExc_ ## EXCNAME = { \
-    PyObject_HEAD_INIT(NULL) \
-    0, \
+    PyVarObject_HEAD_INIT(NULL, 0) \
     EXC_MODULE_NAME # EXCNAME, \
     sizeof(Py ## EXCSTORE ## Object), 0, \
     (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
@@ -1679,8 +1675,7 @@ UnicodeEncodeError_str(PyObject *self)
 }
 
 static PyTypeObject _PyExc_UnicodeEncodeError = {
-    PyObject_HEAD_INIT(NULL)
-    0,
+    PyVarObject_HEAD_INIT(NULL, 0)
     EXC_MODULE_NAME "UnicodeEncodeError",
     sizeof(PyUnicodeErrorObject), 0,
     (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -1764,8 +1759,7 @@ UnicodeDecodeError_str(PyObject *self)
 }
 
 static PyTypeObject _PyExc_UnicodeDecodeError = {
-    PyObject_HEAD_INIT(NULL)
-    0,
+    PyVarObject_HEAD_INIT(NULL, 0)
     EXC_MODULE_NAME "UnicodeDecodeError",
     sizeof(PyUnicodeErrorObject), 0,
     (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -1862,8 +1856,7 @@ UnicodeTranslateError_str(PyObject *self)
 }
 
 static PyTypeObject _PyExc_UnicodeTranslateError = {
-    PyObject_HEAD_INIT(NULL)
-    0,
+    PyVarObject_HEAD_INIT(NULL, 0)
     EXC_MODULE_NAME "UnicodeTranslateError",
     sizeof(PyUnicodeErrorObject), 0,
     (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index a7d64ba16d4..7e07a5376f8 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -427,7 +427,7 @@ close_the_file(PyFileObject *f)
     if (local_fp != NULL) {
         local_close = f->f_close;
         if (local_close != NULL && f->unlocked_count > 0) {
-            if (f->ob_refcnt > 0) {
+            if (Py_REFCNT(f) > 0) {
                 PyErr_SetString(PyExc_IOError,
                     "close() called during concurrent "
                     "operation on the same file object.");
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 801f286e6a4..360ef94d2f7 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -65,7 +65,7 @@ PyFloat_GetMin(void)
     return DBL_MIN;
 }
 
-static PyTypeObject FloatInfoType = {0, 0, 0, 0, 0, 0};
+static PyTypeObject FloatInfoType;
 
 PyDoc_STRVAR(floatinfo__doc__,
 "sys.float_info\n\
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 04e4d299169..3ab00af1e28 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -1486,7 +1486,7 @@ PyInt_ClearFreeList(void)
         for (i = 0, p = &list->objects[0];
              i < N_INTOBJECTS;
              i++, p++) {
-            if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
+            if (PyInt_CheckExact(p) && Py_REFCNT(p) != 0)
                 u++;
         }
         next = list->next;
@@ -1497,7 +1497,7 @@ PyInt_ClearFreeList(void)
                  i < N_INTOBJECTS;
                  i++, p++) {
                 if (!PyInt_CheckExact(p) ||
-                    p->ob_refcnt == 0) {
+                    Py_REFCNT(p) == 0) {
                     Py_TYPE(p) = (struct _typeobject *)
                         free_list;
                     free_list = p;
@@ -1560,14 +1560,14 @@ PyInt_Fini(void)
             for (i = 0, p = &list->objects[0];
                  i < N_INTOBJECTS;
                  i++, p++) {
-                if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
+                if (PyInt_CheckExact(p) && Py_REFCNT(p) != 0)
                     /* XXX(twouters) cast refcount to
                        long until %zd is universally
                        available
                      */
                     fprintf(stderr,
                 "#   <int at %p, refcnt=%ld, val=%ld>\n",
-                                p, (long)p->ob_refcnt,
+                                p, (long)Py_REFCNT(p),
                                 p->ob_ival);
             }
             list = list->next;
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 768a92a9414..5d6ce70d537 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -83,12 +83,12 @@ _PyLong_Copy(PyLongObject *src)
     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 @@ PyLong_FromLong(long ival)
     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 @@ PyLong_AsSsize_t(PyObject *vv) {
         return -1;
     }
     v = (PyLongObject *)vv;
-    i = v->ob_size;
+    i = Py_SIZE(v);
     sign = 1;
     x = 0;
     if (i < 0) {
@@ -464,7 +464,7 @@ PyLong_AsUnsignedLongMask(PyObject *vv)
         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 @@ PyLong_AsLongLong(PyObject *vv)
         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 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv)
         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 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
 
     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 @@ _PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
     *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 @@ long_divrem(PyLongObject *a, PyLongObject *b,
        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 @@ long_hash(PyLongObject *v)
     /* 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 @@ x_sub(PyLongObject *a, PyLongObject *b)
     }
     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 @@ long_add(PyLongObject *v, PyLongObject *w)
 
     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 @@ long_sub(PyLongObject *v, PyLongObject *w)
 
     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 @@ k_mul(PyLongObject *a, PyLongObject *b)
     /* 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 @@ ah*bh and al*bl too.
 
 /* 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 @@ long_mul(PyLongObject *v, PyLongObject *w)
 
     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 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
             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 @@ static PyObject *
 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 @@ long_lshift(PyObject *v, PyObject *w)
     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 @@ long_sizeof(PyLongObject *v)
 {
     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 @@ static PyNumberMethods long_as_number = {
 };
 
 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 */
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index baa8deebe89..888069a433a 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -183,8 +183,7 @@ static PyMethodDef range_methods[] = {
 };
 
 PyTypeObject PyRange_Type = {
-    PyObject_HEAD_INIT(&PyType_Type)
-    0,                          /* Number of items for varobject */
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "xrange",                   /* Name of this type */
     sizeof(rangeobject),        /* Basic object size */
     0,                          /* Item size for varobject */
@@ -256,8 +255,7 @@ static PyMethodDef rangeiter_methods[] = {
 };
 
 static PyTypeObject Pyrangeiter_Type = {
-    PyObject_HEAD_INIT(&PyType_Type)
-    0,                                      /* ob_size */
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "rangeiterator",                        /* tp_name */
     sizeof(rangeiterobject),                /* tp_basicsize */
     0,                                      /* tp_itemsize */
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 0c69fac8e6d..154be43564d 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -583,13 +583,13 @@ set_tp_print(PySetObject *so, FILE *fp, int flags)
         if (status < 0)
             return status;
         Py_BEGIN_ALLOW_THREADS
-        fprintf(fp, "%s(...)", so->ob_type->tp_name);
+        fprintf(fp, "%s(...)", Py_TYPE(so)->tp_name);
         Py_END_ALLOW_THREADS
         return 0;
     }
 
     Py_BEGIN_ALLOW_THREADS
-    fprintf(fp, "%s([", so->ob_type->tp_name);
+    fprintf(fp, "%s([", Py_TYPE(so)->tp_name);
     Py_END_ALLOW_THREADS
     while (set_next(so, &pos, &entry)) {
         Py_BEGIN_ALLOW_THREADS
@@ -617,7 +617,7 @@ set_repr(PySetObject *so)
     if (status != 0) {
         if (status < 0)
             return NULL;
-        return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
+        return PyString_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
     }
 
     keys = PySequence_List((PyObject *)so);
@@ -628,7 +628,7 @@ set_repr(PySetObject *so)
     if (listrepr == NULL)
         goto done;
 
-    result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
+    result = PyString_FromFormat("%s(%s)", Py_TYPE(so)->tp_name,
         PyString_AS_STRING(listrepr));
     Py_DECREF(listrepr);
 done:
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 2eb4fe0b17f..344e6f24fc5 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -196,7 +196,7 @@ weakref_repr(PyWeakReference *self)
 static PyObject *
 weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
 {
-    if ((op != Py_EQ && op != Py_NE) || self->ob_type != other->ob_type) {
+    if ((op != Py_EQ && op != Py_NE) || Py_TYPE(self) != Py_TYPE(other)) {
         Py_INCREF(Py_NotImplemented);
         return Py_NotImplemented;
     }
@@ -914,7 +914,7 @@ PyObject_ClearWeakRefs(PyObject *object)
 
     if (object == NULL
         || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
-        || object->ob_refcnt != 0) {
+        || Py_REFCNT(object) != 0) {
         PyErr_BadInternalCall();
         return;
     }
@@ -937,7 +937,7 @@ PyObject_ClearWeakRefs(PyObject *object)
             current->wr_callback = NULL;
             clear_weakref(current);
             if (callback != NULL) {
-                if (current->ob_refcnt > 0)
+                if (Py_REFCNT(current) > 0)
                     handle_callback(current, callback);
                 Py_DECREF(callback);
             }
@@ -955,7 +955,7 @@ PyObject_ClearWeakRefs(PyObject *object)
             for (i = 0; i < count; ++i) {
                 PyWeakReference *next = current->wr_next;
 
-                if (current->ob_refcnt > 0)
+                if (Py_REFCNT(current) > 0)
                 {
                     Py_INCREF(current);
                     PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);



More information about the Python-checkins mailing list