[Python-checkins] gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956)

Fidget-Spinner webhook-mailer at python.org
Sat Jun 18 10:42:47 EDT 2022


https://github.com/python/cpython/commit/fea1e9bc5cd081b896b328a035719f7ccbf6843e
commit: fea1e9bc5cd081b896b328a035719f7ccbf6843e
branch: main
author: Ken Jin <kenjin at python.org>
committer: Fidget-Spinner <kenjin at python.org>
date: 2022-06-18T22:42:42+08:00
summary:

gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst
M Objects/typeobject.c

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 0000000000000..3b2f0e8c32d74
--- /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 db4682c69ed0e..513055168062f 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) {



More information about the Python-checkins mailing list