gh-144989: Expose special method lookup at Python level#144990
Open
tanloong wants to merge 3 commits intopython:mainfrom
Open
gh-144989: Expose special method lookup at Python level#144990tanloong wants to merge 3 commits intopython:mainfrom
tanloong wants to merge 3 commits intopython:mainfrom
Conversation
Member
ZeroIntensity
left a comment
There was a problem hiding this comment.
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) | ||
| { |
Member
There was a problem hiding this comment.
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; | ||
| } |
Member
There was a problem hiding this comment.
We use 4 space indentations for this file.
| Py_RETURN_NONE; | ||
| } | ||
| PyObject *method = PyStackRef_AsPyObjectSteal(method_and_self[0]); | ||
| return Py_BuildValue("O", method); |
Member
There was a problem hiding this comment.
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 |
Member
There was a problem hiding this comment.
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. |
Member
There was a problem hiding this comment.
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)``. | |||
Member
There was a problem hiding this comment.
Suggested change
| ``types.lookup_special_method(obj, attr)``. | |
| :func:`types.lookup_special_method(obj, attr)`. |
Member
There was a problem hiding this comment.
Is there a way to implement this in pure-Python? If so, we should add it to Lib/types.py as a fallback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This exposes
_PyObject_LookupSpecialMethod()astypes.lookup_special_method(obj, attr).I am not quite sure which one between the two similar funtions in
Objects/typeobject.cshould be preferred to be exposed:_PyObject_LookupSpecial()or_PyObject_LookupSpecialMethod(). I chose the_PyObject_LookupSpecialMethod()because it is the function that is called byLOAD_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/