From fea1e9bc5cd081b896b328a035719f7ccbf6843e Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sat, 18 Jun 2022 22:42:42 +0800 Subject: [PATCH] gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956) --- .../2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst | 1 + Objects/typeobject.c | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-06-17-16-30-24.gh-issue-93955.LmiAe9.rst 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 00000000000..3b2f0e8c32d --- /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 db4682c69ed..51305516806 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) {