From 7bd9dbf8e148f14f9c9c6715a820bfda6adff957 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 2 Oct 2024 16:31:04 +0200 Subject: [PATCH] gh-111178: Fix function signatures in moduleobject.c (#124900) --- Include/internal/pycore_moduleobject.h | 2 +- Objects/moduleobject.c | 63 ++++++++++++++++++-------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Include/internal/pycore_moduleobject.h b/Include/internal/pycore_moduleobject.h index 049677b292e..cc2dda48ed9 100644 --- a/Include/internal/pycore_moduleobject.h +++ b/Include/internal/pycore_moduleobject.h @@ -46,7 +46,7 @@ static inline PyObject* _PyModule_GetDict(PyObject *mod) { } PyObject* _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress); -PyObject* _Py_module_getattro(PyModuleObject *m, PyObject *name); +PyObject* _Py_module_getattro(PyObject *m, PyObject *name); #ifdef __cplusplus } diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index efc74dafb5f..f63ae4e048b 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -15,6 +15,10 @@ #include "osdefs.h" // MAXPATHLEN +#define _PyModule_CAST(op) \ + (assert(PyModule_Check(op)), _Py_CAST(PyModuleObject*, (op))) + + static PyMemberDef module_members[] = { {"__dict__", _Py_T_OBJECT, offsetof(PyModuleObject, md_dict), Py_READONLY}, {0} @@ -225,7 +229,9 @@ _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version) return NULL; } name = _PyImport_ResolveNameWithPackageContext(name); - if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) + + m = (PyModuleObject*)PyModule_New(name); + if (m == NULL) return NULL; if (module->m_size > 0) { @@ -758,22 +764,26 @@ module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc) } static void -module_dealloc(PyModuleObject *m) +module_dealloc(PyObject *self) { - int verbose = _Py_GetConfig()->verbose; + PyModuleObject *m = _PyModule_CAST(self); PyObject_GC_UnTrack(m); + + int verbose = _Py_GetConfig()->verbose; if (verbose && m->md_name) { PySys_FormatStderr("# destroy %U\n", m->md_name); } if (m->md_weaklist != NULL) PyObject_ClearWeakRefs((PyObject *) m); + /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */ if (m->md_def && m->md_def->m_free && (m->md_def->m_size <= 0 || m->md_state != NULL)) { m->md_def->m_free(m); } + Py_XDECREF(m->md_dict); Py_XDECREF(m->md_name); if (m->md_state != NULL) @@ -782,8 +792,9 @@ module_dealloc(PyModuleObject *m) } static PyObject * -module_repr(PyModuleObject *m) +module_repr(PyObject *self) { + PyModuleObject *m = _PyModule_CAST(self); PyInterpreterState *interp = _PyInterpreterState_GET(); return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m); } @@ -1062,14 +1073,17 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress) PyObject* -_Py_module_getattro(PyModuleObject *m, PyObject *name) +_Py_module_getattro(PyObject *self, PyObject *name) { + PyModuleObject *m = _PyModule_CAST(self); return _Py_module_getattro_impl(m, name, 0); } static int -module_traverse(PyModuleObject *m, visitproc visit, void *arg) +module_traverse(PyObject *self, visitproc visit, void *arg) { + PyModuleObject *m = _PyModule_CAST(self); + /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */ if (m->md_def && m->md_def->m_traverse && (m->md_def->m_size <= 0 || m->md_state != NULL)) @@ -1078,13 +1092,16 @@ module_traverse(PyModuleObject *m, visitproc visit, void *arg) if (res) return res; } + Py_VISIT(m->md_dict); return 0; } static int -module_clear(PyModuleObject *m) +module_clear(PyObject *self) { + PyModuleObject *m = _PyModule_CAST(self); + /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */ if (m->md_def && m->md_def->m_clear && (m->md_def->m_size <= 0 || m->md_state != NULL)) @@ -1149,8 +1166,10 @@ module_get_dict(PyModuleObject *m) } static PyObject * -module_get_annotate(PyModuleObject *m, void *Py_UNUSED(ignored)) +module_get_annotate(PyObject *self, void *Py_UNUSED(ignored)) { + PyModuleObject *m = _PyModule_CAST(self); + PyObject *dict = module_get_dict(m); if (dict == NULL) { return NULL; @@ -1168,12 +1187,14 @@ module_get_annotate(PyModuleObject *m, void *Py_UNUSED(ignored)) } static int -module_set_annotate(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored)) +module_set_annotate(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) { + PyModuleObject *m = _PyModule_CAST(self); if (value == NULL) { PyErr_SetString(PyExc_TypeError, "cannot delete __annotate__ attribute"); return -1; } + PyObject *dict = module_get_dict(m); if (dict == NULL) { return -1; @@ -1200,8 +1221,10 @@ module_set_annotate(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored) } static PyObject * -module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored)) +module_get_annotations(PyObject *self, void *Py_UNUSED(ignored)) { + PyModuleObject *m = _PyModule_CAST(self); + PyObject *dict = module_get_dict(m); if (dict == NULL) { return NULL; @@ -1249,14 +1272,16 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored)) } static int -module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored)) +module_set_annotations(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) { - int ret = -1; + PyModuleObject *m = _PyModule_CAST(self); + PyObject *dict = module_get_dict(m); if (dict == NULL) { return -1; } + int ret = -1; if (value != NULL) { /* set */ ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value); @@ -1282,8 +1307,8 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor static PyGetSetDef module_getsets[] = { - {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations}, - {"__annotate__", (getter)module_get_annotate, (setter)module_set_annotate}, + {"__annotations__", module_get_annotations, module_set_annotations}, + {"__annotate__", module_get_annotate, module_set_annotate}, {NULL} }; @@ -1292,26 +1317,26 @@ PyTypeObject PyModule_Type = { "module", /* tp_name */ sizeof(PyModuleObject), /* tp_basicsize */ 0, /* tp_itemsize */ - (destructor)module_dealloc, /* tp_dealloc */ + module_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc)module_repr, /* tp_repr */ + module_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ - (getattrofunc)_Py_module_getattro, /* tp_getattro */ + _Py_module_getattro, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ module___init____doc__, /* tp_doc */ - (traverseproc)module_traverse, /* tp_traverse */ - (inquiry)module_clear, /* tp_clear */ + module_traverse, /* tp_traverse */ + module_clear, /* tp_clear */ 0, /* tp_richcompare */ offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */ 0, /* tp_iter */