summaryrefslogtreecommitdiffstats
path: root/Include/methodobject.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-03-31 08:02:34 (GMT)
committerGitHub <noreply@github.com>2022-03-31 08:02:34 (GMT)
commitf0bc69485677ae8973685866ada0982976d3878f (patch)
tree0d64ad8e39f8e7ad7835d87416b8f6bf9c3d4460 /Include/methodobject.h
parentc14d7e4b816134b8e93ece4066a86d229631ce96 (diff)
downloadcpython-f0bc69485677ae8973685866ada0982976d3878f.zip
cpython-f0bc69485677ae8973685866ada0982976d3878f.tar.gz
cpython-f0bc69485677ae8973685866ada0982976d3878f.tar.bz2
bpo-47164: Add _PyCFunction_CAST() macro (GH-32192)
Use the macro in C files of the Python/ directory.
Diffstat (limited to 'Include/methodobject.h')
-rw-r--r--Include/methodobject.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/Include/methodobject.h b/Include/methodobject.h
index 5d2e06c..959e775 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -26,6 +26,24 @@ typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *,
typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
size_t, PyObject *);
+// 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
+// uses a different pointer type than PyObject* (ex: METH_VARARGS and METH_O
+// calling conventions).
+//
+// The macro can also be used for METH_FASTCALL and METH_VARARGS|METH_KEYWORDS
+// calling conventions to avoid compiler warnings because the function has more
+// than 2 parameters. The macro first casts the function to the
+// "void func(void)" type to prevent compiler warnings.
+//
+// If a function is declared with the METH_NOARGS calling convention, it must
+// have 2 parameters. Since the second parameter is unused, Py_UNUSED() can be
+// used to prevent a compiler warning. If the function has a single parameter,
+// it triggers an undefined behavior when Python calls it with 2 parameters
+// (bpo-33012).
+#define _PyCFunction_CAST(func) ((PyCFunction)(void(*)(void))(func))
+
PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);