diff options
-rw-r--r-- | Include/cpython/dictobject.h | 1 | ||||
-rw-r--r-- | Modules/_csv.c | 10 | ||||
-rw-r--r-- | Modules/_elementtree.c | 20 | ||||
-rw-r--r-- | Modules/_json.c | 5 | ||||
-rw-r--r-- | Modules/_sre.c | 58 | ||||
-rw-r--r-- | Modules/_struct.c | 5 | ||||
-rw-r--r-- | Modules/_testmultiphase.c | 7 | ||||
-rw-r--r-- | Modules/_threadmodule.c | 26 | ||||
-rw-r--r-- | Modules/itertoolsmodule.c | 14 | ||||
-rw-r--r-- | Modules/main.c | 6 | ||||
-rw-r--r-- | Modules/posixmodule.c | 3 | ||||
-rw-r--r-- | Modules/pyexpat.c | 30 | ||||
-rw-r--r-- | Modules/selectmodule.c | 8 | ||||
-rw-r--r-- | Modules/xxlimited.c | 7 | ||||
-rw-r--r-- | Modules/xxmodule.c | 7 | ||||
-rw-r--r-- | Objects/dictobject.c | 43 | ||||
-rw-r--r-- | Objects/exceptions.c | 2 | ||||
-rw-r--r-- | Objects/frameobject.c | 5 | ||||
-rw-r--r-- | Objects/funcobject.c | 6 | ||||
-rw-r--r-- | Objects/moduleobject.c | 5 | ||||
-rw-r--r-- | Objects/namespaceobject.c | 7 | ||||
-rw-r--r-- | Objects/object.c | 31 | ||||
-rw-r--r-- | Objects/typeobject.c | 234 | ||||
-rw-r--r-- | Python/_warnings.c | 21 | ||||
-rw-r--r-- | Python/bltinmodule.c | 17 | ||||
-rw-r--r-- | Python/ceval.c | 76 | ||||
-rw-r--r-- | Python/codecs.c | 16 | ||||
-rw-r--r-- | Python/errors.c | 8 | ||||
-rw-r--r-- | Python/getargs.c | 41 | ||||
-rw-r--r-- | Python/import.c | 43 | ||||
-rw-r--r-- | Python/symtable.c | 18 |
31 files changed, 538 insertions, 242 deletions
diff --git a/Include/cpython/dictobject.h b/Include/cpython/dictobject.h index 4c7457c..64c012a 100644 --- a/Include/cpython/dictobject.h +++ b/Include/cpython/dictobject.h @@ -35,6 +35,7 @@ PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key, Py_hash_t hash); PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key); +PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *); PyAPI_FUNC(PyObject *) PyDict_SetDefault( PyObject *mp, PyObject *key, PyObject *defaultobj); PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key, diff --git a/Modules/_csv.c b/Modules/_csv.c index 6c00974..d86f63e 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -132,7 +132,7 @@ get_dialect_from_registry(PyObject * name_obj) { PyObject *dialect_obj; - dialect_obj = PyDict_GetItem(_csvstate_global->dialects, name_obj); + dialect_obj = PyDict_GetItemWithError(_csvstate_global->dialects, name_obj); if (dialect_obj == NULL) { if (!PyErr_Occurred()) PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); @@ -1434,8 +1434,12 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs) static PyObject * csv_unregister_dialect(PyObject *module, PyObject *name_obj) { - if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0) - return PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); + if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0) { + if (PyErr_ExceptionMatches(PyExc_KeyError)) { + PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); + } + return NULL; + } Py_RETURN_NONE; } diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index b1a9629..1e58cd0 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -339,7 +339,7 @@ get_attrib_from_keywords(PyObject *kwds) if (attrib_str == NULL) { return NULL; } - PyObject *attrib = PyDict_GetItem(kwds, attrib_str); + PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str); if (attrib) { /* If attrib was found in kwds, copy its value and remove it from @@ -356,7 +356,8 @@ get_attrib_from_keywords(PyObject *kwds) Py_DECREF(attrib); attrib = NULL; } - } else { + } + else if (!PyErr_Occurred()) { attrib = PyDict_New(); } @@ -1393,9 +1394,13 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key, if (!self->extra || self->extra->attrib == Py_None) value = default_value; else { - value = PyDict_GetItem(self->extra->attrib, key); - if (!value) + value = PyDict_GetItemWithError(self->extra->attrib, key); + if (!value) { + if (PyErr_Occurred()) { + return NULL; + } value = default_value; + } } Py_INCREF(value); @@ -2848,11 +2853,12 @@ makeuniversal(XMLParserObject* self, const char* string) if (!key) return NULL; - value = PyDict_GetItem(self->names, key); + value = PyDict_GetItemWithError(self->names, key); if (value) { Py_INCREF(value); - } else { + } + else if (!PyErr_Occurred()) { /* new name. convert to universal name, and decode as necessary */ @@ -2974,7 +2980,7 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in, if (!key) return; - value = PyDict_GetItem(self->entity, key); + value = PyDict_GetItemWithError(self->entity, key); if (value) { if (TreeBuilder_CheckExact(self->target)) diff --git a/Modules/_json.c b/Modules/_json.c index 53e1e88..94a7c0d 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -746,12 +746,15 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss key = scanstring_unicode(pystr, idx + 1, s->strict, &next_idx); if (key == NULL) goto bail; - memokey = PyDict_GetItem(s->memo, key); + memokey = PyDict_GetItemWithError(s->memo, key); if (memokey != NULL) { Py_INCREF(memokey); Py_DECREF(key); key = memokey; } + else if (PyErr_Occurred()) { + goto bail; + } else { if (PyDict_SetItem(s->memo, key, key) < 0) goto bail; diff --git a/Modules/_sre.c b/Modules/_sre.c index 21c41b5..5cea756 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1899,15 +1899,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def) void* ptr; Py_ssize_t i, j; - if (index < 0 || index >= self->groups) { - /* raise IndexError if we were given a bad group number */ - PyErr_SetString( - PyExc_IndexError, - "no such group" - ); - return NULL; - } - + assert(0 <= index && index < self->groups); index *= 2; if (self->string == Py_None || self->mark[index] < 0) { @@ -1940,17 +1932,25 @@ match_getindex(MatchObject* self, PyObject* index) return 0; if (PyIndex_Check(index)) { - return PyNumber_AsSsize_t(index, NULL); + i = PyNumber_AsSsize_t(index, NULL); } + else { + i = -1; - i = -1; - - if (self->pattern->groupindex) { - index = PyDict_GetItem(self->pattern->groupindex, index); - if (index && PyLong_Check(index)) { - i = PyLong_AsSsize_t(index); + if (self->pattern->groupindex) { + index = PyDict_GetItemWithError(self->pattern->groupindex, index); + if (index && PyLong_Check(index)) { + i = PyLong_AsSsize_t(index); + } } } + if (i < 0 || i >= self->groups) { + /* raise IndexError if we were given a bad group number */ + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_IndexError, "no such group"); + } + return -1; + } return i; } @@ -1958,7 +1958,13 @@ match_getindex(MatchObject* self, PyObject* index) static PyObject* match_getslice(MatchObject* self, PyObject* index, PyObject* def) { - return match_getslice_by_index(self, match_getindex(self, index), def); + Py_ssize_t i = match_getindex(self, index); + + if (i < 0) { + return NULL; + } + + return match_getslice_by_index(self, i, def); } /*[clinic input] @@ -2114,11 +2120,7 @@ _sre_SRE_Match_start_impl(MatchObject *self, PyObject *group) { Py_ssize_t index = match_getindex(self, group); - if (index < 0 || index >= self->groups) { - PyErr_SetString( - PyExc_IndexError, - "no such group" - ); + if (index < 0) { return -1; } @@ -2141,11 +2143,7 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group) { Py_ssize_t index = match_getindex(self, group); - if (index < 0 || index >= self->groups) { - PyErr_SetString( - PyExc_IndexError, - "no such group" - ); + if (index < 0) { return -1; } @@ -2195,11 +2193,7 @@ _sre_SRE_Match_span_impl(MatchObject *self, PyObject *group) { Py_ssize_t index = match_getindex(self, group); - if (index < 0 || index >= self->groups) { - PyErr_SetString( - PyExc_IndexError, - "no such group" - ); + if (index < 0) { return NULL; } diff --git a/Modules/_struct.c b/Modules/_struct.c index 5954c13..90839b2 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -2088,12 +2088,15 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr) return 0; } - s_object = PyDict_GetItem(cache, fmt); + s_object = PyDict_GetItemWithError(cache, fmt); if (s_object != NULL) { Py_INCREF(s_object); *ptr = (PyStructObject *)s_object; return Py_CLEANUP_SUPPORTED; } + else if (PyErr_Occurred()) { + return 0; + } s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); if (s_object != NULL) { diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index c6dfc2f..db5bb7d 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -53,11 +53,14 @@ static PyObject * Example_getattro(ExampleObject *self, PyObject *name) { if (self->x_attr != NULL) { - PyObject *v = PyDict_GetItem(self->x_attr, name); + PyObject *v = PyDict_GetItemWithError(self->x_attr, name); if (v != NULL) { Py_INCREF(v); return v; } + else if (PyErr_Occurred()) { + return NULL; + } } return PyObject_GenericGetAttr((PyObject *)self, name); } @@ -72,7 +75,7 @@ Example_setattr(ExampleObject *self, const char *name, PyObject *v) } if (v == NULL) { int rv = PyDict_DelItemString(self->x_attr, name); - if (rv < 0) + if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) PyErr_SetString(PyExc_AttributeError, "delete non-existing Example attribute"); return rv; diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index aacce69..73babaf 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -814,8 +814,11 @@ _ldict(localobject *self) return NULL; } - dummy = PyDict_GetItem(tdict, self->key); + dummy = PyDict_GetItemWithError(tdict, self->key); if (dummy == NULL) { + if (PyErr_Occurred()) { + return NULL; + } ldict = _local_create_dummy(self); if (ldict == NULL) return NULL; @@ -931,14 +934,17 @@ local_getattro(localobject *self, PyObject *name) (PyObject *)self, name, ldict, 0); /* Optimization: just look in dict ourselves */ - value = PyDict_GetItem(ldict, name); - if (value == NULL) - /* Fall back on generic to get __class__ and __dict__ */ - return _PyObject_GenericGetAttrWithDict( - (PyObject *)self, name, ldict, 0); - - Py_INCREF(value); - return value; + value = PyDict_GetItemWithError(ldict, name); + if (value != NULL) { + Py_INCREF(value); + return value; + } + else if (PyErr_Occurred()) { + return NULL; + } + /* Fall back on generic to get __class__ and __dict__ */ + return _PyObject_GenericGetAttrWithDict( + (PyObject *)self, name, ldict, 0); } /* Called when a dummy is destroyed. */ @@ -958,7 +964,7 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) self = (localobject *) obj; if (self->dummies != NULL) { PyObject *ldict; - ldict = PyDict_GetItem(self->dummies, dummyweakref); + ldict = PyDict_GetItemWithError(self->dummies, dummyweakref); if (ldict != NULL) { PyDict_DelItem(self->dummies, dummyweakref); } diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 581ae2c..c589dd1 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4418,6 +4418,7 @@ static PyTypeObject ziplongest_type; static PyObject * zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + _Py_IDENTIFIER(fillvalue); ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ @@ -4426,10 +4427,15 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_ssize_t tuplesize; if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { - fillvalue = PyDict_GetItemString(kwds, "fillvalue"); - if (fillvalue == NULL || PyDict_GET_SIZE(kwds) > 1) { - PyErr_SetString(PyExc_TypeError, - "zip_longest() got an unexpected keyword argument"); + fillvalue = NULL; + if (PyDict_GET_SIZE(kwds) == 1) { + fillvalue = _PyDict_GetItemIdWithError(kwds, &PyId_fillvalue); + } + if (fillvalue == NULL) { + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "zip_longest() got an unexpected keyword argument"); + } return NULL; } } diff --git a/Modules/main.c b/Modules/main.c index a062991..f6e7889 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -508,10 +508,14 @@ pymain_free(_PyMain *pymain) static int pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) { + _Py_IDENTIFIER(path); PyObject *sys_path; PyObject *sysdict = interp->sysdict; if (sysdict != NULL) { - sys_path = PyDict_GetItemString(sysdict, "path"); + sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path); + if (sys_path == NULL && PyErr_Occurred()) { + goto error; + } } else { sys_path = NULL; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 05afe9e..540ee9d 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -9837,6 +9837,9 @@ os_unsetenv_impl(PyObject *module, PyObject *name) */ if (PyDict_DelItem(posix_putenv_garbage, name)) { /* really not much we can do; just leak */ + if (!PyErr_ExceptionMatches(PyExc_KeyError)) { + return NULL; + } PyErr_Clear(); } Py_RETURN_NONE; diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 9384081..2e8be37 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -226,10 +226,13 @@ string_intern(xmlparseobject *self, const char* str) return result; if (!self->intern) return result; - value = PyDict_GetItem(self->intern, result); + value = PyDict_GetItemWithError(self->intern, result); if (!value) { - if (PyDict_SetItem(self->intern, result, result) == 0) + if (!PyErr_Occurred() && + PyDict_SetItem(self->intern, result, result) == 0) + { return result; + } else { Py_DECREF(result); return NULL; @@ -1604,13 +1607,18 @@ static int init_handler_descrs(void) hi->getset.set = (setter)xmlparse_handler_setter; hi->getset.closure = &handler_info[i]; - PyObject *descr; - if (PyDict_GetItemString(Xmlparsetype.tp_dict, hi->name)) - continue; - descr = PyDescr_NewGetSet(&Xmlparsetype, &hi->getset); - + PyObject *descr = PyDescr_NewGetSet(&Xmlparsetype, &hi->getset); if (descr == NULL) return -1; + + if (PyDict_GetItemWithError(Xmlparsetype.tp_dict, PyDescr_NAME(descr))) { + Py_DECREF(descr); + continue; + } + else if (PyErr_Occurred()) { + Py_DECREF(descr); + return -1; + } if (PyDict_SetItem(Xmlparsetype.tp_dict, PyDescr_NAME(descr), descr) < 0) { Py_DECREF(descr); return -1; @@ -1682,8 +1690,8 @@ MODULE_INITFUNC(void) Py_DECREF(m); return NULL; } - errors_module = PyDict_GetItem(d, errmod_name); - if (errors_module == NULL) { + errors_module = PyDict_GetItemWithError(d, errmod_name); + if (errors_module == NULL && !PyErr_Occurred()) { errors_module = PyModule_New(MODULE_NAME ".errors"); if (errors_module != NULL) { _PyImport_SetModule(errmod_name, errors_module); @@ -1692,8 +1700,8 @@ MODULE_INITFUNC(void) } } Py_DECREF(errmod_name); - model_module = PyDict_GetItem(d, modelmod_name); - if (model_module == NULL) { + model_module = PyDict_GetItemWithError(d, modelmod_name); + if (model_module == NULL && !PyErr_Occurred()) { model_module = PyModule_New(MODULE_NAME ".model"); if (model_module != NULL) { _PyImport_SetModule(modelmod_name, model_module); diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 7f62ab1..88130a1 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -499,9 +499,11 @@ select_poll_modify_impl(pollObject *self, int fd, unsigned short eventmask) key = PyLong_FromLong(fd); if (key == NULL) return NULL; - if (PyDict_GetItem(self->dict, key) == NULL) { - errno = ENOENT; - PyErr_SetFromErrno(PyExc_OSError); + if (PyDict_GetItemWithError(self->dict, key) == NULL) { + if (!PyErr_Occurred()) { + errno = ENOENT; + PyErr_SetFromErrno(PyExc_OSError); + } Py_DECREF(key); return NULL; } diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c index 5586989..190f993 100644 --- a/Modules/xxlimited.c +++ b/Modules/xxlimited.c @@ -78,11 +78,14 @@ static PyObject * Xxo_getattro(XxoObject *self, PyObject *name) { if (self->x_attr != NULL) { - PyObject *v = PyDict_GetItem(self->x_attr, name); + PyObject *v = PyDict_GetItemWithError(self->x_attr, name); if (v != NULL) { Py_INCREF(v); return v; } + else if (PyErr_Occurred()) { + return NULL; + } } return PyObject_GenericGetAttr((PyObject *)self, name); } @@ -97,7 +100,7 @@ Xxo_setattr(XxoObject *self, const char *name, PyObject *v) } if (v == NULL) { int rv = PyDict_DelItemString(self->x_attr, name); - if (rv < 0) + if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) PyErr_SetString(PyExc_AttributeError, "delete non-existing Xxo attribute"); return rv; diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c index c0564ea..d546901 100644 --- a/Modules/xxmodule.c +++ b/Modules/xxmodule.c @@ -66,11 +66,14 @@ static PyObject * Xxo_getattro(XxoObject *self, PyObject *name) { if (self->x_attr != NULL) { - PyObject *v = PyDict_GetItem(self->x_attr, name); + PyObject *v = PyDict_GetItemWithError(self->x_attr, name); if (v != NULL) { Py_INCREF(v); return v; } + else if (PyErr_Occurred()) { + return NULL; + } } return PyObject_GenericGetAttr((PyObject *)self, name); } @@ -85,7 +88,7 @@ Xxo_setattr(XxoObject *self, const char *name, PyObject *v) } if (v == NULL) { int rv = PyDict_DelItemString(self->x_attr, name); - if (rv < 0) + if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) PyErr_SetString(PyExc_AttributeError, "delete non-existing Xxo attribute"); return rv; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index a871636..83cadda 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1419,6 +1419,19 @@ _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) return PyDict_GetItemWithError(dp, kv); } +PyObject * +_PyDict_GetItemStringWithError(PyObject *v, const char *key) +{ + PyObject *kv, *rv; + kv = PyUnicode_FromString(key); + if (kv == NULL) { + return NULL; + } + rv = PyDict_GetItemWithError(v, kv); + Py_DECREF(kv); + return rv; +} + /* Fast version of global value lookup (LOAD_GLOBAL). * Lookup in globals, then builtins. * @@ -2358,14 +2371,21 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) value = PySequence_Fast_GET_ITEM(fast, 1); Py_INCREF(key); Py_INCREF(value); - if (override || PyDict_GetItem(d, key) == NULL) { - int status = PyDict_SetItem(d, key, value); - if (status < 0) { + if (override) { + if (PyDict_SetItem(d, key, value) < 0) { + Py_DECREF(key); + Py_DECREF(value); + goto Fail; + } + } + else if (PyDict_GetItemWithError(d, key) == NULL) { + if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) { Py_DECREF(key); Py_DECREF(value); goto Fail; } } + Py_DECREF(key); Py_DECREF(value); Py_DECREF(fast); @@ -2489,15 +2509,22 @@ dict_merge(PyObject *a, PyObject *b, int override) return -1; for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { - if (override != 1 && PyDict_GetItem(a, key) != NULL) { - if (override != 0) { - _PyErr_SetKeyError(key); + if (override != 1) { + if (PyDict_GetItemWithError(a, key) != NULL) { + if (override != 0) { + _PyErr_SetKeyError(key); + Py_DECREF(key); + Py_DECREF(iter); + return -1; + } + Py_DECREF(key); + continue; + } + else if (PyErr_Occurred()) { Py_DECREF(key); Py_DECREF(iter); return -1; } - Py_DECREF(key); - continue; } value = PyObject_GetItem(b, key); if (value == NULL) { diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 75ede1c..ad2a54a 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -975,7 +975,7 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (myerrno && PyLong_Check(myerrno) && errnomap && (PyObject *) type == PyExc_OSError) { PyObject *newtype; - newtype = PyDict_GetItem(errnomap, myerrno); + newtype = PyDict_GetItemWithError(errnomap, myerrno); if (newtype) { assert(PyType_Check(newtype)); type = (PyTypeObject *) newtype; diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 8488b96..b668465 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -613,7 +613,7 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code, } #endif if (back == NULL || back->f_globals != globals) { - builtins = _PyDict_GetItemId(globals, &PyId___builtins__); + builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__); if (builtins) { if (PyModule_Check(builtins)) { builtins = PyModule_GetDict(builtins); @@ -621,6 +621,9 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code, } } if (builtins == NULL) { + if (PyErr_Occurred()) { + return NULL; + } /* No builtins! Make up a minimal one Give them 'None', at least. */ builtins = PyDict_New(); diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 4fab358..e8e2d2e 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -54,11 +54,15 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname /* __module__: If module name is in globals, use it. Otherwise, use None. */ - module = PyDict_GetItem(globals, __name__); + module = PyDict_GetItemWithError(globals, __name__); if (module) { Py_INCREF(module); op->func_module = module; } + else if (PyErr_Occurred()) { + Py_DECREF(op); + return NULL; + } if (qualname) op->func_qualname = qualname; else diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index fca8521..9d65332 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -792,16 +792,17 @@ static PyObject * module_dir(PyObject *self, PyObject *args) { _Py_IDENTIFIER(__dict__); + _Py_IDENTIFIER(__dir__); PyObject *result = NULL; PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); if (dict != NULL) { if (PyDict_Check(dict)) { - PyObject *dirfunc = PyDict_GetItemString(dict, "__dir__"); + PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__); if (dirfunc) { result = _PyObject_CallNoArg(dirfunc); } - else { + else if (!PyErr_Occurred()) { result = PyDict_Keys(dict); } } diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 2acf809..aba3ff7 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -102,9 +102,9 @@ namespace_repr(PyObject *ns) if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) { PyObject *value, *item; - value = PyDict_GetItem(d, key); + value = PyDict_GetItemWithError(d, key); if (value != NULL) { - item = PyUnicode_FromFormat("%S=%R", key, value); + item = PyUnicode_FromFormat("%U=%R", key, value); if (item == NULL) { loop_error = 1; } @@ -113,6 +113,9 @@ namespace_repr(PyObject *ns) Py_DECREF(item); } } + else if (PyErr_Occurred()) { + loop_error = 1; + } } Py_DECREF(key); diff --git a/Objects/object.c b/Objects/object.c index 044342f..cf5264b 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1144,7 +1144,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) dictptr = _PyObject_GetDictPtr(obj); if (dictptr != NULL && (dict = *dictptr) != NULL) { Py_INCREF(dict); - attr = PyDict_GetItem(dict, name); + attr = PyDict_GetItemWithError(dict, name); if (attr != NULL) { Py_INCREF(attr); *method = attr; @@ -1152,7 +1152,13 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) Py_XDECREF(descr); return 0; } - Py_DECREF(dict); + else { + Py_DECREF(dict); + if (PyErr_Occurred()) { + Py_XDECREF(descr); + return 0; + } + } } if (meth_found) { @@ -1249,13 +1255,23 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, } if (dict != NULL) { Py_INCREF(dict); - res = PyDict_GetItem(dict, name); + res = PyDict_GetItemWithError(dict, name); if (res != NULL) { Py_INCREF(res); Py_DECREF(dict); goto done; } - Py_DECREF(dict); + else { + Py_DECREF(dict); + if (PyErr_Occurred()) { + if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) { + PyErr_Clear(); + } + else { + goto done; + } + } + } } if (f != NULL) { @@ -1943,8 +1959,11 @@ Py_ReprEnter(PyObject *obj) early on startup. */ if (dict == NULL) return 0; - list = _PyDict_GetItemId(dict, &PyId_Py_Repr); + list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); if (list == NULL) { + if (PyErr_Occurred()) { + return -1; + } list = PyList_New(0); if (list == NULL) return -1; @@ -1976,7 +1995,7 @@ Py_ReprLeave(PyObject *obj) if (dict == NULL) goto finally; - list = _PyDict_GetItemId(dict, &PyId_Py_Repr); + list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); if (list == NULL || !PyList_Check(list)) goto finally; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 4234726..e559c64 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -491,9 +491,11 @@ type_module(PyTypeObject *type, void *context) PyObject *mod; if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { - mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); + mod = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___module__); if (mod == NULL) { - PyErr_Format(PyExc_AttributeError, "__module__"); + if (!PyErr_Occurred()) { + PyErr_Format(PyExc_AttributeError, "__module__"); + } return NULL; } Py_INCREF(mod); @@ -532,11 +534,13 @@ type_abstractmethods(PyTypeObject *type, void *context) /* type itself has an __abstractmethods__ descriptor (this). Don't return that. */ if (type != &PyType_Type) - mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); + mod = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___abstractmethods__); if (!mod) { - PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); - if (message) - PyErr_SetObject(PyExc_AttributeError, message); + if (!PyErr_Occurred()) { + PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); + if (message) + PyErr_SetObject(PyExc_AttributeError, message); + } return NULL; } Py_INCREF(mod); @@ -808,10 +812,12 @@ type_get_doc(PyTypeObject *type, void *context) if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); } - result = _PyDict_GetItemId(type->tp_dict, &PyId___doc__); + result = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___doc__); if (result == NULL) { - result = Py_None; - Py_INCREF(result); + if (!PyErr_Occurred()) { + result = Py_None; + Py_INCREF(result); + } } else if (Py_TYPE(result)->tp_descr_get) { result = Py_TYPE(result)->tp_descr_get(result, NULL, @@ -2452,13 +2458,16 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto error; /* Check for a __slots__ sequence variable in dict, and count it */ - slots = _PyDict_GetItemId(dict, &PyId___slots__); + slots = _PyDict_GetItemIdWithError(dict, &PyId___slots__); nslots = 0; add_dict = 0; add_weak = 0; may_add_dict = base->tp_dictoffset == 0; may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; if (slots == NULL) { + if (PyErr_Occurred()) { + goto error; + } if (may_add_dict) { add_dict++; } @@ -2535,7 +2544,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto error; } PyList_SET_ITEM(newslots, j, tmp); - if (PyDict_GetItem(dict, tmp)) { + if (PyDict_GetItemWithError(dict, tmp)) { /* CPython inserts __qualname__ and __classcell__ (when needed) into the namespace when creating a class. They will be deleted below so won't act as class variables. */ @@ -2548,6 +2557,10 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto error; } } + else if (PyErr_Occurred()) { + Py_DECREF(newslots); + goto error; + } j++; } assert(j == nslots - add_dict - add_weak); @@ -2632,22 +2645,28 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) type->tp_dict = dict; /* Set __module__ in the dict */ - if (_PyDict_GetItemId(dict, &PyId___module__) == NULL) { + if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) { + if (PyErr_Occurred()) { + goto error; + } tmp = PyEval_GetGlobals(); if (tmp != NULL) { - tmp = _PyDict_GetItemId(tmp, &PyId___name__); + tmp = _PyDict_GetItemIdWithError(tmp, &PyId___name__); if (tmp != NULL) { if (_PyDict_SetItemId(dict, &PyId___module__, tmp) < 0) goto error; } + else if (PyErr_Occurred()) { + goto error; + } } } /* Set ht_qualname to dict['__qualname__'] if available, else to __name__. The __qualname__ accessor will look for ht_qualname. */ - qualname = _PyDict_GetItemId(dict, &PyId___qualname__); + qualname = _PyDict_GetItemIdWithError(dict, &PyId___qualname__); if (qualname != NULL) { if (!PyUnicode_Check(qualname)) { PyErr_Format(PyExc_TypeError, @@ -2656,6 +2675,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto error; } } + else if (PyErr_Occurred()) { + goto error; + } et->ht_qualname = qualname ? qualname : et->ht_name; Py_INCREF(et->ht_qualname); if (qualname != NULL && _PyDict_DelItemId(dict, &PyId___qualname__) < 0) @@ -2666,7 +2688,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) if that fails, it will still look into __dict__. */ { - PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); + PyObject *doc = _PyDict_GetItemIdWithError(dict, &PyId___doc__); if (doc != NULL && PyUnicode_Check(doc)) { Py_ssize_t len; const char *doc_str; @@ -2685,11 +2707,14 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) memcpy(tp_doc, doc_str, len + 1); type->tp_doc = tp_doc; } + else if (doc == NULL && PyErr_Occurred()) { + goto error; + } } /* Special-case __new__: if it's a plain function, make it a static function */ - tmp = _PyDict_GetItemId(dict, &PyId___new__); + tmp = _PyDict_GetItemIdWithError(dict, &PyId___new__); if (tmp != NULL && PyFunction_Check(tmp)) { tmp = PyStaticMethod_New(tmp); if (tmp == NULL) @@ -2700,10 +2725,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) } Py_DECREF(tmp); } + else if (tmp == NULL && PyErr_Occurred()) { + goto error; + } /* Special-case __init_subclass__ and __class_getitem__: if they are plain functions, make them classmethods */ - tmp = _PyDict_GetItemId(dict, &PyId___init_subclass__); + tmp = _PyDict_GetItemIdWithError(dict, &PyId___init_subclass__); if (tmp != NULL && PyFunction_Check(tmp)) { tmp = PyClassMethod_New(tmp); if (tmp == NULL) @@ -2714,8 +2742,11 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) } Py_DECREF(tmp); } + else if (tmp == NULL && PyErr_Occurred()) { + goto error; + } - tmp = _PyDict_GetItemId(dict, &PyId___class_getitem__); + tmp = _PyDict_GetItemIdWithError(dict, &PyId___class_getitem__); if (tmp != NULL && PyFunction_Check(tmp)) { tmp = PyClassMethod_New(tmp); if (tmp == NULL) @@ -2726,6 +2757,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) } Py_DECREF(tmp); } + else if (tmp == NULL && PyErr_Occurred()) { + goto error; + } /* Add descriptors for custom slots from __slots__, or for __dict__ */ mp = PyHeapType_GET_MEMBERS(et); @@ -2797,7 +2831,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) type->tp_free = PyObject_Del; /* store type in class' cell if one is supplied */ - cell = _PyDict_GetItemId(dict, &PyId___classcell__); + cell = _PyDict_GetItemIdWithError(dict, &PyId___classcell__); if (cell != NULL) { /* At least one method requires a reference to its defining class */ if (!PyCell_Check(cell)) { @@ -2807,8 +2841,12 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) goto error; } PyCell_Set(cell, (PyObject *) type); - _PyDict_DelItemId(dict, &PyId___classcell__); - PyErr_Clear(); + if (_PyDict_DelItemId(dict, &PyId___classcell__) < 0) { + goto error; + } + } + else if (PyErr_Occurred()) { + goto error; } /* Initialize the rest */ @@ -3741,47 +3779,41 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { - PyObject *abstract_methods = NULL; - PyObject *builtins; - PyObject *sorted; - PyObject *sorted_methods = NULL; - PyObject *joined = NULL; + PyObject *abstract_methods; + PyObject *sorted_methods; + PyObject *joined; PyObject *comma; _Py_static_string(comma_id, ", "); - _Py_IDENTIFIER(sorted); /* Compute ", ".join(sorted(type.__abstractmethods__)) into joined. */ abstract_methods = type_abstractmethods(type, NULL); if (abstract_methods == NULL) - goto error; - builtins = PyEval_GetBuiltins(); - if (builtins == NULL) - goto error; - sorted = _PyDict_GetItemId(builtins, &PyId_sorted); - if (sorted == NULL) - goto error; - sorted_methods = PyObject_CallFunctionObjArgs(sorted, - abstract_methods, - NULL); + return NULL; + sorted_methods = PySequence_List(abstract_methods); + Py_DECREF(abstract_methods); if (sorted_methods == NULL) - goto error; + return NULL; + if (PyList_Sort(sorted_methods)) { + Py_DECREF(sorted_methods); + return NULL; + } comma = _PyUnicode_FromId(&comma_id); - if (comma == NULL) - goto error; + if (comma == NULL) { + Py_DECREF(sorted_methods); + return NULL; + } joined = PyUnicode_Join(comma, sorted_methods); + Py_DECREF(sorted_methods); if (joined == NULL) - goto error; + return NULL; PyErr_Format(PyExc_TypeError, "Can't instantiate abstract class %s " "with abstract methods %U", type->tp_name, joined); - error: - Py_XDECREF(joined); - Py_XDECREF(sorted_methods); - Py_XDECREF(abstract_methods); + Py_DECREF(joined); return NULL; } return type->tp_alloc(type, 0); @@ -4610,14 +4642,12 @@ object___reduce_ex___impl(PyObject *self, int protocol) if (objreduce == NULL) { objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, &PyId___reduce__); - if (objreduce == NULL) - return NULL; } - reduce = _PyObject_GetAttrId(self, &PyId___reduce__); - if (reduce == NULL) - PyErr_Clear(); - else { + if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) { + return NULL; + } + if (reduce != NULL) { PyObject *cls, *clsreduce; int override; @@ -4829,14 +4859,12 @@ static int add_methods(PyTypeObject *type, PyMethodDef *meth) { PyObject *dict = type->tp_dict; + PyObject *name; for (; meth->ml_name != NULL; meth++) { PyObject *descr; int err; int isdescr = 1; - if (PyDict_GetItemString(dict, meth->ml_name) && - !(meth->ml_flags & METH_COEXIST)) - continue; if (meth->ml_flags & METH_CLASS) { if (meth->ml_flags & METH_STATIC) { PyErr_SetString(PyExc_ValueError, @@ -4846,7 +4874,7 @@ add_methods(PyTypeObject *type, PyMethodDef *meth) descr = PyDescr_NewClassMethod(type, meth); } else if (meth->ml_flags & METH_STATIC) { - PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); + PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); if (cfunc == NULL) return -1; descr = PyStaticMethod_New(cfunc); @@ -4858,11 +4886,36 @@ add_methods(PyTypeObject *type, PyMethodDef *meth) } if (descr == NULL) return -1; + if (isdescr) { - err = PyDict_SetItem(dict, PyDescr_NAME(descr), descr); + name = PyDescr_NAME(descr); } else { - err = PyDict_SetItemString(dict, meth->ml_name, descr); + name = PyUnicode_FromString(meth->ml_name); + if (name == NULL) { + Py_DECREF(descr); + return -1; + } + } + + if (!(meth->ml_flags & METH_COEXIST)) { + if (PyDict_GetItemWithError(dict, name)) { + if (!isdescr) { + Py_DECREF(name); + } + Py_DECREF(descr); + continue; + } + else if (PyErr_Occurred()) { + if (!isdescr) { + Py_DECREF(name); + } + return -1; + } + } + err = PyDict_SetItem(dict, name, descr); + if (!isdescr) { + Py_DECREF(name); } Py_DECREF(descr); if (err < 0) @@ -4877,12 +4930,18 @@ add_members(PyTypeObject *type, PyMemberDef *memb) PyObject *dict = type->tp_dict; for (; memb->name != NULL; memb++) { - PyObject *descr; - if (PyDict_GetItemString(dict, memb->name)) - continue; - descr = PyDescr_NewMember(type, memb); + PyObject *descr = PyDescr_NewMember(type, memb); if (descr == NULL) return -1; + + if (PyDict_GetItemWithError(dict, PyDescr_NAME(descr))) { + Py_DECREF(descr); + continue; + } + else if (PyErr_Occurred()) { + Py_DECREF(descr); + return -1; + } if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { Py_DECREF(descr); return -1; @@ -4898,13 +4957,17 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp) PyObject *dict = type->tp_dict; for (; gsp->name != NULL; gsp++) { - PyObject *descr; - if (PyDict_GetItemString(dict, gsp->name)) - continue; - descr = PyDescr_NewGetSet(type, gsp); - + PyObject *descr = PyDescr_NewGetSet(type, gsp); if (descr == NULL) return -1; + + if (PyDict_GetItemWithError(dict, PyDescr_NAME(descr))) { + continue; + } + else if (PyErr_Occurred()) { + Py_DECREF(descr); + return -1; + } if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) { Py_DECREF(descr); return -1; @@ -5309,7 +5372,10 @@ PyType_Ready(PyTypeObject *type) /* if the type dictionary doesn't contain a __doc__, set it from the tp_doc slot. */ - if (_PyDict_GetItemId(type->tp_dict, &PyId___doc__) == NULL) { + if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___doc__) == NULL) { + if (PyErr_Occurred()) { + goto error; + } if (type->tp_doc != NULL) { const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, type->tp_doc); @@ -5335,9 +5401,12 @@ PyType_Ready(PyTypeObject *type) This signals that __hash__ is not inherited. */ if (type->tp_hash == NULL) { - if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { - if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) + if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___hash__) == NULL) { + if (PyErr_Occurred() || + _PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) + { goto error; + } type->tp_hash = PyObject_HashNotImplemented; } } @@ -5988,8 +6057,10 @@ add_tp_new_wrapper(PyTypeObject *type) { PyObject *func; - if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) + if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___new__) != NULL) return 0; + if (PyErr_Occurred()) + return -1; func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); if (func == NULL) return -1; @@ -7414,9 +7485,14 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *name, assert(PyType_Check(subclass)); /* Avoid recursing down into unaffected classes */ dict = subclass->tp_dict; - if (dict != NULL && PyDict_Check(dict) && - PyDict_GetItem(dict, name) != NULL) - continue; + if (dict != NULL && PyDict_Check(dict)) { + if (PyDict_GetItemWithError(dict, name) != NULL) { + continue; + } + if (PyErr_Occurred()) { + return -1; + } + } if (update_subclasses(subclass, name, callback, data) < 0) return -1; } @@ -7468,8 +7544,11 @@ add_operators(PyTypeObject *type) ptr = slotptr(type, p->offset); if (!ptr || !*ptr) continue; - if (PyDict_GetItem(dict, p->name_strobj)) + if (PyDict_GetItemWithError(dict, p->name_strobj)) continue; + if (PyErr_Occurred()) { + return -1; + } if (*ptr == (void *)PyObject_HashNotImplemented) { /* Classes may prevent the inheritance of the tp_hash slot by storing PyObject_HashNotImplemented in it. Make it @@ -7579,7 +7658,7 @@ super_getattro(PyObject *self, PyObject *name) goto skip; /* keep a strong reference to mro because starttype->tp_mro can be - replaced during PyDict_GetItem(dict, name) */ + replaced during PyDict_GetItemWithError(dict, name) */ Py_INCREF(mro); do { PyObject *res, *tmp, *dict; @@ -7591,7 +7670,7 @@ super_getattro(PyObject *self, PyObject *name) dict = ((PyTypeObject *)tmp)->tp_dict; assert(dict != NULL && PyDict_Check(dict)); - res = PyDict_GetItem(dict, name); + res = PyDict_GetItemWithError(dict, name); if (res != NULL) { Py_INCREF(res); @@ -7609,6 +7688,9 @@ super_getattro(PyObject *self, PyObject *name) Py_DECREF(mro); return res; } + else if (PyErr_Occurred()) { + return NULL; + } i++; } while (i < n); diff --git a/Python/_warnings.c b/Python/_warnings.c index 7eedd13..33b4615 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -252,7 +252,7 @@ already_warned(PyObject *registry, PyObject *key, int should_set) if (key == NULL) return -1; - version_obj = _PyDict_GetItemId(registry, &PyId_version); + version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version); if (version_obj == NULL || !PyLong_CheckExact(version_obj) || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) @@ -271,12 +271,15 @@ already_warned(PyObject *registry, PyObject *key, int should_set) Py_DECREF(version_obj); } else { - already_warned = PyDict_GetItem(registry, key); + already_warned = PyDict_GetItemWithError(registry, key); if (already_warned != NULL) { int rc = PyObject_IsTrue(already_warned); if (rc != 0) return rc; } + else if (PyErr_Occurred()) { + return -1; + } } /* This warning wasn't found in the registry, set it. */ @@ -672,6 +675,8 @@ static int setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, PyObject **module, PyObject **registry) { + _Py_IDENTIFIER(__warningregistry__); + _Py_IDENTIFIER(__name__); PyObject *globals; /* Setup globals, filename and lineno. */ @@ -706,15 +711,18 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup registry. */ assert(globals != NULL); assert(PyDict_Check(globals)); - *registry = PyDict_GetItemString(globals, "__warningregistry__"); + *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__); if (*registry == NULL) { int rc; + if (PyErr_Occurred()) { + return 0; + } *registry = PyDict_New(); if (*registry == NULL) return 0; - rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); + rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry); if (rc < 0) goto handle_error; } @@ -722,10 +730,13 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, Py_INCREF(*registry); /* Setup module. */ - *module = PyDict_GetItemString(globals, "__name__"); + *module = _PyDict_GetItemIdWithError(globals, &PyId___name__); if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { Py_INCREF(*module); } + else if (PyErr_Occurred()) { + goto handle_error; + } else { *module = PyUnicode_FromString("<string>"); if (*module == NULL) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index f9b901f..eebdc5b 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -142,7 +142,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, return NULL; } - meta = _PyDict_GetItemId(mkw, &PyId_metaclass); + meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass); if (meta != NULL) { Py_INCREF(meta); if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) { @@ -154,6 +154,11 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, /* metaclass is explicitly given, check if it's indeed a class */ isclass = PyType_Check(meta); } + else if (PyErr_Occurred()) { + Py_DECREF(mkw); + Py_DECREF(bases); + return NULL; + } } if (meta == NULL) { /* if there are no bases, use type: */ @@ -956,11 +961,14 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, return NULL; } - if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) { + if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) { if (_PyDict_SetItemId(globals, &PyId___builtins__, PyEval_GetBuiltins()) != 0) return NULL; } + else if (PyErr_Occurred()) { + return NULL; + } if (PyCode_Check(source)) { if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { @@ -1036,11 +1044,14 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, locals->ob_type->tp_name); return NULL; } - if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) { + if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) { if (_PyDict_SetItemId(globals, &PyId___builtins__, PyEval_GetBuiltins()) != 0) return NULL; } + else if (PyErr_Occurred()) { + return NULL; + } if (PyCode_Check(source)) { if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { diff --git a/Python/ceval.c b/Python/ceval.c index 4e139ce..ff83863 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2090,10 +2090,12 @@ main_loop: PyObject *bc; if (PyDict_CheckExact(f->f_builtins)) { - bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); + bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); if (bc == NULL) { - PyErr_SetString(PyExc_NameError, - "__build_class__ not found"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_NameError, + "__build_class__ not found"); + } goto error; } Py_INCREF(bc); @@ -2241,8 +2243,10 @@ main_loop: int err; err = PyDict_DelItem(f->f_globals, name); if (err != 0) { - format_exc_check_arg( - PyExc_NameError, NAME_ERROR_MSG, name); + if (PyErr_ExceptionMatches(PyExc_KeyError)) { + format_exc_check_arg( + PyExc_NameError, NAME_ERROR_MSG, name); + } goto error; } DISPATCH(); @@ -2258,8 +2262,13 @@ main_loop: goto error; } if (PyDict_CheckExact(locals)) { - v = PyDict_GetItem(locals, name); - Py_XINCREF(v); + v = PyDict_GetItemWithError(locals, name); + if (v != NULL) { + Py_INCREF(v); + } + else if (PyErr_Occurred()) { + goto error; + } } else { v = PyObject_GetItem(locals, name); @@ -2270,15 +2279,22 @@ main_loop: } } if (v == NULL) { - v = PyDict_GetItem(f->f_globals, name); - Py_XINCREF(v); - if (v == NULL) { + v = PyDict_GetItemWithError(f->f_globals, name); + if (v != NULL) { + Py_INCREF(v); + } + else if (PyErr_Occurred()) { + goto error; + } + else { if (PyDict_CheckExact(f->f_builtins)) { - v = PyDict_GetItem(f->f_builtins, name); + v = PyDict_GetItemWithError(f->f_builtins, name); if (v == NULL) { - format_exc_check_arg( + if (!PyErr_Occurred()) { + format_exc_check_arg( PyExc_NameError, NAME_ERROR_MSG, name); + } goto error; } Py_INCREF(v); @@ -2386,8 +2402,13 @@ main_loop: assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); name = PyTuple_GET_ITEM(co->co_freevars, idx); if (PyDict_CheckExact(locals)) { - value = PyDict_GetItem(locals, name); - Py_XINCREF(value); + value = PyDict_GetItemWithError(locals, name); + if (value != NULL) { + Py_INCREF(value); + } + else if (PyErr_Occurred()) { + goto error; + } } else { value = PyObject_GetItem(locals, name); @@ -2591,9 +2612,12 @@ main_loop: } /* check if __annotations__ in locals()... */ if (PyDict_CheckExact(f->f_locals)) { - ann_dict = _PyDict_GetItemId(f->f_locals, + ann_dict = _PyDict_GetItemIdWithError(f->f_locals, &PyId___annotations__); if (ann_dict == NULL) { + if (PyErr_Occurred()) { + goto error; + } /* ...if not, create a new one */ ann_dict = PyDict_New(); if (ann_dict == NULL) { @@ -3921,12 +3945,15 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, continue; name = PyTuple_GET_ITEM(co->co_varnames, i); if (kwdefs != NULL) { - PyObject *def = PyDict_GetItem(kwdefs, name); + PyObject *def = PyDict_GetItemWithError(kwdefs, name); if (def) { Py_INCREF(def); SETLOCAL(i, def); continue; } + else if (PyErr_Occurred()) { + goto fail; + } } missing++; } @@ -4861,9 +4888,11 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve PyObject *import_func, *res; PyObject* stack[5]; - import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); + import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); if (import_func == NULL) { - PyErr_SetString(PyExc_ImportError, "__import__ not found"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "__import__ not found"); + } return NULL; } @@ -5207,10 +5236,13 @@ unicode_concatenate(PyObject *v, PyObject *w, PyObject *names = f->f_code->co_names; PyObject *name = GETITEM(names, oparg); PyObject *locals = f->f_locals; - if (locals && PyDict_CheckExact(locals) && - PyDict_GetItem(locals, name) == v) { - if (PyDict_DelItem(locals, name) != 0) { - PyErr_Clear(); + if (locals && PyDict_CheckExact(locals)) { + PyObject *w = PyDict_GetItemWithError(locals, name); + if ((w == v && PyDict_DelItem(locals, name) != 0) || + (w == NULL && PyErr_Occurred())) + { + Py_DECREF(v); + return NULL; } } break; diff --git a/Python/codecs.c b/Python/codecs.c index ff2142d..d4b34f8 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -120,12 +120,16 @@ PyObject *_PyCodec_Lookup(const char *encoding) PyUnicode_InternInPlace(&v); /* First, try to lookup the name in the registry dictionary */ - result = PyDict_GetItem(interp->codec_search_cache, v); + result = PyDict_GetItemWithError(interp->codec_search_cache, v); if (result != NULL) { Py_INCREF(result); Py_DECREF(v); return result; } + else if (PyErr_Occurred()) { + Py_DECREF(v); + return NULL; + } /* Next, scan the search functions in order of registration */ args = PyTuple_New(1); @@ -648,11 +652,13 @@ PyObject *PyCodec_LookupError(const char *name) if (name==NULL) name = "strict"; - handler = PyDict_GetItemString(interp->codec_error_registry, name); - if (!handler) - PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); - else + handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name); + if (handler) { Py_INCREF(handler); + } + else if (!PyErr_Occurred()) { + PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); + } return handler; } diff --git a/Python/errors.c b/Python/errors.c index febe971..b8af1df 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -858,6 +858,7 @@ PyErr_Format(PyObject *exception, const char *format, ...) PyObject * PyErr_NewException(const char *name, PyObject *base, PyObject *dict) { + _Py_IDENTIFIER(__module__); const char *dot; PyObject *modulename = NULL; PyObject *classname = NULL; @@ -877,12 +878,15 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict) if (dict == NULL) goto failure; } - if (PyDict_GetItemString(dict, "__module__") == NULL) { + if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) { + if (PyErr_Occurred()) { + goto failure; + } modulename = PyUnicode_FromStringAndSize(name, (Py_ssize_t)(dot-name)); if (modulename == NULL) goto failure; - if (PyDict_SetItemString(dict, "__module__", modulename) != 0) + if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0) goto failure; } if (PyTuple_Check(base)) { diff --git a/Python/getargs.c b/Python/getargs.c index 0e07555..ba1a9d4 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -1763,9 +1763,13 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, current_arg = PyTuple_GET_ITEM(args, i); } else if (nkwargs && i >= pos) { - current_arg = PyDict_GetItemString(kwargs, kwlist[i]); - if (current_arg) + current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); + if (current_arg) { --nkwargs; + } + else if (PyErr_Occurred()) { + return cleanreturn(0, &freelist); + } } else { current_arg = NULL; @@ -1844,7 +1848,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, Py_ssize_t j; /* make sure there are no arguments given by name and position */ for (i = pos; i < nargs; i++) { - current_arg = PyDict_GetItemString(kwargs, kwlist[i]); + current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); if (current_arg) { /* arg present in tuple and in dict */ PyErr_Format(PyExc_TypeError, @@ -1855,6 +1859,9 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, kwlist[i], i+1); return cleanreturn(0, &freelist); } + else if (PyErr_Occurred()) { + return cleanreturn(0, &freelist); + } } /* make sure there are no extraneous keyword arguments */ j = 0; @@ -2016,13 +2023,10 @@ parser_clear(struct _PyArg_Parser *parser) } static PyObject* -find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key) +find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key) { Py_ssize_t i, nkwargs; - if (kwargs != NULL) { - return PyDict_GetItem(kwargs, key); - } nkwargs = PyTuple_GET_SIZE(kwnames); for (i=0; i < nkwargs; i++) { PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); @@ -2157,9 +2161,18 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, } else if (nkwargs && i >= pos) { keyword = PyTuple_GET_ITEM(kwtuple, i - pos); - current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); - if (current_arg) + if (kwargs != NULL) { + current_arg = PyDict_GetItemWithError(kwargs, keyword); + if (!current_arg && PyErr_Occurred()) { + return cleanreturn(0, &freelist); + } + } + else { + current_arg = find_keyword(kwnames, kwstack, keyword); + } + if (current_arg) { --nkwargs; + } } else { current_arg = NULL; @@ -2220,7 +2233,15 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, /* make sure there are no arguments given by name and position */ for (i = pos; i < nargs; i++) { keyword = PyTuple_GET_ITEM(kwtuple, i - pos); - current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); + if (kwargs != NULL) { + current_arg = PyDict_GetItemWithError(kwargs, keyword); + if (!current_arg && PyErr_Occurred()) { + return cleanreturn(0, &freelist); + } + } + else { + current_arg = find_keyword(kwnames, kwstack, keyword); + } if (current_arg) { /* arg present in tuple and in dict */ PyErr_Format(PyExc_TypeError, diff --git a/Python/import.c b/Python/import.c index 344f199..898321a 100644 --- a/Python/import.c +++ b/Python/import.c @@ -431,9 +431,13 @@ PyImport_Cleanup(void) for (p = sys_files; *p != NULL; p+=2) { if (Py_VerboseFlag) PySys_WriteStderr("# restore sys.%s\n", *p); - value = PyDict_GetItemString(interp->sysdict, *(p+1)); - if (value == NULL) + value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1)); + if (value == NULL) { + if (PyErr_Occurred()) { + PyErr_WriteUnraisable(NULL); + } value = Py_None; + } if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) { PyErr_WriteUnraisable(NULL); } @@ -718,7 +722,7 @@ _PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename, key = PyTuple_Pack(2, filename, name); if (key == NULL) return NULL; - def = (PyModuleDef *)PyDict_GetItem(extensions, key); + def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key); Py_DECREF(key); if (def == NULL) return NULL; @@ -927,6 +931,7 @@ error: static PyObject * module_dict_for_exec(PyObject *name) { + _Py_IDENTIFIER(__builtins__); PyObject *m, *d = NULL; m = PyImport_AddModuleObject(name); @@ -935,9 +940,11 @@ module_dict_for_exec(PyObject *name) /* If the module is being reloaded, we get the old module back and re-use its dict to exec the new code. */ d = PyModule_GetDict(m); - if (PyDict_GetItemString(d, "__builtins__") == NULL) { - if (PyDict_SetItemString(d, "__builtins__", - PyEval_GetBuiltins()) != 0) { + if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) { + if (PyErr_Occurred() || + _PyDict_SetItemId(d, &PyId___builtins__, + PyEval_GetBuiltins()) != 0) + { remove_module(name); return NULL; } @@ -1107,8 +1114,8 @@ get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, if (nhooks < 0) return NULL; /* Shouldn't happen */ - importer = PyDict_GetItem(path_importer_cache, p); - if (importer != NULL) + importer = PyDict_GetItemWithError(path_importer_cache, p); + if (importer != NULL || PyErr_Occurred()) return importer; /* set path_importer_cache[p] to None to avoid recursion */ @@ -1496,11 +1503,17 @@ resolve_name(PyObject *name, PyObject *globals, int level) PyErr_SetString(PyExc_TypeError, "globals must be a dict"); goto error; } - package = _PyDict_GetItemId(globals, &PyId___package__); + package = _PyDict_GetItemIdWithError(globals, &PyId___package__); if (package == Py_None) { package = NULL; } - spec = _PyDict_GetItemId(globals, &PyId___spec__); + else if (package == NULL && PyErr_Occurred()) { + goto error; + } + spec = _PyDict_GetItemIdWithError(globals, &PyId___spec__); + if (spec == NULL && PyErr_Occurred()) { + goto error; + } if (package != NULL) { Py_INCREF(package); @@ -1546,9 +1559,11 @@ resolve_name(PyObject *name, PyObject *globals, int level) goto error; } - package = _PyDict_GetItemId(globals, &PyId___name__); + package = _PyDict_GetItemIdWithError(globals, &PyId___name__); if (package == NULL) { - PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); + } goto error; } @@ -1558,10 +1573,10 @@ resolve_name(PyObject *name, PyObject *globals, int level) goto error; } - if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { + if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) { Py_ssize_t dot; - if (PyUnicode_READY(package) < 0) { + if (PyErr_Occurred() || PyUnicode_READY(package) < 0) { goto error; } diff --git a/Python/symtable.c b/Python/symtable.c index cade304..6e2df2f 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -359,12 +359,12 @@ PySymtable_Lookup(struct symtable *st, void *key) k = PyLong_FromVoidPtr(key); if (k == NULL) return NULL; - v = PyDict_GetItem(st->st_blocks, k); + v = PyDict_GetItemWithError(st->st_blocks, k); if (v) { assert(PySTEntry_Check(v)); Py_INCREF(v); } - else { + else if (!PyErr_Occurred()) { PyErr_SetString(PyExc_KeyError, "unknown symbol table entry"); } @@ -637,7 +637,7 @@ update_symbols(PyObject *symbols, PyObject *scopes, } while ((name = PyIter_Next(itr))) { - v = PyDict_GetItem(symbols, name); + v = PyDict_GetItemWithError(symbols, name); /* Handle symbol that already exists in this scope */ if (v) { @@ -662,6 +662,9 @@ update_symbols(PyObject *symbols, PyObject *scopes, Py_DECREF(name); continue; } + else if (PyErr_Occurred()) { + goto error; + } /* Handle global symbol */ if (bound && !PySet_Contains(bound, name)) { Py_DECREF(name); @@ -991,7 +994,7 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s if (!mangled) return 0; dict = ste->ste_symbols; - if ((o = PyDict_GetItem(dict, mangled))) { + if ((o = PyDict_GetItemWithError(dict, mangled))) { val = PyLong_AS_LONG(o); if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { /* Is it better to use 'mangled' or 'name' here? */ @@ -1002,8 +1005,13 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s goto error; } val |= flag; - } else + } + else if (PyErr_Occurred()) { + goto error; + } + else { val = flag; + } o = PyLong_FromLong(val); if (o == NULL) goto error; |