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>
This commit is contained in:
Victor Stinner 2023-08-23 22:59:00 +02:00 committed by GitHub
parent 1700d34d31
commit 4dc9f48930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 39 deletions

View File

@ -8,7 +8,6 @@
*/ */
#include "Python.h" #include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_tuple.h" // _PyTuple_FromArray() #include "pycore_tuple.h" // _PyTuple_FromArray()
#include "pycore_object.h" // _PyObject_GC_TRACK() #include "pycore_object.h" // _PyObject_GC_TRACK()
@ -149,7 +148,6 @@ static PyObject *
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
/*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/ /*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
{ {
PyObject *ob;
PyStructSequence *res = NULL; PyStructSequence *res = NULL;
Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
@ -219,21 +217,18 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
} }
Py_DECREF(arg); Py_DECREF(arg);
for (; i < max_len; ++i) { for (; i < max_len; ++i) {
if (dict == NULL) { PyObject *ob = NULL;
ob = Py_None; if (dict != NULL) {
} const char *name = type->tp_members[i-n_unnamed_fields].name;
else { if (PyDict_GetItemStringRef(dict, name, &ob) < 0) {
ob = _PyDict_GetItemStringWithError(dict, Py_DECREF(res);
type->tp_members[i-n_unnamed_fields].name); return NULL;
if (ob == NULL) {
if (PyErr_Occurred()) {
Py_DECREF(res);
return NULL;
}
ob = Py_None;
} }
} }
res->ob_item[i] = Py_NewRef(ob); if (ob == NULL) {
ob = Py_NewRef(Py_None);
}
res->ob_item[i] = ob;
} }
_PyObject_GC_TRACK(res); _PyObject_GC_TRACK(res);

View File

@ -10,7 +10,6 @@ Copyright (c) Corporation for National Research Initiatives.
#include "Python.h" #include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_interp.h" // PyInterpreterState.codec_search_path #include "pycore_interp.h" // PyInterpreterState.codec_search_path
#include "pycore_pyerrors.h" // _PyErr_FormatNote() #include "pycore_pyerrors.h" // _PyErr_FormatNote()
#include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_pystate.h" // _PyInterpreterState_GET()
@ -618,20 +617,19 @@ int PyCodec_RegisterError(const char *name, PyObject *error)
the error handling callback for strict encoding will be returned. */ the error handling callback for strict encoding will be returned. */
PyObject *PyCodec_LookupError(const char *name) PyObject *PyCodec_LookupError(const char *name)
{ {
PyObject *handler = NULL;
PyInterpreterState *interp = _PyInterpreterState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
return NULL; return NULL;
if (name==NULL) if (name==NULL)
name = "strict"; name = "strict";
handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name); PyObject *handler;
if (handler) { if (PyDict_GetItemStringRef(interp->codec_error_registry, name, &handler) < 0) {
Py_INCREF(handler); return NULL;
} }
else if (!PyErr_Occurred()) { if (handler == NULL) {
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
return NULL;
} }
return handler; return handler;
} }

View File

@ -1,7 +1,6 @@
/* Module definition and import implementation */ /* Module definition and import implementation */
#include "Python.h" #include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_hashtable.h" // _Py_hashtable_new_full() #include "pycore_hashtable.h" // _Py_hashtable_new_full()
#include "pycore_import.h" // _PyImport_BootstrapImp() #include "pycore_import.h" // _PyImport_BootstrapImp()
#include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_initconfig.h" // _PyStatus_OK()
@ -2435,12 +2434,11 @@ int
_PyImport_InitDefaultImportFunc(PyInterpreterState *interp) _PyImport_InitDefaultImportFunc(PyInterpreterState *interp)
{ {
// Get the __import__ function // Get the __import__ function
PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins, PyObject *import_func;
"__import__"); if (PyDict_GetItemStringRef(interp->builtins, "__import__", &import_func) <= 0) {
if (import_func == NULL) {
return -1; return -1;
} }
IMPORT_FUNC(interp) = Py_NewRef(import_func); IMPORT_FUNC(interp) = import_func;
return 0; return 0;
} }

View File

@ -1372,17 +1372,17 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
if (verbose) { if (verbose) {
PySys_WriteStderr("# restore sys.%s\n", name); PySys_WriteStderr("# restore sys.%s\n", name);
} }
PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict, PyObject *value;
orig_name); if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) {
PyErr_WriteUnraisable(NULL);
}
if (value == NULL) { if (value == NULL) {
if (_PyErr_Occurred(tstate)) { value = Py_NewRef(Py_None);
PyErr_WriteUnraisable(NULL);
}
value = Py_None;
} }
if (PyDict_SetItemString(interp->sysdict, name, value) < 0) { if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
Py_DECREF(value);
} }
} }
@ -2207,7 +2207,7 @@ _Py_IsInterpreterFinalizing(PyInterpreterState *interp)
static PyStatus static PyStatus
add_main_module(PyInterpreterState *interp) add_main_module(PyInterpreterState *interp)
{ {
PyObject *m, *d, *loader, *ann_dict; PyObject *m, *d, *ann_dict;
m = PyImport_AddModule("__main__"); m = PyImport_AddModule("__main__");
if (m == NULL) if (m == NULL)
return _PyStatus_ERR("can't create __main__ module"); 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 * will be set if __main__ gets further initialized later in the startup
* process. * process.
*/ */
loader = _PyDict_GetItemStringWithError(d, "__loader__"); PyObject *loader;
if (loader == NULL || loader == Py_None) { if (PyDict_GetItemStringRef(d, "__loader__", &loader) < 0) {
if (PyErr_Occurred()) { return _PyStatus_ERR("Failed to test __main__.__loader__");
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, PyObject *loader = _PyImport_GetImportlibLoader(interp,
"BuiltinImporter"); "BuiltinImporter");
if (loader == NULL) { if (loader == NULL) {