From: Ken Jin Date: Sat, 18 Jun 2022 14:42:42 +0000 (+0800) Subject: gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956) X-Git-Tag: v3.12.0a1~1211 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fea1e9bc5cd081b896b328a035719f7ccbf6843e;p=thirdparty%2FPython%2Fcpython.git gh-93955: Use unbound methods for slot `__getattr__` and `__getattribute__` (GH-93956) --- 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 000000000000..3b2f0e8c32d7 --- /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 db4682c69ed0..513055168062 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) {