gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827) · python/cpython@18203a6 · GitHub
Skip to content

Commit 18203a6

Browse files
gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827)
1 parent e31d65e commit 18203a6

5 files changed

Lines changed: 76 additions & 120 deletions

File tree

Objects/abstract.c

Lines changed: 1 addition & 6 deletions

Objects/funcobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ PyFunction_ClearWatcher(int watcher_id)
9292
PyFunctionObject *
9393
_PyFunction_FromConstructor(PyFrameConstructor *constr)
9494
{
95-
PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
96-
if (!module && PyErr_Occurred()) {
95+
PyObject *module;
96+
if (PyDict_GetItemRef(constr->fc_globals, &_Py_ID(__name__), &module) < 0) {
9797
return NULL;
9898
}
9999

@@ -158,12 +158,11 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
158158
Py_INCREF(doc);
159159

160160
// __module__: Use globals['__name__'] if it exists, or NULL.
161-
PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
161+
PyObject *module;
162162
PyObject *builtins = NULL;
163-
if (module == NULL && _PyErr_Occurred(tstate)) {
163+
if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &module) < 0) {
164164
goto error;
165165
}
166-
Py_XINCREF(module);
167166

168167
builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
169168
if (builtins == NULL) {

Objects/moduleobject.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ PyModule_GetNameObject(PyObject *mod)
522522
}
523523
PyObject *name;
524524
if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
525+
// error or not found
525526
goto error;
526527
}
527528
if (!PyUnicode_Check(name)) {
@@ -562,6 +563,7 @@ PyModule_GetFilenameObject(PyObject *mod)
562563
}
563564
PyObject *fileobj;
564565
if (PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj) <= 0) {
566+
// error or not found
565567
goto error;
566568
}
567569
if (!PyUnicode_Check(fileobj)) {
@@ -816,28 +818,28 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
816818
PyErr_Clear();
817819
}
818820
assert(m->md_dict != NULL);
819-
getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
821+
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
822+
return NULL;
823+
}
820824
if (getattr) {
821825
PyObject *result = PyObject_CallOneArg(getattr, name);
822826
if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
823827
// suppress AttributeError
824828
PyErr_Clear();
825829
}
830+
Py_DECREF(getattr);
826831
return result;
827832
}
828-
if (PyErr_Occurred()) {
833+
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
829834
return NULL;
830835
}
831-
mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
832836
if (mod_name && PyUnicode_Check(mod_name)) {
833-
Py_INCREF(mod_name);
834-
PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
835-
if (spec == NULL && PyErr_Occurred()) {
837+
PyObject *spec;
838+
if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
836839
Py_DECREF(mod_name);
837840
return NULL;
838841
}
839842
if (suppress != 1) {
840-
Py_XINCREF(spec);
841843
if (_PyModuleSpec_IsInitializing(spec)) {
842844
PyErr_Format(PyExc_AttributeError,
843845
"partially initialized "
@@ -856,14 +858,12 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
856858
"module '%U' has no attribute '%U'",
857859
mod_name, name);
858860
}
859-
Py_XDECREF(spec);
860861
}
862+
Py_XDECREF(spec);
861863
Py_DECREF(mod_name);
862864
return NULL;
863865
}
864-
else if (PyErr_Occurred()) {
865-
return NULL;
866-
}
866+
Py_XDECREF(mod_name);
867867
if (suppress != 1) {
868868
PyErr_Format(PyExc_AttributeError,
869869
"module has no attribute '%U'", name);
@@ -957,11 +957,8 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
957957
return NULL;
958958
}
959959

960-
PyObject *annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
961-
if (annotations) {
962-
Py_INCREF(annotations);
963-
}
964-
else if (!PyErr_Occurred()) {
960+
PyObject *annotations;
961+
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
965962
annotations = PyDict_New();
966963
if (annotations) {
967964
int result = PyDict_SetItem(

Objects/object.c

Lines changed: 11 additions & 20 deletions

0 commit comments

Comments
 (0)