gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827)

This commit is contained in:
Serhiy Storchaka 2023-11-14 11:25:39 +02:00 committed by GitHub
parent e31d65e0b7
commit 18203a6bc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 120 deletions

View file

@ -522,6 +522,7 @@ PyModule_GetNameObject(PyObject *mod)
}
PyObject *name;
if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
// error or not found
goto error;
}
if (!PyUnicode_Check(name)) {
@ -562,6 +563,7 @@ PyModule_GetFilenameObject(PyObject *mod)
}
PyObject *fileobj;
if (PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj) <= 0) {
// error or not found
goto error;
}
if (!PyUnicode_Check(fileobj)) {
@ -816,28 +818,28 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
PyErr_Clear();
}
assert(m->md_dict != NULL);
getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
return NULL;
}
if (getattr) {
PyObject *result = PyObject_CallOneArg(getattr, name);
if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
// suppress AttributeError
PyErr_Clear();
}
Py_DECREF(getattr);
return result;
}
if (PyErr_Occurred()) {
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
return NULL;
}
mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
if (mod_name && PyUnicode_Check(mod_name)) {
Py_INCREF(mod_name);
PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
if (spec == NULL && PyErr_Occurred()) {
PyObject *spec;
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
Py_DECREF(mod_name);
return NULL;
}
if (suppress != 1) {
Py_XINCREF(spec);
if (_PyModuleSpec_IsInitializing(spec)) {
PyErr_Format(PyExc_AttributeError,
"partially initialized "
@ -856,14 +858,12 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
"module '%U' has no attribute '%U'",
mod_name, name);
}
Py_XDECREF(spec);
}
Py_XDECREF(spec);
Py_DECREF(mod_name);
return NULL;
}
else if (PyErr_Occurred()) {
return NULL;
}
Py_XDECREF(mod_name);
if (suppress != 1) {
PyErr_Format(PyExc_AttributeError,
"module has no attribute '%U'", name);
@ -957,11 +957,8 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
return NULL;
}
PyObject *annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
if (annotations) {
Py_INCREF(annotations);
}
else if (!PyErr_Occurred()) {
PyObject *annotations;
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
annotations = PyDict_New();
if (annotations) {
int result = PyDict_SetItem(