Skip to content

Commit fea1e9b

Browse files
gh-93955: Use unbound methods for slot __getattr__ and __getattribute__ (GH-93956)
1 parent 7a2cc35 commit fea1e9b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of attribute lookups on objects with custom ``__getattribute__`` and ``__getattr__``. Patch by Ken Jin.

Objects/typeobject.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -7782,10 +7782,17 @@ slot_tp_getattro(PyObject *self, PyObject *name)
77827782
return vectorcall_method(&_Py_ID(__getattribute__), stack, 2);
77837783
}
77847784

7785-
static PyObject *
7785+
static inline PyObject *
77867786
call_attribute(PyObject *self, PyObject *attr, PyObject *name)
77877787
{
77887788
PyObject *res, *descr = NULL;
7789+
7790+
if (_PyType_HasFeature(Py_TYPE(attr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
7791+
PyObject *args[] = { self, name };
7792+
res = PyObject_Vectorcall(attr, args, 2, NULL);
7793+
return res;
7794+
}
7795+
77897796
descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
77907797

77917798
if (f != NULL) {

0 commit comments

Comments
 (0)