bpo-38787: Add PyCFunction_CheckExact() macro for exact type checks (GH-20024)

… now that we allow subtypes of PyCFunction.

Also add PyCMethod_CheckExact() and PyCMethod_Check() for checks against the PyCMethod subtype.
This commit is contained in:
scoder 2020-05-12 16:12:41 +02:00 committed by GitHub
parent 5650e76f63
commit 4c9ea093cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 4 deletions

View File

@ -4,6 +4,9 @@
PyAPI_DATA(PyTypeObject) PyCMethod_Type; PyAPI_DATA(PyTypeObject) PyCMethod_Type;
#define PyCMethod_CheckExact(op) Py_IS_TYPE(op, &PyCMethod_Type)
#define PyCMethod_Check(op) PyObject_TypeCheck(op, &PyCMethod_Type)
/* Macros for direct access to these values. Type checks are *not* /* Macros for direct access to these values. Type checks are *not*
done, so use with care. */ done, so use with care. */
#define PyCFunction_GET_FUNCTION(func) \ #define PyCFunction_GET_FUNCTION(func) \

View File

@ -13,7 +13,8 @@ extern "C" {
PyAPI_DATA(PyTypeObject) PyCFunction_Type; PyAPI_DATA(PyTypeObject) PyCFunction_Type;
#define PyCFunction_Check(op) (Py_IS_TYPE(op, &PyCFunction_Type) || (PyType_IsSubtype(Py_TYPE(op), &PyCFunction_Type))) #define PyCFunction_CheckExact(op) Py_IS_TYPE(op, &PyCFunction_Type)
#define PyCFunction_Check(op) PyObject_TypeCheck(op, &PyCFunction_Type)
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *); typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t); typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);

View File

@ -0,0 +1,2 @@
Add PyCFunction_CheckExact() macro for exact type checks now that we allow subtypes of PyCFunction,
as well as PyCMethod_CheckExact() and PyCMethod_Check() for the new PyCMethod subtype.

View File

@ -900,7 +900,7 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
Py_DECREF(result); Py_DECREF(result);
if (op_slot == NB_SLOT(nb_rshift) && if (op_slot == NB_SLOT(nb_rshift) &&
PyCFunction_Check(v) && PyCFunction_CheckExact(v) &&
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0) strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
{ {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,

View File

@ -5054,7 +5054,7 @@ trace_call_function(PyThreadState *tstate,
PyObject *kwnames) PyObject *kwnames)
{ {
PyObject *x; PyObject *x;
if (PyCFunction_Check(func)) { if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames)); C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
return x; return x;
} }
@ -5115,7 +5115,7 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject
{ {
PyObject *result; PyObject *result;
if (PyCFunction_Check(func)) { if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
C_TRACE(result, PyObject_Call(func, callargs, kwdict)); C_TRACE(result, PyObject_Call(func, callargs, kwdict));
return result; return result;
} }