summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKen Jin <kenjin@python.org>2022-06-18 14:42:42 (GMT)
committerGitHub <noreply@github.com>2022-06-18 14:42:42 (GMT)
commitfea1e9bc5cd081b896b328a035719f7ccbf6843e (patch)
tree104c63aacbbb7df8df271147480a82bab9027df3
parent7a2cc35e1ca6808a735b90269756d5286077a152 (diff)
downloadcpython-fea1e9bc5cd081b896b328a035719f7ccbf6843e.zip
cpython-fea1e9bc5cd081b896b328a035719f7ccbf6843e.tar.gz
cpython-fea1e9bc5cd081b896b328a035719f7ccbf6843e.tar.bz2
gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst1
-rw-r--r--Objects/typeobject.c9
2 files changed, 9 insertions, 1 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst b/Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst
new file mode 100644
index 0000000..3b2f0e8
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst
@@ -0,0 +1 @@
+Improve performance of attribute lookups on objects with custom ``__getattribute__`` and ``__getattr__``. Patch by Ken Jin.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index db4682c..5130551 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -7782,10 +7782,17 @@ slot_tp_getattro(PyObject *self, PyObject *name)
return vectorcall_method(&_Py_ID(__getattribute__), stack, 2);
}
-static PyObject *
+static inline PyObject *
call_attribute(PyObject *self, PyObject *attr, PyObject *name)
{
PyObject *res, *descr = NULL;
+
+ if (_PyType_HasFeature(Py_TYPE(attr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
+ PyObject *args[] = { self, name };
+ res = PyObject_Vectorcall(attr, args, 2, NULL);
+ return res;
+ }
+
descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
if (f != NULL) {