bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)

diff --git a/Python/_warnings.c b/Python/_warnings.c
index 7eedd13..33b4615 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -252,7 +252,7 @@
     if (key == NULL)
         return -1;
 
-    version_obj = _PyDict_GetItemId(registry, &PyId_version);
+    version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version);
     if (version_obj == NULL
         || !PyLong_CheckExact(version_obj)
         || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
@@ -271,12 +271,15 @@
         Py_DECREF(version_obj);
     }
     else {
-        already_warned = PyDict_GetItem(registry, key);
+        already_warned = PyDict_GetItemWithError(registry, key);
         if (already_warned != NULL) {
             int rc = PyObject_IsTrue(already_warned);
             if (rc != 0)
                 return rc;
         }
+        else if (PyErr_Occurred()) {
+            return -1;
+        }
     }
 
     /* This warning wasn't found in the registry, set it. */
@@ -672,6 +675,8 @@
 setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
               PyObject **module, PyObject **registry)
 {
+    _Py_IDENTIFIER(__warningregistry__);
+    _Py_IDENTIFIER(__name__);
     PyObject *globals;
 
     /* Setup globals, filename and lineno. */
@@ -706,15 +711,18 @@
     /* Setup registry. */
     assert(globals != NULL);
     assert(PyDict_Check(globals));
-    *registry = PyDict_GetItemString(globals, "__warningregistry__");
+    *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__);
     if (*registry == NULL) {
         int rc;
 
+        if (PyErr_Occurred()) {
+            return 0;
+        }
         *registry = PyDict_New();
         if (*registry == NULL)
             return 0;
 
-         rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
+         rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry);
          if (rc < 0)
             goto handle_error;
     }
@@ -722,10 +730,13 @@
         Py_INCREF(*registry);
 
     /* Setup module. */
-    *module = PyDict_GetItemString(globals, "__name__");
+    *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
     if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
         Py_INCREF(*module);
     }
+    else if (PyErr_Occurred()) {
+        goto handle_error;
+    }
     else {
         *module = PyUnicode_FromString("<string>");
         if (*module == NULL)
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index f9b901f..eebdc5b 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -142,7 +142,7 @@
             return NULL;
         }
 
-        meta = _PyDict_GetItemId(mkw, &PyId_metaclass);
+        meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass);
         if (meta != NULL) {
             Py_INCREF(meta);
             if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
@@ -154,6 +154,11 @@
             /* metaclass is explicitly given, check if it's indeed a class */
             isclass = PyType_Check(meta);
         }
+        else if (PyErr_Occurred()) {
+            Py_DECREF(mkw);
+            Py_DECREF(bases);
+            return NULL;
+        }
     }
     if (meta == NULL) {
         /* if there are no bases, use type: */
@@ -956,11 +961,14 @@
         return NULL;
     }
 
-    if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
+    if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
         if (_PyDict_SetItemId(globals, &PyId___builtins__,
                               PyEval_GetBuiltins()) != 0)
             return NULL;
     }
+    else if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     if (PyCode_Check(source)) {
         if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
@@ -1036,11 +1044,14 @@
             locals->ob_type->tp_name);
         return NULL;
     }
-    if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
+    if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
         if (_PyDict_SetItemId(globals, &PyId___builtins__,
                               PyEval_GetBuiltins()) != 0)
             return NULL;
     }
+    else if (PyErr_Occurred()) {
+        return NULL;
+    }
 
     if (PyCode_Check(source)) {
         if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
diff --git a/Python/ceval.c b/Python/ceval.c
index 4e139ce..ff83863 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2090,10 +2090,12 @@
 
             PyObject *bc;
             if (PyDict_CheckExact(f->f_builtins)) {
-                bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
+                bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
                 if (bc == NULL) {
-                    PyErr_SetString(PyExc_NameError,
-                                    "__build_class__ not found");
+                    if (!PyErr_Occurred()) {
+                        PyErr_SetString(PyExc_NameError,
+                                        "__build_class__ not found");
+                    }
                     goto error;
                 }
                 Py_INCREF(bc);
@@ -2241,8 +2243,10 @@
             int err;
             err = PyDict_DelItem(f->f_globals, name);
             if (err != 0) {
-                format_exc_check_arg(
-                    PyExc_NameError, NAME_ERROR_MSG, name);
+                if (PyErr_ExceptionMatches(PyExc_KeyError)) {
+                    format_exc_check_arg(
+                        PyExc_NameError, NAME_ERROR_MSG, name);
+                }
                 goto error;
             }
             DISPATCH();
@@ -2258,8 +2262,13 @@
                 goto error;
             }
             if (PyDict_CheckExact(locals)) {
-                v = PyDict_GetItem(locals, name);
-                Py_XINCREF(v);
+                v = PyDict_GetItemWithError(locals, name);
+                if (v != NULL) {
+                    Py_INCREF(v);
+                }
+                else if (PyErr_Occurred()) {
+                    goto error;
+                }
             }
             else {
                 v = PyObject_GetItem(locals, name);
@@ -2270,15 +2279,22 @@
                 }
             }
             if (v == NULL) {
-                v = PyDict_GetItem(f->f_globals, name);
-                Py_XINCREF(v);
-                if (v == NULL) {
+                v = PyDict_GetItemWithError(f->f_globals, name);
+                if (v != NULL) {
+                    Py_INCREF(v);
+                }
+                else if (PyErr_Occurred()) {
+                    goto error;
+                }
+                else {
                     if (PyDict_CheckExact(f->f_builtins)) {
-                        v = PyDict_GetItem(f->f_builtins, name);
+                        v = PyDict_GetItemWithError(f->f_builtins, name);
                         if (v == NULL) {
-                            format_exc_check_arg(
+                            if (!PyErr_Occurred()) {
+                                format_exc_check_arg(
                                         PyExc_NameError,
                                         NAME_ERROR_MSG, name);
+                            }
                             goto error;
                         }
                         Py_INCREF(v);
@@ -2386,8 +2402,13 @@
             assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
             name = PyTuple_GET_ITEM(co->co_freevars, idx);
             if (PyDict_CheckExact(locals)) {
-                value = PyDict_GetItem(locals, name);
-                Py_XINCREF(value);
+                value = PyDict_GetItemWithError(locals, name);
+                if (value != NULL) {
+                    Py_INCREF(value);
+                }
+                else if (PyErr_Occurred()) {
+                    goto error;
+                }
             }
             else {
                 value = PyObject_GetItem(locals, name);
@@ -2591,9 +2612,12 @@
             }
             /* check if __annotations__ in locals()... */
             if (PyDict_CheckExact(f->f_locals)) {
-                ann_dict = _PyDict_GetItemId(f->f_locals,
+                ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
                                              &PyId___annotations__);
                 if (ann_dict == NULL) {
+                    if (PyErr_Occurred()) {
+                        goto error;
+                    }
                     /* ...if not, create a new one */
                     ann_dict = PyDict_New();
                     if (ann_dict == NULL) {
@@ -3921,12 +3945,15 @@
                 continue;
             name = PyTuple_GET_ITEM(co->co_varnames, i);
             if (kwdefs != NULL) {
-                PyObject *def = PyDict_GetItem(kwdefs, name);
+                PyObject *def = PyDict_GetItemWithError(kwdefs, name);
                 if (def) {
                     Py_INCREF(def);
                     SETLOCAL(i, def);
                     continue;
                 }
+                else if (PyErr_Occurred()) {
+                    goto fail;
+                }
             }
             missing++;
         }
@@ -4861,9 +4888,11 @@
     PyObject *import_func, *res;
     PyObject* stack[5];
 
-    import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
+    import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
     if (import_func == NULL) {
-        PyErr_SetString(PyExc_ImportError, "__import__ not found");
+        if (!PyErr_Occurred()) {
+            PyErr_SetString(PyExc_ImportError, "__import__ not found");
+        }
         return NULL;
     }
 
@@ -5207,10 +5236,13 @@
             PyObject *names = f->f_code->co_names;
             PyObject *name = GETITEM(names, oparg);
             PyObject *locals = f->f_locals;
-            if (locals && PyDict_CheckExact(locals) &&
-                PyDict_GetItem(locals, name) == v) {
-                if (PyDict_DelItem(locals, name) != 0) {
-                    PyErr_Clear();
+            if (locals && PyDict_CheckExact(locals)) {
+                PyObject *w = PyDict_GetItemWithError(locals, name);
+                if ((w == v && PyDict_DelItem(locals, name) != 0) ||
+                    (w == NULL && PyErr_Occurred()))
+                {
+                    Py_DECREF(v);
+                    return NULL;
                 }
             }
             break;
diff --git a/Python/codecs.c b/Python/codecs.c
index ff2142d..d4b34f8 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -120,12 +120,16 @@
     PyUnicode_InternInPlace(&v);
 
     /* First, try to lookup the name in the registry dictionary */
-    result = PyDict_GetItem(interp->codec_search_cache, v);
+    result = PyDict_GetItemWithError(interp->codec_search_cache, v);
     if (result != NULL) {
         Py_INCREF(result);
         Py_DECREF(v);
         return result;
     }
+    else if (PyErr_Occurred()) {
+        Py_DECREF(v);
+        return NULL;
+    }
 
     /* Next, scan the search functions in order of registration */
     args = PyTuple_New(1);
@@ -648,11 +652,13 @@
 
     if (name==NULL)
         name = "strict";
-    handler = PyDict_GetItemString(interp->codec_error_registry, name);
-    if (!handler)
-        PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
-    else
+    handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name);
+    if (handler) {
         Py_INCREF(handler);
+    }
+    else if (!PyErr_Occurred()) {
+        PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
+    }
     return handler;
 }
 
diff --git a/Python/errors.c b/Python/errors.c
index febe971..b8af1df 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -858,6 +858,7 @@
 PyObject *
 PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
 {
+    _Py_IDENTIFIER(__module__);
     const char *dot;
     PyObject *modulename = NULL;
     PyObject *classname = NULL;
@@ -877,12 +878,15 @@
         if (dict == NULL)
             goto failure;
     }
-    if (PyDict_GetItemString(dict, "__module__") == NULL) {
+    if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
+        if (PyErr_Occurred()) {
+            goto failure;
+        }
         modulename = PyUnicode_FromStringAndSize(name,
                                              (Py_ssize_t)(dot-name));
         if (modulename == NULL)
             goto failure;
-        if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
+        if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0)
             goto failure;
     }
     if (PyTuple_Check(base)) {
diff --git a/Python/getargs.c b/Python/getargs.c
index 0e07555..ba1a9d4 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1763,9 +1763,13 @@
                 current_arg = PyTuple_GET_ITEM(args, i);
             }
             else if (nkwargs && i >= pos) {
-                current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
-                if (current_arg)
+                current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
+                if (current_arg) {
                     --nkwargs;
+                }
+                else if (PyErr_Occurred()) {
+                    return cleanreturn(0, &freelist);
+                }
             }
             else {
                 current_arg = NULL;
@@ -1844,7 +1848,7 @@
         Py_ssize_t j;
         /* make sure there are no arguments given by name and position */
         for (i = pos; i < nargs; i++) {
-            current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
+            current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
             if (current_arg) {
                 /* arg present in tuple and in dict */
                 PyErr_Format(PyExc_TypeError,
@@ -1855,6 +1859,9 @@
                              kwlist[i], i+1);
                 return cleanreturn(0, &freelist);
             }
+            else if (PyErr_Occurred()) {
+                return cleanreturn(0, &freelist);
+            }
         }
         /* make sure there are no extraneous keyword arguments */
         j = 0;
@@ -2016,13 +2023,10 @@
 }
 
 static PyObject*
-find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
+find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
 {
     Py_ssize_t i, nkwargs;
 
-    if (kwargs != NULL) {
-        return PyDict_GetItem(kwargs, key);
-    }
     nkwargs = PyTuple_GET_SIZE(kwnames);
     for (i=0; i < nkwargs; i++) {
         PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
@@ -2157,9 +2161,18 @@
         }
         else if (nkwargs && i >= pos) {
             keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
-            current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
-            if (current_arg)
+            if (kwargs != NULL) {
+                current_arg = PyDict_GetItemWithError(kwargs, keyword);
+                if (!current_arg && PyErr_Occurred()) {
+                    return cleanreturn(0, &freelist);
+                }
+            }
+            else {
+                current_arg = find_keyword(kwnames, kwstack, keyword);
+            }
+            if (current_arg) {
                 --nkwargs;
+            }
         }
         else {
             current_arg = NULL;
@@ -2220,7 +2233,15 @@
         /* make sure there are no arguments given by name and position */
         for (i = pos; i < nargs; i++) {
             keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
-            current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
+            if (kwargs != NULL) {
+                current_arg = PyDict_GetItemWithError(kwargs, keyword);
+                if (!current_arg && PyErr_Occurred()) {
+                    return cleanreturn(0, &freelist);
+                }
+            }
+            else {
+                current_arg = find_keyword(kwnames, kwstack, keyword);
+            }
             if (current_arg) {
                 /* arg present in tuple and in dict */
                 PyErr_Format(PyExc_TypeError,
diff --git a/Python/import.c b/Python/import.c
index 344f199..898321a 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -431,9 +431,13 @@
     for (p = sys_files; *p != NULL; p+=2) {
         if (Py_VerboseFlag)
             PySys_WriteStderr("# restore sys.%s\n", *p);
-        value = PyDict_GetItemString(interp->sysdict, *(p+1));
-        if (value == NULL)
+        value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1));
+        if (value == NULL) {
+            if (PyErr_Occurred()) {
+                PyErr_WriteUnraisable(NULL);
+            }
             value = Py_None;
+        }
         if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) {
             PyErr_WriteUnraisable(NULL);
         }
@@ -718,7 +722,7 @@
     key = PyTuple_Pack(2, filename, name);
     if (key == NULL)
         return NULL;
-    def = (PyModuleDef *)PyDict_GetItem(extensions, key);
+    def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key);
     Py_DECREF(key);
     if (def == NULL)
         return NULL;
@@ -927,6 +931,7 @@
 static PyObject *
 module_dict_for_exec(PyObject *name)
 {
+    _Py_IDENTIFIER(__builtins__);
     PyObject *m, *d = NULL;
 
     m = PyImport_AddModuleObject(name);
@@ -935,9 +940,11 @@
     /* If the module is being reloaded, we get the old module back
        and re-use its dict to exec the new code. */
     d = PyModule_GetDict(m);
-    if (PyDict_GetItemString(d, "__builtins__") == NULL) {
-        if (PyDict_SetItemString(d, "__builtins__",
-                                 PyEval_GetBuiltins()) != 0) {
+    if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) {
+        if (PyErr_Occurred() ||
+            _PyDict_SetItemId(d, &PyId___builtins__,
+                              PyEval_GetBuiltins()) != 0)
+        {
             remove_module(name);
             return NULL;
         }
@@ -1107,8 +1114,8 @@
     if (nhooks < 0)
         return NULL; /* Shouldn't happen */
 
-    importer = PyDict_GetItem(path_importer_cache, p);
-    if (importer != NULL)
+    importer = PyDict_GetItemWithError(path_importer_cache, p);
+    if (importer != NULL || PyErr_Occurred())
         return importer;
 
     /* set path_importer_cache[p] to None to avoid recursion */
@@ -1496,11 +1503,17 @@
         PyErr_SetString(PyExc_TypeError, "globals must be a dict");
         goto error;
     }
-    package = _PyDict_GetItemId(globals, &PyId___package__);
+    package = _PyDict_GetItemIdWithError(globals, &PyId___package__);
     if (package == Py_None) {
         package = NULL;
     }
-    spec = _PyDict_GetItemId(globals, &PyId___spec__);
+    else if (package == NULL && PyErr_Occurred()) {
+        goto error;
+    }
+    spec = _PyDict_GetItemIdWithError(globals, &PyId___spec__);
+    if (spec == NULL && PyErr_Occurred()) {
+        goto error;
+    }
 
     if (package != NULL) {
         Py_INCREF(package);
@@ -1546,9 +1559,11 @@
             goto error;
         }
 
-        package = _PyDict_GetItemId(globals, &PyId___name__);
+        package = _PyDict_GetItemIdWithError(globals, &PyId___name__);
         if (package == NULL) {
-            PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
+            if (!PyErr_Occurred()) {
+                PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
+            }
             goto error;
         }
 
@@ -1558,10 +1573,10 @@
             goto error;
         }
 
-        if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
+        if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) {
             Py_ssize_t dot;
 
-            if (PyUnicode_READY(package) < 0) {
+            if (PyErr_Occurred() || PyUnicode_READY(package) < 0) {
                 goto error;
             }
 
diff --git a/Python/symtable.c b/Python/symtable.c
index cade304..6e2df2f 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -359,12 +359,12 @@
     k = PyLong_FromVoidPtr(key);
     if (k == NULL)
         return NULL;
-    v = PyDict_GetItem(st->st_blocks, k);
+    v = PyDict_GetItemWithError(st->st_blocks, k);
     if (v) {
         assert(PySTEntry_Check(v));
         Py_INCREF(v);
     }
-    else {
+    else if (!PyErr_Occurred()) {
         PyErr_SetString(PyExc_KeyError,
                         "unknown symbol table entry");
     }
@@ -637,7 +637,7 @@
     }
 
     while ((name = PyIter_Next(itr))) {
-        v = PyDict_GetItem(symbols, name);
+        v = PyDict_GetItemWithError(symbols, name);
 
         /* Handle symbol that already exists in this scope */
         if (v) {
@@ -662,6 +662,9 @@
             Py_DECREF(name);
             continue;
         }
+        else if (PyErr_Occurred()) {
+            goto error;
+        }
         /* Handle global symbol */
         if (bound && !PySet_Contains(bound, name)) {
             Py_DECREF(name);
@@ -991,7 +994,7 @@
     if (!mangled)
         return 0;
     dict = ste->ste_symbols;
-    if ((o = PyDict_GetItem(dict, mangled))) {
+    if ((o = PyDict_GetItemWithError(dict, mangled))) {
         val = PyLong_AS_LONG(o);
         if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
             /* Is it better to use 'mangled' or 'name' here? */
@@ -1002,8 +1005,13 @@
             goto error;
         }
         val |= flag;
-    } else
+    }
+    else if (PyErr_Occurred()) {
+        goto error;
+    }
+    else {
         val = flag;
+    }
     o = PyLong_FromLong(val);
     if (o == NULL)
         goto error;