diff options
author | Victor Stinner <vstinner@python.org> | 2023-08-24 13:59:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 13:59:12 (GMT) |
commit | 67266266469fe0e817736227f39537182534c1a5 (patch) | |
tree | 1ae99ebf16335cbd67678f3911702e9819cbe039 /Python | |
parent | c163d7f0b67a568e9b64eeb9c1cbbaa127818596 (diff) | |
download | cpython-67266266469fe0e817736227f39537182534c1a5.zip cpython-67266266469fe0e817736227f39537182534c1a5.tar.gz cpython-67266266469fe0e817736227f39537182534c1a5.tar.bz2 |
gh-108314: Add PyDict_ContainsString() function (#108323)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pylifecycle.c | 9 | ||||
-rw-r--r-- | Python/pythonrun.c | 23 |
2 files changed, 19 insertions, 13 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 1861426..7d362af 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2220,10 +2220,11 @@ add_main_module(PyInterpreterState *interp) } Py_DECREF(ann_dict); - if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) { - if (PyErr_Occurred()) { - return _PyStatus_ERR("Failed to test __main__.__builtins__"); - } + int has_builtins = PyDict_ContainsString(d, "__builtins__"); + if (has_builtins < 0) { + return _PyStatus_ERR("Failed to test __main__.__builtins__"); + } + if (!has_builtins) { PyObject *bimod = PyImport_ImportModule("builtins"); if (bimod == NULL) { return _PyStatus_ERR("Failed to retrieve builtins module"); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 05b7dfa..0e118b0 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -413,10 +413,11 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit, PyObject *dict = PyModule_GetDict(main_module); // borrowed ref int set_file_name = 0; - if (_PyDict_GetItemStringWithError(dict, "__file__") == NULL) { - if (PyErr_Occurred()) { - goto done; - } + int has_file = PyDict_ContainsString(dict, "__file__"); + if (has_file < 0) { + goto done; + } + if (!has_file) { if (PyDict_SetItemString(dict, "__file__", filename) < 0) { goto done; } @@ -1713,13 +1714,17 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py _PyRuntime.signals.unhandled_keyboard_interrupt = 0; /* Set globals['__builtins__'] if it doesn't exist */ - if (globals != NULL && _PyDict_GetItemStringWithError(globals, "__builtins__") == NULL) { - if (PyErr_Occurred() || - PyDict_SetItemString(globals, "__builtins__", - tstate->interp->builtins) < 0) - { + if (globals != NULL) { + int has_builtins = PyDict_ContainsString(globals, "__builtins__"); + if (has_builtins < 0) { return NULL; } + if (!has_builtins) { + if (PyDict_SetItemString(globals, "__builtins__", + tstate->interp->builtins) < 0) { + return NULL; + } + } } v = PyEval_EvalCode((PyObject*)co, globals, locals); |