summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-09-01 14:01:05 +0300
committerGitHub <noreply@github.com>2019-09-01 14:01:05 +0300
commit353053d9ad08fea0e205e6c008b8a4350c0188e6 (patch)
tree3bd4434c152e934fb260c1c1651c080c3df29a14 /Objects
parent6922b9e4fce635339cb94c2fdef6bba4e2a99621 (diff)
downloadcpython-git-353053d9ad08fea0e205e6c008b8a4350c0188e6.tar.gz
[3.8] bpo-37994: Fix silencing all errors if an attribute lookup fails. (GH-15630) (GH-15635)
Only AttributeError should be silenced. (cherry picked from commit 41c57b335330ff48af098d47e379e0f9ba09d233)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c5
-rw-r--r--Objects/descrobject.c34
-rw-r--r--Objects/fileobject.c6
-rw-r--r--Objects/setobject.c5
-rw-r--r--Objects/typeobject.c129
-rw-r--r--Objects/weakrefobject.c6
6 files changed, 95 insertions, 90 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 1bb19a9271..590b806056 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2063,9 +2063,10 @@ _common_reduce(PyByteArrayObject *self, int proto)
_Py_IDENTIFIER(__dict__);
char *buf;
- dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
+ if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
+ return NULL;
+ }
if (dict == NULL) {
- PyErr_Clear();
dict = Py_None;
Py_INCREF(dict);
}
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index dbb44de846..119be35db0 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1622,29 +1622,25 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
/* if no docstring given and the getter has one, use that one */
if ((doc == NULL || doc == Py_None) && fget != NULL) {
_Py_IDENTIFIER(__doc__);
- PyObject *get_doc = _PyObject_GetAttrId(fget, &PyId___doc__);
- if (get_doc) {
- if (Py_TYPE(self) == &PyProperty_Type) {
- Py_XSETREF(self->prop_doc, get_doc);
- }
- else {
- /* If this is a property subclass, put __doc__
- in dict of the subclass instance instead,
- otherwise it gets shadowed by __doc__ in the
- class's dict. */
- int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
- Py_DECREF(get_doc);
- if (err < 0)
- return -1;
- }
- self->getter_doc = 1;
+ PyObject *get_doc;
+ int rc = _PyObject_LookupAttrId(fget, &PyId___doc__, &get_doc);
+ if (rc <= 0) {
+ return rc;
}
- else if (PyErr_ExceptionMatches(PyExc_Exception)) {
- PyErr_Clear();
+ if (Py_TYPE(self) == &PyProperty_Type) {
+ Py_XSETREF(self->prop_doc, get_doc);
}
else {
- return -1;
+ /* If this is a property subclass, put __doc__
+ in dict of the subclass instance instead,
+ otherwise it gets shadowed by __doc__ in the
+ class's dict. */
+ int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
+ Py_DECREF(get_doc);
+ if (err < 0)
+ return -1;
}
+ self->getter_doc = 1;
}
return 0;
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 3791241e5c..dd42d51689 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -185,8 +185,10 @@ PyObject_AsFileDescriptor(PyObject *o)
if (PyLong_Check(o)) {
fd = _PyLong_AsInt(o);
}
- else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL)
- {
+ else if (_PyObject_LookupAttrId(o, &PyId_fileno, &meth) < 0) {
+ return -1;
+ }
+ else if (meth != NULL) {
PyObject *fno = _PyObject_CallNoArg(meth);
Py_DECREF(meth);
if (fno == NULL)
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 8cd95ba890..f8ae0c05e2 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1954,9 +1954,10 @@ set_reduce(PySetObject *so, PyObject *Py_UNUSED(ignored))
args = PyTuple_Pack(1, keys);
if (args == NULL)
goto done;
- dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
+ if (_PyObject_LookupAttrId((PyObject *)so, &PyId___dict__, &dict) < 0) {
+ goto done;
+ }
if (dict == NULL) {
- PyErr_Clear();
dict = Py_None;
Py_INCREF(dict);
}
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index bf0c3e0729..8ece218275 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1547,17 +1547,10 @@ tail_contains(PyObject *tuple, int whence, PyObject *o)
static PyObject *
class_name(PyObject *cls)
{
- PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__);
- if (name == NULL) {
- PyErr_Clear();
+ PyObject *name;
+ if (_PyObject_LookupAttrId(cls, &PyId___name__, &name) == 0) {
name = PyObject_Repr(cls);
}
- if (name == NULL)
- return NULL;
- if (!PyUnicode_Check(name)) {
- Py_DECREF(name);
- return NULL;
- }
return name;
}
@@ -1575,13 +1568,15 @@ check_duplicates(PyObject *tuple)
if (PyTuple_GET_ITEM(tuple, j) == o) {
o = class_name(o);
if (o != NULL) {
- PyErr_Format(PyExc_TypeError,
- "duplicate base class %U",
- o);
+ if (PyUnicode_Check(o)) {
+ PyErr_Format(PyExc_TypeError,
+ "duplicate base class %U", o);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "duplicate base class");
+ }
Py_DECREF(o);
- } else {
- PyErr_SetString(PyExc_TypeError,
- "duplicate base class");
}
return -1;
}
@@ -1625,13 +1620,20 @@ consistent method resolution\norder (MRO) for bases");
i = 0;
while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
PyObject *name = class_name(k);
- const char *name_str;
+ const char *name_str = NULL;
if (name != NULL) {
- name_str = PyUnicode_AsUTF8(name);
- if (name_str == NULL)
+ if (PyUnicode_Check(name)) {
+ name_str = PyUnicode_AsUTF8(name);
+ }
+ else {
name_str = "?";
- } else
- name_str = "?";
+ }
+ }
+ if (name_str == NULL) {
+ Py_XDECREF(name);
+ Py_DECREF(set);
+ return;
+ }
off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str);
Py_XDECREF(name);
if (--n && (size_t)(off+1) < sizeof(buf)) {
@@ -3418,10 +3420,10 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
assert(aclass);
/* Merge in the type's dict (if any). */
- classdict = _PyObject_GetAttrId(aclass, &PyId___dict__);
- if (classdict == NULL)
- PyErr_Clear();
- else {
+ if (_PyObject_LookupAttrId(aclass, &PyId___dict__, &classdict) < 0) {
+ return -1;
+ }
+ if (classdict != NULL) {
int status = PyDict_Update(dict, classdict);
Py_DECREF(classdict);
if (status < 0)
@@ -3429,15 +3431,17 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
}
/* Recursively merge in the base types' (if any) dicts. */
- bases = _PyObject_GetAttrId(aclass, &PyId___bases__);
- if (bases == NULL)
- PyErr_Clear();
- else {
+ if (_PyObject_LookupAttrId(aclass, &PyId___bases__, &bases) < 0) {
+ return -1;
+ }
+ if (bases != NULL) {
/* We have no guarantee that bases is a real tuple */
Py_ssize_t i, n;
n = PySequence_Size(bases); /* This better be right */
- if (n < 0)
- PyErr_Clear();
+ if (n < 0) {
+ Py_DECREF(bases);
+ return -1;
+ }
else {
for (i = 0; i < n; i++) {
int status;
@@ -4725,9 +4729,10 @@ object___dir___impl(PyObject *self)
PyObject *itsclass = NULL;
/* Get __dict__ (which may or may not be a real dict...) */
- dict = _PyObject_GetAttrId(self, &PyId___dict__);
+ if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) {
+ return NULL;
+ }
if (dict == NULL) {
- PyErr_Clear();
dict = PyDict_New();
}
else if (!PyDict_Check(dict)) {
@@ -4745,12 +4750,12 @@ object___dir___impl(PyObject *self)
goto error;
/* Merge in attrs reachable from its class. */
- itsclass = _PyObject_GetAttrId(self, &PyId___class__);
- if (itsclass == NULL)
- /* XXX(tomer): Perhaps fall back to obj->ob_type if no
- __class__ exists? */
- PyErr_Clear();
- else if (merge_class_dict(dict, itsclass) != 0)
+ if (_PyObject_LookupAttrId(self, &PyId___class__, &itsclass) < 0) {
+ goto error;
+ }
+ /* XXX(tomer): Perhaps fall back to obj->ob_type if no
+ __class__ exists? */
+ if (itsclass != NULL && merge_class_dict(dict, itsclass) < 0)
goto error;
result = PyDict_Keys(dict);
@@ -6105,16 +6110,19 @@ method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *nam
PyObject *a, *b;
int ok;
- b = _PyObject_GetAttrId((PyObject *)(Py_TYPE(right)), name);
+ if (_PyObject_LookupAttrId((PyObject *)(Py_TYPE(right)), name, &b) < 0) {
+ return -1;
+ }
if (b == NULL) {
- PyErr_Clear();
/* If right doesn't have it, it's not overloaded */
return 0;
}
- a = _PyObject_GetAttrId((PyObject *)(Py_TYPE(left)), name);
+ if (_PyObject_LookupAttrId((PyObject *)(Py_TYPE(left)), name, &a) < 0) {
+ Py_DECREF(b);
+ return -1;
+ }
if (a == NULL) {
- PyErr_Clear();
Py_DECREF(b);
/* If right has it but left doesn't, it's overloaded */
return 1;
@@ -6123,11 +6131,6 @@ method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *nam
ok = PyObject_RichCompareBool(a, b, Py_NE);
Py_DECREF(a);
Py_DECREF(b);
- if (ok < 0) {
- PyErr_Clear();
- return 0;
- }
-
return ok;
}
@@ -6145,15 +6148,19 @@ FUNCNAME(PyObject *self, PyObject *other) \
if (Py_TYPE(self)->tp_as_number != NULL && \
Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
PyObject *r; \
- if (do_other && \
- PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
- method_is_overloaded(self, other, &rop_id)) { \
- stack[0] = self; \
- r = call_maybe(other, &rop_id, stack, 1); \
- if (r != Py_NotImplemented) \
- return r; \
- Py_DECREF(r); \
- do_other = 0; \
+ if (do_other && PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { \
+ int ok = method_is_overloaded(self, other, &rop_id); \
+ if (ok < 0) { \
+ return NULL; \
+ } \
+ if (ok) { \
+ stack[0] = self; \
+ r = call_maybe(other, &rop_id, stack, 1); \
+ if (r != Py_NotImplemented) \
+ return r; \
+ Py_DECREF(r); \
+ do_other = 0; \
+ } \
} \
stack[0] = other; \
r = call_maybe(self, &op_id, stack, 1); \
@@ -7739,7 +7746,9 @@ supercheck(PyTypeObject *type, PyObject *obj)
/* Try the slow way */
PyObject *class_attr;
- class_attr = _PyObject_GetAttrId(obj, &PyId___class__);
+ if (_PyObject_LookupAttrId(obj, &PyId___class__, &class_attr) < 0) {
+ return NULL;
+ }
if (class_attr != NULL &&
PyType_Check(class_attr) &&
(PyTypeObject *)class_attr != Py_TYPE(obj))
@@ -7749,11 +7758,7 @@ supercheck(PyTypeObject *type, PyObject *obj)
if (ok)
return (PyTypeObject *)class_attr;
}
-
- if (class_attr == NULL)
- PyErr_Clear();
- else
- Py_DECREF(class_attr);
+ Py_XDECREF(class_attr);
}
PyErr_SetString(PyExc_TypeError,
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 8b8e71031a..4b81450de8 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -163,10 +163,10 @@ weakref_repr(PyWeakReference *self)
if (PyWeakref_GET_OBJECT(self) == Py_None)
return PyUnicode_FromFormat("<weakref at %p; dead>", self);
- name = _PyObject_GetAttrId(PyWeakref_GET_OBJECT(self), &PyId___name__);
+ if (_PyObject_LookupAttrId(PyWeakref_GET_OBJECT(self), &PyId___name__, &name) < 0) {
+ return NULL;
+ }
if (name == NULL || !PyUnicode_Check(name)) {
- if (name == NULL)
- PyErr_Clear();
repr = PyUnicode_FromFormat(
"<weakref at %p; to '%s' at %p>",
self,