Skip to content

Commit 18203a6

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

File tree

5 files changed

+76
-120
lines changed

5 files changed

+76
-120
lines changed

Objects/abstract.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,7 @@ int
204204
PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
205205
{
206206
if (PyDict_CheckExact(obj)) {
207-
*result = PyDict_GetItemWithError(obj, key); /* borrowed */
208-
if (*result) {
209-
Py_INCREF(*result);
210-
return 1;
211-
}
212-
return PyErr_Occurred() ? -1 : 0;
207+
return PyDict_GetItemRef(obj, key, result);
213208
}
214209

215210
*result = PyObject_GetItem(obj, key);

Objects/funcobject.c

+4-5
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

+13-16
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

+11-20
Original file line numberDiff line numberDiff line change
@@ -1490,19 +1490,14 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
14901490
}
14911491
if (dict != NULL) {
14921492
Py_INCREF(dict);
1493-
PyObject *attr = PyDict_GetItemWithError(dict, name);
1494-
if (attr != NULL) {
1495-
*method = Py_NewRef(attr);
1493+
if (PyDict_GetItemRef(dict, name, method) != 0) {
1494+
// found or error
14961495
Py_DECREF(dict);
14971496
Py_XDECREF(descr);
14981497
return 0;
14991498
}
1499+
// not found
15001500
Py_DECREF(dict);
1501-
1502-
if (PyErr_Occurred()) {
1503-
Py_XDECREF(descr);
1504-
return 0;
1505-
}
15061501
}
15071502

15081503
if (meth_found) {
@@ -1607,21 +1602,17 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
16071602
}
16081603
if (dict != NULL) {
16091604
Py_INCREF(dict);
1610-
res = PyDict_GetItemWithError(dict, name);
1605+
int rc = PyDict_GetItemRef(dict, name, &res);
1606+
Py_DECREF(dict);
16111607
if (res != NULL) {
1612-
Py_INCREF(res);
1613-
Py_DECREF(dict);
16141608
goto done;
16151609
}
1616-
else {
1617-
Py_DECREF(dict);
1618-
if (PyErr_Occurred()) {
1619-
if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1620-
PyErr_Clear();
1621-
}
1622-
else {
1623-
goto done;
1624-
}
1610+
else if (rc < 0) {
1611+
if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1612+
PyErr_Clear();
1613+
}
1614+
else {
1615+
goto done;
16251616
}
16261617
}
16271618
}

0 commit comments

Comments
 (0)