aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Include/methodobject.h
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-04-18 12:24:34 +0200
committerGitHub <noreply@github.com>2025-04-18 12:24:34 +0200
commit379352620ce4d77f7248939a4cf211db48fdd241 (patch)
tree49dd21e4e3d0b20aabce878df71e2d4ce04d23fd /Include/methodobject.h
parentf3d877a27abca355f9d05decf3e2ce0874983288 (diff)
downloadcpython-379352620ce4d77f7248939a4cf211db48fdd241.tar.gz
cpython-379352620ce4d77f7248939a4cf211db48fdd241.zip
gh-132097: use a macro for semantically casting function pointers (#132406)
Diffstat (limited to 'Include/methodobject.h')
-rw-r--r--Include/methodobject.h15
1 files changed, 12 insertions, 3 deletions
diff --git a/Include/methodobject.h b/Include/methodobject.h
index cfff05f8033..e6ec6421d1e 100644
--- a/Include/methodobject.h
+++ b/Include/methodobject.h
@@ -33,7 +33,7 @@ typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
typedef PyCFunctionFast _PyCFunctionFast;
typedef PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords;
-// Cast an function to the PyCFunction type to use it with PyMethodDef.
+// Cast a 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
@@ -49,8 +49,17 @@ typedef PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords;
// 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) \
- _Py_CAST(PyCFunction, _Py_CAST(void(*)(void), (func)))
+#define _PyCFunction_CAST(func) \
+ _Py_FUNC_CAST(PyCFunction, func)
+// The macros below are given for semantic convenience, allowing users
+// to see whether a cast to suppress an undefined behavior is necessary.
+// Note: At runtime, the original function signature must be respected.
+#define _PyCFunctionFast_CAST(func) \
+ _Py_FUNC_CAST(PyCFunctionFast, func)
+#define _PyCFunctionWithKeywords_CAST(func) \
+ _Py_FUNC_CAST(PyCFunctionWithKeywords, func)
+#define _PyCFunctionFastWithKeywords_CAST(func) \
+ _Py_FUNC_CAST(PyCFunctionFastWithKeywords, func)
PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);