bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)

This commit is contained in:
Serhiy Storchaka 2019-02-25 17:59:46 +02:00 committed by GitHub
parent a180b007d9
commit a24107b04c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 538 additions and 242 deletions

View File

@ -35,6 +35,7 @@ PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash); Py_hash_t hash);
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp, PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
struct _Py_Identifier *key); struct _Py_Identifier *key);
PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
PyAPI_FUNC(PyObject *) PyDict_SetDefault( PyAPI_FUNC(PyObject *) PyDict_SetDefault(
PyObject *mp, PyObject *key, PyObject *defaultobj); PyObject *mp, PyObject *key, PyObject *defaultobj);
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key, PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,

View File

@ -132,7 +132,7 @@ get_dialect_from_registry(PyObject * name_obj)
{ {
PyObject *dialect_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 (dialect_obj == NULL) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); PyErr_Format(_csvstate_global->error_obj, "unknown dialect");
@ -1434,8 +1434,12 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
static PyObject * static PyObject *
csv_unregister_dialect(PyObject *module, PyObject *name_obj) csv_unregister_dialect(PyObject *module, PyObject *name_obj)
{ {
if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0) if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0) {
return PyErr_Format(_csvstate_global->error_obj, "unknown dialect"); if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Format(_csvstate_global->error_obj, "unknown dialect");
}
return NULL;
}
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -339,7 +339,7 @@ get_attrib_from_keywords(PyObject *kwds)
if (attrib_str == NULL) { if (attrib_str == NULL) {
return NULL; return NULL;
} }
PyObject *attrib = PyDict_GetItem(kwds, attrib_str); PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
if (attrib) { if (attrib) {
/* If attrib was found in kwds, copy its value and remove it from /* 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); Py_DECREF(attrib);
attrib = NULL; attrib = NULL;
} }
} else { }
else if (!PyErr_Occurred()) {
attrib = PyDict_New(); attrib = PyDict_New();
} }
@ -1393,9 +1394,13 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
if (!self->extra || self->extra->attrib == Py_None) if (!self->extra || self->extra->attrib == Py_None)
value = default_value; value = default_value;
else { else {
value = PyDict_GetItem(self->extra->attrib, key); value = PyDict_GetItemWithError(self->extra->attrib, key);
if (!value) if (!value) {
if (PyErr_Occurred()) {
return NULL;
}
value = default_value; value = default_value;
}
} }
Py_INCREF(value); Py_INCREF(value);
@ -2848,11 +2853,12 @@ makeuniversal(XMLParserObject* self, const char* string)
if (!key) if (!key)
return NULL; return NULL;
value = PyDict_GetItem(self->names, key); value = PyDict_GetItemWithError(self->names, key);
if (value) { if (value) {
Py_INCREF(value); Py_INCREF(value);
} else { }
else if (!PyErr_Occurred()) {
/* new name. convert to universal name, and decode as /* new name. convert to universal name, and decode as
necessary */ necessary */
@ -2974,7 +2980,7 @@ expat_default_handler(XMLParserObject* self, const XML_Char* data_in,
if (!key) if (!key)
return; return;
value = PyDict_GetItem(self->entity, key); value = PyDict_GetItemWithError(self->entity, key);
if (value) { if (value) {
if (TreeBuilder_CheckExact(self->target)) if (TreeBuilder_CheckExact(self->target))

View File

@ -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); key = scanstring_unicode(pystr, idx + 1, s->strict, &next_idx);
if (key == NULL) if (key == NULL)
goto bail; goto bail;
memokey = PyDict_GetItem(s->memo, key); memokey = PyDict_GetItemWithError(s->memo, key);
if (memokey != NULL) { if (memokey != NULL) {
Py_INCREF(memokey); Py_INCREF(memokey);
Py_DECREF(key); Py_DECREF(key);
key = memokey; key = memokey;
} }
else if (PyErr_Occurred()) {
goto bail;
}
else { else {
if (PyDict_SetItem(s->memo, key, key) < 0) if (PyDict_SetItem(s->memo, key, key) < 0)
goto bail; goto bail;

View File

@ -1899,15 +1899,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
void* ptr; void* ptr;
Py_ssize_t i, j; Py_ssize_t i, j;
if (index < 0 || index >= self->groups) { assert(0 <= index && index < self->groups);
/* raise IndexError if we were given a bad group number */
PyErr_SetString(
PyExc_IndexError,
"no such group"
);
return NULL;
}
index *= 2; index *= 2;
if (self->string == Py_None || self->mark[index] < 0) { if (self->string == Py_None || self->mark[index] < 0) {
@ -1940,17 +1932,25 @@ match_getindex(MatchObject* self, PyObject* index)
return 0; return 0;
if (PyIndex_Check(index)) { 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_GetItemWithError(self->pattern->groupindex, index);
if (self->pattern->groupindex) { if (index && PyLong_Check(index)) {
index = PyDict_GetItem(self->pattern->groupindex, index); i = PyLong_AsSsize_t(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; return i;
} }
@ -1958,7 +1958,13 @@ match_getindex(MatchObject* self, PyObject* index)
static PyObject* static PyObject*
match_getslice(MatchObject* self, PyObject* index, PyObject* def) 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] /*[clinic input]
@ -2114,11 +2120,7 @@ _sre_SRE_Match_start_impl(MatchObject *self, PyObject *group)
{ {
Py_ssize_t index = match_getindex(self, group); Py_ssize_t index = match_getindex(self, group);
if (index < 0 || index >= self->groups) { if (index < 0) {
PyErr_SetString(
PyExc_IndexError,
"no such group"
);
return -1; return -1;
} }
@ -2141,11 +2143,7 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group)
{ {
Py_ssize_t index = match_getindex(self, group); Py_ssize_t index = match_getindex(self, group);
if (index < 0 || index >= self->groups) { if (index < 0) {
PyErr_SetString(
PyExc_IndexError,
"no such group"
);
return -1; return -1;
} }
@ -2195,11 +2193,7 @@ _sre_SRE_Match_span_impl(MatchObject *self, PyObject *group)
{ {
Py_ssize_t index = match_getindex(self, group); Py_ssize_t index = match_getindex(self, group);
if (index < 0 || index >= self->groups) { if (index < 0) {
PyErr_SetString(
PyExc_IndexError,
"no such group"
);
return NULL; return NULL;
} }

View File

@ -2088,12 +2088,15 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
return 0; return 0;
} }
s_object = PyDict_GetItem(cache, fmt); s_object = PyDict_GetItemWithError(cache, fmt);
if (s_object != NULL) { if (s_object != NULL) {
Py_INCREF(s_object); Py_INCREF(s_object);
*ptr = (PyStructObject *)s_object; *ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED; return Py_CLEANUP_SUPPORTED;
} }
else if (PyErr_Occurred()) {
return 0;
}
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
if (s_object != NULL) { if (s_object != NULL) {

View File

@ -53,11 +53,14 @@ static PyObject *
Example_getattro(ExampleObject *self, PyObject *name) Example_getattro(ExampleObject *self, PyObject *name)
{ {
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItem(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
return v; return v;
} }
else if (PyErr_Occurred()) {
return NULL;
}
} }
return PyObject_GenericGetAttr((PyObject *)self, name); return PyObject_GenericGetAttr((PyObject *)self, name);
} }
@ -72,7 +75,7 @@ Example_setattr(ExampleObject *self, const char *name, PyObject *v)
} }
if (v == NULL) { if (v == NULL) {
int rv = PyDict_DelItemString(self->x_attr, name); int rv = PyDict_DelItemString(self->x_attr, name);
if (rv < 0) if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
PyErr_SetString(PyExc_AttributeError, PyErr_SetString(PyExc_AttributeError,
"delete non-existing Example attribute"); "delete non-existing Example attribute");
return rv; return rv;

View File

@ -814,8 +814,11 @@ _ldict(localobject *self)
return NULL; return NULL;
} }
dummy = PyDict_GetItem(tdict, self->key); dummy = PyDict_GetItemWithError(tdict, self->key);
if (dummy == NULL) { if (dummy == NULL) {
if (PyErr_Occurred()) {
return NULL;
}
ldict = _local_create_dummy(self); ldict = _local_create_dummy(self);
if (ldict == NULL) if (ldict == NULL)
return NULL; return NULL;
@ -931,14 +934,17 @@ local_getattro(localobject *self, PyObject *name)
(PyObject *)self, name, ldict, 0); (PyObject *)self, name, ldict, 0);
/* Optimization: just look in dict ourselves */ /* Optimization: just look in dict ourselves */
value = PyDict_GetItem(ldict, name); value = PyDict_GetItemWithError(ldict, name);
if (value == NULL) if (value != NULL) {
/* Fall back on generic to get __class__ and __dict__ */ Py_INCREF(value);
return _PyObject_GenericGetAttrWithDict( return value;
(PyObject *)self, name, ldict, 0); }
else if (PyErr_Occurred()) {
Py_INCREF(value); return NULL;
return value; }
/* Fall back on generic to get __class__ and __dict__ */
return _PyObject_GenericGetAttrWithDict(
(PyObject *)self, name, ldict, 0);
} }
/* Called when a dummy is destroyed. */ /* Called when a dummy is destroyed. */
@ -958,7 +964,7 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
self = (localobject *) obj; self = (localobject *) obj;
if (self->dummies != NULL) { if (self->dummies != NULL) {
PyObject *ldict; PyObject *ldict;
ldict = PyDict_GetItem(self->dummies, dummyweakref); ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
if (ldict != NULL) { if (ldict != NULL) {
PyDict_DelItem(self->dummies, dummyweakref); PyDict_DelItem(self->dummies, dummyweakref);
} }

View File

@ -4418,6 +4418,7 @@ static PyTypeObject ziplongest_type;
static PyObject * static PyObject *
zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
_Py_IDENTIFIER(fillvalue);
ziplongestobject *lz; ziplongestobject *lz;
Py_ssize_t i; Py_ssize_t i;
PyObject *ittuple; /* tuple of iterators */ PyObject *ittuple; /* tuple of iterators */
@ -4426,10 +4427,15 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_ssize_t tuplesize; Py_ssize_t tuplesize;
if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) {
fillvalue = PyDict_GetItemString(kwds, "fillvalue"); fillvalue = NULL;
if (fillvalue == NULL || PyDict_GET_SIZE(kwds) > 1) { if (PyDict_GET_SIZE(kwds) == 1) {
PyErr_SetString(PyExc_TypeError, fillvalue = _PyDict_GetItemIdWithError(kwds, &PyId_fillvalue);
"zip_longest() got an unexpected keyword argument"); }
if (fillvalue == NULL) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"zip_longest() got an unexpected keyword argument");
}
return NULL; return NULL;
} }
} }

View File

@ -508,10 +508,14 @@ pymain_free(_PyMain *pymain)
static int static int
pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
{ {
_Py_IDENTIFIER(path);
PyObject *sys_path; PyObject *sys_path;
PyObject *sysdict = interp->sysdict; PyObject *sysdict = interp->sysdict;
if (sysdict != NULL) { 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 { else {
sys_path = NULL; sys_path = NULL;

View File

@ -9837,6 +9837,9 @@ os_unsetenv_impl(PyObject *module, PyObject *name)
*/ */
if (PyDict_DelItem(posix_putenv_garbage, name)) { if (PyDict_DelItem(posix_putenv_garbage, name)) {
/* really not much we can do; just leak */ /* really not much we can do; just leak */
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
return NULL;
}
PyErr_Clear(); PyErr_Clear();
} }
Py_RETURN_NONE; Py_RETURN_NONE;

View File

@ -226,10 +226,13 @@ string_intern(xmlparseobject *self, const char* str)
return result; return result;
if (!self->intern) if (!self->intern)
return result; return result;
value = PyDict_GetItem(self->intern, result); value = PyDict_GetItemWithError(self->intern, result);
if (!value) { if (!value) {
if (PyDict_SetItem(self->intern, result, result) == 0) if (!PyErr_Occurred() &&
PyDict_SetItem(self->intern, result, result) == 0)
{
return result; return result;
}
else { else {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
@ -1604,13 +1607,18 @@ static int init_handler_descrs(void)
hi->getset.set = (setter)xmlparse_handler_setter; hi->getset.set = (setter)xmlparse_handler_setter;
hi->getset.closure = &handler_info[i]; hi->getset.closure = &handler_info[i];
PyObject *descr; PyObject *descr = PyDescr_NewGetSet(&Xmlparsetype, &hi->getset);
if (PyDict_GetItemString(Xmlparsetype.tp_dict, hi->name))
continue;
descr = PyDescr_NewGetSet(&Xmlparsetype, &hi->getset);
if (descr == NULL) if (descr == NULL)
return -1; 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) { if (PyDict_SetItem(Xmlparsetype.tp_dict, PyDescr_NAME(descr), descr) < 0) {
Py_DECREF(descr); Py_DECREF(descr);
return -1; return -1;
@ -1682,8 +1690,8 @@ MODULE_INITFUNC(void)
Py_DECREF(m); Py_DECREF(m);
return NULL; return NULL;
} }
errors_module = PyDict_GetItem(d, errmod_name); errors_module = PyDict_GetItemWithError(d, errmod_name);
if (errors_module == NULL) { if (errors_module == NULL && !PyErr_Occurred()) {
errors_module = PyModule_New(MODULE_NAME ".errors"); errors_module = PyModule_New(MODULE_NAME ".errors");
if (errors_module != NULL) { if (errors_module != NULL) {
_PyImport_SetModule(errmod_name, errors_module); _PyImport_SetModule(errmod_name, errors_module);
@ -1692,8 +1700,8 @@ MODULE_INITFUNC(void)
} }
} }
Py_DECREF(errmod_name); Py_DECREF(errmod_name);
model_module = PyDict_GetItem(d, modelmod_name); model_module = PyDict_GetItemWithError(d, modelmod_name);
if (model_module == NULL) { if (model_module == NULL && !PyErr_Occurred()) {
model_module = PyModule_New(MODULE_NAME ".model"); model_module = PyModule_New(MODULE_NAME ".model");
if (model_module != NULL) { if (model_module != NULL) {
_PyImport_SetModule(modelmod_name, model_module); _PyImport_SetModule(modelmod_name, model_module);

View File

@ -499,9 +499,11 @@ select_poll_modify_impl(pollObject *self, int fd, unsigned short eventmask)
key = PyLong_FromLong(fd); key = PyLong_FromLong(fd);
if (key == NULL) if (key == NULL)
return NULL; return NULL;
if (PyDict_GetItem(self->dict, key) == NULL) { if (PyDict_GetItemWithError(self->dict, key) == NULL) {
errno = ENOENT; if (!PyErr_Occurred()) {
PyErr_SetFromErrno(PyExc_OSError); errno = ENOENT;
PyErr_SetFromErrno(PyExc_OSError);
}
Py_DECREF(key); Py_DECREF(key);
return NULL; return NULL;
} }

View File

@ -78,11 +78,14 @@ static PyObject *
Xxo_getattro(XxoObject *self, PyObject *name) Xxo_getattro(XxoObject *self, PyObject *name)
{ {
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItem(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
return v; return v;
} }
else if (PyErr_Occurred()) {
return NULL;
}
} }
return PyObject_GenericGetAttr((PyObject *)self, name); return PyObject_GenericGetAttr((PyObject *)self, name);
} }
@ -97,7 +100,7 @@ Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
} }
if (v == NULL) { if (v == NULL) {
int rv = PyDict_DelItemString(self->x_attr, name); int rv = PyDict_DelItemString(self->x_attr, name);
if (rv < 0) if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
PyErr_SetString(PyExc_AttributeError, PyErr_SetString(PyExc_AttributeError,
"delete non-existing Xxo attribute"); "delete non-existing Xxo attribute");
return rv; return rv;

View File

@ -66,11 +66,14 @@ static PyObject *
Xxo_getattro(XxoObject *self, PyObject *name) Xxo_getattro(XxoObject *self, PyObject *name)
{ {
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItem(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
return v; return v;
} }
else if (PyErr_Occurred()) {
return NULL;
}
} }
return PyObject_GenericGetAttr((PyObject *)self, name); return PyObject_GenericGetAttr((PyObject *)self, name);
} }
@ -85,7 +88,7 @@ Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
} }
if (v == NULL) { if (v == NULL) {
int rv = PyDict_DelItemString(self->x_attr, name); int rv = PyDict_DelItemString(self->x_attr, name);
if (rv < 0) if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
PyErr_SetString(PyExc_AttributeError, PyErr_SetString(PyExc_AttributeError,
"delete non-existing Xxo attribute"); "delete non-existing Xxo attribute");
return rv; return rv;

View File

@ -1419,6 +1419,19 @@ _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
return PyDict_GetItemWithError(dp, kv); 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). /* Fast version of global value lookup (LOAD_GLOBAL).
* Lookup in globals, then builtins. * Lookup in globals, then builtins.
* *
@ -2358,14 +2371,21 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
value = PySequence_Fast_GET_ITEM(fast, 1); value = PySequence_Fast_GET_ITEM(fast, 1);
Py_INCREF(key); Py_INCREF(key);
Py_INCREF(value); Py_INCREF(value);
if (override || PyDict_GetItem(d, key) == NULL) { if (override) {
int status = PyDict_SetItem(d, key, value); if (PyDict_SetItem(d, key, value) < 0) {
if (status < 0) {
Py_DECREF(key); Py_DECREF(key);
Py_DECREF(value); Py_DECREF(value);
goto Fail; 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(key);
Py_DECREF(value); Py_DECREF(value);
Py_DECREF(fast); Py_DECREF(fast);
@ -2489,15 +2509,22 @@ dict_merge(PyObject *a, PyObject *b, int override)
return -1; return -1;
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
if (override != 1 && PyDict_GetItem(a, key) != NULL) { if (override != 1) {
if (override != 0) { if (PyDict_GetItemWithError(a, key) != NULL) {
_PyErr_SetKeyError(key); 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(key);
Py_DECREF(iter); Py_DECREF(iter);
return -1; return -1;
} }
Py_DECREF(key);
continue;
} }
value = PyObject_GetItem(b, key); value = PyObject_GetItem(b, key);
if (value == NULL) { if (value == NULL) {

View File

@ -975,7 +975,7 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (myerrno && PyLong_Check(myerrno) && if (myerrno && PyLong_Check(myerrno) &&
errnomap && (PyObject *) type == PyExc_OSError) { errnomap && (PyObject *) type == PyExc_OSError) {
PyObject *newtype; PyObject *newtype;
newtype = PyDict_GetItem(errnomap, myerrno); newtype = PyDict_GetItemWithError(errnomap, myerrno);
if (newtype) { if (newtype) {
assert(PyType_Check(newtype)); assert(PyType_Check(newtype));
type = (PyTypeObject *) newtype; type = (PyTypeObject *) newtype;

View File

@ -613,7 +613,7 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
} }
#endif #endif
if (back == NULL || back->f_globals != globals) { if (back == NULL || back->f_globals != globals) {
builtins = _PyDict_GetItemId(globals, &PyId___builtins__); builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__);
if (builtins) { if (builtins) {
if (PyModule_Check(builtins)) { if (PyModule_Check(builtins)) {
builtins = PyModule_GetDict(builtins); builtins = PyModule_GetDict(builtins);
@ -621,6 +621,9 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
} }
} }
if (builtins == NULL) { if (builtins == NULL) {
if (PyErr_Occurred()) {
return NULL;
}
/* No builtins! Make up a minimal one /* No builtins! Make up a minimal one
Give them 'None', at least. */ Give them 'None', at least. */
builtins = PyDict_New(); builtins = PyDict_New();

View File

@ -54,11 +54,15 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
/* __module__: If module name is in globals, use it. /* __module__: If module name is in globals, use it.
Otherwise, use None. */ Otherwise, use None. */
module = PyDict_GetItem(globals, __name__); module = PyDict_GetItemWithError(globals, __name__);
if (module) { if (module) {
Py_INCREF(module); Py_INCREF(module);
op->func_module = module; op->func_module = module;
} }
else if (PyErr_Occurred()) {
Py_DECREF(op);
return NULL;
}
if (qualname) if (qualname)
op->func_qualname = qualname; op->func_qualname = qualname;
else else

View File

@ -792,16 +792,17 @@ static PyObject *
module_dir(PyObject *self, PyObject *args) module_dir(PyObject *self, PyObject *args)
{ {
_Py_IDENTIFIER(__dict__); _Py_IDENTIFIER(__dict__);
_Py_IDENTIFIER(__dir__);
PyObject *result = NULL; PyObject *result = NULL;
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
if (dict != NULL) { if (dict != NULL) {
if (PyDict_Check(dict)) { if (PyDict_Check(dict)) {
PyObject *dirfunc = PyDict_GetItemString(dict, "__dir__"); PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
if (dirfunc) { if (dirfunc) {
result = _PyObject_CallNoArg(dirfunc); result = _PyObject_CallNoArg(dirfunc);
} }
else { else if (!PyErr_Occurred()) {
result = PyDict_Keys(dict); result = PyDict_Keys(dict);
} }
} }

View File

@ -102,9 +102,9 @@ namespace_repr(PyObject *ns)
if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) { if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
PyObject *value, *item; PyObject *value, *item;
value = PyDict_GetItem(d, key); value = PyDict_GetItemWithError(d, key);
if (value != NULL) { if (value != NULL) {
item = PyUnicode_FromFormat("%S=%R", key, value); item = PyUnicode_FromFormat("%U=%R", key, value);
if (item == NULL) { if (item == NULL) {
loop_error = 1; loop_error = 1;
} }
@ -113,6 +113,9 @@ namespace_repr(PyObject *ns)
Py_DECREF(item); Py_DECREF(item);
} }
} }
else if (PyErr_Occurred()) {
loop_error = 1;
}
} }
Py_DECREF(key); Py_DECREF(key);

View File

@ -1144,7 +1144,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
dictptr = _PyObject_GetDictPtr(obj); dictptr = _PyObject_GetDictPtr(obj);
if (dictptr != NULL && (dict = *dictptr) != NULL) { if (dictptr != NULL && (dict = *dictptr) != NULL) {
Py_INCREF(dict); Py_INCREF(dict);
attr = PyDict_GetItem(dict, name); attr = PyDict_GetItemWithError(dict, name);
if (attr != NULL) { if (attr != NULL) {
Py_INCREF(attr); Py_INCREF(attr);
*method = attr; *method = attr;
@ -1152,7 +1152,13 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
Py_XDECREF(descr); Py_XDECREF(descr);
return 0; return 0;
} }
Py_DECREF(dict); else {
Py_DECREF(dict);
if (PyErr_Occurred()) {
Py_XDECREF(descr);
return 0;
}
}
} }
if (meth_found) { if (meth_found) {
@ -1249,13 +1255,23 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
} }
if (dict != NULL) { if (dict != NULL) {
Py_INCREF(dict); Py_INCREF(dict);
res = PyDict_GetItem(dict, name); res = PyDict_GetItemWithError(dict, name);
if (res != NULL) { if (res != NULL) {
Py_INCREF(res); Py_INCREF(res);
Py_DECREF(dict); Py_DECREF(dict);
goto done; 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) { if (f != NULL) {
@ -1943,8 +1959,11 @@ Py_ReprEnter(PyObject *obj)
early on startup. */ early on startup. */
if (dict == NULL) if (dict == NULL)
return 0; return 0;
list = _PyDict_GetItemId(dict, &PyId_Py_Repr); list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
if (list == NULL) { if (list == NULL) {
if (PyErr_Occurred()) {
return -1;
}
list = PyList_New(0); list = PyList_New(0);
if (list == NULL) if (list == NULL)
return -1; return -1;
@ -1976,7 +1995,7 @@ Py_ReprLeave(PyObject *obj)
if (dict == NULL) if (dict == NULL)
goto finally; goto finally;
list = _PyDict_GetItemId(dict, &PyId_Py_Repr); list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
if (list == NULL || !PyList_Check(list)) if (list == NULL || !PyList_Check(list))
goto finally; goto finally;

View File

@ -491,9 +491,11 @@ type_module(PyTypeObject *type, void *context)
PyObject *mod; PyObject *mod;
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { 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) { if (mod == NULL) {
PyErr_Format(PyExc_AttributeError, "__module__"); if (!PyErr_Occurred()) {
PyErr_Format(PyExc_AttributeError, "__module__");
}
return NULL; return NULL;
} }
Py_INCREF(mod); Py_INCREF(mod);
@ -532,11 +534,13 @@ type_abstractmethods(PyTypeObject *type, void *context)
/* type itself has an __abstractmethods__ descriptor (this). Don't return /* type itself has an __abstractmethods__ descriptor (this). Don't return
that. */ that. */
if (type != &PyType_Type) if (type != &PyType_Type)
mod = _PyDict_GetItemId(type->tp_dict, &PyId___abstractmethods__); mod = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___abstractmethods__);
if (!mod) { if (!mod) {
PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); if (!PyErr_Occurred()) {
if (message) PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__);
PyErr_SetObject(PyExc_AttributeError, message); if (message)
PyErr_SetObject(PyExc_AttributeError, message);
}
return NULL; return NULL;
} }
Py_INCREF(mod); 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) { if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) {
return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); 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) { if (result == NULL) {
result = Py_None; if (!PyErr_Occurred()) {
Py_INCREF(result); result = Py_None;
Py_INCREF(result);
}
} }
else if (Py_TYPE(result)->tp_descr_get) { else if (Py_TYPE(result)->tp_descr_get) {
result = Py_TYPE(result)->tp_descr_get(result, NULL, result = Py_TYPE(result)->tp_descr_get(result, NULL,
@ -2452,13 +2458,16 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
goto error; goto error;
/* Check for a __slots__ sequence variable in dict, and count it */ /* 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; nslots = 0;
add_dict = 0; add_dict = 0;
add_weak = 0; add_weak = 0;
may_add_dict = base->tp_dictoffset == 0; may_add_dict = base->tp_dictoffset == 0;
may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0; may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
if (slots == NULL) { if (slots == NULL) {
if (PyErr_Occurred()) {
goto error;
}
if (may_add_dict) { if (may_add_dict) {
add_dict++; add_dict++;
} }
@ -2535,7 +2544,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
goto error; goto error;
} }
PyList_SET_ITEM(newslots, j, tmp); PyList_SET_ITEM(newslots, j, tmp);
if (PyDict_GetItem(dict, tmp)) { if (PyDict_GetItemWithError(dict, tmp)) {
/* CPython inserts __qualname__ and __classcell__ (when needed) /* CPython inserts __qualname__ and __classcell__ (when needed)
into the namespace when creating a class. They will be deleted into the namespace when creating a class. They will be deleted
below so won't act as class variables. */ below so won't act as class variables. */
@ -2548,6 +2557,10 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
goto error; goto error;
} }
} }
else if (PyErr_Occurred()) {
Py_DECREF(newslots);
goto error;
}
j++; j++;
} }
assert(j == nslots - add_dict - add_weak); assert(j == nslots - add_dict - add_weak);
@ -2632,22 +2645,28 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
type->tp_dict = dict; type->tp_dict = dict;
/* Set __module__ in the 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(); tmp = PyEval_GetGlobals();
if (tmp != NULL) { if (tmp != NULL) {
tmp = _PyDict_GetItemId(tmp, &PyId___name__); tmp = _PyDict_GetItemIdWithError(tmp, &PyId___name__);
if (tmp != NULL) { if (tmp != NULL) {
if (_PyDict_SetItemId(dict, &PyId___module__, if (_PyDict_SetItemId(dict, &PyId___module__,
tmp) < 0) tmp) < 0)
goto error; goto error;
} }
else if (PyErr_Occurred()) {
goto error;
}
} }
} }
/* Set ht_qualname to dict['__qualname__'] if available, else to /* Set ht_qualname to dict['__qualname__'] if available, else to
__name__. The __qualname__ accessor will look for ht_qualname. __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 (qualname != NULL) {
if (!PyUnicode_Check(qualname)) { if (!PyUnicode_Check(qualname)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
@ -2656,6 +2675,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
goto error; goto error;
} }
} }
else if (PyErr_Occurred()) {
goto error;
}
et->ht_qualname = qualname ? qualname : et->ht_name; et->ht_qualname = qualname ? qualname : et->ht_name;
Py_INCREF(et->ht_qualname); Py_INCREF(et->ht_qualname);
if (qualname != NULL && _PyDict_DelItemId(dict, &PyId___qualname__) < 0) 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__. 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)) { if (doc != NULL && PyUnicode_Check(doc)) {
Py_ssize_t len; Py_ssize_t len;
const char *doc_str; const char *doc_str;
@ -2685,11 +2707,14 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
memcpy(tp_doc, doc_str, len + 1); memcpy(tp_doc, doc_str, len + 1);
type->tp_doc = tp_doc; type->tp_doc = tp_doc;
} }
else if (doc == NULL && PyErr_Occurred()) {
goto error;
}
} }
/* Special-case __new__: if it's a plain function, /* Special-case __new__: if it's a plain function,
make it a static function */ make it a static function */
tmp = _PyDict_GetItemId(dict, &PyId___new__); tmp = _PyDict_GetItemIdWithError(dict, &PyId___new__);
if (tmp != NULL && PyFunction_Check(tmp)) { if (tmp != NULL && PyFunction_Check(tmp)) {
tmp = PyStaticMethod_New(tmp); tmp = PyStaticMethod_New(tmp);
if (tmp == NULL) if (tmp == NULL)
@ -2700,10 +2725,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
} }
Py_DECREF(tmp); Py_DECREF(tmp);
} }
else if (tmp == NULL && PyErr_Occurred()) {
goto error;
}
/* Special-case __init_subclass__ and __class_getitem__: /* Special-case __init_subclass__ and __class_getitem__:
if they are plain functions, make them classmethods */ 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)) { if (tmp != NULL && PyFunction_Check(tmp)) {
tmp = PyClassMethod_New(tmp); tmp = PyClassMethod_New(tmp);
if (tmp == NULL) if (tmp == NULL)
@ -2714,8 +2742,11 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
} }
Py_DECREF(tmp); 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)) { if (tmp != NULL && PyFunction_Check(tmp)) {
tmp = PyClassMethod_New(tmp); tmp = PyClassMethod_New(tmp);
if (tmp == NULL) if (tmp == NULL)
@ -2726,6 +2757,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
} }
Py_DECREF(tmp); Py_DECREF(tmp);
} }
else if (tmp == NULL && PyErr_Occurred()) {
goto error;
}
/* Add descriptors for custom slots from __slots__, or for __dict__ */ /* Add descriptors for custom slots from __slots__, or for __dict__ */
mp = PyHeapType_GET_MEMBERS(et); mp = PyHeapType_GET_MEMBERS(et);
@ -2797,7 +2831,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
type->tp_free = PyObject_Del; type->tp_free = PyObject_Del;
/* store type in class' cell if one is supplied */ /* store type in class' cell if one is supplied */
cell = _PyDict_GetItemId(dict, &PyId___classcell__); cell = _PyDict_GetItemIdWithError(dict, &PyId___classcell__);
if (cell != NULL) { if (cell != NULL) {
/* At least one method requires a reference to its defining class */ /* At least one method requires a reference to its defining class */
if (!PyCell_Check(cell)) { if (!PyCell_Check(cell)) {
@ -2807,8 +2841,12 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
goto error; goto error;
} }
PyCell_Set(cell, (PyObject *) type); PyCell_Set(cell, (PyObject *) type);
_PyDict_DelItemId(dict, &PyId___classcell__); if (_PyDict_DelItemId(dict, &PyId___classcell__) < 0) {
PyErr_Clear(); goto error;
}
}
else if (PyErr_Occurred()) {
goto error;
} }
/* Initialize the rest */ /* Initialize the rest */
@ -3741,47 +3779,41 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
} }
if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
PyObject *abstract_methods = NULL; PyObject *abstract_methods;
PyObject *builtins; PyObject *sorted_methods;
PyObject *sorted; PyObject *joined;
PyObject *sorted_methods = NULL;
PyObject *joined = NULL;
PyObject *comma; PyObject *comma;
_Py_static_string(comma_id, ", "); _Py_static_string(comma_id, ", ");
_Py_IDENTIFIER(sorted);
/* Compute ", ".join(sorted(type.__abstractmethods__)) /* Compute ", ".join(sorted(type.__abstractmethods__))
into joined. */ into joined. */
abstract_methods = type_abstractmethods(type, NULL); abstract_methods = type_abstractmethods(type, NULL);
if (abstract_methods == NULL) if (abstract_methods == NULL)
goto error; return NULL;
builtins = PyEval_GetBuiltins(); sorted_methods = PySequence_List(abstract_methods);
if (builtins == NULL) Py_DECREF(abstract_methods);
goto error;
sorted = _PyDict_GetItemId(builtins, &PyId_sorted);
if (sorted == NULL)
goto error;
sorted_methods = PyObject_CallFunctionObjArgs(sorted,
abstract_methods,
NULL);
if (sorted_methods == NULL) if (sorted_methods == NULL)
goto error; return NULL;
if (PyList_Sort(sorted_methods)) {
Py_DECREF(sorted_methods);
return NULL;
}
comma = _PyUnicode_FromId(&comma_id); comma = _PyUnicode_FromId(&comma_id);
if (comma == NULL) if (comma == NULL) {
goto error; Py_DECREF(sorted_methods);
return NULL;
}
joined = PyUnicode_Join(comma, sorted_methods); joined = PyUnicode_Join(comma, sorted_methods);
Py_DECREF(sorted_methods);
if (joined == NULL) if (joined == NULL)
goto error; return NULL;
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"Can't instantiate abstract class %s " "Can't instantiate abstract class %s "
"with abstract methods %U", "with abstract methods %U",
type->tp_name, type->tp_name,
joined); joined);
error: Py_DECREF(joined);
Py_XDECREF(joined);
Py_XDECREF(sorted_methods);
Py_XDECREF(abstract_methods);
return NULL; return NULL;
} }
return type->tp_alloc(type, 0); return type->tp_alloc(type, 0);
@ -4610,14 +4642,12 @@ object___reduce_ex___impl(PyObject *self, int protocol)
if (objreduce == NULL) { if (objreduce == NULL) {
objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict, objreduce = _PyDict_GetItemId(PyBaseObject_Type.tp_dict,
&PyId___reduce__); &PyId___reduce__);
if (objreduce == NULL)
return NULL;
} }
reduce = _PyObject_GetAttrId(self, &PyId___reduce__); if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) {
if (reduce == NULL) return NULL;
PyErr_Clear(); }
else { if (reduce != NULL) {
PyObject *cls, *clsreduce; PyObject *cls, *clsreduce;
int override; int override;
@ -4829,14 +4859,12 @@ static int
add_methods(PyTypeObject *type, PyMethodDef *meth) add_methods(PyTypeObject *type, PyMethodDef *meth)
{ {
PyObject *dict = type->tp_dict; PyObject *dict = type->tp_dict;
PyObject *name;
for (; meth->ml_name != NULL; meth++) { for (; meth->ml_name != NULL; meth++) {
PyObject *descr; PyObject *descr;
int err; int err;
int isdescr = 1; 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_CLASS) {
if (meth->ml_flags & METH_STATIC) { if (meth->ml_flags & METH_STATIC) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
@ -4846,7 +4874,7 @@ add_methods(PyTypeObject *type, PyMethodDef *meth)
descr = PyDescr_NewClassMethod(type, meth); descr = PyDescr_NewClassMethod(type, meth);
} }
else if (meth->ml_flags & METH_STATIC) { 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) if (cfunc == NULL)
return -1; return -1;
descr = PyStaticMethod_New(cfunc); descr = PyStaticMethod_New(cfunc);
@ -4858,11 +4886,36 @@ add_methods(PyTypeObject *type, PyMethodDef *meth)
} }
if (descr == NULL) if (descr == NULL)
return -1; return -1;
if (isdescr) { if (isdescr) {
err = PyDict_SetItem(dict, PyDescr_NAME(descr), descr); name = PyDescr_NAME(descr);
} }
else { 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); Py_DECREF(descr);
if (err < 0) if (err < 0)
@ -4877,12 +4930,18 @@ add_members(PyTypeObject *type, PyMemberDef *memb)
PyObject *dict = type->tp_dict; PyObject *dict = type->tp_dict;
for (; memb->name != NULL; memb++) { for (; memb->name != NULL; memb++) {
PyObject *descr; PyObject *descr = PyDescr_NewMember(type, memb);
if (PyDict_GetItemString(dict, memb->name))
continue;
descr = PyDescr_NewMember(type, memb);
if (descr == NULL) if (descr == NULL)
return -1; 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) { if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) {
Py_DECREF(descr); Py_DECREF(descr);
return -1; return -1;
@ -4898,13 +4957,17 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
PyObject *dict = type->tp_dict; PyObject *dict = type->tp_dict;
for (; gsp->name != NULL; gsp++) { for (; gsp->name != NULL; gsp++) {
PyObject *descr; PyObject *descr = PyDescr_NewGetSet(type, gsp);
if (PyDict_GetItemString(dict, gsp->name))
continue;
descr = PyDescr_NewGetSet(type, gsp);
if (descr == NULL) if (descr == NULL)
return -1; 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) { if (PyDict_SetItem(dict, PyDescr_NAME(descr), descr) < 0) {
Py_DECREF(descr); Py_DECREF(descr);
return -1; return -1;
@ -5309,7 +5372,10 @@ PyType_Ready(PyTypeObject *type)
/* if the type dictionary doesn't contain a __doc__, set it from /* if the type dictionary doesn't contain a __doc__, set it from
the tp_doc slot. 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) { if (type->tp_doc != NULL) {
const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, const char *old_doc = _PyType_DocWithoutSignature(type->tp_name,
type->tp_doc); type->tp_doc);
@ -5335,9 +5401,12 @@ PyType_Ready(PyTypeObject *type)
This signals that __hash__ is not inherited. This signals that __hash__ is not inherited.
*/ */
if (type->tp_hash == NULL) { if (type->tp_hash == NULL) {
if (_PyDict_GetItemId(type->tp_dict, &PyId___hash__) == NULL) { if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___hash__) == NULL) {
if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) if (PyErr_Occurred() ||
_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0)
{
goto error; goto error;
}
type->tp_hash = PyObject_HashNotImplemented; type->tp_hash = PyObject_HashNotImplemented;
} }
} }
@ -5988,8 +6057,10 @@ add_tp_new_wrapper(PyTypeObject *type)
{ {
PyObject *func; PyObject *func;
if (_PyDict_GetItemId(type->tp_dict, &PyId___new__) != NULL) if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___new__) != NULL)
return 0; return 0;
if (PyErr_Occurred())
return -1;
func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL);
if (func == NULL) if (func == NULL)
return -1; return -1;
@ -7414,9 +7485,14 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *name,
assert(PyType_Check(subclass)); assert(PyType_Check(subclass));
/* Avoid recursing down into unaffected classes */ /* Avoid recursing down into unaffected classes */
dict = subclass->tp_dict; dict = subclass->tp_dict;
if (dict != NULL && PyDict_Check(dict) && if (dict != NULL && PyDict_Check(dict)) {
PyDict_GetItem(dict, name) != NULL) if (PyDict_GetItemWithError(dict, name) != NULL) {
continue; continue;
}
if (PyErr_Occurred()) {
return -1;
}
}
if (update_subclasses(subclass, name, callback, data) < 0) if (update_subclasses(subclass, name, callback, data) < 0)
return -1; return -1;
} }
@ -7468,8 +7544,11 @@ add_operators(PyTypeObject *type)
ptr = slotptr(type, p->offset); ptr = slotptr(type, p->offset);
if (!ptr || !*ptr) if (!ptr || !*ptr)
continue; continue;
if (PyDict_GetItem(dict, p->name_strobj)) if (PyDict_GetItemWithError(dict, p->name_strobj))
continue; continue;
if (PyErr_Occurred()) {
return -1;
}
if (*ptr == (void *)PyObject_HashNotImplemented) { if (*ptr == (void *)PyObject_HashNotImplemented) {
/* Classes may prevent the inheritance of the tp_hash /* Classes may prevent the inheritance of the tp_hash
slot by storing PyObject_HashNotImplemented in it. Make it slot by storing PyObject_HashNotImplemented in it. Make it
@ -7579,7 +7658,7 @@ super_getattro(PyObject *self, PyObject *name)
goto skip; goto skip;
/* keep a strong reference to mro because starttype->tp_mro can be /* 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); Py_INCREF(mro);
do { do {
PyObject *res, *tmp, *dict; PyObject *res, *tmp, *dict;
@ -7591,7 +7670,7 @@ super_getattro(PyObject *self, PyObject *name)
dict = ((PyTypeObject *)tmp)->tp_dict; dict = ((PyTypeObject *)tmp)->tp_dict;
assert(dict != NULL && PyDict_Check(dict)); assert(dict != NULL && PyDict_Check(dict));
res = PyDict_GetItem(dict, name); res = PyDict_GetItemWithError(dict, name);
if (res != NULL) { if (res != NULL) {
Py_INCREF(res); Py_INCREF(res);
@ -7609,6 +7688,9 @@ super_getattro(PyObject *self, PyObject *name)
Py_DECREF(mro); Py_DECREF(mro);
return res; return res;
} }
else if (PyErr_Occurred()) {
return NULL;
}
i++; i++;
} while (i < n); } while (i < n);

View File

@ -252,7 +252,7 @@ already_warned(PyObject *registry, PyObject *key, int should_set)
if (key == NULL) if (key == NULL)
return -1; return -1;
version_obj = _PyDict_GetItemId(registry, &PyId_version); version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version);
if (version_obj == NULL if (version_obj == NULL
|| !PyLong_CheckExact(version_obj) || !PyLong_CheckExact(version_obj)
|| PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) || 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); Py_DECREF(version_obj);
} }
else { else {
already_warned = PyDict_GetItem(registry, key); already_warned = PyDict_GetItemWithError(registry, key);
if (already_warned != NULL) { if (already_warned != NULL) {
int rc = PyObject_IsTrue(already_warned); int rc = PyObject_IsTrue(already_warned);
if (rc != 0) if (rc != 0)
return rc; return rc;
} }
else if (PyErr_Occurred()) {
return -1;
}
} }
/* This warning wasn't found in the registry, set it. */ /* 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, setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
PyObject **module, PyObject **registry) PyObject **module, PyObject **registry)
{ {
_Py_IDENTIFIER(__warningregistry__);
_Py_IDENTIFIER(__name__);
PyObject *globals; PyObject *globals;
/* Setup globals, filename and lineno. */ /* Setup globals, filename and lineno. */
@ -706,15 +711,18 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
/* Setup registry. */ /* Setup registry. */
assert(globals != NULL); assert(globals != NULL);
assert(PyDict_Check(globals)); assert(PyDict_Check(globals));
*registry = PyDict_GetItemString(globals, "__warningregistry__"); *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__);
if (*registry == NULL) { if (*registry == NULL) {
int rc; int rc;
if (PyErr_Occurred()) {
return 0;
}
*registry = PyDict_New(); *registry = PyDict_New();
if (*registry == NULL) if (*registry == NULL)
return 0; return 0;
rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry);
if (rc < 0) if (rc < 0)
goto handle_error; goto handle_error;
} }
@ -722,10 +730,13 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
Py_INCREF(*registry); Py_INCREF(*registry);
/* Setup module. */ /* Setup module. */
*module = PyDict_GetItemString(globals, "__name__"); *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
Py_INCREF(*module); Py_INCREF(*module);
} }
else if (PyErr_Occurred()) {
goto handle_error;
}
else { else {
*module = PyUnicode_FromString("<string>"); *module = PyUnicode_FromString("<string>");
if (*module == NULL) if (*module == NULL)

View File

@ -142,7 +142,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
return NULL; return NULL;
} }
meta = _PyDict_GetItemId(mkw, &PyId_metaclass); meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass);
if (meta != NULL) { if (meta != NULL) {
Py_INCREF(meta); Py_INCREF(meta);
if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) { 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 */ /* metaclass is explicitly given, check if it's indeed a class */
isclass = PyType_Check(meta); isclass = PyType_Check(meta);
} }
else if (PyErr_Occurred()) {
Py_DECREF(mkw);
Py_DECREF(bases);
return NULL;
}
} }
if (meta == NULL) { if (meta == NULL) {
/* if there are no bases, use type: */ /* if there are no bases, use type: */
@ -956,11 +961,14 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
return NULL; return NULL;
} }
if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) { if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
if (_PyDict_SetItemId(globals, &PyId___builtins__, if (_PyDict_SetItemId(globals, &PyId___builtins__,
PyEval_GetBuiltins()) != 0) PyEval_GetBuiltins()) != 0)
return NULL; return NULL;
} }
else if (PyErr_Occurred()) {
return NULL;
}
if (PyCode_Check(source)) { if (PyCode_Check(source)) {
if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
@ -1036,11 +1044,14 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
locals->ob_type->tp_name); locals->ob_type->tp_name);
return NULL; return NULL;
} }
if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) { if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
if (_PyDict_SetItemId(globals, &PyId___builtins__, if (_PyDict_SetItemId(globals, &PyId___builtins__,
PyEval_GetBuiltins()) != 0) PyEval_GetBuiltins()) != 0)
return NULL; return NULL;
} }
else if (PyErr_Occurred()) {
return NULL;
}
if (PyCode_Check(source)) { if (PyCode_Check(source)) {
if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {

View File

@ -2090,10 +2090,12 @@ main_loop:
PyObject *bc; PyObject *bc;
if (PyDict_CheckExact(f->f_builtins)) { 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) { if (bc == NULL) {
PyErr_SetString(PyExc_NameError, if (!PyErr_Occurred()) {
"__build_class__ not found"); PyErr_SetString(PyExc_NameError,
"__build_class__ not found");
}
goto error; goto error;
} }
Py_INCREF(bc); Py_INCREF(bc);
@ -2241,8 +2243,10 @@ main_loop:
int err; int err;
err = PyDict_DelItem(f->f_globals, name); err = PyDict_DelItem(f->f_globals, name);
if (err != 0) { if (err != 0) {
format_exc_check_arg( if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyExc_NameError, NAME_ERROR_MSG, name); format_exc_check_arg(
PyExc_NameError, NAME_ERROR_MSG, name);
}
goto error; goto error;
} }
DISPATCH(); DISPATCH();
@ -2258,8 +2262,13 @@ main_loop:
goto error; goto error;
} }
if (PyDict_CheckExact(locals)) { if (PyDict_CheckExact(locals)) {
v = PyDict_GetItem(locals, name); v = PyDict_GetItemWithError(locals, name);
Py_XINCREF(v); if (v != NULL) {
Py_INCREF(v);
}
else if (PyErr_Occurred()) {
goto error;
}
} }
else { else {
v = PyObject_GetItem(locals, name); v = PyObject_GetItem(locals, name);
@ -2270,15 +2279,22 @@ main_loop:
} }
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItem(f->f_globals, name); v = PyDict_GetItemWithError(f->f_globals, name);
Py_XINCREF(v); if (v != NULL) {
if (v == NULL) { Py_INCREF(v);
}
else if (PyErr_Occurred()) {
goto error;
}
else {
if (PyDict_CheckExact(f->f_builtins)) { if (PyDict_CheckExact(f->f_builtins)) {
v = PyDict_GetItem(f->f_builtins, name); v = PyDict_GetItemWithError(f->f_builtins, name);
if (v == NULL) { if (v == NULL) {
format_exc_check_arg( if (!PyErr_Occurred()) {
format_exc_check_arg(
PyExc_NameError, PyExc_NameError,
NAME_ERROR_MSG, name); NAME_ERROR_MSG, name);
}
goto error; goto error;
} }
Py_INCREF(v); Py_INCREF(v);
@ -2386,8 +2402,13 @@ main_loop:
assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
name = PyTuple_GET_ITEM(co->co_freevars, idx); name = PyTuple_GET_ITEM(co->co_freevars, idx);
if (PyDict_CheckExact(locals)) { if (PyDict_CheckExact(locals)) {
value = PyDict_GetItem(locals, name); value = PyDict_GetItemWithError(locals, name);
Py_XINCREF(value); if (value != NULL) {
Py_INCREF(value);
}
else if (PyErr_Occurred()) {
goto error;
}
} }
else { else {
value = PyObject_GetItem(locals, name); value = PyObject_GetItem(locals, name);
@ -2591,9 +2612,12 @@ main_loop:
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
if (PyDict_CheckExact(f->f_locals)) { if (PyDict_CheckExact(f->f_locals)) {
ann_dict = _PyDict_GetItemId(f->f_locals, ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
&PyId___annotations__); &PyId___annotations__);
if (ann_dict == NULL) { if (ann_dict == NULL) {
if (PyErr_Occurred()) {
goto error;
}
/* ...if not, create a new one */ /* ...if not, create a new one */
ann_dict = PyDict_New(); ann_dict = PyDict_New();
if (ann_dict == NULL) { if (ann_dict == NULL) {
@ -3921,12 +3945,15 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
continue; continue;
name = PyTuple_GET_ITEM(co->co_varnames, i); name = PyTuple_GET_ITEM(co->co_varnames, i);
if (kwdefs != NULL) { if (kwdefs != NULL) {
PyObject *def = PyDict_GetItem(kwdefs, name); PyObject *def = PyDict_GetItemWithError(kwdefs, name);
if (def) { if (def) {
Py_INCREF(def); Py_INCREF(def);
SETLOCAL(i, def); SETLOCAL(i, def);
continue; continue;
} }
else if (PyErr_Occurred()) {
goto fail;
}
} }
missing++; missing++;
} }
@ -4861,9 +4888,11 @@ import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *leve
PyObject *import_func, *res; PyObject *import_func, *res;
PyObject* stack[5]; 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) { if (import_func == NULL) {
PyErr_SetString(PyExc_ImportError, "__import__ not found"); if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ImportError, "__import__ not found");
}
return NULL; return NULL;
} }
@ -5207,10 +5236,13 @@ unicode_concatenate(PyObject *v, PyObject *w,
PyObject *names = f->f_code->co_names; PyObject *names = f->f_code->co_names;
PyObject *name = GETITEM(names, oparg); PyObject *name = GETITEM(names, oparg);
PyObject *locals = f->f_locals; PyObject *locals = f->f_locals;
if (locals && PyDict_CheckExact(locals) && if (locals && PyDict_CheckExact(locals)) {
PyDict_GetItem(locals, name) == v) { PyObject *w = PyDict_GetItemWithError(locals, name);
if (PyDict_DelItem(locals, name) != 0) { if ((w == v && PyDict_DelItem(locals, name) != 0) ||
PyErr_Clear(); (w == NULL && PyErr_Occurred()))
{
Py_DECREF(v);
return NULL;
} }
} }
break; break;

View File

@ -120,12 +120,16 @@ PyObject *_PyCodec_Lookup(const char *encoding)
PyUnicode_InternInPlace(&v); PyUnicode_InternInPlace(&v);
/* First, try to lookup the name in the registry dictionary */ /* 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) { if (result != NULL) {
Py_INCREF(result); Py_INCREF(result);
Py_DECREF(v); Py_DECREF(v);
return result; return result;
} }
else if (PyErr_Occurred()) {
Py_DECREF(v);
return NULL;
}
/* Next, scan the search functions in order of registration */ /* Next, scan the search functions in order of registration */
args = PyTuple_New(1); args = PyTuple_New(1);
@ -648,11 +652,13 @@ PyObject *PyCodec_LookupError(const char *name)
if (name==NULL) if (name==NULL)
name = "strict"; name = "strict";
handler = PyDict_GetItemString(interp->codec_error_registry, name); handler = _PyDict_GetItemStringWithError(interp->codec_error_registry, name);
if (!handler) if (handler) {
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
else
Py_INCREF(handler); Py_INCREF(handler);
}
else if (!PyErr_Occurred()) {
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
}
return handler; return handler;
} }

View File

@ -858,6 +858,7 @@ PyErr_Format(PyObject *exception, const char *format, ...)
PyObject * PyObject *
PyErr_NewException(const char *name, PyObject *base, PyObject *dict) PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
{ {
_Py_IDENTIFIER(__module__);
const char *dot; const char *dot;
PyObject *modulename = NULL; PyObject *modulename = NULL;
PyObject *classname = NULL; PyObject *classname = NULL;
@ -877,12 +878,15 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
if (dict == NULL) if (dict == NULL)
goto failure; goto failure;
} }
if (PyDict_GetItemString(dict, "__module__") == NULL) { if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
if (PyErr_Occurred()) {
goto failure;
}
modulename = PyUnicode_FromStringAndSize(name, modulename = PyUnicode_FromStringAndSize(name,
(Py_ssize_t)(dot-name)); (Py_ssize_t)(dot-name));
if (modulename == NULL) if (modulename == NULL)
goto failure; goto failure;
if (PyDict_SetItemString(dict, "__module__", modulename) != 0) if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0)
goto failure; goto failure;
} }
if (PyTuple_Check(base)) { if (PyTuple_Check(base)) {

View File

@ -1763,9 +1763,13 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
current_arg = PyTuple_GET_ITEM(args, i); current_arg = PyTuple_GET_ITEM(args, i);
} }
else if (nkwargs && i >= pos) { else if (nkwargs && i >= pos) {
current_arg = PyDict_GetItemString(kwargs, kwlist[i]); current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
if (current_arg) if (current_arg) {
--nkwargs; --nkwargs;
}
else if (PyErr_Occurred()) {
return cleanreturn(0, &freelist);
}
} }
else { else {
current_arg = NULL; current_arg = NULL;
@ -1844,7 +1848,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Py_ssize_t j; Py_ssize_t j;
/* make sure there are no arguments given by name and position */ /* make sure there are no arguments given by name and position */
for (i = pos; i < nargs; i++) { for (i = pos; i < nargs; i++) {
current_arg = PyDict_GetItemString(kwargs, kwlist[i]); current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
if (current_arg) { if (current_arg) {
/* arg present in tuple and in dict */ /* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
@ -1855,6 +1859,9 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
kwlist[i], i+1); kwlist[i], i+1);
return cleanreturn(0, &freelist); return cleanreturn(0, &freelist);
} }
else if (PyErr_Occurred()) {
return cleanreturn(0, &freelist);
}
} }
/* make sure there are no extraneous keyword arguments */ /* make sure there are no extraneous keyword arguments */
j = 0; j = 0;
@ -2016,13 +2023,10 @@ parser_clear(struct _PyArg_Parser *parser)
} }
static PyObject* 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; Py_ssize_t i, nkwargs;
if (kwargs != NULL) {
return PyDict_GetItem(kwargs, key);
}
nkwargs = PyTuple_GET_SIZE(kwnames); nkwargs = PyTuple_GET_SIZE(kwnames);
for (i=0; i < nkwargs; i++) { for (i=0; i < nkwargs; i++) {
PyObject *kwname = PyTuple_GET_ITEM(kwnames, 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) { else if (nkwargs && i >= pos) {
keyword = PyTuple_GET_ITEM(kwtuple, i - pos); keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); if (kwargs != NULL) {
if (current_arg) 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; --nkwargs;
}
} }
else { else {
current_arg = NULL; 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 */ /* make sure there are no arguments given by name and position */
for (i = pos; i < nargs; i++) { for (i = pos; i < nargs; i++) {
keyword = PyTuple_GET_ITEM(kwtuple, i - pos); 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) { if (current_arg) {
/* arg present in tuple and in dict */ /* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,

View File

@ -431,9 +431,13 @@ PyImport_Cleanup(void)
for (p = sys_files; *p != NULL; p+=2) { for (p = sys_files; *p != NULL; p+=2) {
if (Py_VerboseFlag) if (Py_VerboseFlag)
PySys_WriteStderr("# restore sys.%s\n", *p); PySys_WriteStderr("# restore sys.%s\n", *p);
value = PyDict_GetItemString(interp->sysdict, *(p+1)); value = _PyDict_GetItemStringWithError(interp->sysdict, *(p+1));
if (value == NULL) if (value == NULL) {
if (PyErr_Occurred()) {
PyErr_WriteUnraisable(NULL);
}
value = Py_None; value = Py_None;
}
if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) { if (PyDict_SetItemString(interp->sysdict, *p, value) < 0) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
@ -718,7 +722,7 @@ _PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
key = PyTuple_Pack(2, filename, name); key = PyTuple_Pack(2, filename, name);
if (key == NULL) if (key == NULL)
return NULL; return NULL;
def = (PyModuleDef *)PyDict_GetItem(extensions, key); def = (PyModuleDef *)PyDict_GetItemWithError(extensions, key);
Py_DECREF(key); Py_DECREF(key);
if (def == NULL) if (def == NULL)
return NULL; return NULL;
@ -927,6 +931,7 @@ error:
static PyObject * static PyObject *
module_dict_for_exec(PyObject *name) module_dict_for_exec(PyObject *name)
{ {
_Py_IDENTIFIER(__builtins__);
PyObject *m, *d = NULL; PyObject *m, *d = NULL;
m = PyImport_AddModuleObject(name); 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 /* If the module is being reloaded, we get the old module back
and re-use its dict to exec the new code. */ and re-use its dict to exec the new code. */
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (PyDict_GetItemString(d, "__builtins__") == NULL) { if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) {
if (PyDict_SetItemString(d, "__builtins__", if (PyErr_Occurred() ||
PyEval_GetBuiltins()) != 0) { _PyDict_SetItemId(d, &PyId___builtins__,
PyEval_GetBuiltins()) != 0)
{
remove_module(name); remove_module(name);
return NULL; return NULL;
} }
@ -1107,8 +1114,8 @@ get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
if (nhooks < 0) if (nhooks < 0)
return NULL; /* Shouldn't happen */ return NULL; /* Shouldn't happen */
importer = PyDict_GetItem(path_importer_cache, p); importer = PyDict_GetItemWithError(path_importer_cache, p);
if (importer != NULL) if (importer != NULL || PyErr_Occurred())
return importer; return importer;
/* set path_importer_cache[p] to None to avoid recursion */ /* 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"); PyErr_SetString(PyExc_TypeError, "globals must be a dict");
goto error; goto error;
} }
package = _PyDict_GetItemId(globals, &PyId___package__); package = _PyDict_GetItemIdWithError(globals, &PyId___package__);
if (package == Py_None) { if (package == Py_None) {
package = NULL; 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) { if (package != NULL) {
Py_INCREF(package); Py_INCREF(package);
@ -1546,9 +1559,11 @@ resolve_name(PyObject *name, PyObject *globals, int level)
goto error; goto error;
} }
package = _PyDict_GetItemId(globals, &PyId___name__); package = _PyDict_GetItemIdWithError(globals, &PyId___name__);
if (package == NULL) { if (package == NULL) {
PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
}
goto error; goto error;
} }
@ -1558,10 +1573,10 @@ resolve_name(PyObject *name, PyObject *globals, int level)
goto error; goto error;
} }
if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) {
Py_ssize_t dot; Py_ssize_t dot;
if (PyUnicode_READY(package) < 0) { if (PyErr_Occurred() || PyUnicode_READY(package) < 0) {
goto error; goto error;
} }

View File

@ -359,12 +359,12 @@ PySymtable_Lookup(struct symtable *st, void *key)
k = PyLong_FromVoidPtr(key); k = PyLong_FromVoidPtr(key);
if (k == NULL) if (k == NULL)
return NULL; return NULL;
v = PyDict_GetItem(st->st_blocks, k); v = PyDict_GetItemWithError(st->st_blocks, k);
if (v) { if (v) {
assert(PySTEntry_Check(v)); assert(PySTEntry_Check(v));
Py_INCREF(v); Py_INCREF(v);
} }
else { else if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_KeyError, PyErr_SetString(PyExc_KeyError,
"unknown symbol table entry"); "unknown symbol table entry");
} }
@ -637,7 +637,7 @@ update_symbols(PyObject *symbols, PyObject *scopes,
} }
while ((name = PyIter_Next(itr))) { while ((name = PyIter_Next(itr))) {
v = PyDict_GetItem(symbols, name); v = PyDict_GetItemWithError(symbols, name);
/* Handle symbol that already exists in this scope */ /* Handle symbol that already exists in this scope */
if (v) { if (v) {
@ -662,6 +662,9 @@ update_symbols(PyObject *symbols, PyObject *scopes,
Py_DECREF(name); Py_DECREF(name);
continue; continue;
} }
else if (PyErr_Occurred()) {
goto error;
}
/* Handle global symbol */ /* Handle global symbol */
if (bound && !PySet_Contains(bound, name)) { if (bound && !PySet_Contains(bound, name)) {
Py_DECREF(name); Py_DECREF(name);
@ -991,7 +994,7 @@ symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _s
if (!mangled) if (!mangled)
return 0; return 0;
dict = ste->ste_symbols; dict = ste->ste_symbols;
if ((o = PyDict_GetItem(dict, mangled))) { if ((o = PyDict_GetItemWithError(dict, mangled))) {
val = PyLong_AS_LONG(o); val = PyLong_AS_LONG(o);
if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
/* Is it better to use 'mangled' or 'name' here? */ /* 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; goto error;
} }
val |= flag; val |= flag;
} else }
else if (PyErr_Occurred()) {
goto error;
}
else {
val = flag; val = flag;
}
o = PyLong_FromLong(val); o = PyLong_FromLong(val);
if (o == NULL) if (o == NULL)
goto error; goto error;