PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 1f0204d..49b877d 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -57,7 +57,7 @@
if (!PyString_Check(value)) {
PyErr_Format(PyExc_TypeError,
"can only assign string to %s.__name__, not '%s'",
- type->tp_name, value->ob_type->tp_name);
+ type->tp_name, Py_Type(value)->tp_name);
return -1;
}
if (strlen(PyString_AS_STRING(value))
@@ -203,7 +203,7 @@
if (!PyTuple_Check(value)) {
PyErr_Format(PyExc_TypeError,
"can only assign tuple to %s.__bases__, not %s",
- type->tp_name, value->ob_type->tp_name);
+ type->tp_name, Py_Type(value)->tp_name);
return -1;
}
if (PyTuple_GET_SIZE(value) == 0) {
@@ -218,7 +218,7 @@
PyErr_Format(
PyExc_TypeError,
"%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
- type->tp_name, ob->ob_type->tp_name);
+ type->tp_name, Py_Type(ob)->tp_name);
return -1;
}
if (PyType_Check(ob)) {
@@ -343,8 +343,8 @@
result = Py_None;
Py_INCREF(result);
}
- else if (result->ob_type->tp_descr_get) {
- result = result->ob_type->tp_descr_get(result, NULL,
+ else if (Py_Type(result)->tp_descr_get) {
+ result = Py_Type(result)->tp_descr_get(result, NULL,
(PyObject *)type);
}
else {
@@ -488,7 +488,7 @@
Py_ssize_t i, n;
PyMemberDef *mp;
- n = type->ob_size;
+ n = Py_Size(type);
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
for (i = 0; i < n; i++, mp++) {
if (mp->type == T_OBJECT_EX) {
@@ -512,10 +512,10 @@
/* Find the nearest base with a different tp_traverse,
and traverse slots while we're at it */
- type = self->ob_type;
+ type = Py_Type(self);
base = type;
while ((basetraverse = base->tp_traverse) == subtype_traverse) {
- if (base->ob_size) {
+ if (Py_Size(base)) {
int err = traverse_slots(base, self, visit, arg);
if (err)
return err;
@@ -547,7 +547,7 @@
Py_ssize_t i, n;
PyMemberDef *mp;
- n = type->ob_size;
+ n = Py_Size(type);
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
for (i = 0; i < n; i++, mp++) {
if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
@@ -569,10 +569,10 @@
/* Find the nearest base with a different tp_clear
and clear slots while we're at it */
- type = self->ob_type;
+ type = Py_Type(self);
base = type;
while ((baseclear = base->tp_clear) == subtype_clear) {
- if (base->ob_size)
+ if (Py_Size(base))
clear_slots(base, self);
base = base->tp_base;
assert(base);
@@ -593,7 +593,7 @@
destructor basedealloc;
/* Extract the type; we expect it to be a heap type */
- type = self->ob_type;
+ type = Py_Type(self);
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
/* Test whether the type has GC exactly once */
@@ -615,7 +615,7 @@
/* Find the nearest base with a different tp_dealloc */
base = type;
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
- assert(base->ob_size == 0);
+ assert(Py_Size(base) == 0);
base = base->tp_base;
assert(base);
}
@@ -683,7 +683,7 @@
/* Clear slots up to the nearest base with a different tp_dealloc */
base = type;
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
- if (base->ob_size)
+ if (Py_Size(base))
clear_slots(base, self);
base = base->tp_base;
assert(base);
@@ -877,13 +877,13 @@
if (*attrobj == NULL)
return NULL;
}
- res = _PyType_Lookup(self->ob_type, *attrobj);
+ res = _PyType_Lookup(Py_Type(self), *attrobj);
if (res != NULL) {
descrgetfunc f;
- if ((f = res->ob_type->tp_descr_get) == NULL)
+ if ((f = Py_Type(res)->tp_descr_get) == NULL)
Py_INCREF(res);
else
- res = f(res, self, (PyObject *)(self->ob_type));
+ res = f(res, self, (PyObject *)(Py_Type(self)));
}
return res;
}
@@ -1301,7 +1301,7 @@
PyObject *mro, *result, *tuple;
int checkit = 0;
- if (type->ob_type == &PyType_Type) {
+ if (Py_Type(type) == &PyType_Type) {
result = mro_implementation(type);
}
else {
@@ -1336,7 +1336,7 @@
else if (!PyType_Check(cls)) {
PyErr_Format(PyExc_TypeError,
"mro() returned a non-class ('%.500s')",
- cls->ob_type->tp_name);
+ Py_Type(cls)->tp_name);
Py_DECREF(tuple);
return -1;
}
@@ -1565,7 +1565,7 @@
if (value != NULL && !PyDict_Check(value)) {
PyErr_Format(PyExc_TypeError,
"__dict__ must be set to a dictionary, "
- "not a '%.200s'", value->ob_type->tp_name);
+ "not a '%.200s'", Py_Type(value)->tp_name);
return -1;
}
dict = *dictptr;
@@ -1581,16 +1581,16 @@
PyObject **weaklistptr;
PyObject *result;
- if (obj->ob_type->tp_weaklistoffset == 0) {
+ if (Py_Type(obj)->tp_weaklistoffset == 0) {
PyErr_SetString(PyExc_AttributeError,
"This object has no __weakref__");
return NULL;
}
- assert(obj->ob_type->tp_weaklistoffset > 0);
- assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
- (size_t)(obj->ob_type->tp_basicsize));
+ assert(Py_Type(obj)->tp_weaklistoffset > 0);
+ assert(Py_Type(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
+ (size_t)(Py_Type(obj)->tp_basicsize));
weaklistptr = (PyObject **)
- ((char *)obj + obj->ob_type->tp_weaklistoffset);
+ ((char *)obj + Py_Type(obj)->tp_weaklistoffset);
if (*weaklistptr == NULL)
result = Py_None;
else
@@ -1630,7 +1630,7 @@
if (!PyString_Check(s)) {
PyErr_Format(PyExc_TypeError,
"__slots__ items must be strings, not '%.200s'",
- s->ob_type->tp_name);
+ Py_Type(s)->tp_name);
return 0;
}
p = (unsigned char *) PyString_AS_STRING(s);
@@ -1740,8 +1740,8 @@
if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
PyObject *x = PyTuple_GET_ITEM(args, 0);
- Py_INCREF(x->ob_type);
- return (PyObject *) x->ob_type;
+ Py_INCREF(Py_Type(x));
+ return (PyObject *) Py_Type(x);
}
/* SF bug 475327 -- if that didn't trigger, we need 3
@@ -2176,7 +2176,7 @@
static PyObject *
type_getattro(PyTypeObject *type, PyObject *name)
{
- PyTypeObject *metatype = type->ob_type;
+ PyTypeObject *metatype = Py_Type(type);
PyObject *meta_attribute, *attribute;
descrgetfunc meta_get;
@@ -2193,7 +2193,7 @@
meta_attribute = _PyType_Lookup(metatype, name);
if (meta_attribute != NULL) {
- meta_get = meta_attribute->ob_type->tp_descr_get;
+ meta_get = Py_Type(meta_attribute)->tp_descr_get;
if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
/* Data descriptors implement tp_descr_set to intercept
@@ -2211,7 +2211,7 @@
attribute = _PyType_Lookup(type, name);
if (attribute != NULL) {
/* Implement descriptor functionality, if any */
- descrgetfunc local_get = attribute->ob_type->tp_descr_get;
+ descrgetfunc local_get = Py_Type(attribute)->tp_descr_get;
Py_XDECREF(meta_attribute);
@@ -2289,7 +2289,7 @@
PyObject_Free((char *)type->tp_doc);
Py_XDECREF(et->ht_name);
Py_XDECREF(et->ht_slots);
- type->ob_type->tp_free((PyObject *)type);
+ Py_Type(type)->tp_free((PyObject *)type);
}
static PyObject *
@@ -2397,8 +2397,7 @@
}
PyTypeObject PyType_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0, /* ob_size */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
"type", /* tp_name */
sizeof(PyHeapTypeObject), /* tp_basicsize */
sizeof(PyMemberDef), /* tp_itemsize */
@@ -2500,7 +2499,7 @@
{
int err = 0;
if (excess_args(args, kwds)) {
- PyTypeObject *type = self->ob_type;
+ PyTypeObject *type = Py_Type(self);
if (type->tp_init != object_init &&
type->tp_new != object_new)
{
@@ -2547,7 +2546,7 @@
static void
object_dealloc(PyObject *self)
{
- self->ob_type->tp_free(self);
+ Py_Type(self)->tp_free(self);
}
static PyObject *
@@ -2556,7 +2555,7 @@
PyTypeObject *type;
PyObject *mod, *name, *rtn;
- type = self->ob_type;
+ type = Py_Type(self);
mod = type_module(type, NULL);
if (mod == NULL)
PyErr_Clear();
@@ -2585,7 +2584,7 @@
{
unaryfunc f;
- f = self->ob_type->tp_repr;
+ f = Py_Type(self)->tp_repr;
if (f == NULL)
f = object_repr;
return f(self);
@@ -2600,8 +2599,8 @@
static PyObject *
object_get_class(PyObject *self, void *closure)
{
- Py_INCREF(self->ob_type);
- return (PyObject *)(self->ob_type);
+ Py_INCREF(Py_Type(self));
+ return (PyObject *)(Py_Type(self));
}
static int
@@ -2686,7 +2685,7 @@
static int
object_set_class(PyObject *self, PyObject *value, void *closure)
{
- PyTypeObject *oldto = self->ob_type;
+ PyTypeObject *oldto = Py_Type(self);
PyTypeObject *newto;
if (value == NULL) {
@@ -2697,7 +2696,7 @@
if (!PyType_Check(value)) {
PyErr_Format(PyExc_TypeError,
"__class__ must be set to new-style class, not '%s' object",
- value->ob_type->tp_name);
+ Py_Type(value)->tp_name);
return -1;
}
newto = (PyTypeObject *)value;
@@ -2710,7 +2709,7 @@
}
if (compatible_for_assignment(newto, oldto, "__class__")) {
Py_INCREF(newto);
- self->ob_type = newto;
+ Py_Type(self) = newto;
Py_DECREF(oldto);
return 0;
}
@@ -2806,7 +2805,7 @@
if (args != NULL && !PyTuple_Check(args)) {
PyErr_Format(PyExc_TypeError,
"__getnewargs__ should return a tuple, "
- "not '%.200s'", args->ob_type->tp_name);
+ "not '%.200s'", Py_Type(args)->tp_name);
goto end;
}
}
@@ -3018,8 +3017,7 @@
PyTypeObject PyBaseObject_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0, /* ob_size */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
"object", /* tp_name */
sizeof(PyObject), /* tp_basicsize */
0, /* tp_itemsize */
@@ -3461,8 +3459,8 @@
NULL when type is &PyBaseObject_Type, and we know its ob_type is
not NULL (it's initialized to &PyType_Type). But coverity doesn't
know that. */
- if (type->ob_type == NULL && base != NULL)
- type->ob_type = base->ob_type;
+ if (Py_Type(type) == NULL && base != NULL)
+ Py_Type(type) = Py_Type(base);
/* Initialize tp_bases */
bases = type->tp_bases;
@@ -3822,7 +3820,7 @@
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0) {
- PySequenceMethods *sq = self->ob_type->tp_as_sequence;
+ PySequenceMethods *sq = Py_Type(self)->tp_as_sequence;
if (sq && sq->sq_length) {
Py_ssize_t n = (*sq->sq_length)(self);
if (n < 0)
@@ -3998,14 +3996,14 @@
if (!check_num_args(args, 1))
return NULL;
other = PyTuple_GET_ITEM(args, 0);
- if (other->ob_type->tp_compare != func &&
- !PyType_IsSubtype(other->ob_type, self->ob_type)) {
+ if (Py_Type(other)->tp_compare != func &&
+ !PyType_IsSubtype(Py_Type(other), Py_Type(self))) {
PyErr_Format(
PyExc_TypeError,
"%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
- self->ob_type->tp_name,
- self->ob_type->tp_name,
- other->ob_type->tp_name);
+ Py_Type(self)->tp_name,
+ Py_Type(self)->tp_name,
+ Py_Type(other)->tp_name);
return NULL;
}
res = (*func)(self, other);
@@ -4019,7 +4017,7 @@
static int
hackcheck(PyObject *self, setattrofunc func, char *what)
{
- PyTypeObject *type = self->ob_type;
+ PyTypeObject *type = Py_Type(self);
while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
type = type->tp_base;
/* If type is NULL now, this is a really weird type.
@@ -4219,7 +4217,7 @@
PyErr_Format(PyExc_TypeError,
"%s.__new__(X): X is not a type object (%s)",
type->tp_name,
- arg0->ob_type->tp_name);
+ Py_Type(arg0)->tp_name);
return NULL;
}
subtype = (PyTypeObject *)arg0;
@@ -4310,14 +4308,14 @@
PyObject *a, *b;
int ok;
- b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
+ b = PyObject_GetAttrString((PyObject *)(Py_Type(right)), name);
if (b == NULL) {
PyErr_Clear();
/* If right doesn't have it, it's not overloaded */
return 0;
}
- a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
+ a = PyObject_GetAttrString((PyObject *)(Py_Type(left)), name);
if (a == NULL) {
PyErr_Clear();
Py_DECREF(b);
@@ -4342,14 +4340,14 @@
FUNCNAME(PyObject *self, PyObject *other) \
{ \
static PyObject *cache_str, *rcache_str; \
- int do_other = self->ob_type != other->ob_type && \
- other->ob_type->tp_as_number != NULL && \
- other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
- if (self->ob_type->tp_as_number != NULL && \
- self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
+ int do_other = Py_Type(self) != Py_Type(other) && \
+ Py_Type(other)->tp_as_number != NULL && \
+ Py_Type(other)->tp_as_number->SLOTNAME == TESTFUNC; \
+ if (Py_Type(self)->tp_as_number != NULL && \
+ Py_Type(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
PyObject *r; \
if (do_other && \
- PyType_IsSubtype(other->ob_type, self->ob_type) && \
+ PyType_IsSubtype(Py_Type(other), Py_Type(self)) && \
method_is_overloaded(self, other, ROPSTR)) { \
r = call_maybe( \
other, ROPSTR, &rcache_str, "(O)", self); \
@@ -4361,7 +4359,7 @@
r = call_maybe( \
self, OPSTR, &cache_str, "(O)", other); \
if (r != Py_NotImplemented || \
- other->ob_type == self->ob_type) \
+ Py_Type(other) == Py_Type(self)) \
return r; \
Py_DECREF(r); \
} \
@@ -4419,12 +4417,12 @@
if (getitem_str == NULL)
return NULL;
}
- func = _PyType_Lookup(self->ob_type, getitem_str);
+ func = _PyType_Lookup(Py_Type(self), getitem_str);
if (func != NULL) {
- if ((f = func->ob_type->tp_descr_get) == NULL)
+ if ((f = Py_Type(func)->tp_descr_get) == NULL)
Py_INCREF(func);
else {
- func = f(func, self, (PyObject *)(self->ob_type));
+ func = f(func, self, (PyObject *)(Py_Type(self)));
if (func == NULL) {
return NULL;
}
@@ -4563,8 +4561,8 @@
/* Three-arg power doesn't use __rpow__. But ternary_op
can call this when the second argument's type uses
slot_nb_power, so check before calling self.__pow__. */
- if (self->ob_type->tp_as_number != NULL &&
- self->ob_type->tp_as_number->nb_power == slot_nb_power) {
+ if (Py_Type(self)->tp_as_number != NULL &&
+ Py_Type(self)->tp_as_number->nb_power == slot_nb_power) {
return call_method(self, "__pow__", &pow_str,
"(OO)", other, modulus);
}
@@ -4754,12 +4752,12 @@
{
int c;
- if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
+ if (Py_Type(self)->tp_compare == _PyObject_SlotCompare) {
c = half_compare(self, other);
if (c <= 1)
return c;
}
- if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
+ if (Py_Type(other)->tp_compare == _PyObject_SlotCompare) {
c = half_compare(other, self);
if (c < -1)
return -2;
@@ -4784,7 +4782,7 @@
}
PyErr_Clear();
return PyString_FromFormat("<%s object at %p>",
- self->ob_type->tp_name, self);
+ Py_Type(self)->tp_name, self);
}
static PyObject *
@@ -4893,7 +4891,7 @@
static PyObject *
slot_tp_getattr_hook(PyObject *self, PyObject *name)
{
- PyTypeObject *tp = self->ob_type;
+ PyTypeObject *tp = Py_Type(self);
PyObject *getattr, *getattribute, *res;
static PyObject *getattribute_str = NULL;
static PyObject *getattr_str = NULL;
@@ -4917,7 +4915,7 @@
}
getattribute = _PyType_Lookup(tp, getattribute_str);
if (getattribute == NULL ||
- (getattribute->ob_type == &PyWrapperDescr_Type &&
+ (Py_Type(getattribute) == &PyWrapperDescr_Type &&
((PyWrapperDescrObject *)getattribute)->d_wrapped ==
(void *)PyObject_GenericGetAttr))
res = PyObject_GenericGetAttr(self, name);
@@ -4986,13 +4984,13 @@
{
PyObject *res;
- if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
+ if (Py_Type(self)->tp_richcompare == slot_tp_richcompare) {
res = half_richcompare(self, other, op);
if (res != Py_NotImplemented)
return res;
Py_DECREF(res);
}
- if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
+ if (Py_Type(other)->tp_richcompare == slot_tp_richcompare) {
res = half_richcompare(other, self, _Py_SwappedOp[op]);
if (res != Py_NotImplemented) {
return res;
@@ -5025,7 +5023,7 @@
if (func == NULL) {
PyErr_Format(PyExc_TypeError,
"'%.200s' object is not iterable",
- self->ob_type->tp_name);
+ Py_Type(self)->tp_name);
return NULL;
}
Py_DECREF(func);
@@ -5042,7 +5040,7 @@
static PyObject *
slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- PyTypeObject *tp = self->ob_type;
+ PyTypeObject *tp = Py_Type(self);
PyObject *get;
static PyObject *get_str = NULL;
@@ -5100,7 +5098,7 @@
if (res != Py_None) {
PyErr_Format(PyExc_TypeError,
"__init__() should return None, not '%.200s'",
- res->ob_type->tp_name);
+ Py_Type(res)->tp_name);
Py_DECREF(res);
return -1;
}
@@ -5185,7 +5183,7 @@
_Py_NewReference(self);
self->ob_refcnt = refcnt;
}
- assert(!PyType_IS_GC(self->ob_type) ||
+ assert(!PyType_IS_GC(Py_Type(self)) ||
_Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
* we need to undo that. */
@@ -5197,8 +5195,8 @@
* undone.
*/
#ifdef COUNT_ALLOCS
- --self->ob_type->tp_frees;
- --self->ob_type->tp_allocs;
+ --Py_Type(self)->tp_frees;
+ --Py_Type(self)->tp_allocs;
#endif
}
@@ -5560,7 +5558,7 @@
descr = _PyType_Lookup(type, p->name_strobj);
if (descr == NULL)
continue;
- if (descr->ob_type == &PyWrapperDescr_Type) {
+ if (Py_Type(descr) == &PyWrapperDescr_Type) {
void **tptr = resolve_slotdups(type, p->name_strobj);
if (tptr == NULL || tptr == ptr)
generic = p->function;
@@ -5575,7 +5573,7 @@
use_generic = 1;
}
}
- else if (descr->ob_type == &PyCFunction_Type &&
+ else if (Py_Type(descr) == &PyCFunction_Type &&
PyCFunction_GET_FUNCTION(descr) ==
(PyCFunction)tp_new_wrapper &&
strcmp(p->name, "__new__") == 0)
@@ -5846,7 +5844,7 @@
Py_XDECREF(su->obj);
Py_XDECREF(su->type);
Py_XDECREF(su->obj_type);
- self->ob_type->tp_free(self);
+ Py_Type(self)->tp_free(self);
}
static PyObject *
@@ -5911,7 +5909,7 @@
res = PyDict_GetItem(dict, name);
if (res != NULL) {
Py_INCREF(res);
- f = res->ob_type->tp_descr_get;
+ f = Py_Type(res)->tp_descr_get;
if (f != NULL) {
tmp = f(res,
/* Only pass 'obj' param if
@@ -5947,7 +5945,7 @@
the normal case; the return value is obj.__class__.
But... when obj is an instance, we want to allow for the case where
- obj->ob_type is not a subclass of type, but obj.__class__ is!
+ Py_Type(obj) is not a subclass of type, but obj.__class__ is!
This will allow using super() with a proxy for obj.
*/
@@ -5958,9 +5956,9 @@
}
/* Normal case */
- if (PyType_IsSubtype(obj->ob_type, type)) {
- Py_INCREF(obj->ob_type);
- return obj->ob_type;
+ if (PyType_IsSubtype(Py_Type(obj), type)) {
+ Py_INCREF(Py_Type(obj));
+ return Py_Type(obj);
}
else {
/* Try the slow way */
@@ -5977,7 +5975,7 @@
if (class_attr != NULL &&
PyType_Check(class_attr) &&
- (PyTypeObject *)class_attr != obj->ob_type)
+ (PyTypeObject *)class_attr != Py_Type(obj))
{
int ok = PyType_IsSubtype(
(PyTypeObject *)class_attr, type);
@@ -6008,10 +6006,10 @@
Py_INCREF(self);
return self;
}
- if (su->ob_type != &PySuper_Type)
+ if (Py_Type(su) != &PySuper_Type)
/* If su is an instance of a (strict) subclass of super,
call its type */
- return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
+ return PyObject_CallFunctionObjArgs((PyObject *)Py_Type(su),
su->type, obj, NULL);
else {
/* Inline the common case */
@@ -6080,8 +6078,7 @@
}
PyTypeObject PySuper_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0, /* ob_size */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
"super", /* tp_name */
sizeof(superobject), /* tp_basicsize */
0, /* tp_itemsize */