Skip to content

gh-144989: Expose special method lookup at Python level#144990

Open
tanloong wants to merge 3 commits intopython:mainfrom
tanloong:expose-lookupspeicalmethod
Open

gh-144989: Expose special method lookup at Python level#144990
tanloong wants to merge 3 commits intopython:mainfrom
tanloong:expose-lookupspeicalmethod

Conversation

@tanloong
Copy link
Contributor

@tanloong tanloong commented Feb 19, 2026

This exposes _PyObject_LookupSpecialMethod() as types.lookup_special_method(obj, attr).

I am not quite sure which one between the two similar funtions in Objects/typeobject.c should be preferred to be exposed: _PyObject_LookupSpecial() or _PyObject_LookupSpecialMethod(). I chose the _PyObject_LookupSpecialMethod() because it is the function that is called by LOAD_SPECIAL. So I suppose using this one is more consistent with the CPython interpreter when parsing with-statements (#144386 (comment)).

However, I am open to adjusting the PR to expose _PyObject_LookupSpecial() instead if that one is more suitable.


📚 Documentation preview 📚: https://cpython-previews--144990.org.readthedocs.build/

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a "What's New in Python 3.15" entry.

Comment on lines +12 to +14
static PyObject *
_types_lookup_special_method_impl(PyObject *self, PyObject *args)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the Argument Clinic instead of using PyArg_ParseTuple.

Comment on lines +15 to +18
PyObject *obj, *attr;
if (!PyArg_ParseTuple(args, "OO", &obj, &attr)) {
return NULL;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use 4 space indentations for this file.

Py_RETURN_NONE;
}
PyObject *method = PyStackRef_AsPyObjectSteal(method_and_self[0]);
return Py_BuildValue("O", method);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as return method?

but still binding it to the instance. Returns None if the method is not
found.

.. versionadded:: 3.15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. versionadded:: 3.15
.. versionadded:: next

Comment on lines +527 to +528
but still binding it to the instance. Returns None if the method is not
found.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
but still binding it to the instance. Returns None if the method is not
found.
but still binding it to the instance. Returns ``None`` if the method is not
found.

@@ -0,0 +1,2 @@
Expose ``_PyObject_LookupSpecialMethod()`` as
``types.lookup_special_method(obj, attr)``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``types.lookup_special_method(obj, attr)``.
:func:`types.lookup_special_method(obj, attr)`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to implement this in pure-Python? If so, we should add it to Lib/types.py as a fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments