Commit 7a6873cd authored by Jeroen Demeyer's avatar Jeroen Demeyer Committed by Petr Viktorin

bpo-37151: remove special case for PyCFunction from PyObject_Call (GH-14684)

bpo-37151: remove special case for PyCFunction from PyObject_Call

Alse, make the undocumented function PyCFunction_Call an alias
of PyObject_Call and deprecate it.
parent 2d8d597b
...@@ -39,7 +39,7 @@ PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *); ...@@ -39,7 +39,7 @@ PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
#define PyCFunction_GET_FLAGS(func) \ #define PyCFunction_GET_FLAGS(func) \
(((PyCFunctionObject *)func) -> m_ml -> ml_flags) (((PyCFunctionObject *)func) -> m_ml -> ml_flags)
#endif #endif
PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *); Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
struct PyMethodDef { struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */ const char *ml_name; /* The name of the built-in function/method */
......
``PyCFunction_Call`` is now a deprecated alias of :c:func:`PyObject_Call`.
...@@ -5,9 +5,6 @@ ...@@ -5,9 +5,6 @@
#include "frameobject.h" #include "frameobject.h"
static PyObject *
cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs);
static PyObject *const * static PyObject *const *
_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs, _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
PyObject **p_kwnames); PyObject **p_kwnames);
...@@ -236,11 +233,6 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) ...@@ -236,11 +233,6 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
if (_PyVectorcall_Function(callable) != NULL) { if (_PyVectorcall_Function(callable) != NULL) {
return PyVectorcall_Call(callable, args, kwargs); 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 { else {
call = callable->ob_type->tp_call; call = callable->ob_type->tp_call;
if (call == NULL) { if (call == NULL) {
...@@ -261,6 +253,13 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) ...@@ -261,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 ---------------------------------- */ /* --- PyFunction call functions ---------------------------------- */
static PyObject* _Py_HOT_FUNCTION static PyObject* _Py_HOT_FUNCTION
...@@ -363,60 +362,6 @@ _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack, ...@@ -363,60 +362,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 -------------------------------- */ /* --- More complex call functions -------------------------------- */
/* External interface to call any callable object. /* External interface to call any callable object.
......
...@@ -19,6 +19,8 @@ static PyObject * cfunction_vectorcall_NOARGS( ...@@ -19,6 +19,8 @@ static PyObject * cfunction_vectorcall_NOARGS(
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
static PyObject * cfunction_vectorcall_O( static PyObject * cfunction_vectorcall_O(
PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
static PyObject * cfunction_call(
PyObject *func, PyObject *args, PyObject *kwargs);
PyObject * PyObject *
...@@ -289,7 +291,7 @@ PyTypeObject PyCFunction_Type = { ...@@ -289,7 +291,7 @@ PyTypeObject PyCFunction_Type = {
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)meth_hash, /* tp_hash */ (hashfunc)meth_hash, /* tp_hash */
PyCFunction_Call, /* tp_call */ cfunction_call, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
...@@ -441,3 +443,36 @@ cfunction_vectorcall_O( ...@@ -441,3 +443,36 @@ cfunction_vectorcall_O(
Py_LeaveRecursiveCall(); Py_LeaveRecursiveCall();
return result; return result;
} }
static PyObject *
cfunction_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);
}
...@@ -5001,7 +5001,7 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject ...@@ -5001,7 +5001,7 @@ do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject
PyObject *result; PyObject *result;
if (PyCFunction_Check(func)) { if (PyCFunction_Check(func)) {
C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); C_TRACE(result, PyObject_Call(func, callargs, kwdict));
return result; return result;
} }
else if (Py_TYPE(func) == &PyMethodDescr_Type) { else if (Py_TYPE(func) == &PyMethodDescr_Type) {
......
...@@ -1564,7 +1564,7 @@ class Frame(object): ...@@ -1564,7 +1564,7 @@ class Frame(object):
return False return False
if (caller.startswith('cfunction_vectorcall_') or if (caller.startswith('cfunction_vectorcall_') or
caller == 'cfunction_call_varargs'): caller == 'cfunction_call'):
arg_name = 'func' arg_name = 'func'
# Within that frame: # Within that frame:
# "func" is the local containing the PyObject* of the # "func" is the local containing the PyObject* of the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment