Skip to content

Commit

Permalink
pythongh-104078: Improve performance of PyObject_HasAttrString
Browse files Browse the repository at this point in the history
  • Loading branch information
itamaro committed May 2, 2023
1 parent 605f878 commit d09b82e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,13 +918,24 @@ PyObject_GetAttrString(PyObject *v, const char *name)
int
PyObject_HasAttrString(PyObject *v, const char *name)
{
PyObject *res = PyObject_GetAttrString(v, name);
if (res != NULL) {
Py_DECREF(res);
return 1;
if (Py_TYPE(v)->tp_getattr != NULL) {
PyObject *res = (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
if (res != NULL) {
Py_DECREF(res);
return 1;
}
PyErr_Clear();
return 0;
}
PyErr_Clear();
return 0;

PyObject *attr_name = PyUnicode_FromString(name);
if (attr_name == NULL) {
PyErr_Clear();
return 0;
}
int ok = PyObject_HasAttr(v, attr_name);
Py_DECREF(attr_name);
return ok;
}

int
Expand Down

0 comments on commit d09b82e

Please sign in to comment.