summaryrefslogtreecommitdiff
path: root/Objects/classobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-01-25 10:49:40 +0200
committerINADA Naoki <methane@users.noreply.github.com>2018-01-25 17:49:40 +0900
commitf320be77ffb73e3b9e7fc98c37b8df3975d84b40 (patch)
tree552338f0200938249233fa4aa7b00add61965337 /Objects/classobject.c
parent2b822a0bb1de2612c85d8f75e3ce89eda2ac9f68 (diff)
downloadcpython-git-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.tar.gz
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/classobject.c')
-rw-r--r--Objects/classobject.c30
1 files changed, 10 insertions, 20 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index e88c95cbfb..3dc23b796c 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -246,21 +246,14 @@ method_repr(PyMethodObject *a)
{
PyObject *self = a->im_self;
PyObject *func = a->im_func;
- PyObject *funcname = NULL, *result = NULL;
+ PyObject *funcname, *result;
const char *defname = "?";
- funcname = _PyObject_GetAttrId(func, &PyId___qualname__);
- if (funcname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
-
- funcname = _PyObject_GetAttrId(func, &PyId___name__);
- if (funcname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- }
+ if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
+ (funcname == NULL &&
+ _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
+ {
+ return NULL;
}
if (funcname != NULL && !PyUnicode_Check(funcname)) {
@@ -542,7 +535,7 @@ static PyObject *
instancemethod_repr(PyObject *self)
{
PyObject *func = PyInstanceMethod_Function(self);
- PyObject *funcname = NULL , *result = NULL;
+ PyObject *funcname, *result;
const char *defname = "?";
if (func == NULL) {
@@ -550,13 +543,10 @@ instancemethod_repr(PyObject *self)
return NULL;
}
- funcname = _PyObject_GetAttrId(func, &PyId___name__);
- if (funcname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
+ if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
+ return NULL;
}
- else if (!PyUnicode_Check(funcname)) {
+ if (funcname != NULL && !PyUnicode_Check(funcname)) {
Py_DECREF(funcname);
funcname = NULL;
}