gh-124153: Remove `_PyType_GetModuleByDef2` private function (GH-124261)

Thank you!
This commit is contained in:
neonene 2024-09-27 01:21:11 +09:00 committed by GitHub
parent 2c472d36b7
commit d7248cdbc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 46 deletions

View File

@ -209,7 +209,6 @@ extern PyObject * _PyType_GetBases(PyTypeObject *type);
extern PyObject * _PyType_GetMRO(PyTypeObject *type); extern PyObject * _PyType_GetMRO(PyTypeObject *type);
extern PyObject* _PyType_GetSubclasses(PyTypeObject *); extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
extern int _PyType_HasSubclasses(PyTypeObject *); extern int _PyType_HasSubclasses(PyTypeObject *);
PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef2(PyTypeObject *, PyTypeObject *, PyModuleDef *);
// Export for _testinternalcapi extension. // Export for _testinternalcapi extension.
PyAPI_FUNC(PyObject *) _PyType_GetSlotWrapperNames(void); PyAPI_FUNC(PyObject *) _PyType_GetSlotWrapperNames(void);

View File

@ -2179,6 +2179,8 @@ typedef struct {
PyObject *default_factory; PyObject *default_factory;
} defdictobject; } defdictobject;
static PyType_Spec defdict_spec;
PyDoc_STRVAR(defdict_missing_doc, PyDoc_STRVAR(defdict_missing_doc,
"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\ "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
if self.default_factory is None: raise KeyError((key,))\n\ if self.default_factory is None: raise KeyError((key,))\n\
@ -2358,23 +2360,16 @@ defdict_or(PyObject* left, PyObject* right)
{ {
PyObject *self, *other; PyObject *self, *other;
// Find module state int ret = PyType_GetBaseByToken(Py_TYPE(left), &defdict_spec, NULL);
PyTypeObject *tp = Py_TYPE(left); if (ret < 0) {
PyObject *mod = PyType_GetModuleByDef(tp, &_collectionsmodule); return NULL;
if (mod == NULL) {
PyErr_Clear();
tp = Py_TYPE(right);
mod = PyType_GetModuleByDef(tp, &_collectionsmodule);
} }
assert(mod != NULL); if (ret) {
collections_state *state = get_module_state(mod);
if (PyObject_TypeCheck(left, state->defdict_type)) {
self = left; self = left;
other = right; other = right;
} }
else { else {
assert(PyObject_TypeCheck(right, state->defdict_type)); assert(PyType_GetBaseByToken(Py_TYPE(right), &defdict_spec, NULL) == 1);
self = right; self = right;
other = left; other = left;
} }
@ -2454,6 +2449,7 @@ passed to the dict constructor, including keyword arguments.\n\
#define DEFERRED_ADDRESS(ADDR) 0 #define DEFERRED_ADDRESS(ADDR) 0
static PyType_Slot defdict_slots[] = { static PyType_Slot defdict_slots[] = {
{Py_tp_token, Py_TP_USE_SPEC},
{Py_tp_dealloc, defdict_dealloc}, {Py_tp_dealloc, defdict_dealloc},
{Py_tp_repr, defdict_repr}, {Py_tp_repr, defdict_repr},
{Py_nb_or, defdict_or}, {Py_nb_or, defdict_or},

View File

@ -122,6 +122,7 @@ get_module_state(PyObject *mod)
} }
static struct PyModuleDef _decimal_module; static struct PyModuleDef _decimal_module;
static PyType_Spec dec_spec;
static inline decimal_state * static inline decimal_state *
get_module_state_by_def(PyTypeObject *tp) get_module_state_by_def(PyTypeObject *tp)
@ -134,10 +135,16 @@ get_module_state_by_def(PyTypeObject *tp)
static inline decimal_state * static inline decimal_state *
find_state_left_or_right(PyObject *left, PyObject *right) find_state_left_or_right(PyObject *left, PyObject *right)
{ {
PyObject *mod = _PyType_GetModuleByDef2(Py_TYPE(left), Py_TYPE(right), PyTypeObject *base;
&_decimal_module); if (PyType_GetBaseByToken(Py_TYPE(left), &dec_spec, &base) != 1) {
assert(mod != NULL); assert(!PyErr_Occurred());
return get_module_state(mod); PyType_GetBaseByToken(Py_TYPE(right), &dec_spec, &base);
}
assert(base != NULL);
void *state = _PyType_GetModuleState(base);
assert(state != NULL);
Py_DECREF(base);
return (decimal_state *)state;
} }
@ -745,7 +752,7 @@ signaldict_richcompare(PyObject *v, PyObject *w, int op)
{ {
PyObject *res = Py_NotImplemented; PyObject *res = Py_NotImplemented;
decimal_state *state = find_state_left_or_right(v, w); decimal_state *state = get_module_state_by_def(Py_TYPE(v));
assert(PyDecSignalDict_Check(state, v)); assert(PyDecSignalDict_Check(state, v));
if ((SdFlagAddr(v) == NULL) || (SdFlagAddr(w) == NULL)) { if ((SdFlagAddr(v) == NULL) || (SdFlagAddr(w) == NULL)) {
@ -5041,6 +5048,7 @@ static PyMethodDef dec_methods [] =
}; };
static PyType_Slot dec_slots[] = { static PyType_Slot dec_slots[] = {
{Py_tp_token, Py_TP_USE_SPEC},
{Py_tp_dealloc, dec_dealloc}, {Py_tp_dealloc, dec_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr}, {Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_traverse, dec_traverse}, {Py_tp_traverse, dec_traverse},

View File

@ -5207,8 +5207,8 @@ PyType_GetModuleState(PyTypeObject *type)
/* Get the module of the first superclass where the module has the /* Get the module of the first superclass where the module has the
* given PyModuleDef. * given PyModuleDef.
*/ */
static inline PyObject * PyObject *
get_module_by_def(PyTypeObject *type, PyModuleDef *def) PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
{ {
assert(PyType_Check(type)); assert(PyType_Check(type));
@ -5254,37 +5254,14 @@ get_module_by_def(PyTypeObject *type, PyModuleDef *def)
} }
} }
END_TYPE_LOCK(); END_TYPE_LOCK();
return res;
}
PyObject * if (res == NULL) {
PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
{
PyObject *module = get_module_by_def(type, def);
if (module == NULL) {
PyErr_Format( PyErr_Format(
PyExc_TypeError, PyExc_TypeError,
"PyType_GetModuleByDef: No superclass of '%s' has the given module", "PyType_GetModuleByDef: No superclass of '%s' has the given module",
type->tp_name); type->tp_name);
} }
return module; return res;
}
PyObject *
_PyType_GetModuleByDef2(PyTypeObject *left, PyTypeObject *right,
PyModuleDef *def)
{
PyObject *module = get_module_by_def(left, def);
if (module == NULL) {
module = get_module_by_def(right, def);
if (module == NULL) {
PyErr_Format(
PyExc_TypeError,
"PyType_GetModuleByDef: No superclass of '%s' nor '%s' has "
"the given module", left->tp_name, right->tp_name);
}
}
return module;
} }