diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-01-25 10:49:40 +0200 |
---|---|---|
committer | INADA Naoki <methane@users.noreply.github.com> | 2018-01-25 17:49:40 +0900 |
commit | f320be77ffb73e3b9e7fc98c37b8df3975d84b40 (patch) | |
tree | 552338f0200938249233fa4aa7b00add61965337 /Objects/typeobject.c | |
parent | bpo-32652: Defer pymain_set_global_config() call (#5303) (diff) | |
download | cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.tar.gz cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.tar.bz2 cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.zip |
bpo-32571: Avoid raising unneeded AttributeError and silencing it in C code (GH-5222)
Add two new private APIs: _PyObject_LookupAttr() and _PyObject_LookupAttrId()
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r-- | Objects/typeobject.c | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 0c8f0adfb78..a22173878e8 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2403,7 +2403,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) if (PyType_Check(tmp)) { continue; } - tmp = _PyObject_GetAttrId(tmp, &PyId___mro_entries__); + if (_PyObject_LookupAttrId(tmp, &PyId___mro_entries__, &tmp) < 0) { + return NULL; + } if (tmp != NULL) { PyErr_SetString(PyExc_TypeError, "type() doesn't support MRO entry resolution; " @@ -2411,12 +2413,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) Py_DECREF(tmp); return NULL; } - else if (PyErr_ExceptionMatches(PyExc_AttributeError)) { - PyErr_Clear(); - } - else { - return NULL; - } } /* Search the bases for the proper metatype to deal with this: */ winner = _PyType_CalculateMetaclass(metatype, bases); @@ -4099,15 +4095,12 @@ _PyObject_GetState(PyObject *obj, int required) PyObject *getstate; _Py_IDENTIFIER(__getstate__); - getstate = _PyObject_GetAttrId(obj, &PyId___getstate__); + if (_PyObject_LookupAttrId(obj, &PyId___getstate__, &getstate) < 0) { + return NULL; + } if (getstate == NULL) { PyObject *slotnames; - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { - return NULL; - } - PyErr_Clear(); - if (required && obj->ob_type->tp_itemsize) { PyErr_Format(PyExc_TypeError, "can't pickle %.200s objects", @@ -4174,14 +4167,12 @@ _PyObject_GetState(PyObject *obj, int required) name = PyList_GET_ITEM(slotnames, i); Py_INCREF(name); - value = PyObject_GetAttr(obj, name); + if (_PyObject_LookupAttr(obj, name, &value) < 0) { + goto error; + } if (value == NULL) { Py_DECREF(name); - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { - goto error; - } /* It is not an error if the attribute is not present. */ - PyErr_Clear(); } else { int err = PyDict_SetItem(slots, name, value); |