Skip to content

Commit 4dc9f48

Browse files
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 <[email protected]>
1 parent 1700d34 commit 4dc9f48

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

Objects/structseq.c

+10-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
#include "Python.h"
11-
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
1211
#include "pycore_tuple.h" // _PyTuple_FromArray()
1312
#include "pycore_object.h" // _PyObject_GC_TRACK()
1413

@@ -149,7 +148,6 @@ static PyObject *
149148
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
150149
/*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
151150
{
152-
PyObject *ob;
153151
PyStructSequence *res = NULL;
154152
Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
155153

@@ -219,21 +217,18 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
219217
}
220218
Py_DECREF(arg);
221219
for (; i < max_len; ++i) {
222-
if (dict == NULL) {
223-
ob = Py_None;
224-
}
225-
else {
226-
ob = _PyDict_GetItemStringWithError(dict,
227-
type->tp_members[i-n_unnamed_fields].name);
228-
if (ob == NULL) {
229-
if (PyErr_Occurred()) {
230-
Py_DECREF(res);
231-
return NULL;
232-
}
233-
ob = Py_None;
220+
PyObject *ob = NULL;
221+
if (dict != NULL) {
222+
const char *name = type->tp_members[i-n_unnamed_fields].name;
223+
if (PyDict_GetItemStringRef(dict, name, &ob) < 0) {
224+
Py_DECREF(res);
225+
return NULL;
234226
}
235227
}
236-
res->ob_item[i] = Py_NewRef(ob);
228+
if (ob == NULL) {
229+
ob = Py_NewRef(Py_None);
230+
}
231+
res->ob_item[i] = ob;
237232
}
238233

239234
_PyObject_GC_TRACK(res);

Python/codecs.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Copyright (c) Corporation for National Research Initiatives.
1010

1111
#include "Python.h"
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
13-
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
1413
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
1514
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
1615
#include "pycore_pystate.h" // _PyInterpreterState_GET()
@@ -618,20 +617,19 @@ int PyCodec_RegisterError(const char *name, PyObject *error)
618617
the error handling callback for strict encoding will be returned. */
619618
PyObject *PyCodec_LookupError(const char *name)
620619
{
621-
PyObject *handler = NULL;
622-
623620
PyInterpreterState *interp = _PyInterpreterState_GET();
624621
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
625622
return NULL;
626623

627624
if (name==NULL)
628625
name = "strict";
629-
handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name);
630-
if (handler) {
631-
Py_INCREF(handler);
626+
PyObject *handler;
627+
if (PyDict_GetItemStringRef(interp->codec_error_registry, name, &handler) < 0) {
628+
return NULL;
632629
}
633-
else if (!PyErr_Occurred()) {
630+
if (handler == NULL) {
634631
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
632+
return NULL;
635633
}
636634
return handler;
637635
}

Python/import.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* Module definition and import implementation */
22

33
#include "Python.h"
4-
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
54
#include "pycore_hashtable.h" // _Py_hashtable_new_full()
65
#include "pycore_import.h" // _PyImport_BootstrapImp()
76
#include "pycore_initconfig.h" // _PyStatus_OK()
@@ -2435,12 +2434,11 @@ int
24352434
_PyImport_InitDefaultImportFunc(PyInterpreterState *interp)
24362435
{
24372436
// Get the __import__ function
2438-
PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
2439-
"__import__");
2440-
if (import_func == NULL) {
2437+
PyObject *import_func;
2438+
if (PyDict_GetItemStringRef(interp->builtins, "__import__", &import_func) <= 0) {
24412439
return -1;
24422440
}
2443-
IMPORT_FUNC(interp) = Py_NewRef(import_func);
2441+
IMPORT_FUNC(interp) = import_func;
24442442
return 0;
24452443
}
24462444

Python/pylifecycle.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -1372,17 +1372,17 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
13721372
if (verbose) {
13731373
PySys_WriteStderr("# restore sys.%s\n", name);
13741374
}
1375-
PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
1376-
orig_name);
1375+
PyObject *value;
1376+
if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) {
1377+
PyErr_WriteUnraisable(NULL);
1378+
}
13771379
if (value == NULL) {
1378-
if (_PyErr_Occurred(tstate)) {
1379-
PyErr_WriteUnraisable(NULL);
1380-
}
1381-
value = Py_None;
1380+
value = Py_NewRef(Py_None);
13821381
}
13831382
if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
13841383
PyErr_WriteUnraisable(NULL);
13851384
}
1385+
Py_DECREF(value);
13861386
}
13871387
}
13881388

@@ -2207,7 +2207,7 @@ _Py_IsInterpreterFinalizing(PyInterpreterState *interp)
22072207
static PyStatus
22082208
add_main_module(PyInterpreterState *interp)
22092209
{
2210-
PyObject *m, *d, *loader, *ann_dict;
2210+
PyObject *m, *d, *ann_dict;
22112211
m = PyImport_AddModule("__main__");
22122212
if (m == NULL)
22132213
return _PyStatus_ERR("can't create __main__ module");
@@ -2239,11 +2239,13 @@ add_main_module(PyInterpreterState *interp)
22392239
* will be set if __main__ gets further initialized later in the startup
22402240
* process.
22412241
*/
2242-
loader = _PyDict_GetItemStringWithError(d, "__loader__");
2243-
if (loader == NULL || loader == Py_None) {
2244-
if (PyErr_Occurred()) {
2245-
return _PyStatus_ERR("Failed to test __main__.__loader__");
2246-
}
2242+
PyObject *loader;
2243+
if (PyDict_GetItemStringRef(d, "__loader__", &loader) < 0) {
2244+
return _PyStatus_ERR("Failed to test __main__.__loader__");
2245+
}
2246+
int has_loader = !(loader == NULL || loader == Py_None);
2247+
Py_XDECREF(loader);
2248+
if (!has_loader) {
22472249
PyObject *loader = _PyImport_GetImportlibLoader(interp,
22482250
"BuiltinImporter");
22492251
if (loader == NULL) {

0 commit comments

Comments
 (0)