Skip to content

gh-114626: add PyCFunctionFast and PyCFunctionFastWithKeywords #114627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Doc/c-api/structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,26 @@ Implementing functions and methods
PyObject *kwargs);


.. c:type:: _PyCFunctionFast
.. c:type:: PyCFunctionFast

Type of the functions used to implement Python callables in C
with signature :c:macro:`METH_FASTCALL`.
The function signature is::

PyObject *_PyCFunctionFast(PyObject *self,
PyObject *const *args,
Py_ssize_t nargs);
PyObject *PyCFunctionFast(PyObject *self,
PyObject *const *args,
Py_ssize_t nargs);

.. c:type:: _PyCFunctionFastWithKeywords
.. c:type:: PyCFunctionFastWithKeywords

Type of the functions used to implement Python callables in C
with signature :ref:`METH_FASTCALL | METH_KEYWORDS <METH_FASTCALL-METH_KEYWORDS>`.
The function signature is::

PyObject *_PyCFunctionFastWithKeywords(PyObject *self,
PyObject *const *args,
Py_ssize_t nargs,
PyObject *kwnames);
PyObject *PyCFunctionFastWithKeywords(PyObject *self,
PyObject *const *args,
Py_ssize_t nargs,
PyObject *kwnames);

.. c:type:: PyCMethod

Expand Down Expand Up @@ -290,7 +290,7 @@ There are these calling conventions:
.. c:macro:: METH_FASTCALL

Fast calling convention supporting only positional arguments.
The methods have the type :c:type:`_PyCFunctionFast`.
The methods have the type :c:type:`PyCFunctionFast`.
The first parameter is *self*, the second parameter is a C array
of :c:expr:`PyObject*` values indicating the arguments and the third
parameter is the number of arguments (the length of the array).
Expand All @@ -306,7 +306,7 @@ There are these calling conventions:

:c:expr:`METH_FASTCALL | METH_KEYWORDS`
Extension of :c:macro:`METH_FASTCALL` supporting also keyword arguments,
with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
with methods of type :c:type:`PyCFunctionFastWithKeywords`.
Keyword arguments are passed the same way as in the
:ref:`vectorcall protocol <vectorcall>`:
there is an additional fourth :c:expr:`PyObject*` parameter
Expand Down
2 changes: 2 additions & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Include/methodobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ PyAPI_DATA(PyTypeObject) PyCFunction_Type;
#define PyCFunction_Check(op) PyObject_TypeCheck((op), &PyCFunction_Type)

typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
typedef PyObject *(*PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
PyObject *);
typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *,
PyObject *const *, Py_ssize_t,
PyObject *);
typedef PyObject *(*PyCFunctionFastWithKeywords) (PyObject *,
PyObject *const *, Py_ssize_t,
PyObject *);
typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
size_t, PyObject *);

// For backwards compatibility. `METH_FASTCALL` was added to the stable API in
// 3.10 alongside `_PyCFunctionFastWithKeywords` and `_PyCFunctionFast`.
// Note that the underscore-prefixed names were documented in public docs;
// people may be using them.
typedef PyCFunctionFast _PyCFunctionFast;
typedef PyCFunctionWithKeywords _PyCFunctionWithKeywords;

// Cast an function to the PyCFunction type to use it with PyMethodDef.
//
// This macro can be used to prevent compiler warnings if the first parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``PyCFunctionFast`` and ``PyCFunctionFastWithKeywords`` typedefs (identical to the existing ``_PyCFunctionFast`` and ``_PyCFunctionFastWithKeywords`` typedefs, just without a leading ``_`` prefix).
7 changes: 7 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2489,3 +2489,10 @@
added = '3.13'
[function.PyList_GetItemRef]
added = '3.13'
[typedef.PyCFunctionFast]
added = '3.13'
# "abi-only" since 3.10. (Callback type names aren't used in C code,
# but this function signature was expected with METH_FASTCALL.)
[typedef.PyCFunctionFastWithKeywords]
added = '3.13'
# "abi-only" since 3.10. (Same story as PyCFunctionFast.)
4 changes: 2 additions & 2 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ method_vectorcall_FASTCALL(
if (method_check_args(func, args, nargs, kwnames)) {
return NULL;
}
_PyCFunctionFast meth = (_PyCFunctionFast)
PyCFunctionFast meth = (PyCFunctionFast)
method_enter_call(tstate, func);
if (meth == NULL) {
return NULL;
Expand All @@ -412,7 +412,7 @@ method_vectorcall_FASTCALL_KEYWORDS(
if (method_check_args(func, args, nargs, NULL)) {
return NULL;
}
_PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
PyCFunctionFastWithKeywords meth = (PyCFunctionFastWithKeywords)
method_enter_call(tstate, func);
if (meth == NULL) {
return NULL;
Expand Down
5 changes: 2 additions & 3 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ cfunction_vectorcall_FASTCALL(
return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
_PyCFunctionFast meth = (_PyCFunctionFast)
PyCFunctionFast meth = (PyCFunctionFast)
cfunction_enter_call(tstate, func);
if (meth == NULL) {
return NULL;
Expand All @@ -433,7 +433,7 @@ cfunction_vectorcall_FASTCALL_KEYWORDS(
{
PyThreadState *tstate = _PyThreadState_GET();
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
_PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords)
PyCFunctionFastWithKeywords meth = (PyCFunctionFastWithKeywords)
cfunction_enter_call(tstate, func);
if (meth == NULL) {
return NULL;
Expand Down Expand Up @@ -552,4 +552,3 @@ cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
}
return _Py_CheckFunctionResult(tstate, func, result, NULL);
}

14 changes: 7 additions & 7 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3371,7 +3371,7 @@ dummy_func(
STAT_INC(CALL, hit);
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
/* res = func(self, args, nargs) */
res = ((_PyCFunctionFast)(void(*)(void))cfunc)(
res = ((PyCFunctionFast)(void(*)(void))cfunc)(
PyCFunction_GET_SELF(callable),
args,
total_args);
Expand Down Expand Up @@ -3402,8 +3402,8 @@ dummy_func(
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != (METH_FASTCALL | METH_KEYWORDS));
STAT_INC(CALL, hit);
/* res = func(self, args, nargs, kwnames) */
_PyCFunctionFastWithKeywords cfunc =
(_PyCFunctionFastWithKeywords)(void(*)(void))
PyCFunctionFastWithKeywords cfunc =
(PyCFunctionFastWithKeywords)(void(*)(void))
PyCFunction_GET_FUNCTION(callable);
res = cfunc(PyCFunction_GET_SELF(callable), args, total_args, NULL);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Expand Down Expand Up @@ -3534,8 +3534,8 @@ dummy_func(
DEOPT_IF(!Py_IS_TYPE(self, d_type));
STAT_INC(CALL, hit);
int nargs = total_args - 1;
_PyCFunctionFastWithKeywords cfunc =
(_PyCFunctionFastWithKeywords)(void(*)(void))meth->ml_meth;
PyCFunctionFastWithKeywords cfunc =
(PyCFunctionFastWithKeywords)(void(*)(void))meth->ml_meth;
res = cfunc(self, args + 1, nargs, NULL);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));

Expand Down Expand Up @@ -3592,8 +3592,8 @@ dummy_func(
PyObject *self = args[0];
DEOPT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
STAT_INC(CALL, hit);
_PyCFunctionFast cfunc =
(_PyCFunctionFast)(void(*)(void))meth->ml_meth;
PyCFunctionFast cfunc =
(PyCFunctionFast)(void(*)(void))meth->ml_meth;
int nargs = total_args - 1;
res = cfunc(self, args + 1, nargs);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Expand Down
14 changes: 7 additions & 7 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.