Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-92216: improve performance of hasattr for type objects #99979

Merged
merged 9 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
4 changes: 4 additions & 0 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ extern static_builtin_state * _PyStaticType_GetState(PyTypeObject *);
extern void _PyStaticType_ClearWeakRefs(PyTypeObject *type);
extern void _PyStaticType_Dealloc(PyTypeObject *type);

PyObject *
_Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *flag);
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
PyObject *
_Py_type_getattro(PyTypeObject *type, PyObject *name);

PyObject *_Py_slot_tp_getattro(PyObject *self, PyObject *name);
PyObject *_Py_slot_tp_getattr_hook(PyObject *self, PyObject *name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve the performance of :func:`hasattr` for type objects with a missing attribute.
10 changes: 9 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,15 @@ _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
}
return 0;
}
if (tp->tp_getattro != NULL) {
if (tp->tp_getattro == (getattrofunc)_Py_type_getattro) {
int flag = 0;
*result = _Py_type_getattro_impl((PyTypeObject*)v, name, &flag);
if (flag) {
// return 0 without having to clear the exception
return 0;
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
}
}
else if (tp->tp_getattro != NULL) {
*result = (*tp->tp_getattro)(v, name);
}
else if (tp->tp_getattr != NULL) {
Expand Down
25 changes: 19 additions & 6 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4220,8 +4220,8 @@ _PyType_LookupId(PyTypeObject *type, _Py_Identifier *name)

/* This is similar to PyObject_GenericGetAttr(),
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
static PyObject *
type_getattro(PyTypeObject *type, PyObject *name)
PyObject *
_Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress)
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
{
PyTypeObject *metatype = Py_TYPE(type);
PyObject *meta_attribute, *attribute;
Expand Down Expand Up @@ -4301,12 +4301,25 @@ type_getattro(PyTypeObject *type, PyObject *name)
}

/* Give up */
PyErr_Format(PyExc_AttributeError,
"type object '%.50s' has no attribute '%U'",
type->tp_name, name);
if (suppress == NULL) {
PyErr_Format(PyExc_AttributeError,
"type object '%.50s' has no attribute '%U'",
type->tp_name, name);
} else {
// signal the caller we have not set an PyExc_AttributeError and gave up
*suppress = 1;
}
return NULL;
}

/* This is similar to PyObject_GenericGetAttr(),
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
PyObject *
_Py_type_getattro(PyTypeObject *type, PyObject *name)
{
return _Py_type_getattro_impl(type, name, NULL);
}

static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
Expand Down Expand Up @@ -4798,7 +4811,7 @@ PyTypeObject PyType_Type = {
0, /* tp_hash */
(ternaryfunc)type_call, /* tp_call */
0, /* tp_str */
(getattrofunc)type_getattro, /* tp_getattro */
(getattrofunc)_Py_type_getattro, /* tp_getattro */
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
(setattrofunc)type_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Expand Down