diff options
author | 2018-01-25 10:49:40 +0200 | |
---|---|---|
committer | 2018-01-25 17:49:40 +0900 | |
commit | f320be77ffb73e3b9e7fc98c37b8df3975d84b40 (patch) | |
tree | 552338f0200938249233fa4aa7b00add61965337 /Python/bltinmodule.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 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 7fc2261ec64..d8971bef2ae 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -71,12 +71,10 @@ update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs) } continue; } - meth = _PyObject_GetAttrId(base, &PyId___mro_entries__); + if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) { + goto error; + } if (!meth) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { - goto error; - } - PyErr_Clear(); if (new_bases) { if (PyList_Append(new_bases, base) < 0) { goto error; @@ -218,18 +216,11 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, } /* else: meta is not a class, so we cannot do the metaclass calculation, so we will use the explicitly given object as it is */ - prep = _PyObject_GetAttrId(meta, &PyId___prepare__); - if (prep == NULL) { - if (PyErr_ExceptionMatches(PyExc_AttributeError)) { - PyErr_Clear(); - ns = PyDict_New(); - } - else { - Py_DECREF(meta); - Py_XDECREF(mkw); - Py_DECREF(bases); - return NULL; - } + if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) { + ns = NULL; + } + else if (prep == NULL) { + ns = PyDict_New(); } else { PyObject *pargs[2] = {name, bases}; @@ -1127,8 +1118,7 @@ builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return NULL; } if (dflt != NULL) { - result = _PyObject_GetAttrWithoutError(v, name); - if (result == NULL && !PyErr_Occurred()) { + if (_PyObject_LookupAttr(v, name, &result) == 0) { Py_INCREF(dflt); return dflt; } @@ -1191,13 +1181,12 @@ builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name) "hasattr(): attribute name must be string"); return NULL; } - v = _PyObject_GetAttrWithoutError(obj, name); - if (v == NULL) { - if (!PyErr_Occurred()) { - Py_RETURN_FALSE; - } + if (_PyObject_LookupAttr(obj, name, &v) < 0) { return NULL; } + if (v == NULL) { + Py_RETURN_FALSE; + } Py_DECREF(v); Py_RETURN_TRUE; } |