diff options
author | Victor Stinner <vstinner@python.org> | 2023-08-23 22:59:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-23 22:59:00 +0200 |
commit | 4dc9f4893084f7c3acf78a0384620cd44f604a0d (patch) | |
tree | e68ff4cc214daa87aedf3f508ad07c3f0d61ff66 /Python/pylifecycle.c | |
parent | 1700d34d314f5304a7a75363bda295a8c15c371f (diff) | |
download | cpython-4dc9f4893084f7c3acf78a0384620cd44f604a0d.tar.gz cpython-4dc9f4893084f7c3acf78a0384620cd44f604a0d.zip |
gh-108308: Replace _PyDict_GetItemStringWithError() (#108372)
Replace _PyDict_GetItemStringWithError() calls with
PyDict_GetItemStringRef() which returns a strong reference to the
item.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8c321fbcfe4..18614264989 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1372,17 +1372,17 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose) if (verbose) { PySys_WriteStderr("# restore sys.%s\n", name); } - PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict, - orig_name); + PyObject *value; + if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) { + PyErr_WriteUnraisable(NULL); + } if (value == NULL) { - if (_PyErr_Occurred(tstate)) { - PyErr_WriteUnraisable(NULL); - } - value = Py_None; + value = Py_NewRef(Py_None); } if (PyDict_SetItemString(interp->sysdict, name, value) < 0) { PyErr_WriteUnraisable(NULL); } + Py_DECREF(value); } } @@ -2207,7 +2207,7 @@ _Py_IsInterpreterFinalizing(PyInterpreterState *interp) static PyStatus add_main_module(PyInterpreterState *interp) { - PyObject *m, *d, *loader, *ann_dict; + PyObject *m, *d, *ann_dict; m = PyImport_AddModule("__main__"); if (m == NULL) return _PyStatus_ERR("can't create __main__ module"); @@ -2239,11 +2239,13 @@ add_main_module(PyInterpreterState *interp) * will be set if __main__ gets further initialized later in the startup * process. */ - loader = _PyDict_GetItemStringWithError(d, "__loader__"); - if (loader == NULL || loader == Py_None) { - if (PyErr_Occurred()) { - return _PyStatus_ERR("Failed to test __main__.__loader__"); - } + PyObject *loader; + if (PyDict_GetItemStringRef(d, "__loader__", &loader) < 0) { + return _PyStatus_ERR("Failed to test __main__.__loader__"); + } + int has_loader = !(loader == NULL || loader == Py_None); + Py_XDECREF(loader); + if (!has_loader) { PyObject *loader = _PyImport_GetImportlibLoader(interp, "BuiltinImporter"); if (loader == NULL) { |