Skip to content

Commit 58ac700

Browse files
authored
bpo-39573: Use Py_TYPE() macro in Objects directory (GH-18392)
Replace direct access to PyObject.ob_type with Py_TYPE().
1 parent a102ed7 commit 58ac700

20 files changed

+109
-109
lines changed

Objects/bytearrayobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
856856
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
857857
PyErr_Format(PyExc_TypeError,
858858
"cannot convert '%.200s' object to bytearray",
859-
arg->ob_type->tp_name);
859+
Py_TYPE(arg)->tp_name);
860860
}
861861
return -1;
862862
}
@@ -1630,7 +1630,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
16301630
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
16311631
PyErr_Format(PyExc_TypeError,
16321632
"can't extend bytearray with %.100s",
1633-
iterable_of_ints->ob_type->tp_name);
1633+
Py_TYPE(iterable_of_ints)->tp_name);
16341634
}
16351635
return NULL;
16361636
}

Objects/bytesobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ PyBytes_FromObject(PyObject *x)
27622762

27632763
PyErr_Format(PyExc_TypeError,
27642764
"cannot convert '%.200s' object to bytes",
2765-
x->ob_type->tp_name);
2765+
Py_TYPE(x)->tp_name);
27662766
return NULL;
27672767
}
27682768

Objects/call.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ _PyObject_Call(PyThreadState *tstate, PyObject *callable,
263263
return PyVectorcall_Call(callable, args, kwargs);
264264
}
265265
else {
266-
call = callable->ob_type->tp_call;
266+
call = Py_TYPE(callable)->tp_call;
267267
if (call == NULL) {
268268
_PyErr_Format(tstate, PyExc_TypeError,
269269
"'%.200s' object is not callable",
270-
callable->ob_type->tp_name);
270+
Py_TYPE(callable)->tp_name);
271271
return NULL;
272272
}
273273

Objects/cellobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ cell_repr(PyCellObject *op)
112112
return PyUnicode_FromFormat("<cell at %p: empty>", op);
113113

114114
return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
115-
op, op->ob_ref->ob_type->tp_name,
115+
op, Py_TYPE(op->ob_ref)->tp_name,
116116
op->ob_ref);
117117
}
118118

Objects/classobject.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static PyObject *
178178
method_getattro(PyObject *obj, PyObject *name)
179179
{
180180
PyMethodObject *im = (PyMethodObject *)obj;
181-
PyTypeObject *tp = obj->ob_type;
181+
PyTypeObject *tp = Py_TYPE(obj);
182182
PyObject *descr = NULL;
183183

184184
{
@@ -190,9 +190,9 @@ method_getattro(PyObject *obj, PyObject *name)
190190
}
191191

192192
if (descr != NULL) {
193-
descrgetfunc f = TP_DESCR_GET(descr->ob_type);
193+
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
194194
if (f != NULL)
195-
return f(descr, obj, (PyObject *)obj->ob_type);
195+
return f(descr, obj, (PyObject *)Py_TYPE(obj));
196196
else {
197197
Py_INCREF(descr);
198198
return descr;
@@ -425,7 +425,7 @@ static PyGetSetDef instancemethod_getset[] = {
425425
static PyObject *
426426
instancemethod_getattro(PyObject *self, PyObject *name)
427427
{
428-
PyTypeObject *tp = self->ob_type;
428+
PyTypeObject *tp = Py_TYPE(self);
429429
PyObject *descr = NULL;
430430

431431
if (tp->tp_dict == NULL) {
@@ -435,9 +435,9 @@ instancemethod_getattro(PyObject *self, PyObject *name)
435435
descr = _PyType_Lookup(tp, name);
436436

437437
if (descr != NULL) {
438-
descrgetfunc f = TP_DESCR_GET(descr->ob_type);
438+
descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
439439
if (f != NULL)
440-
return f(descr, self, (PyObject *)self->ob_type);
440+
return f(descr, self, (PyObject *)Py_TYPE(self));
441441
else {
442442
Py_INCREF(descr);
443443
return descr;

Objects/codeobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ validate_and_copy_tuple(PyObject *tup)
416416
PyExc_TypeError,
417417
"name tuples must contain only "
418418
"strings, not '%.500s'",
419-
item->ob_type->tp_name);
419+
Py_TYPE(item)->tp_name);
420420
Py_DECREF(newtuple);
421421
return NULL;
422422
}

Objects/complexobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ try_complex_special_method(PyObject *op)
296296
if (!PyComplex_Check(res)) {
297297
PyErr_Format(PyExc_TypeError,
298298
"__complex__ returned non-complex (type %.200s)",
299-
res->ob_type->tp_name);
299+
Py_TYPE(res)->tp_name);
300300
Py_DECREF(res);
301301
return NULL;
302302
}
@@ -305,7 +305,7 @@ try_complex_special_method(PyObject *op)
305305
"__complex__ returned non-complex (type %.200s). "
306306
"The ability to return an instance of a strict subclass of complex "
307307
"is deprecated, and may be removed in a future version of Python.",
308-
res->ob_type->tp_name)) {
308+
Py_TYPE(res)->tp_name)) {
309309
Py_DECREF(res);
310310
return NULL;
311311
}
@@ -958,7 +958,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
958958
return NULL;
959959
}
960960

961-
nbr = r->ob_type->tp_as_number;
961+
nbr = Py_TYPE(r)->tp_as_number;
962962
if (nbr == NULL || (nbr->nb_float == NULL && nbr->nb_index == NULL)) {
963963
PyErr_Format(PyExc_TypeError,
964964
"complex() first argument must be a string or a number, "
@@ -970,7 +970,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
970970
return NULL;
971971
}
972972
if (i != NULL) {
973-
nbi = i->ob_type->tp_as_number;
973+
nbi = Py_TYPE(i)->tp_as_number;
974974
if (nbi == NULL || (nbi->nb_float == NULL && nbi->nb_index == NULL)) {
975975
PyErr_Format(PyExc_TypeError,
976976
"complex() second argument must be a number, "

Objects/descrobject.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
8484
"doesn't apply to a '%.100s' object",
8585
descr_name((PyDescrObject *)descr), "?",
8686
descr->d_type->tp_name,
87-
obj->ob_type->tp_name);
87+
Py_TYPE(obj)->tp_name);
8888
*pres = NULL;
8989
return 1;
9090
}
@@ -97,7 +97,7 @@ classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
9797
/* Ensure a valid type. Class methods ignore obj. */
9898
if (type == NULL) {
9999
if (obj != NULL)
100-
type = (PyObject *)obj->ob_type;
100+
type = (PyObject *)Py_TYPE(obj);
101101
else {
102102
/* Wot - no type?! */
103103
PyErr_Format(PyExc_TypeError,
@@ -114,7 +114,7 @@ classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
114114
"needs a type, not a '%.100s' as arg 2",
115115
descr_name((PyDescrObject *)descr), "?",
116116
PyDescr_TYPE(descr)->tp_name,
117-
type->ob_type->tp_name);
117+
Py_TYPE(type)->tp_name);
118118
return NULL;
119119
}
120120
if (!PyType_IsSubtype((PyTypeObject *)type, PyDescr_TYPE(descr))) {
@@ -194,7 +194,7 @@ descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
194194
"doesn't apply to a '%.100s' object",
195195
descr_name(descr), "?",
196196
descr->d_type->tp_name,
197-
obj->ob_type->tp_name);
197+
Py_TYPE(obj)->tp_name);
198198
*pres = -1;
199199
return 1;
200200
}
@@ -506,7 +506,7 @@ wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
506506
"but received a '%.100s'",
507507
descr_name((PyDescrObject *)descr), "?",
508508
PyDescr_TYPE(descr)->tp_name,
509-
self->ob_type->tp_name);
509+
Py_TYPE(self)->tp_name);
510510
return NULL;
511511
}
512512

@@ -1234,7 +1234,7 @@ wrapper_repr(wrapperobject *wp)
12341234
{
12351235
return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
12361236
wp->descr->d_base->name,
1237-
wp->self->ob_type->tp_name,
1237+
Py_TYPE(wp->self)->tp_name,
12381238
wp->self);
12391239
}
12401240

@@ -1476,7 +1476,7 @@ property_dealloc(PyObject *self)
14761476
Py_XDECREF(gs->prop_set);
14771477
Py_XDECREF(gs->prop_del);
14781478
Py_XDECREF(gs->prop_doc);
1479-
self->ob_type->tp_free(self);
1479+
Py_TYPE(self)->tp_free(self);
14801480
}
14811481

14821482
static PyObject *

Objects/dictobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4015,7 +4015,7 @@ _PyDictView_New(PyObject *dict, PyTypeObject *type)
40154015
/* XXX Get rid of this restriction later */
40164016
PyErr_Format(PyExc_TypeError,
40174017
"%s() requires a dict argument, not '%s'",
4018-
type->tp_name, dict->ob_type->tp_name);
4018+
type->tp_name, Py_TYPE(dict)->tp_name);
40194019
return NULL;
40204020
}
40214021
dv = PyObject_GC_New(_PyDictViewObject, type);

Objects/floatobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ PyFloat_AsDouble(PyObject *op)
256256
return val;
257257
}
258258
PyErr_Format(PyExc_TypeError, "must be real number, not %.50s",
259-
op->ob_type->tp_name);
259+
Py_TYPE(op)->tp_name);
260260
return -1;
261261
}
262262

@@ -268,15 +268,15 @@ PyFloat_AsDouble(PyObject *op)
268268
if (!PyFloat_Check(res)) {
269269
PyErr_Format(PyExc_TypeError,
270270
"%.50s.__float__ returned non-float (type %.50s)",
271-
op->ob_type->tp_name, res->ob_type->tp_name);
271+
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
272272
Py_DECREF(res);
273273
return -1;
274274
}
275275
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
276276
"%.50s.__float__ returned non-float (type %.50s). "
277277
"The ability to return an instance of a strict subclass of float "
278278
"is deprecated, and may be removed in a future version of Python.",
279-
op->ob_type->tp_name, res->ob_type->tp_name)) {
279+
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
280280
Py_DECREF(res);
281281
return -1;
282282
}

Objects/funcobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ PyFunction_SetClosure(PyObject *op, PyObject *closure)
196196
else {
197197
PyErr_Format(PyExc_SystemError,
198198
"expected tuple for closure, got '%.100s'",
199-
closure->ob_type->tp_name);
199+
Py_TYPE(closure)->tp_name);
200200
return -1;
201201
}
202202
Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
@@ -541,7 +541,7 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
541541
if (!PyCell_Check(o)) {
542542
return PyErr_Format(PyExc_TypeError,
543543
"arg 5 (closure) expected cell, found %s",
544-
o->ob_type->tp_name);
544+
Py_TYPE(o)->tp_name);
545545
}
546546
}
547547
}

Objects/interpreteridobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ interp_id_converter(PyObject *arg, void *ptr)
5656
else {
5757
PyErr_Format(PyExc_TypeError,
5858
"interpreter ID must be an int, got %.100s",
59-
arg->ob_type->tp_name);
59+
Py_TYPE(arg)->tp_name);
6060
return 0;
6161
}
6262
*(int64_t *)ptr = id;

Objects/listobject.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ list_concat(PyListObject *a, PyObject *bb)
493493
if (!PyList_Check(bb)) {
494494
PyErr_Format(PyExc_TypeError,
495495
"can only concatenate list (not \"%.200s\") to list",
496-
bb->ob_type->tp_name);
496+
Py_TYPE(bb)->tp_name);
497497
return NULL;
498498
}
499499
#define b ((PyListObject *)bb)
@@ -892,7 +892,7 @@ list_extend(PyListObject *self, PyObject *iterable)
892892
it = PyObject_GetIter(iterable);
893893
if (it == NULL)
894894
return NULL;
895-
iternext = *it->ob_type->tp_iternext;
895+
iternext = *Py_TYPE(it)->tp_iternext;
896896

897897
/* Guess a result list size. */
898898
n = PyObject_LengthHint(iterable, 8);
@@ -1179,7 +1179,7 @@ struct s_MergeState {
11791179

11801180
/* This function is used by unsafe_object_compare to optimize comparisons
11811181
* when we know our list is type-homogeneous but we can't assume anything else.
1182-
* In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1182+
* In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
11831183
PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
11841184

11851185
/* This function is used by unsafe_tuple_compare to compare the first elements
@@ -2015,7 +2015,7 @@ unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
20152015
PyObject *res_obj; int res;
20162016

20172017
/* No assumptions, because we check first: */
2018-
if (v->ob_type->tp_richcompare != ms->key_richcompare)
2018+
if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
20192019
return PyObject_RichCompareBool(v, w, Py_LT);
20202020

20212021
assert(ms->key_richcompare != NULL);
@@ -2052,8 +2052,8 @@ unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
20522052
int res;
20532053

20542054
/* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2055-
assert(v->ob_type == w->ob_type);
2056-
assert(v->ob_type == &PyUnicode_Type);
2055+
assert(Py_TYPE(v) == Py_TYPE(w));
2056+
assert(Py_TYPE(v) == &PyUnicode_Type);
20572057
assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
20582058
assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
20592059

@@ -2075,8 +2075,8 @@ unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
20752075
PyLongObject *vl, *wl; sdigit v0, w0; int res;
20762076

20772077
/* Modified from Objects/longobject.c:long_compare, assuming: */
2078-
assert(v->ob_type == w->ob_type);
2079-
assert(v->ob_type == &PyLong_Type);
2078+
assert(Py_TYPE(v) == Py_TYPE(w));
2079+
assert(Py_TYPE(v) == &PyLong_Type);
20802080
assert(Py_ABS(Py_SIZE(v)) <= 1);
20812081
assert(Py_ABS(Py_SIZE(w)) <= 1);
20822082

@@ -2103,8 +2103,8 @@ unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
21032103
int res;
21042104

21052105
/* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2106-
assert(v->ob_type == w->ob_type);
2107-
assert(v->ob_type == &PyFloat_Type);
2106+
assert(Py_TYPE(v) == Py_TYPE(w));
2107+
assert(Py_TYPE(v) == &PyFloat_Type);
21082108

21092109
res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
21102110
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
@@ -2125,8 +2125,8 @@ unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
21252125
int k;
21262126

21272127
/* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2128-
assert(v->ob_type == w->ob_type);
2129-
assert(v->ob_type == &PyTuple_Type);
2128+
assert(Py_TYPE(v) == Py_TYPE(w));
2129+
assert(Py_TYPE(v) == &PyTuple_Type);
21302130
assert(Py_SIZE(v) > 0);
21312131
assert(Py_SIZE(w) > 0);
21322132

@@ -2247,12 +2247,12 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
22472247
* set ms appropriately. */
22482248
if (saved_ob_size > 1) {
22492249
/* Assume the first element is representative of the whole list. */
2250-
int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2250+
int keys_are_in_tuples = (Py_TYPE(lo.keys[0]) == &PyTuple_Type &&
22512251
Py_SIZE(lo.keys[0]) > 0);
22522252

22532253
PyTypeObject* key_type = (keys_are_in_tuples ?
2254-
PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2255-
lo.keys[0]->ob_type);
2254+
Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2255+
Py_TYPE(lo.keys[0]));
22562256

22572257
int keys_are_all_same_type = 1;
22582258
int strings_are_latin = 1;
@@ -2262,7 +2262,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
22622262
for (i=0; i < saved_ob_size; i++) {
22632263

22642264
if (keys_are_in_tuples &&
2265-
!(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2265+
!(Py_TYPE(lo.keys[i]) == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
22662266
keys_are_in_tuples = 0;
22672267
keys_are_all_same_type = 0;
22682268
break;
@@ -2275,7 +2275,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
22752275
PyTuple_GET_ITEM(lo.keys[i], 0) :
22762276
lo.keys[i]);
22772277

2278-
if (key->ob_type != key_type) {
2278+
if (Py_TYPE(key) != key_type) {
22792279
keys_are_all_same_type = 0;
22802280
/* If keys are in tuple we must loop over the whole list to make
22812281
sure all items are tuples */
@@ -2818,7 +2818,7 @@ list_subscript(PyListObject* self, PyObject* item)
28182818
else {
28192819
PyErr_Format(PyExc_TypeError,
28202820
"list indices must be integers or slices, not %.200s",
2821-
item->ob_type->tp_name);
2821+
Py_TYPE(item)->tp_name);
28222822
return NULL;
28232823
}
28242824
}
@@ -2981,7 +2981,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29812981
else {
29822982
PyErr_Format(PyExc_TypeError,
29832983
"list indices must be integers or slices, not %.200s",
2984-
item->ob_type->tp_name);
2984+
Py_TYPE(item)->tp_name);
29852985
return -1;
29862986
}
29872987
}

0 commit comments

Comments
 (0)