From e0f6c09da4cadaa4722492bb9952c332dfb9afcc Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Wed, 10 Jul 2019 13:17:07 +0200 Subject: [PATCH 1/3] bpo-37151: remove special case for PyCFunction from PyObject_Call --- Objects/call.c | 62 ------------------------------------------ Objects/methodobject.c | 33 ++++++++++++++++++++++ Tools/gdb/libpython.py | 2 +- 3 files changed, 34 insertions(+), 63 deletions(-) diff --git a/Objects/call.c b/Objects/call.c index df90595d6c6a530..f0e3e58bb0dc3e0 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -5,9 +5,6 @@ #include "frameobject.h" -static PyObject * -cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs); - static PyObject *const * _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs, PyObject **p_kwnames); @@ -236,11 +233,6 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) if (_PyVectorcall_Function(callable) != NULL) { return PyVectorcall_Call(callable, args, kwargs); } - else if (PyCFunction_Check(callable)) { - /* This must be a METH_VARARGS function, otherwise we would be - * in the previous case */ - return cfunction_call_varargs(callable, args, kwargs); - } else { call = callable->ob_type->tp_call; if (call == NULL) { @@ -364,60 +356,6 @@ _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack, } -/* --- PyCFunction call functions --------------------------------- */ - -static PyObject * -cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs) -{ - assert(!PyErr_Occurred()); - assert(kwargs == NULL || PyDict_Check(kwargs)); - - PyCFunction meth = PyCFunction_GET_FUNCTION(func); - PyObject *self = PyCFunction_GET_SELF(func); - PyObject *result; - - assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS); - if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) { - if (Py_EnterRecursiveCall(" while calling a Python object")) { - return NULL; - } - - result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs); - - Py_LeaveRecursiveCall(); - } - else { - if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { - PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", - ((PyCFunctionObject*)func)->m_ml->ml_name); - return NULL; - } - - if (Py_EnterRecursiveCall(" while calling a Python object")) { - return NULL; - } - - result = (*meth)(self, args); - - Py_LeaveRecursiveCall(); - } - - return _Py_CheckFunctionResult(func, result, NULL); -} - - -PyObject * -PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs) -{ - /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer - * is NULL. This is intentional, since vectorcall would be slower. */ - if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) { - return cfunction_call_varargs(func, args, kwargs); - } - return PyVectorcall_Call(func, args, kwargs); -} - - /* --- More complex call functions -------------------------------- */ /* External interface to call any callable object. diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 3494f11d80fe759..1dd72d025b99a0e 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -482,3 +482,36 @@ cfunction_vectorcall_O( Py_LeaveRecursiveCall(); return result; } + + +PyObject * +PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs) +{ + assert(!PyErr_Occurred()); + assert(kwargs == NULL || PyDict_Check(kwargs)); + + int flags = PyCFunction_GET_FLAGS(func); + if (!(flags & METH_VARARGS)) { + /* If this is not a METH_VARARGS function, delegate to vectorcall */ + return PyVectorcall_Call(func, args, kwargs); + } + + /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer + * is NULL. This is intentional, since vectorcall would be slower. */ + PyCFunction meth = PyCFunction_GET_FUNCTION(func); + PyObject *self = PyCFunction_GET_SELF(func); + + PyObject *result; + if (flags & METH_KEYWORDS) { + result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs); + } + else { + if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { + PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", + ((PyCFunctionObject*)func)->m_ml->ml_name); + return NULL; + } + result = meth(self, args); + } + return _Py_CheckFunctionResult(func, result, NULL); +} diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py index e40f79b5c3c7d33..b12741bf8783fee 100755 --- a/Tools/gdb/libpython.py +++ b/Tools/gdb/libpython.py @@ -1564,7 +1564,7 @@ def is_other_python_frame(self): return False if (caller.startswith('cfunction_vectorcall_') or - caller == 'cfunction_call_varargs'): + caller == 'PyCFunction_Call'): arg_name = 'func' # Within that frame: # "func" is the local containing the PyObject* of the From 37152cffbdf9b9b94fa7fbe39a34c8ec7c1d4222 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Tue, 16 Jul 2019 11:02:15 +0200 Subject: [PATCH 2/3] bpo-37151: make PyCFunction_Call an alias of PyObject_Call --- .../next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst | 1 + Objects/call.c | 7 +++++++ Objects/methodobject.c | 8 +++++--- Python/ceval.c | 2 +- Tools/gdb/libpython.py | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst diff --git a/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst b/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst new file mode 100644 index 000000000000000..c7ff74330599d60 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst @@ -0,0 +1 @@ +``PyCFunction_Call`` is now an alias of :c:func:`PyObject_Call`. diff --git a/Objects/call.c b/Objects/call.c index f0e3e58bb0dc3e0..2b4dade0ceca797 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -253,6 +253,13 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) } +PyObject * +PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs) +{ + return PyObject_Call(callable, args, kwargs); +} + + /* --- PyFunction call functions ---------------------------------- */ static PyObject* _Py_HOT_FUNCTION diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 1dd72d025b99a0e..0c0f71f3297ad92 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -28,6 +28,8 @@ static PyObject * cfunction_vectorcall_NOARGS( PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); static PyObject * cfunction_vectorcall_O( PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); +static PyObject * cfunction_call( + PyObject *func, PyObject *args, PyObject *kwargs); PyObject * @@ -312,7 +314,7 @@ PyTypeObject PyCFunction_Type = { 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ (hashfunc)meth_hash, /* tp_hash */ - PyCFunction_Call, /* tp_call */ + cfunction_call, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ @@ -484,8 +486,8 @@ cfunction_vectorcall_O( } -PyObject * -PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs) +static PyObject * +cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs) { assert(!PyErr_Occurred()); assert(kwargs == NULL || PyDict_Check(kwargs)); diff --git a/Python/ceval.c b/Python/ceval.c index fdd299561c6156d..01b16dede840c00 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4997,7 +4997,7 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject PyObject *result; if (PyCFunction_Check(func)) { - C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); + C_TRACE(result, PyObject_Call(func, callargs, kwdict)); return result; } else if (Py_TYPE(func) == &PyMethodDescr_Type) { diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py index b12741bf8783fee..e28513db261ba36 100755 --- a/Tools/gdb/libpython.py +++ b/Tools/gdb/libpython.py @@ -1564,7 +1564,7 @@ def is_other_python_frame(self): return False if (caller.startswith('cfunction_vectorcall_') or - caller == 'PyCFunction_Call'): + caller == 'cfunction_call'): arg_name = 'func' # Within that frame: # "func" is the local containing the PyObject* of the From 5b7f9fc67ba8727ecc41322500e157c03c890e21 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Fri, 19 Jul 2019 11:48:40 +0200 Subject: [PATCH 3/3] Deprecate PyCFunction_Call --- Include/methodobject.h | 2 +- Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/methodobject.h b/Include/methodobject.h index ab66b03f7a97dbd..0a3b9a3394d0a4c 100644 --- a/Include/methodobject.h +++ b/Include/methodobject.h @@ -39,7 +39,7 @@ PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *); #define PyCFunction_GET_FLAGS(func) \ (((PyCFunctionObject *)func) -> m_ml -> ml_flags) #endif -PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *); +Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *); struct PyMethodDef { const char *ml_name; /* The name of the built-in function/method */ diff --git a/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst b/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst index c7ff74330599d60..0841e570f6c913f 100644 --- a/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst +++ b/Misc/NEWS.d/next/C API/2019-07-16-11-02-00.bpo-37151.YKfuNA.rst @@ -1 +1 @@ -``PyCFunction_Call`` is now an alias of :c:func:`PyObject_Call`. +``PyCFunction_Call`` is now a deprecated alias of :c:func:`PyObject_Call`.