summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-10-26 06:43:39 (GMT)
committerGitHub <noreply@github.com>2020-10-26 06:43:39 (GMT)
commitfb5db7ec58624cab0797b4050735be865d380823 (patch)
tree7b0421bb759ba01f0d735296738472faa4ce11b8 /Python/sysmodule.c
parent96a9eed2457c05af6953890d89463704c9d99c57 (diff)
downloadcpython-fb5db7ec58624cab0797b4050735be865d380823.zip
cpython-fb5db7ec58624cab0797b4050735be865d380823.tar.gz
cpython-fb5db7ec58624cab0797b4050735be865d380823.tar.bz2
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.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c62
1 files changed, 38 insertions, 24 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index bfcf4e8..749b964 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -68,7 +68,13 @@ sys_get_object_id(PyThreadState *tstate, _Py_Identifier *key)
if (sd == NULL) {
return NULL;
}
- return _PyDict_GetItemId(sd, key);
+ PyObject *exc_type, *exc_value, *exc_tb;
+ _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
+ PyObject *value = _PyDict_GetItemIdWithError(sd, key);
+ /* XXX Suppress a new exception if it was raised and restore
+ * the old one. */
+ _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
+ return value;
}
PyObject *
@@ -86,26 +92,41 @@ PySys_GetObject(const char *name)
if (sd == NULL) {
return NULL;
}
- return PyDict_GetItemString(sd, name);
+ PyObject *exc_type, *exc_value, *exc_tb;
+ _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
+ PyObject *value = _PyDict_GetItemStringWithError(sd, name);
+ /* XXX Suppress a new exception if it was raised and restore
+ * the old one. */
+ _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
+ return value;
}
static int
-sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v)
+sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v)
{
+ if (key == NULL) {
+ return -1;
+ }
PyObject *sd = tstate->interp->sysdict;
if (v == NULL) {
- if (_PyDict_GetItemId(sd, key) == NULL) {
- return 0;
- }
- else {
- return _PyDict_DelItemId(sd, key);
+ v = _PyDict_Pop(sd, key, Py_None);
+ if (v == NULL) {
+ return -1;
}
+ Py_DECREF(v);
+ return 0;
}
else {
- return _PyDict_SetItemId(sd, key, v);
+ return PyDict_SetItem(sd, key, v);
}
}
+static int
+sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v)
+{
+ return sys_set_object(tstate, _PyUnicode_FromId(key), v);
+}
+
int
_PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
{
@@ -114,27 +135,20 @@ _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
}
static int
-sys_set_object(PyThreadState *tstate, const char *name, PyObject *v)
+sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v)
{
- PyObject *sd = tstate->interp->sysdict;
- if (v == NULL) {
- if (PyDict_GetItemString(sd, name) == NULL) {
- return 0;
- }
- else {
- return PyDict_DelItemString(sd, name);
- }
- }
- else {
- return PyDict_SetItemString(sd, name, v);
- }
+ PyObject *key = v ? PyUnicode_InternFromString(name)
+ : PyUnicode_FromString(name);
+ int r = sys_set_object(tstate, key, v);
+ Py_XDECREF(key);
+ return r;
}
int
PySys_SetObject(const char *name, PyObject *v)
{
PyThreadState *tstate = _PyThreadState_GET();
- return sys_set_object(tstate, name, v);
+ return sys_set_object_str(tstate, name, v);
}
@@ -3083,7 +3097,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
if (av == NULL) {
Py_FatalError("no mem for sys.argv");
}
- if (sys_set_object(tstate, "argv", av) != 0) {
+ if (sys_set_object_str(tstate, "argv", av) != 0) {
Py_DECREF(av);
Py_FatalError("can't assign sys.argv");
}