Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
_PyObject_GenericGetAttrWithDict() suppress AttirubteError from descr…
…iptor.
  • Loading branch information
methane committed Jan 15, 2018
commit 477544cfbbaeabc505dfcf7654880154448bc850
22 changes: 14 additions & 8 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,13 +899,12 @@ _PyObject_GetAttrWithoutError(PyObject *v, PyObject *name)
name->ob_type->tp_name);
return NULL;
}

if (tp->tp_getattro == PyObject_GenericGetAttr) {
return _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
}
if (tp->tp_getattro != NULL) {
if (tp->tp_getattro == PyObject_GenericGetAttr) {
ret = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
}
else {
ret = (*tp->tp_getattro)(v, name);
}
ret = (*tp->tp_getattro)(v, name);
}
else if (tp->tp_getattr != NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is some code duplication with PyObject_GetAttr, does it make sense to factor it out?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@methane Yes this idea looks good.

const char *name_str = PyUnicode_AsUTF8(name);
Expand Down Expand Up @@ -1136,8 +1135,7 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
/* Make sure the logic of _PyObject_GetMethod is in sync with
this method.

When suppress=1, this function doesn't raise AttributeError.
But descriptors may raise it.
When suppress=1, this function suppress AttributeError.
*/

PyTypeObject *tp = Py_TYPE(obj);
Expand Down Expand Up @@ -1168,6 +1166,10 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
f = descr->ob_type->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
res = f(descr, obj, (PyObject *)obj->ob_type);
if (res == NULL && suppress &&
PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
goto done;
}
}
Expand Down Expand Up @@ -1207,6 +1209,10 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,

if (f != NULL) {
res = f(descr, obj, (PyObject *)Py_TYPE(obj));
if (res == NULL && suppress &&
PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
goto done;
}

Expand Down