Skip to content

gh-111178: remove redundant casts for functions with correct signatures #131673

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 24 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
70861ec
remove un-necessary casts to PyCFunction
picnixz Feb 6, 2025
d356465
remove un-necessary casts to `getter`
picnixz Feb 6, 2025
f641773
remove un-necessary casts to `setter`
picnixz Feb 6, 2025
a197210
remove un-necessary casts for `initproc`
picnixz Mar 20, 2025
5a39cb7
add TODO note to remove casts to `destructor`
picnixz Mar 20, 2025
222b662
un-necessary cast for module free functions
picnixz Mar 20, 2025
ad434c0
fixup: None and NoneType
picnixz Mar 20, 2025
2fea60f
fixup: _PyDictViewObject
picnixz Mar 20, 2025
47bc0ee
fixup: bool_invert
picnixz Mar 20, 2025
a894c4c
fixup: PyUnicodeObject methods
picnixz Mar 20, 2025
1c17bfc
fixup: rangeobject
picnixz Mar 20, 2025
e415739
fixup: zoneinfo_init_subclass
picnixz Mar 20, 2025
86bc47c
fixup: ga_iter
picnixz Mar 24, 2025
5d98a84
fixup: hamt_baseiter_tp_iternext
picnixz Mar 24, 2025
1053101
remove dead code
picnixz Mar 24, 2025
5e68e29
Merge branch 'main' into fix/ubsan/casts-111178
picnixz Mar 24, 2025
937aa63
Merge branch 'main' into fix/ubsan/casts-111178
picnixz Mar 24, 2025
9c780ad
Update Include/cpython/object.h
picnixz Mar 24, 2025
3124512
Merge remote-tracking branch 'upstream/main' into fix/ubsan/casts-111178
picnixz Mar 30, 2025
92d648e
fixup comments
picnixz Mar 30, 2025
25c2a3f
Merge branch 'main' into fix/ubsan/casts-111178
picnixz Mar 31, 2025
4bf2a1e
Update Modules/faulthandler.c
picnixz Apr 1, 2025
db5dae1
Update Include/cpython/object.h
picnixz Apr 1, 2025
6cfed6c
Update Python/ceval.c
picnixz Apr 1, 2025
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
4 changes: 2 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ static PyType_Slot Task_slots[] = {
{Py_tp_iter, future_new_iter},
{Py_tp_methods, TaskType_methods},
{Py_tp_getset, TaskType_getsetlist},
{Py_tp_init, (initproc)_asyncio_Task___init__},
{Py_tp_init, _asyncio_Task___init__},
{Py_tp_new, PyType_GenericNew},
{Py_tp_finalize, TaskObj_finalize},

Expand Down Expand Up @@ -4396,7 +4396,7 @@ static struct PyModuleDef _asynciomodule = {
.m_slots = module_slots,
.m_traverse = module_traverse,
.m_clear = module_clear,
.m_free = (freefunc)module_free,
.m_free = module_free,
};

PyMODINIT_FUNC
Expand Down
2 changes: 1 addition & 1 deletion Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@ static PyMethodDef PyCursesWindow_methods[] = {
_CURSES_WINDOW_SETSCRREG_METHODDEF
{"standend", PyCursesWindow_wstandend, METH_NOARGS},
{"standout", PyCursesWindow_wstandout, METH_NOARGS},
{"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
{"subpad", _curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
_CURSES_WINDOW_SUBWIN_METHODDEF
{"syncdown", PyCursesWindow_wsyncdown, METH_NOARGS},
#ifdef HAVE_CURSES_SYNCOK
Expand Down
12 changes: 6 additions & 6 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1634,19 +1634,19 @@ _functools__lru_cache_wrapper_cache_clear_impl(PyObject *self)
}

static PyObject *
lru_cache_reduce(PyObject *self, PyObject *unused)
lru_cache_reduce(PyObject *self, PyObject *Py_UNUSED(dummy))
{
return PyObject_GetAttrString(self, "__qualname__");
}

static PyObject *
lru_cache_copy(PyObject *self, PyObject *unused)
lru_cache_copy(PyObject *self, PyObject *Py_UNUSED(args))
{
return Py_NewRef(self);
}

static PyObject *
lru_cache_deepcopy(PyObject *self, PyObject *unused)
lru_cache_deepcopy(PyObject *self, PyObject *Py_UNUSED(args))
{
return Py_NewRef(self);
}
Expand Down Expand Up @@ -1693,9 +1693,9 @@ cache_info_type: namedtuple class with the fields:\n\
static PyMethodDef lru_cache_methods[] = {
_FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_INFO_METHODDEF
_FUNCTOOLS__LRU_CACHE_WRAPPER_CACHE_CLEAR_METHODDEF
{"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS},
{"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS},
{"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS},
{"__reduce__", lru_cache_reduce, METH_NOARGS},
{"__copy__", lru_cache_copy, METH_VARARGS},
{"__deepcopy__", lru_cache_deepcopy, METH_VARARGS},
{NULL}
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_interpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3595,7 +3595,7 @@ static struct PyModuleDef moduledef = {
.m_slots = module_slots,
.m_traverse = module_traverse,
.m_clear = module_clear,
.m_free = (freefunc)module_free,
.m_free = module_free,
};

PyMODINIT_FUNC
Expand Down
2 changes: 0 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3386,8 +3386,6 @@ static PyMemberDef textiowrapper_members[] = {
static PyGetSetDef textiowrapper_getset[] = {
_IO_TEXTIOWRAPPER_NAME_GETSETDEF
_IO_TEXTIOWRAPPER_CLOSED_GETSETDEF
/* {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL},
*/
_IO_TEXTIOWRAPPER_NEWLINES_GETSETDEF
_IO_TEXTIOWRAPPER_ERRORS_GETSETDEF
_IO_TEXTIOWRAPPER__CHUNK_SIZE_GETSETDEF
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ static PyMethodDef winconsoleio_methods[] = {
_IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
_IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
_IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
{"_isatty_open_only", (PyCFunction)_io__WindowsConsoleIO_isatty, METH_NOARGS},
{"_isatty_open_only", _io__WindowsConsoleIO_isatty, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ PyDoc_STRVAR(_lzma__encode_filter_properties__doc__,
"The result does not include the filter ID itself, only the options.");

#define _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF \
{"_encode_filter_properties", (PyCFunction)_lzma__encode_filter_properties, METH_O, _lzma__encode_filter_properties__doc__},
{"_encode_filter_properties", _lzma__encode_filter_properties, METH_O, _lzma__encode_filter_properties__doc__},

static PyObject *
_lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter);
Expand Down
48 changes: 24 additions & 24 deletions Modules/_testcapi/docstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,42 @@ test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))

static PyMethodDef test_methods[] = {
{"docstring_empty",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_empty},
{"docstring_no_signature",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_no_signature},
{"docstring_no_signature_noargs",
(PyCFunction)test_with_docstring, METH_NOARGS,
test_with_docstring, METH_NOARGS,
docstring_no_signature},
{"docstring_no_signature_o",
(PyCFunction)test_with_docstring, METH_O,
test_with_docstring, METH_O,
docstring_no_signature},
{"docstring_with_invalid_signature",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_invalid_signature},
{"docstring_with_invalid_signature2",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_invalid_signature2},
{"docstring_with_signature",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_signature},
{"docstring_with_signature_and_extra_newlines",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_signature_and_extra_newlines},
{"docstring_with_signature_but_no_doc",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_signature_but_no_doc},
{"docstring_with_signature_with_defaults",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
docstring_with_signature_with_defaults},
{"no_docstring",
(PyCFunction)test_with_docstring, METH_VARARGS},
test_with_docstring, METH_VARARGS},
{"test_with_docstring",
test_with_docstring, METH_VARARGS,
PyDoc_STR("This is a pretty normal docstring.")},
{"func_with_unrepresentable_signature",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
PyDoc_STR(
"func_with_unrepresentable_signature($module, /, a, b=<x>)\n"
"--\n\n"
Expand All @@ -112,28 +112,28 @@ static PyMethodDef test_methods[] = {

static PyMethodDef DocStringNoSignatureTest_methods[] = {
{"meth_noargs",
(PyCFunction)test_with_docstring, METH_NOARGS,
test_with_docstring, METH_NOARGS,
docstring_no_signature},
{"meth_o",
(PyCFunction)test_with_docstring, METH_O,
test_with_docstring, METH_O,
docstring_no_signature},
{"meth_noargs_class",
(PyCFunction)test_with_docstring, METH_NOARGS|METH_CLASS,
test_with_docstring, METH_NOARGS|METH_CLASS,
docstring_no_signature},
{"meth_o_class",
(PyCFunction)test_with_docstring, METH_O|METH_CLASS,
test_with_docstring, METH_O|METH_CLASS,
docstring_no_signature},
{"meth_noargs_static",
(PyCFunction)test_with_docstring, METH_NOARGS|METH_STATIC,
test_with_docstring, METH_NOARGS|METH_STATIC,
docstring_no_signature},
{"meth_o_static",
(PyCFunction)test_with_docstring, METH_O|METH_STATIC,
test_with_docstring, METH_O|METH_STATIC,
docstring_no_signature},
{"meth_noargs_coexist",
(PyCFunction)test_with_docstring, METH_NOARGS|METH_COEXIST,
test_with_docstring, METH_NOARGS|METH_COEXIST,
docstring_no_signature},
{"meth_o_coexist",
(PyCFunction)test_with_docstring, METH_O|METH_COEXIST,
test_with_docstring, METH_O|METH_COEXIST,
docstring_no_signature},
{NULL},
};
Expand All @@ -149,28 +149,28 @@ static PyTypeObject DocStringNoSignatureTest = {

static PyMethodDef DocStringUnrepresentableSignatureTest_methods[] = {
{"meth",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
PyDoc_STR(
"meth($self, /, a, b=<x>)\n"
"--\n\n"
"This docstring has a signature with unrepresentable default."
)},
{"classmeth",
(PyCFunction)test_with_docstring, METH_VARARGS|METH_CLASS,
test_with_docstring, METH_VARARGS|METH_CLASS,
PyDoc_STR(
"classmeth($type, /, a, b=<x>)\n"
"--\n\n"
"This docstring has a signature with unrepresentable default."
)},
{"staticmeth",
(PyCFunction)test_with_docstring, METH_VARARGS|METH_STATIC,
test_with_docstring, METH_VARARGS|METH_STATIC,
PyDoc_STR(
"staticmeth(a, b=<x>)\n"
"--\n\n"
"This docstring has a signature with unrepresentable default."
)},
{"with_default",
(PyCFunction)test_with_docstring, METH_VARARGS,
test_with_docstring, METH_VARARGS,
PyDoc_STR(
"with_default($self, /, x=ONE)\n"
"--\n\n"
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ static PyTypeObject PyRecursingInfinitelyError_Type = {
.tp_basicsize = sizeof(PyBaseExceptionObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = PyDoc_STR("Instantiating this exception starts infinite recursion."),
.tp_init = (initproc)recurse_infinitely_error_init,
.tp_init = recurse_infinitely_error_init,
};

static PyMethodDef test_methods[] = {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ obj_extra_data_set(PyObject *self, PyObject *newval, void *Py_UNUSED(ignored))
}

static PyGetSetDef obj_extra_data_getset[] = {
{"extra", (getter)obj_extra_data_get, (setter)obj_extra_data_set, NULL},
{"extra", obj_extra_data_get, obj_extra_data_set, NULL},
{NULL}
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ static PyMethodDef test_methods[] = {
{"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
{"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,
PyDoc_STR("Remove memory hooks.")},
{"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
{"set_nomemory", set_nomemory, METH_VARARGS,
PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
{"test_pymem_setallocators", test_pymem_setallocators, METH_NOARGS},
Expand Down
13 changes: 6 additions & 7 deletions Modules/_testcapi/watchers.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ get_code_watcher_num_destroyed_events(PyObject *self, PyObject *watcher_id)
}

static PyObject *
allocate_too_many_code_watchers(PyObject *self, PyObject *args)
allocate_too_many_code_watchers(PyObject *self, PyObject *Py_UNUSED(args))
{
int watcher_ids[CODE_MAX_WATCHERS + 1];
int num_watchers = 0;
Expand Down Expand Up @@ -742,7 +742,7 @@ get_context_switches(PyObject *Py_UNUSED(self), PyObject *watcher_id)
}

static PyObject *
allocate_too_many_context_watchers(PyObject *self, PyObject *args)
allocate_too_many_context_watchers(PyObject *self, PyObject *Py_UNUSED(args))
{
int watcher_ids[CONTEXT_MAX_WATCHERS + 1];
int num_watchers = 0;
Expand Down Expand Up @@ -811,16 +811,15 @@ static PyMethodDef test_methods[] = {
{"clear_dict_watcher", clear_dict_watcher, METH_O, NULL},
_TESTCAPI_WATCH_DICT_METHODDEF
_TESTCAPI_UNWATCH_DICT_METHODDEF
{"get_dict_watcher_events",
(PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL},
{"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},

// Type watchers.
{"add_type_watcher", add_type_watcher, METH_O, NULL},
{"clear_type_watcher", clear_type_watcher, METH_O, NULL},
_TESTCAPI_WATCH_TYPE_METHODDEF
_TESTCAPI_UNWATCH_TYPE_METHODDEF
{"get_type_modified_events",
(PyCFunction) get_type_modified_events, METH_NOARGS, NULL},
get_type_modified_events, METH_NOARGS, NULL},

// Code object watchers.
{"add_code_watcher", add_code_watcher, METH_O, NULL},
Expand All @@ -830,7 +829,7 @@ static PyMethodDef test_methods[] = {
{"get_code_watcher_num_destroyed_events",
get_code_watcher_num_destroyed_events, METH_O, NULL},
{"allocate_too_many_code_watchers",
(PyCFunction) allocate_too_many_code_watchers, METH_NOARGS, NULL},
allocate_too_many_code_watchers, METH_NOARGS, NULL},

// Function watchers.
{"add_func_watcher", add_func_watcher, METH_O, NULL},
Expand All @@ -846,7 +845,7 @@ static PyMethodDef test_methods[] = {
{"clear_context_stack", clear_context_stack, METH_NOARGS, NULL},
{"get_context_switches", get_context_switches, METH_O, NULL},
{"allocate_too_many_context_watchers",
(PyCFunction) allocate_too_many_context_watchers, METH_NOARGS, NULL},
allocate_too_many_context_watchers, METH_NOARGS, NULL},
{NULL},
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ static struct PyModuleDef _testcapimodule = {
.m_slots = module_slots,
.m_traverse = module_traverse,
.m_clear = module_clear,
.m_free = (freefunc)module_free,
.m_free = module_free,
};


Expand Down
2 changes: 1 addition & 1 deletion Modules/_xxtestfuzz/_xxtestfuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static PyObject* _fuzz_run(PyObject* self, PyObject* args) {
}

static PyMethodDef module_methods[] = {
{"run", (PyCFunction)_fuzz_run, METH_VARARGS, ""},
{"run", _fuzz_run, METH_VARARGS, ""},
{NULL},
};

Expand Down
4 changes: 2 additions & 2 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2605,10 +2605,10 @@ static PyMethodDef zoneinfo_methods[] = {
ZONEINFO_ZONEINFO_UTCOFFSET_METHODDEF
ZONEINFO_ZONEINFO_DST_METHODDEF
ZONEINFO_ZONEINFO_TZNAME_METHODDEF
{"fromutc", (PyCFunction)zoneinfo_fromutc, METH_O,
{"fromutc", zoneinfo_fromutc, METH_O,
PyDoc_STR("Given a datetime with local time in UTC, retrieve an adjusted "
"datetime in local time.")},
{"__reduce__", (PyCFunction)zoneinfo_reduce, METH_NOARGS,
{"__reduce__", zoneinfo_reduce, METH_NOARGS,
PyDoc_STR("Function for serialization with the pickle protocol.")},
ZONEINFO_ZONEINFO__UNPICKLE_METHODDEF
{"__init_subclass__", _PyCFunction_CAST(zoneinfo_init_subclass),
Expand Down
16 changes: 7 additions & 9 deletions Modules/atexitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Run all registered exit functions.\n\
If a callback raises an exception, it is logged with sys.unraisablehook.");

static PyObject *
atexit_run_exitfuncs(PyObject *module, PyObject *unused)
atexit_run_exitfuncs(PyObject *module, PyObject *Py_UNUSED(dummy))
{
struct atexit_state *state = get_atexit_state();
atexit_callfuncs(state);
Expand All @@ -231,7 +231,7 @@ PyDoc_STRVAR(atexit_clear__doc__,
Clear the list of previously registered exit functions.");

static PyObject *
atexit_clear(PyObject *module, PyObject *unused)
atexit_clear(PyObject *module, PyObject *Py_UNUSED(dummy))
{
atexit_cleanup(get_atexit_state());
Py_RETURN_NONE;
Expand All @@ -244,7 +244,7 @@ PyDoc_STRVAR(atexit_ncallbacks__doc__,
Return the number of registered exit functions.");

static PyObject *
atexit_ncallbacks(PyObject *module, PyObject *unused)
atexit_ncallbacks(PyObject *module, PyObject *Py_UNUSED(dummy))
{
struct atexit_state *state = get_atexit_state();
assert(state->callbacks != NULL);
Expand Down Expand Up @@ -300,13 +300,11 @@ atexit_unregister(PyObject *module, PyObject *func)
static PyMethodDef atexit_methods[] = {
{"register", _PyCFunction_CAST(atexit_register), METH_VARARGS|METH_KEYWORDS,
atexit_register__doc__},
{"_clear", (PyCFunction) atexit_clear, METH_NOARGS,
atexit_clear__doc__},
{"unregister", (PyCFunction) atexit_unregister, METH_O,
atexit_unregister__doc__},
{"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS,
{"_clear", atexit_clear, METH_NOARGS, atexit_clear__doc__},
{"unregister", atexit_unregister, METH_O, atexit_unregister__doc__},
{"_run_exitfuncs", atexit_run_exitfuncs, METH_NOARGS,
atexit_run_exitfuncs__doc__},
{"_ncallbacks", (PyCFunction) atexit_ncallbacks, METH_NOARGS,
{"_ncallbacks", atexit_ncallbacks, METH_NOARGS,
atexit_ncallbacks__doc__},
{NULL, NULL} /* sentinel */
};
Expand Down
2 changes: 1 addition & 1 deletion Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ _cjk_free(void *mod)
}

static struct PyMethodDef _cjk_methods[] = {
{"getcodec", (PyCFunction)getcodec, METH_O, ""},
{"getcodec", getcodec, METH_O, ""},
{NULL, NULL},
};

Expand Down
Loading
Loading