diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-10-26 06:43:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-26 06:43:39 (GMT) |
commit | fb5db7ec58624cab0797b4050735be865d380823 (patch) | |
tree | 7b0421bb759ba01f0d735296738472faa4ce11b8 | |
parent | 96a9eed2457c05af6953890d89463704c9d99c57 (diff) | |
download | cpython-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.
-rw-r--r-- | Include/cpython/dictobject.h | 4 | ||||
-rw-r--r-- | Modules/_decimal/_decimal.c | 47 | ||||
-rw-r--r-- | Modules/_threadmodule.c | 8 | ||||
-rw-r--r-- | Modules/_zoneinfo.c | 18 | ||||
-rw-r--r-- | Modules/signalmodule.c | 3 | ||||
-rw-r--r-- | Modules/socketmodule.c | 29 | ||||
-rw-r--r-- | Objects/dictobject.c | 24 | ||||
-rw-r--r-- | Objects/moduleobject.c | 28 | ||||
-rw-r--r-- | Objects/setobject.c | 2 | ||||
-rw-r--r-- | Objects/structseq.c | 75 | ||||
-rw-r--r-- | Objects/typeobject.c | 40 | ||||
-rw-r--r-- | Python/ceval.c | 11 | ||||
-rw-r--r-- | Python/compile.c | 6 | ||||
-rw-r--r-- | Python/pylifecycle.c | 12 | ||||
-rw-r--r-- | Python/pythonrun.c | 13 | ||||
-rw-r--r-- | Python/symtable.c | 9 | ||||
-rw-r--r-- | Python/sysmodule.c | 62 |
17 files changed, 254 insertions, 137 deletions
diff --git a/Include/cpython/dictobject.h b/Include/cpython/dictobject.h index f67c321..6822a65 100644 --- a/Include/cpython/dictobject.h +++ b/Include/cpython/dictobject.h @@ -46,7 +46,8 @@ PyAPI_FUNC(int) _PyDict_Next( /* Get the number of items of a dictionary. */ #define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used) -PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash); +PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t); +PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, struct _Py_Identifier *); PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused); PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp); PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp); @@ -63,7 +64,6 @@ PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *); argument is raised. */ PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override); -PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key); PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item); PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key); diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index e7c44ac..ea16c5a 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -3186,6 +3186,31 @@ dotsep_as_utf8(const char *s) return utf8; } +static int +dict_get_item_string(PyObject *dict, const char *key, PyObject **valueobj, const char **valuestr) +{ + *valueobj = NULL; + PyObject *keyobj = PyUnicode_FromString(key); + if (keyobj == NULL) { + return -1; + } + PyObject *value = PyDict_GetItemWithError(dict, keyobj); + Py_DECREF(keyobj); + if (value == NULL) { + if (PyErr_Occurred()) { + return -1; + } + return 0; + } + value = PyUnicode_AsUTF8String(value); + if (value == NULL) { + return -1; + } + *valueobj = value; + *valuestr = PyBytes_AS_STRING(value); + return 0; +} + /* Formatted representation of a PyDecObject. */ static PyObject * dec_format(PyObject *dec, PyObject *args) @@ -3256,23 +3281,11 @@ dec_format(PyObject *dec, PyObject *args) "optional argument must be a dict"); goto finish; } - if ((dot = PyDict_GetItemString(override, "decimal_point"))) { - if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) { - goto finish; - } - spec.dot = PyBytes_AS_STRING(dot); - } - if ((sep = PyDict_GetItemString(override, "thousands_sep"))) { - if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { - goto finish; - } - spec.sep = PyBytes_AS_STRING(sep); - } - if ((grouping = PyDict_GetItemString(override, "grouping"))) { - if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) { - goto finish; - } - spec.grouping = PyBytes_AS_STRING(grouping); + if (dict_get_item_string(override, "decimal_point", &dot, &spec.dot) || + dict_get_item_string(override, "thousands_sep", &sep, &spec.sep) || + dict_get_item_string(override, "grouping", &grouping, &spec.grouping)) + { + goto finish; } if (mpd_validate_lconv(&spec) < 0) { PyErr_SetString(PyExc_ValueError, diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 77baba4..56ed8a2 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -816,10 +816,14 @@ local_clear(localobject *self) for(tstate = PyInterpreterState_ThreadHead(tstate->interp); tstate; tstate = PyThreadState_Next(tstate)) - if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) { - if (PyDict_DelItem(tstate->dict, self->key)) { + if (tstate->dict) { + PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None); + if (v == NULL) { PyErr_Clear(); } + else { + Py_DECREF(v); + } } } return 0; diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c index bee59b8..76b667d 100644 --- a/Modules/_zoneinfo.c +++ b/Modules/_zoneinfo.c @@ -722,17 +722,16 @@ zoneinfo__unpickle(PyTypeObject *cls, PyObject *args) static PyObject * load_timedelta(long seconds) { - PyObject *rv = NULL; + PyObject *rv; PyObject *pyoffset = PyLong_FromLong(seconds); if (pyoffset == NULL) { return NULL; } - int contains = PyDict_Contains(TIMEDELTA_CACHE, pyoffset); - if (contains == -1) { - goto error; - } - - if (!contains) { + rv = PyDict_GetItemWithError(TIMEDELTA_CACHE, pyoffset); + if (rv == NULL) { + if (PyErr_Occurred()) { + goto error; + } PyObject *tmp = PyDateTimeAPI->Delta_FromDelta( 0, seconds, 0, 1, PyDateTimeAPI->DeltaType); @@ -743,12 +742,9 @@ load_timedelta(long seconds) rv = PyDict_SetDefault(TIMEDELTA_CACHE, pyoffset, tmp); Py_DECREF(tmp); } - else { - rv = PyDict_GetItem(TIMEDELTA_CACHE, pyoffset); - } + Py_XINCREF(rv); Py_DECREF(pyoffset); - Py_INCREF(rv); return rv; error: Py_DECREF(pyoffset); diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 3440894..0ab3a71 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1427,10 +1427,9 @@ signal_exec(PyObject *m) return -1; #endif - IntHandler = PyDict_GetItemString(d, "default_int_handler"); + IntHandler = PyMapping_GetItemString(d, "default_int_handler"); if (!IntHandler) return -1; - Py_INCREF(IntHandler); _Py_atomic_store_relaxed(&Handlers[0].tripped, 0); for (int i = 1; i < NSIG; i++) { diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index db0eeaa..d773836 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -324,7 +324,7 @@ static FlagRuntimeInfo win_runtime_flags[] = { {14393, "TCP_FASTOPEN"} }; -static void +static int remove_unusable_flags(PyObject *m) { PyObject *dict; @@ -333,7 +333,7 @@ remove_unusable_flags(PyObject *m) dict = PyModule_GetDict(m); if (dict == NULL) { - return; + return -1; } /* set to Windows 10, except BuildNumber. */ @@ -359,19 +359,19 @@ remove_unusable_flags(PyObject *m) break; } else { - if (PyDict_GetItemString( - dict, - win_runtime_flags[i].flag_name) != NULL) - { - if (PyDict_DelItemString( - dict, - win_runtime_flags[i].flag_name)) - { - PyErr_Clear(); - } + PyObject *flag_name = PyUnicode_FromString(win_runtime_flags[i].flag_name); + if (flag_name == NULL) { + return -1; + } + PyObject *v = _PyDict_Pop(dict, flag_name, Py_None); + Py_DECREF(flag_name); + if (v == NULL) { + return -1; } + Py_DECREF(v); } } + return 0; } #endif @@ -8382,7 +8382,10 @@ PyInit__socket(void) #ifdef MS_WINDOWS /* remove some flags on older version Windows during run-time */ - remove_unusable_flags(m); + if (remove_unusable_flags(m) < 0) { + Py_DECREF(m); + return NULL; + } #endif return m; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 42d71e5..00d6ab3 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3434,7 +3434,7 @@ PyDict_Contains(PyObject *op, PyObject *key) /* Internal version of PyDict_Contains used when the hash value is already known */ int -_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) +_PyDict_Contains_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash) { PyDictObject *mp = (PyDictObject *)op; PyObject *value; @@ -3446,6 +3446,16 @@ _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) return (ix != DKIX_EMPTY && value != NULL); } +int +_PyDict_ContainsId(PyObject *op, struct _Py_Identifier *key) +{ + PyObject *kv = _PyUnicode_FromId(key); /* borrowed */ + if (kv == NULL) { + return -1; + } + return PyDict_Contains(op, kv); +} + /* Hack to implement "key in dict" */ static PySequenceMethods dict_as_sequence = { 0, /* sq_length */ @@ -3590,18 +3600,6 @@ PyTypeObject PyDict_Type = { .tp_vectorcall = dict_vectorcall, }; -PyObject * -_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key) -{ - PyObject *kv; - kv = _PyUnicode_FromId(key); /* borrowed */ - if (kv == NULL) { - PyErr_Clear(); - return NULL; - } - return PyDict_GetItem(dp, kv); -} - /* For backward compatibility with old dictionary interface */ PyObject * diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index ee4ed97..c3ceb78 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -477,10 +477,12 @@ PyModule_GetNameObject(PyObject *m) } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || - (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL || + (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL || !PyUnicode_Check(name)) { - PyErr_SetString(PyExc_SystemError, "nameless module"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_SystemError, "nameless module"); + } return NULL; } Py_INCREF(name); @@ -509,10 +511,12 @@ PyModule_GetFilenameObject(PyObject *m) } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || - (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL || + (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL || !PyUnicode_Check(fileobj)) { - PyErr_SetString(PyExc_SystemError, "module filename missing"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_SystemError, "module filename missing"); + } return NULL; } Py_INCREF(fileobj); @@ -721,14 +725,21 @@ module_getattro(PyModuleObject *m, PyObject *name) PyErr_Clear(); if (m->md_dict) { _Py_IDENTIFIER(__getattr__); - getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__); + getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__); if (getattr) { return PyObject_CallOneArg(getattr, name); } - mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); + if (PyErr_Occurred()) { + return NULL; + } + mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__); if (mod_name && PyUnicode_Check(mod_name)) { Py_INCREF(mod_name); - PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__); + PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__); + if (spec == NULL && PyErr_Occurred()) { + Py_DECREF(mod_name); + return NULL; + } Py_XINCREF(spec); if (_PyModuleSpec_IsInitializing(spec)) { PyErr_Format(PyExc_AttributeError, @@ -746,6 +757,9 @@ module_getattro(PyModuleObject *m, PyObject *name) Py_DECREF(mod_name); return NULL; } + else if (PyErr_Occurred()) { + return NULL; + } } PyErr_Format(PyExc_AttributeError, "module has no attribute '%U'", name); diff --git a/Objects/setobject.c b/Objects/setobject.c index b271149..af8ee03 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1498,7 +1498,7 @@ set_difference(PySetObject *so, PyObject *other) while (set_next(so, &pos, &entry)) { key = entry->key; hash = entry->hash; - rv = _PyDict_Contains(other, key, hash); + rv = _PyDict_Contains_KnownHash(other, key, hash); if (rv < 0) { Py_DECREF(result); return NULL; diff --git a/Objects/structseq.c b/Objects/structseq.c index 8ae8f28..5caa3bd 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -19,20 +19,33 @@ static const char unnamed_fields_key[] = "n_unnamed_fields"; /* Fields with this name have only a field index, not a field name. They are only allowed for indices < n_visible_fields. */ const char * const PyStructSequence_UnnamedField = "unnamed field"; + _Py_IDENTIFIER(n_sequence_fields); _Py_IDENTIFIER(n_fields); _Py_IDENTIFIER(n_unnamed_fields); -#define VISIBLE_SIZE(op) Py_SIZE(op) -#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \ - _PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields)) +static ssize_t +get_type_attr_as_size(PyTypeObject *tp, _Py_Identifier *id) +{ + PyObject *name = _PyUnicode_FromId(id); + if (name == NULL) { + return -1; + } + PyObject *v = PyDict_GetItemWithError(tp->tp_dict, name); + if (v == NULL && !PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + "Missed attribute '%U' of type %s", + name, tp->tp_name); + } + return PyLong_AsSsize_t(v); +} -#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \ - _PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields)) +#define VISIBLE_SIZE(op) Py_SIZE(op) +#define VISIBLE_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_sequence_fields) +#define REAL_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_fields) #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) -#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \ - _PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields)) +#define UNNAMED_FIELDS_TP(tp) get_type_attr_as_size(tp, &PyId_n_unnamed_fields) #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) @@ -41,13 +54,20 @@ PyStructSequence_New(PyTypeObject *type) { PyStructSequence *obj; Py_ssize_t size = REAL_SIZE_TP(type), i; + if (size < 0) { + return NULL; + } + Py_ssize_t vsize = VISIBLE_SIZE_TP(type); + if (vsize < 0) { + return NULL; + } obj = PyObject_GC_NewVar(PyStructSequence, type, size); if (obj == NULL) return NULL; /* Hack the size of the variable object, so invisible fields don't appear to Python code. */ - Py_SET_SIZE(obj, VISIBLE_SIZE_TP(type)); + Py_SET_SIZE(obj, vsize); for (i = 0; i < size; i++) obj->ob_item[i] = NULL; @@ -121,6 +141,19 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) PyStructSequence *res = NULL; Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; + min_len = VISIBLE_SIZE_TP(type); + if (min_len < 0) { + return NULL; + } + max_len = REAL_SIZE_TP(type); + if (max_len < 0) { + return NULL; + } + n_unnamed_fields = UNNAMED_FIELDS_TP(type); + if (n_unnamed_fields < 0) { + return NULL; + } + arg = PySequence_Fast(arg, "constructor requires a sequence"); if (!arg) { @@ -136,10 +169,6 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) } len = PySequence_Fast_GET_SIZE(arg); - min_len = VISIBLE_SIZE_TP(type); - max_len = REAL_SIZE_TP(type); - n_unnamed_fields = UNNAMED_FIELDS_TP(type); - if (min_len != max_len) { if (len < min_len) { PyErr_Format(PyExc_TypeError, @@ -177,18 +206,26 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) Py_INCREF(v); res->ob_item[i] = v; } + Py_DECREF(arg); for (; i < max_len; ++i) { - if (dict && (ob = PyDict_GetItemString( - dict, type->tp_members[i-n_unnamed_fields].name))) { + if (dict == NULL) { + ob = Py_None; } else { - ob = Py_None; + ob = _PyDict_GetItemStringWithError(dict, + type->tp_members[i-n_unnamed_fields].name); + if (ob == NULL) { + if (PyErr_Occurred()) { + Py_DECREF(res); + return NULL; + } + ob = Py_None; + } } Py_INCREF(ob); res->ob_item[i] = ob; } - Py_DECREF(arg); _PyObject_GC_TRACK(res); return (PyObject*) res; } @@ -288,8 +325,14 @@ structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored)) Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i; n_fields = REAL_SIZE(self); + if (n_fields < 0) { + return NULL; + } n_visible_fields = VISIBLE_SIZE(self); n_unnamed_fields = UNNAMED_FIELDS(self); + if (n_unnamed_fields < 0) { + return NULL; + } tup = _PyTuple_FromArray(self->ob_item, n_visible_fields); if (!tup) goto error; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 36c7662..6626169 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4767,8 +4767,11 @@ object___reduce_ex___impl(PyObject *self, int protocol) _Py_IDENTIFIER(__reduce__); if (objreduce == NULL) { - objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, - &PyId___reduce__); + objreduce = _PyDict_GetItemIdWithError(PyBaseObject_Type.tp_dict, + &PyId___reduce__); + if (objreduce == NULL && PyErr_Occurred()) { + return NULL; + } } if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) { @@ -5181,14 +5184,14 @@ overrides_hash(PyTypeObject *type) _Py_IDENTIFIER(__eq__); assert(dict != NULL); - if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) - return 1; - if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) - return 1; - return 0; + int r = _PyDict_ContainsId(dict, &PyId___eq__); + if (r == 0) { + r = _PyDict_ContainsId(dict, &PyId___hash__); + } + return r; } -static void +static int inherit_slots(PyTypeObject *type, PyTypeObject *base) { PyTypeObject *basebase; @@ -5331,11 +5334,16 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base) /* Copy comparison-related slots only when not overriding them anywhere */ if (type->tp_richcompare == NULL && - type->tp_hash == NULL && - !overrides_hash(type)) + type->tp_hash == NULL) { - type->tp_richcompare = base->tp_richcompare; - type->tp_hash = base->tp_hash; + int r = overrides_hash(type); + if (r < 0) { + return -1; + } + if (!r) { + type->tp_richcompare = base->tp_richcompare; + type->tp_hash = base->tp_hash; + } } } { @@ -5378,6 +5386,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base) * obvious to be done -- the type is on its own. */ } + return 0; } static int add_operators(PyTypeObject *); @@ -5507,8 +5516,11 @@ PyType_Ready(PyTypeObject *type) n = PyTuple_GET_SIZE(bases); for (i = 1; i < n; i++) { PyObject *b = PyTuple_GET_ITEM(bases, i); - if (PyType_Check(b)) - inherit_slots(type, (PyTypeObject *)b); + if (PyType_Check(b)) { + if (inherit_slots(type, (PyTypeObject *)b) < 0) { + goto error; + } + } } /* All bases of statically allocated type should be statically allocated */ diff --git a/Python/ceval.c b/Python/ceval.c index fafbf75..7338be5 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1446,11 +1446,18 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag) } #ifdef LLTRACE - lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; + { + int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__); + if (r < 0) { + goto exit_eval_frame; + } + lltrace = r; + } #endif - if (throwflag) /* support for generator.throw() */ + if (throwflag) { /* support for generator.throw() */ goto error; + } #ifdef Py_DEBUG /* _PyEval_EvalFrameDefault() must not be called with an exception set, diff --git a/Python/compile.c b/Python/compile.c index ddd2a04..a0089b2 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -489,8 +489,8 @@ dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) /* XXX this should probably be a macro in symtable.h */ long vi; k = PyList_GET_ITEM(sorted_keys, key_i); - v = PyDict_GetItem(src, k); - assert(PyLong_Check(v)); + v = PyDict_GetItemWithError(src, k); + assert(v && PyLong_Check(v)); vi = PyLong_AS_LONG(v); scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; @@ -1889,7 +1889,7 @@ static int compiler_lookup_arg(PyObject *dict, PyObject *name) { PyObject *v; - v = PyDict_GetItem(dict, name); + v = PyDict_GetItemWithError(dict, name); if (v == NULL) return -1; return PyLong_AS_LONG(v); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 75d5780..774a4f9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -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) { diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a45ca3b..bd49c40 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -351,7 +351,10 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, return -1; Py_INCREF(m); d = PyModule_GetDict(m); - if (PyDict_GetItemString(d, "__file__") == NULL) { + if (_PyDict_GetItemStringWithError(d, "__file__") == NULL) { + if (PyErr_Occurred()) { + goto done; + } PyObject *f; f = PyUnicode_DecodeFSDefault(filename); if (f == NULL) @@ -1116,9 +1119,11 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py _Py_UnhandledKeyboardInterrupt = 0; /* Set globals['__builtins__'] if it doesn't exist */ - if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) { - if (PyDict_SetItemString(globals, "__builtins__", - tstate->interp->builtins) < 0) { + if (globals != NULL && _PyDict_GetItemStringWithError(globals, "__builtins__") == NULL) { + if (PyErr_Occurred() || + PyDict_SetItemString(globals, "__builtins__", + tstate->interp->builtins) < 0) + { return NULL; } } diff --git a/Python/symtable.c b/Python/symtable.c index 4a98e79..0464cd8 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -392,7 +392,7 @@ PySymtable_Lookup(struct symtable *st, void *key) static long _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name) { - PyObject *v = PyDict_GetItem(ste->ste_symbols, name); + PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name); if (!v) return 0; assert(PyLong_Check(v)); @@ -634,7 +634,7 @@ update_symbols(PyObject *symbols, PyObject *scopes, long scope, flags; assert(PyLong_Check(v)); flags = PyLong_AS_LONG(v); - v_scope = PyDict_GetItem(scopes, name); + v_scope = PyDict_GetItemWithError(scopes, name); assert(v_scope && PyLong_Check(v_scope)); scope = PyLong_AS_LONG(v_scope); flags |= (scope << SCOPE_OFFSET); @@ -1071,9 +1071,12 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s /* XXX need to update DEF_GLOBAL for other flags too; perhaps only DEF_FREE_GLOBAL */ val = flag; - if ((o = PyDict_GetItem(st->st_global, mangled))) { + if ((o = PyDict_GetItemWithError(st->st_global, mangled))) { val |= PyLong_AS_LONG(o); } + else if (PyErr_Occurred()) { + goto error; + } o = PyLong_FromLong(val); if (o == NULL) goto error; 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"); } |