summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-11-14 09:25:39 (GMT)
committerGitHub <noreply@github.com>2023-11-14 09:25:39 (GMT)
commit18203a6bc9ccf3a9ba901574dfc772474b027e2a (patch)
tree7c74d43b8155489ccc8990538890b45ef5e5f9fd /Objects/moduleobject.c
parente31d65e0b7bb6d6fee4e8df54e10976b4cfab1de (diff)
downloadcpython-18203a6bc9ccf3a9ba901574dfc772474b027e2a.zip
cpython-18203a6bc9ccf3a9ba901574dfc772474b027e2a.tar.gz
cpython-18203a6bc9ccf3a9ba901574dfc772474b027e2a.tar.bz2
gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827)
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r--Objects/moduleobject.c29
1 files changed, 13 insertions, 16 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index a3d55b7..bba77ce 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -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(