Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,7 @@ int
PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
{
if (PyDict_CheckExact(obj)) {
*result = PyDict_GetItemWithError(obj, key); /* borrowed */
if (*result) {
Py_INCREF(*result);
return 1;
}
return PyErr_Occurred() ? -1 : 0;
return PyDict_GetItemRef(obj, key, result);
}

*result = PyObject_GetItem(obj, key);
Expand Down
9 changes: 4 additions & 5 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ PyFunction_ClearWatcher(int watcher_id)
PyFunctionObject *
_PyFunction_FromConstructor(PyFrameConstructor *constr)
{
PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
if (!module && PyErr_Occurred()) {
PyObject *module;
if (PyDict_GetItemRef(constr->fc_globals, &_Py_ID(__name__), &module) < 0) {
return NULL;
}

Expand Down Expand Up @@ -158,12 +158,11 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
Py_INCREF(doc);

// __module__: Use globals['__name__'] if it exists, or NULL.
PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
PyObject *module;
PyObject *builtins = NULL;
if (module == NULL && _PyErr_Occurred(tstate)) {
if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &module) < 0) {
goto error;
}
Py_XINCREF(module);

builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
if (builtins == NULL) {
Expand Down
29 changes: 13 additions & 16 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 "
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 11 additions & 20 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,19 +1490,14 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
}
if (dict != NULL) {
Py_INCREF(dict);
PyObject *attr = PyDict_GetItemWithError(dict, name);
if (attr != NULL) {
*method = Py_NewRef(attr);
if (PyDict_GetItemRef(dict, name, method) != 0) {
// found or error
Py_DECREF(dict);
Py_XDECREF(descr);
return 0;
}
// not found
Py_DECREF(dict);

if (PyErr_Occurred()) {
Py_XDECREF(descr);
return 0;
}
}

if (meth_found) {
Expand Down Expand Up @@ -1607,21 +1602,17 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
}
if (dict != NULL) {
Py_INCREF(dict);
res = PyDict_GetItemWithError(dict, name);
int rc = PyDict_GetItemRef(dict, name, &res);
Py_DECREF(dict);
if (res != NULL) {
Py_INCREF(res);
Py_DECREF(dict);
goto done;
}
else {
Py_DECREF(dict);
if (PyErr_Occurred()) {
if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
else {
goto done;
}
else if (rc < 0) {
if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
else {
goto done;
}
}
}
Expand Down
Loading