summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-02-25 15:59:46 (GMT)
committerGitHub <noreply@github.com>2019-02-25 15:59:46 (GMT)
commita24107b04c1277e3c1105f98aff5bfa3a98b33a0 (patch)
tree55aa5a700e08e3ba27b0361df2b1043be5c4701a /Objects
parenta180b007d96fe68b32f11dec720fbd0cd5b6758a (diff)
downloadcpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.zip
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.gz
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.bz2
bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/dictobject.c43
-rw-r--r--Objects/exceptions.c2
-rw-r--r--Objects/frameobject.c5
-rw-r--r--Objects/funcobject.c6
-rw-r--r--Objects/moduleobject.c5
-rw-r--r--Objects/namespaceobject.c7
-rw-r--r--Objects/object.c31
-rw-r--r--Objects/typeobject.c234
8 files changed, 236 insertions, 97 deletions
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);