Skip to content

Commit a102ed7

Browse files
authored
bpo-39573: Use Py_TYPE() macro in Python and Include directories (GH-18391)
Replace direct access to PyObject.ob_type with Py_TYPE().
1 parent 38aaaaa commit a102ed7

File tree

12 files changed

+39
-39
lines changed

12 files changed

+39
-39
lines changed

Include/classobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct {
1919

2020
PyAPI_DATA(PyTypeObject) PyMethod_Type;
2121

22-
#define PyMethod_Check(op) ((op)->ob_type == &PyMethod_Type)
22+
#define PyMethod_Check(op) (Py_TYPE(op)== &PyMethod_Type)
2323

2424
PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *);
2525

@@ -40,7 +40,7 @@ typedef struct {
4040

4141
PyAPI_DATA(PyTypeObject) PyInstanceMethod_Type;
4242

43-
#define PyInstanceMethod_Check(op) ((op)->ob_type == &PyInstanceMethod_Type)
43+
#define PyInstanceMethod_Check(op) (Py_TYPE(op) == &PyInstanceMethod_Type)
4444

4545
PyAPI_FUNC(PyObject *) PyInstanceMethod_New(PyObject *);
4646
PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);

Include/cpython/abstract.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
246246

247247
/* Return 1 if the getbuffer function is available, otherwise return 0. */
248248
#define PyObject_CheckBuffer(obj) \
249-
(((obj)->ob_type->tp_as_buffer != NULL) && \
250-
((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
249+
((Py_TYPE(obj)->tp_as_buffer != NULL) && \
250+
(Py_TYPE(obj)->tp_as_buffer->bf_getbuffer != NULL))
251251

252252
/* This is a C-API version of the getbuffer function call. It checks
253253
to make sure object has the required function pointer and issues the
@@ -315,14 +315,14 @@ PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
315315
/* ==== Iterators ================================================ */
316316

317317
#define PyIter_Check(obj) \
318-
((obj)->ob_type->tp_iternext != NULL && \
319-
(obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
318+
(Py_TYPE(obj)->tp_iternext != NULL && \
319+
Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
320320

321321
/* === Number Protocol ================================================== */
322322

323323
#define PyIndex_Check(obj) \
324-
((obj)->ob_type->tp_as_number != NULL && \
325-
(obj)->ob_type->tp_as_number->nb_index != NULL)
324+
(Py_TYPE(obj)->tp_as_number != NULL && \
325+
Py_TYPE(obj)->tp_as_number->nb_index != NULL)
326326

327327
/* === Sequence protocol ================================================ */
328328

Include/pyerrors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
5454
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
5555

5656
#define PyExceptionInstance_Check(x) \
57-
PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
57+
PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)
5858

5959
PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
6060

61-
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
61+
#define PyExceptionInstance_Class(x) ((PyObject*)Py_TYPE(x))
6262

6363

6464
/* Predefined exceptions */

Python/_warnings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ warn_explicit(PyObject *category, PyObject *message,
650650
text = PyObject_Str(message);
651651
if (text == NULL)
652652
goto cleanup;
653-
category = (PyObject*)message->ob_type;
653+
category = (PyObject*)Py_TYPE(message);
654654
}
655655
else {
656656
text = message;
@@ -906,7 +906,7 @@ get_category(PyObject *message, PyObject *category)
906906
return NULL;
907907

908908
if (rc == 1)
909-
category = (PyObject*)message->ob_type;
909+
category = (PyObject*)Py_TYPE(message);
910910
else if (category == NULL || category == Py_None)
911911
category = PyExc_UserWarning;
912912

Python/bltinmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
170170
/* else get the type of the first base */
171171
else {
172172
PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
173-
meta = (PyObject *) (base0->ob_type);
173+
meta = (PyObject *)Py_TYPE(base0);
174174
}
175175
Py_INCREF(meta);
176176
isclass = 1; /* meta is really a class */
@@ -1002,13 +1002,13 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
10021002

10031003
if (!PyDict_Check(globals)) {
10041004
PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
1005-
globals->ob_type->tp_name);
1005+
Py_TYPE(globals)->tp_name);
10061006
return NULL;
10071007
}
10081008
if (!PyMapping_Check(locals)) {
10091009
PyErr_Format(PyExc_TypeError,
10101010
"locals must be a mapping or None, not %.100s",
1011-
locals->ob_type->tp_name);
1011+
Py_TYPE(locals)->tp_name);
10121012
return NULL;
10131013
}
10141014
if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
@@ -1383,11 +1383,11 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
13831383
if (!PyIter_Check(it)) {
13841384
PyErr_Format(PyExc_TypeError,
13851385
"'%.200s' object is not an iterator",
1386-
it->ob_type->tp_name);
1386+
Py_TYPE(it)->tp_name);
13871387
return NULL;
13881388
}
13891389

1390-
res = (*it->ob_type->tp_iternext)(it);
1390+
res = (*Py_TYPE(it)->tp_iternext)(it);
13911391
if (res != NULL) {
13921392
return res;
13931393
} else if (nargs > 1) {
@@ -1788,7 +1788,7 @@ builtin_ord(PyObject *module, PyObject *c)
17881788
else {
17891789
PyErr_Format(PyExc_TypeError,
17901790
"ord() expected string of length 1, but " \
1791-
"%.200s found", c->ob_type->tp_name);
1791+
"%.200s found", Py_TYPE(c)->tp_name);
17921792
return NULL;
17931793
}
17941794

@@ -1856,7 +1856,7 @@ builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
18561856
else if (sep && !PyUnicode_Check(sep)) {
18571857
PyErr_Format(PyExc_TypeError,
18581858
"sep must be None or a string, not %.200s",
1859-
sep->ob_type->tp_name);
1859+
Py_TYPE(sep)->tp_name);
18601860
return NULL;
18611861
}
18621862
if (end == Py_None) {
@@ -1865,7 +1865,7 @@ builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
18651865
else if (end && !PyUnicode_Check(end)) {
18661866
PyErr_Format(PyExc_TypeError,
18671867
"end must be None or a string, not %.200s",
1868-
end->ob_type->tp_name);
1868+
Py_TYPE(end)->tp_name);
18691869
return NULL;
18701870
}
18711871

Python/ceval.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
26332633
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
26342634
if (none_val == NULL) {
26352635
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
2636-
(iterable->ob_type->tp_iter == NULL && !PySequence_Check(iterable)))
2636+
(Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
26372637
{
26382638
_PyErr_Clear(tstate);
26392639
_PyErr_Format(tstate, PyExc_TypeError,
@@ -2803,7 +2803,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
28032803
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
28042804
_PyErr_Format(tstate, PyExc_TypeError,
28052805
"'%.200s' object is not a mapping",
2806-
update->ob_type->tp_name);
2806+
Py_TYPE(update)->tp_name);
28072807
}
28082808
Py_DECREF(update);
28092809
goto error;
@@ -3158,7 +3158,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
31583158
PREDICTED(FOR_ITER);
31593159
/* before: [iter]; after: [iter, iter()] *or* [] */
31603160
PyObject *iter = TOP();
3161-
PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3161+
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
31623162
if (next != NULL) {
31633163
PUSH(next);
31643164
PREDICT(STORE_FAST);
@@ -4369,11 +4369,11 @@ unpack_iterable(PyThreadState *tstate, PyObject *v,
43694369
it = PyObject_GetIter(v);
43704370
if (it == NULL) {
43714371
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
4372-
v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4372+
Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
43734373
{
43744374
_PyErr_Format(tstate, PyExc_TypeError,
43754375
"cannot unpack non-iterable %.200s object",
4376-
v->ob_type->tp_name);
4376+
Py_TYPE(v)->tp_name);
43774377
}
43784378
return 0;
43794379
}
@@ -4790,7 +4790,7 @@ PyEval_GetFuncName(PyObject *func)
47904790
else if (PyCFunction_Check(func))
47914791
return ((PyCFunctionObject*)func)->m_ml->ml_name;
47924792
else
4793-
return func->ob_type->tp_name;
4793+
return Py_TYPE(func)->tp_name;
47944794
}
47954795

47964796
const char *
@@ -5197,7 +5197,7 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
51975197
static int
51985198
check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
51995199
{
5200-
if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5200+
if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
52015201
/* check_args_iterable() may be called with a live exception:
52025202
* clear it to prevent calling _PyObject_FunctionStr() with an
52035203
* exception set. */

Python/codecs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static void wrong_exception_type(PyObject *exc)
658658
{
659659
PyErr_Format(PyExc_TypeError,
660660
"don't know how to handle %.200s in error callback",
661-
exc->ob_type->tp_name);
661+
Py_TYPE(exc)->tp_name);
662662
}
663663

664664
PyObject *PyCodec_StrictErrors(PyObject *exc)

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3988,7 +3988,7 @@ infer_type(expr_ty e)
39883988
case FormattedValue_kind:
39893989
return &PyUnicode_Type;
39903990
case Constant_kind:
3991-
return e->v.Constant.value->ob_type;
3991+
return Py_TYPE(e->v.Constant.value);
39923992
default:
39933993
return NULL;
39943994
}

Python/formatter_unicode.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ _PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
14471447
return format_string_internal(obj, &format, writer);
14481448
default:
14491449
/* unknown */
1450-
unknown_presentation_type(format.type, obj->ob_type->tp_name);
1450+
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
14511451
return -1;
14521452
}
14531453
}
@@ -1505,7 +1505,7 @@ _PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
15051505

15061506
default:
15071507
/* unknown */
1508-
unknown_presentation_type(format.type, obj->ob_type->tp_name);
1508+
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
15091509
goto done;
15101510
}
15111511

@@ -1549,7 +1549,7 @@ _PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
15491549

15501550
default:
15511551
/* unknown */
1552-
unknown_presentation_type(format.type, obj->ob_type->tp_name);
1552+
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
15531553
return -1;
15541554
}
15551555
}
@@ -1587,7 +1587,7 @@ _PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
15871587

15881588
default:
15891589
/* unknown */
1590-
unknown_presentation_type(format.type, obj->ob_type->tp_name);
1590+
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
15911591
return -1;
15921592
}
15931593
}

Python/getargs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
531531
toplevel ? "expected %d arguments, not %.50s" :
532532
"must be %d-item sequence, not %.50s",
533533
n,
534-
arg == Py_None ? "None" : arg->ob_type->tp_name);
534+
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
535535
return msgbuf;
536536
}
537537

@@ -621,7 +621,7 @@ _PyArg_BadArgument(const char *fname, const char *displayname,
621621
PyErr_Format(PyExc_TypeError,
622622
"%.200s() %.200s must be %.50s, not %.50s",
623623
fname, displayname, expected,
624-
arg == Py_None ? "None" : arg->ob_type->tp_name);
624+
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
625625
}
626626

627627
static const char *
@@ -636,7 +636,7 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
636636
else {
637637
PyOS_snprintf(msgbuf, bufsize,
638638
"must be %.50s, not %.50s", expected,
639-
arg == Py_None ? "None" : arg->ob_type->tp_name);
639+
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
640640
}
641641
return msgbuf;
642642
}
@@ -1331,7 +1331,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
13311331
type = va_arg(*p_va, PyTypeObject*);
13321332
p = va_arg(*p_va, PyObject **);
13331333
format++;
1334-
if (PyType_IsSubtype(arg->ob_type, type))
1334+
if (PyType_IsSubtype(Py_TYPE(arg), type))
13351335
*p = arg;
13361336
else
13371337
return converterr(type->tp_name, arg, msgbuf, bufsize);

Python/marshal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ marshal_load(PyObject *module, PyObject *file)
16961696
if (!PyBytes_Check(data)) {
16971697
PyErr_Format(PyExc_TypeError,
16981698
"file.read() returned not bytes but %.100s",
1699-
data->ob_type->tp_name);
1699+
Py_TYPE(data)->tp_name);
17001700
result = NULL;
17011701
}
17021702
else {

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ sys_intern_impl(PyObject *module, PyObject *s)
840840
}
841841
else {
842842
_PyErr_Format(tstate, PyExc_TypeError,
843-
"can't intern %.400s", s->ob_type->tp_name);
843+
"can't intern %.400s", Py_TYPE(s)->tp_name);
844844
return NULL;
845845
}
846846
}

0 commit comments

Comments
 (0)