Skip to content

gh-132097: use a macro for semantically casting function pointers #132406

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 8 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 7 additions & 6 deletions Include/internal/pycore_emscripten_trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ _PyEM_TrampolineCall(PyCFunctionWithKeywords func,
PyObject* kw);

#define _PyCFunction_TrampolineCall(meth, self, args) \
_PyEM_TrampolineCall( \
(*(PyCFunctionWithKeywords)(void(*)(void))(meth)), (self), (args), NULL)
_PyEM_TrampolineCall(*_PyCFunctionWithKeywords_CAST(meth), (self), (args), NULL)

#define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \
_PyEM_TrampolineCall((meth), (self), (args), (kw))

#define descr_set_trampoline_call(set, obj, value, closure) \
((int)_PyEM_TrampolineCall((PyCFunctionWithKeywords)(set), (obj), (value), (PyObject*)(closure)))
#define descr_set_trampoline_call(set, obj, value, closure) \
((int)_PyEM_TrampolineCall(_PyCFunctionWithKeywords_CAST(set), (obj), \
(value), (PyObject*)(closure)))

#define descr_get_trampoline_call(get, obj, closure) \
_PyEM_TrampolineCall((PyCFunctionWithKeywords)(get), (obj), (PyObject*)(closure), NULL)
#define descr_get_trampoline_call(get, obj, closure) \
_PyEM_TrampolineCall(_PyCFunctionWithKeywords_CAST(get), (obj), \
(PyObject*)(closure), NULL)


#else // defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
Expand Down
12 changes: 10 additions & 2 deletions Include/methodobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ 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)
// Other casts are given for semantic conveniences, allowing
// users to see whether a cast to suppress a UB is necessary.
#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 *);
Expand Down
1 change: 1 addition & 0 deletions Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#endif
// Macro to use the more powerful/dangerous C-style cast even in C++.
#define _Py_CAST(type, expr) ((type)(expr))
#define _Py_FUNC_CAST(T, func) _Py_CAST(T, _Py_CAST(void(*)(void), (func)))

// Static inline functions should use _Py_NULL rather than using directly NULL
// to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
Expand Down
2 changes: 1 addition & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self,
wrapperfunc wrapper = descr->d_base->wrapper;

if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
wrapperfunc_kwds wk = (wrapperfunc_kwds)(void(*)(void))wrapper;
wrapperfunc_kwds wk = _Py_FUNC_CAST(wrapperfunc_kwds, wrapper);
return (*wk)(self, args, descr->d_wrapped, kwds);
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ cfunction_call(PyObject *func, PyObject *args, PyObject *kwargs)
PyObject *result;
if (flags & METH_KEYWORDS) {
result = _PyCFunctionWithKeywords_TrampolineCall(
(*(PyCFunctionWithKeywords)(void(*)(void))meth),
*_PyCFunctionWithKeywords_CAST(meth),
self, args, kwargs);
}
else {
Expand Down
6 changes: 4 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10777,7 +10777,8 @@ static pytype_slotdef slotdefs[] = {
"__repr__($self, /)\n--\n\nReturn repr(self)."),
TPSLOT(__hash__, tp_hash, slot_tp_hash, wrap_hashfunc,
"__hash__($self, /)\n--\n\nReturn hash(self)."),
FLSLOT(__call__, tp_call, slot_tp_call, (wrapperfunc)(void(*)(void))wrap_call,
FLSLOT(__call__, tp_call, slot_tp_call,
_Py_FUNC_CAST(wrapperfunc, wrap_call),
"__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.",
PyWrapperFlag_KEYWORDS),
TPSLOT(__str__, tp_str, slot_tp_str, wrap_unaryfunc,
Expand Down Expand Up @@ -10814,7 +10815,8 @@ static pytype_slotdef slotdefs[] = {
TPSLOT(__delete__, tp_descr_set, slot_tp_descr_set,
wrap_descr_delete,
"__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."),
FLSLOT(__init__, tp_init, slot_tp_init, (wrapperfunc)(void(*)(void))wrap_init,
FLSLOT(__init__, tp_init, slot_tp_init,
_Py_FUNC_CAST(wrapperfunc, wrap_init),
"__init__($self, /, *args, **kwargs)\n--\n\n"
"Initialize self. See help(type(self)) for accurate signature.",
PyWrapperFlag_KEYWORDS),
Expand Down
10 changes: 4 additions & 6 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4170,7 +4170,7 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
PyObject *res_o = ((PyCFunctionFast)(void(*)(void))cfunc)(
PyObject *res_o = _PyCFunctionFast_CAST(cfunc)(
PyCFunction_GET_SELF(callable_o),
args_o,
total_args);
Expand Down Expand Up @@ -4202,8 +4202,7 @@ dummy_func(
STAT_INC(CALL, hit);
/* res = func(self, arguments, nargs, kwnames) */
PyCFunctionFastWithKeywords cfunc =
(PyCFunctionFastWithKeywords)(void(*)(void))
PyCFunction_GET_FUNCTION(callable_o);
_PyCFunctionFastWithKeywords_CAST(PyCFunction_GET_FUNCTION(callable_o));

STACKREFS_TO_PYOBJECTS(arguments, total_args, args_o);
if (CONVERSION_FAILED(args_o)) {
Expand Down Expand Up @@ -4371,7 +4370,7 @@ dummy_func(
ERROR_IF(true, error);
}
PyCFunctionFastWithKeywords cfunc =
(PyCFunctionFastWithKeywords)(void(*)(void))meth->ml_meth;
_PyCFunctionFastWithKeywords_CAST(meth->ml_meth);
PyObject *res_o = cfunc(self, (args_o + 1), nargs, NULL);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Expand Down Expand Up @@ -4450,8 +4449,7 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
PyCFunctionFast cfunc =
(PyCFunctionFast)(void(*)(void))meth->ml_meth;
PyCFunctionFast cfunc = _PyCFunctionFast_CAST(meth->ml_meth);
PyObject *res_o = cfunc(self, (args_o + 1), nargs);
STACKREFS_TO_PYOBJECTS_CLEANUP(args_o);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Expand Down
10 changes: 4 additions & 6 deletions Python/executor_cases.c.h

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

10 changes: 4 additions & 6 deletions Python/generated_cases.c.h

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

Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy