bpo-39573: Use Py_IS_TYPE() macro to check for types (GH-18809)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Andy Lester 2020-03-06 16:53:17 -06:00 committed by GitHub
parent e59334ebc9
commit 557287075c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 15 additions and 17 deletions

View File

@ -573,7 +573,7 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
PyObject *answer; PyObject *answer;
PyObject* stack[2]; PyObject* stack[2];
if (Py_TYPE(other) != &keyobject_type){ if (!Py_IS_TYPE(other, &keyobject_type)) {
PyErr_Format(PyExc_TypeError, "other argument must be K instance"); PyErr_Format(PyExc_TypeError, "other argument must be K instance");
return NULL; return NULL;
} }

View File

@ -938,7 +938,7 @@ local_getattro(localobject *self, PyObject *name)
if (r == -1) if (r == -1)
return NULL; return NULL;
if (Py_TYPE(self) != &localtype) if (!Py_IS_TYPE(self, &localtype))
/* use generic lookup for subtypes */ /* use generic lookup for subtypes */
return _PyObject_GenericGetAttrWithDict( return _PyObject_GenericGetAttrWithDict(
(PyObject *)self, name, ldict, 0); (PyObject *)self, name, ldict, 0);
@ -1400,7 +1400,7 @@ static PyStructSequence_Desc ExceptHookArgs_desc = {
static PyObject * static PyObject *
thread_excepthook(PyObject *self, PyObject *args) thread_excepthook(PyObject *self, PyObject *args)
{ {
if (Py_TYPE(args) != &ExceptHookArgsType) { if (!Py_IS_TYPE(args, &ExceptHookArgsType)) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"_thread.excepthook argument type " "_thread.excepthook argument type "
"must be ExceptHookArgs"); "must be ExceptHookArgs");

View File

@ -614,7 +614,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
if (len == LINKCELLS) { if (len == LINKCELLS) {
if (next != Py_None) { if (next != Py_None) {
if (Py_TYPE(next) != &teedataobject_type) if (!Py_IS_TYPE(next, &teedataobject_type))
goto err; goto err;
assert(tdo->nextlink == NULL); assert(tdo->nextlink == NULL);
Py_INCREF(next); Py_INCREF(next);

View File

@ -6380,8 +6380,7 @@ convert_sched_param(PyObject *param, struct sched_param *res)
{ {
long priority; long priority;
PyObject *SchedParamType = _posixstate_global->SchedParamType; if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) {
if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) {
PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
return 0; return 0;
} }

View File

@ -834,7 +834,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot)
if (Py_TYPE(v)->tp_as_number != NULL) if (Py_TYPE(v)->tp_as_number != NULL)
slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot); slotv = NB_BINOP(Py_TYPE(v)->tp_as_number, op_slot);
if (Py_TYPE(w) != Py_TYPE(v) && if (!Py_IS_TYPE(w, Py_TYPE(v)) &&
Py_TYPE(w)->tp_as_number != NULL) { Py_TYPE(w)->tp_as_number != NULL) {
slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot); slotw = NB_BINOP(Py_TYPE(w)->tp_as_number, op_slot);
if (slotw == slotv) if (slotw == slotv)
@ -925,8 +925,7 @@ ternary_op(PyObject *v,
mw = Py_TYPE(w)->tp_as_number; mw = Py_TYPE(w)->tp_as_number;
if (mv != NULL) if (mv != NULL)
slotv = NB_TERNOP(mv, op_slot); slotv = NB_TERNOP(mv, op_slot);
if (Py_TYPE(w) != Py_TYPE(v) && if (!Py_IS_TYPE(w, Py_TYPE(v)) && mw != NULL) {
mw != NULL) {
slotw = NB_TERNOP(mw, op_slot); slotw = NB_TERNOP(mw, op_slot);
if (slotw == slotv) if (slotw == slotv)
slotw = NULL; slotw = NULL;

View File

@ -657,7 +657,7 @@ do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
PyObject *res; PyObject *res;
int checked_reverse_op = 0; int checked_reverse_op = 0;
if (Py_TYPE(v) != Py_TYPE(w) && if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) && PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
(f = Py_TYPE(w)->tp_richcompare) != NULL) { (f = Py_TYPE(w)->tp_richcompare) != NULL) {
checked_reverse_op = 1; checked_reverse_op = 1;
@ -1907,7 +1907,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
return NULL; return NULL;
for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
while (op == self || op == args || op == res || op == t || while (op == self || op == args || op == res || op == t ||
(t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
op = op->_ob_next; op = op->_ob_next;
if (op == &refchain) if (op == &refchain)
return res; return res;

View File

@ -608,7 +608,7 @@ set_repr(PySetObject *so)
goto done; goto done;
listrepr = tmp; listrepr = tmp;
if (Py_TYPE(so) != &PySet_Type) if (!Py_IS_TYPE(so, &PySet_Type))
result = PyUnicode_FromFormat("%s({%U})", result = PyUnicode_FromFormat("%s({%U})",
Py_TYPE(so)->tp_name, Py_TYPE(so)->tp_name,
listrepr); listrepr);

View File

@ -881,7 +881,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
Py_ssize_t oldsize; Py_ssize_t oldsize;
v = (PyTupleObject *) *pv; v = (PyTupleObject *) *pv;
if (v == NULL || Py_TYPE(v) != &PyTuple_Type || if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
(Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) { (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
*pv = 0; *pv = 0;
Py_XDECREF(v); Py_XDECREF(v);

View File

@ -1889,7 +1889,7 @@ mro_invoke(PyTypeObject *type)
{ {
PyObject *mro_result; PyObject *mro_result;
PyObject *new_mro; PyObject *new_mro;
int custom = (Py_TYPE(type) != &PyType_Type); const int custom = !Py_IS_TYPE(type, &PyType_Type);
if (custom) { if (custom) {
int unbound; int unbound;
@ -6191,7 +6191,7 @@ FUNCNAME(PyObject *self, PyObject *other) \
PyThreadState *tstate = _PyThreadState_GET(); \ PyThreadState *tstate = _PyThreadState_GET(); \
_Py_static_string(op_id, OPSTR); \ _Py_static_string(op_id, OPSTR); \
_Py_static_string(rop_id, ROPSTR); \ _Py_static_string(rop_id, ROPSTR); \
int do_other = Py_TYPE(self) != Py_TYPE(other) && \ int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \
Py_TYPE(other)->tp_as_number != NULL && \ Py_TYPE(other)->tp_as_number != NULL && \
Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
if (Py_TYPE(self)->tp_as_number != NULL && \ if (Py_TYPE(self)->tp_as_number != NULL && \
@ -7901,7 +7901,7 @@ super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Py_INCREF(self); Py_INCREF(self);
return self; return self;
} }
if (Py_TYPE(su) != &PySuper_Type) if (!Py_IS_TYPE(su, &PySuper_Type))
/* If su is an instance of a (strict) subclass of super, /* If su is an instance of a (strict) subclass of super,
call its type */ call its type */
return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),

View File

@ -1321,7 +1321,7 @@ _PyErr_WriteUnraisableDefaultHook(PyObject *args)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
if (Py_TYPE(args) != &UnraisableHookArgsType) { if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) {
_PyErr_SetString(tstate, PyExc_TypeError, _PyErr_SetString(tstate, PyExc_TypeError,
"sys.unraisablehook argument type " "sys.unraisablehook argument type "
"must be UnraisableHookArgs"); "must be UnraisableHookArgs");