mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-42006: Stop using PyDict_GetItem, PyDict_GetItemString and _PyDict_GetItemId. (GH-22648)
These functions are considered not safe because they suppress all internal errors and can return wrong result. PyDict_GetItemString and _PyDict_GetItemId can also silence current exception in rare cases. Remove no longer used _PyDict_GetItemId. Add _PyDict_ContainsId and rename _PyDict_Contains into _PyDict_Contains_KnownHash.
This commit is contained in:
parent
96a9eed245
commit
fb5db7ec58
17 changed files with 259 additions and 142 deletions
|
@ -160,7 +160,7 @@ init_importlib(PyThreadState *tstate, PyObject *sysmod)
|
|||
interp->importlib = importlib;
|
||||
Py_INCREF(interp->importlib);
|
||||
|
||||
interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
|
||||
interp->import_func = _PyDict_GetItemStringWithError(interp->builtins, "__import__");
|
||||
if (interp->import_func == NULL)
|
||||
return _PyStatus_ERR("__import__ not found");
|
||||
Py_INCREF(interp->import_func);
|
||||
|
@ -1683,7 +1683,10 @@ add_main_module(PyInterpreterState *interp)
|
|||
}
|
||||
Py_DECREF(ann_dict);
|
||||
|
||||
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
||||
if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
|
||||
if (PyErr_Occurred()) {
|
||||
return _PyStatus_ERR("Failed to test __main__.__builtins__");
|
||||
}
|
||||
PyObject *bimod = PyImport_ImportModule("builtins");
|
||||
if (bimod == NULL) {
|
||||
return _PyStatus_ERR("Failed to retrieve builtins module");
|
||||
|
@ -1700,8 +1703,11 @@ add_main_module(PyInterpreterState *interp)
|
|||
* be set if __main__ gets further initialized later in the startup
|
||||
* process.
|
||||
*/
|
||||
loader = PyDict_GetItemString(d, "__loader__");
|
||||
loader = _PyDict_GetItemStringWithError(d, "__loader__");
|
||||
if (loader == NULL || loader == Py_None) {
|
||||
if (PyErr_Occurred()) {
|
||||
return _PyStatus_ERR("Failed to test __main__.__loader__");
|
||||
}
|
||||
PyObject *loader = PyObject_GetAttrString(interp->importlib,
|
||||
"BuiltinImporter");
|
||||
if (loader == NULL) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue