Skip to content

Commit 5572870

Browse files
petdancevstinner
andauthored
bpo-39573: Use Py_IS_TYPE() macro to check for types (GH-18809)
Co-authored-by: Victor Stinner <[email protected]>
1 parent e59334e commit 5572870

10 files changed

+15
-17
lines changed

Modules/_functoolsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
573573
PyObject *answer;
574574
PyObject* stack[2];
575575

576-
if (Py_TYPE(other) != &keyobject_type){
576+
if (!Py_IS_TYPE(other, &keyobject_type)) {
577577
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
578578
return NULL;
579579
}

Modules/_threadmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ local_getattro(localobject *self, PyObject *name)
938938
if (r == -1)
939939
return NULL;
940940

941-
if (Py_TYPE(self) != &localtype)
941+
if (!Py_IS_TYPE(self, &localtype))
942942
/* use generic lookup for subtypes */
943943
return _PyObject_GenericGetAttrWithDict(
944944
(PyObject *)self, name, ldict, 0);
@@ -1400,7 +1400,7 @@ static PyStructSequence_Desc ExceptHookArgs_desc = {
14001400
static PyObject *
14011401
thread_excepthook(PyObject *self, PyObject *args)
14021402
{
1403-
if (Py_TYPE(args) != &ExceptHookArgsType) {
1403+
if (!Py_IS_TYPE(args, &ExceptHookArgsType)) {
14041404
PyErr_SetString(PyExc_TypeError,
14051405
"_thread.excepthook argument type "
14061406
"must be ExceptHookArgs");

Modules/itertoolsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
614614

615615
if (len == LINKCELLS) {
616616
if (next != Py_None) {
617-
if (Py_TYPE(next) != &teedataobject_type)
617+
if (!Py_IS_TYPE(next, &teedataobject_type))
618618
goto err;
619619
assert(tdo->nextlink == NULL);
620620
Py_INCREF(next);

Modules/posixmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6380,8 +6380,7 @@ convert_sched_param(PyObject *param, struct sched_param *res)
63806380
{
63816381
long priority;
63826382

6383-
PyObject *SchedParamType = _posixstate_global->SchedParamType;
6384-
if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) {
6383+
if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) {
63856384
PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
63866385
return 0;
63876386
}

Objects/abstract.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot)
834834

835835
if (Py_TYPE(v)->tp_as_number != NULL)
836836
slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot);
837-
if (Py_TYPE(w) != Py_TYPE(v) &&
837+
if (!Py_IS_TYPE(w, Py_TYPE(v)) &&
838838
Py_TYPE(w)->tp_as_number != NULL) {
839839
slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot);
840840
if (slotw == slotv)
@@ -925,8 +925,7 @@ ternary_op(PyObject *v,
925925
mw = Py_TYPE(w)->tp_as_number;
926926
if (mv != NULL)
927927
slotv = NB_TERNOP(mv, op_slot);
928-
if (Py_TYPE(w) != Py_TYPE(v) &&
929-
mw != NULL) {
928+
if (!Py_IS_TYPE(w, Py_TYPE(v)) && mw != NULL) {
930929
slotw = NB_TERNOP(mw, op_slot);
931930
if (slotw == slotv)
932931
slotw = NULL;

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
657657
PyObject *res;
658658
int checked_reverse_op = 0;
659659

660-
if (Py_TYPE(v) != Py_TYPE(w) &&
660+
if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
661661
PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
662662
(f = Py_TYPE(w)->tp_richcompare) != NULL) {
663663
checked_reverse_op = 1;
@@ -1907,7 +1907,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
19071907
return NULL;
19081908
for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
19091909
while (op == self || op == args || op == res || op == t ||
1910-
(t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1910+
(t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
19111911
op = op->_ob_next;
19121912
if (op == &refchain)
19131913
return res;

Objects/setobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ set_repr(PySetObject *so)
608608
goto done;
609609
listrepr = tmp;
610610

611-
if (Py_TYPE(so) != &PySet_Type)
611+
if (!Py_IS_TYPE(so, &PySet_Type))
612612
result = PyUnicode_FromFormat("%s({%U})",
613613
Py_TYPE(so)->tp_name,
614614
listrepr);

Objects/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
881881
Py_ssize_t oldsize;
882882

883883
v = (PyTupleObject *) *pv;
884-
if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
884+
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
885885
(Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
886886
*pv = 0;
887887
Py_XDECREF(v);

Objects/typeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ mro_invoke(PyTypeObject *type)
18891889
{
18901890
PyObject *mro_result;
18911891
PyObject *new_mro;
1892-
int custom = (Py_TYPE(type) != &PyType_Type);
1892+
const int custom = !Py_IS_TYPE(type, &PyType_Type);
18931893

18941894
if (custom) {
18951895
int unbound;
@@ -6191,7 +6191,7 @@ FUNCNAME(PyObject *self, PyObject *other) \
61916191
PyThreadState *tstate = _PyThreadState_GET(); \
61926192
_Py_static_string(op_id, OPSTR); \
61936193
_Py_static_string(rop_id, ROPSTR); \
6194-
int do_other = Py_TYPE(self) != Py_TYPE(other) && \
6194+
int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \
61956195
Py_TYPE(other)->tp_as_number != NULL && \
61966196
Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
61976197
if (Py_TYPE(self)->tp_as_number != NULL && \
@@ -7901,7 +7901,7 @@ super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
79017901
Py_INCREF(self);
79027902
return self;
79037903
}
7904-
if (Py_TYPE(su) != &PySuper_Type)
7904+
if (!Py_IS_TYPE(su, &PySuper_Type))
79057905
/* If su is an instance of a (strict) subclass of super,
79067906
call its type */
79077907
return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),

Python/errors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ _PyErr_WriteUnraisableDefaultHook(PyObject *args)
13211321
{
13221322
PyThreadState *tstate = _PyThreadState_GET();
13231323

1324-
if (Py_TYPE(args) != &UnraisableHookArgsType) {
1324+
if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) {
13251325
_PyErr_SetString(tstate, PyExc_TypeError,
13261326
"sys.unraisablehook argument type "
13271327
"must be UnraisableHookArgs");

0 commit comments

Comments
 (0)