Skip to content

Commit 03b456e

Browse files
committed
gh-108308: Replace _PyDict_GetItemStringWithError()
Replace _PyDict_GetItemStringWithError() calls with PyDict_GetItemStringRef() which returns a strong reference to the item.
1 parent 422f81b commit 03b456e

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

Objects/structseq.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ static PyObject *
149149
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
150150
/*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
151151
{
152-
PyObject *ob;
153152
PyStructSequence *res = NULL;
154153
Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
155154

@@ -219,21 +218,21 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
219218
}
220219
Py_DECREF(arg);
221220
for (; i < max_len; ++i) {
221+
PyObject *ob;
222222
if (dict == NULL) {
223-
ob = Py_None;
223+
ob = Py_NewRef(Py_None);
224224
}
225225
else {
226-
ob = _PyDict_GetItemStringWithError(dict,
227-
type->tp_members[i-n_unnamed_fields].name);
226+
const char *name = type->tp_members[i-n_unnamed_fields].name;
227+
if (PyDict_GetItemStringRef(dict, name, &ob) < 0) {
228+
Py_DECREF(res);
229+
return NULL;
230+
}
228231
if (ob == NULL) {
229-
if (PyErr_Occurred()) {
230-
Py_DECREF(res);
231-
return NULL;
232-
}
233-
ob = Py_None;
232+
ob = Py_NewRef(Py_None);
234233
}
235234
}
236-
res->ob_item[i] = Py_NewRef(ob);
235+
res->ob_item[i] = ob;
237236
}
238237

239238
_PyObject_GC_TRACK(res);

Python/codecs.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -618,20 +618,19 @@ int PyCodec_RegisterError(const char *name, PyObject *error)
618618
the error handling callback for strict encoding will be returned. */
619619
PyObject *PyCodec_LookupError(const char *name)
620620
{
621-
PyObject *handler = NULL;
622-
623621
PyInterpreterState *interp = _PyInterpreterState_GET();
624622
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
625623
return NULL;
626624

627625
if (name==NULL)
628626
name = "strict";
629-
handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name);
630-
if (handler) {
631-
Py_INCREF(handler);
627+
PyObject *handler;
628+
if (PyDict_GetItemStringRef(interp->codec_error_registry, name, &handler) < 0) {
629+
return NULL;
632630
}
633-
else if (!PyErr_Occurred()) {
631+
if (handler == NULL) {
634632
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
633+
return NULL;
635634
}
636635
return handler;
637636
}

Python/import.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,12 +2435,11 @@ int
24352435
_PyImport_InitDefaultImportFunc(PyInterpreterState *interp)
24362436
{
24372437
// Get the __import__ function
2438-
PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
2439-
"__import__");
2440-
if (import_func == NULL) {
2438+
PyObject *import_func;
2439+
if (PyDict_GetItemStringRef(interp->builtins, "__import__", &import_func) <= 0) {
24412440
return -1;
24422441
}
2443-
IMPORT_FUNC(interp) = Py_NewRef(import_func);
2442+
IMPORT_FUNC(interp) = import_func;
24442443
return 0;
24452444
}
24462445

Python/pylifecycle.c

Lines changed: 22 additions & 17 deletions
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);
1377-
if (value == NULL) {
1378-
if (_PyErr_Occurred(tstate)) {
1379-
PyErr_WriteUnraisable(NULL);
1380-
}
1381-
value = Py_None;
1375+
PyObject *value;
1376+
if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) {
1377+
PyErr_WriteUnraisable(NULL);
1378+
value = Py_NewRef(Py_None);
1379+
} else if (value == NULL) {
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");
@@ -2220,10 +2220,13 @@ add_main_module(PyInterpreterState *interp)
22202220
}
22212221
Py_DECREF(ann_dict);
22222222

2223-
if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
2224-
if (PyErr_Occurred()) {
2225-
return _PyStatus_ERR("Failed to test __main__.__builtins__");
2226-
}
2223+
PyObject *builtins;
2224+
if (PyDict_GetItemStringRef(d, "__builtins__", &builtins) < 0) {
2225+
return _PyStatus_ERR("Failed to test __main__.__builtins__");
2226+
}
2227+
int has_buitlins = (builtins != NULL);
2228+
Py_XDECREF(builtins);
2229+
if (!has_buitlins) {
22272230
PyObject *bimod = PyImport_ImportModule("builtins");
22282231
if (bimod == NULL) {
22292232
return _PyStatus_ERR("Failed to retrieve builtins module");
@@ -2239,11 +2242,13 @@ add_main_module(PyInterpreterState *interp)
22392242
* will be set if __main__ gets further initialized later in the startup
22402243
* process.
22412244
*/
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-
}
2245+
PyObject *loader;
2246+
if (PyDict_GetItemStringRef(d, "__loader__", &loader) < 0) {
2247+
return _PyStatus_ERR("Failed to test __main__.__loader__");
2248+
}
2249+
int has_loader = !(loader == NULL || loader == Py_None);
2250+
Py_XDECREF(loader);
2251+
if (!has_loader) {
22472252
PyObject *loader = _PyImport_GetImportlibLoader(interp,
22482253
"BuiltinImporter");
22492254
if (loader == NULL) {

0 commit comments

Comments
 (0)