-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add more list functions to the c-api #8045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+115
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 108
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 6883
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1249
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 12850
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 5750
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 10897
🌐 Web query:
CPython PyList_Sort implementation listobject.c calls overridden sort subclass💡 Result:
In CPython, the
list.sort()method is implemented inObjects/listobject.cusing a highly optimized C function,list_sort_impl(which corresponds to thelist.sortoperation). This implementation does not call overriddensortmethods defined in subclasses oflist[1][2][3]. The internal C implementation treatslistsubclasses as standardlistobjects for the purposes of sorting [1][3]. Whenlist.sort()is called on an instance of a list subclass, the C code directly manipulates the underlying data structure of the object—effectively ignoring any customsortlogic defined in the Python layer of the subclass [1][2][3]. This is a documented behavior pattern in CPython where many C-implemented methods for built-in types (likelist,dict, andheapqoperations) operate on the concrete C structure and bypass Python-level overrides [4][1][2][3]. The implementation utilizes an adaptive, stable, natural mergesort algorithm (often referred to as Timsort), designed for efficiency by minimizing comparisons, which are expensive in CPython [5][6][7]. Because the implementation relies on direct memory access and low-level C functions to handle the sorting logic, it does not perform the standard Python method resolution order (MRO) lookups that would be required to detect and execute a subclass-overriddensortmethod [1][3]. Consequently, if a user overridessortin alistsubclass, calling.sort()on an instance of that class will still execute the native C implementation, not the custom Python code [1][2].Citations:
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 38370
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 6400
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 8851
Avoid Python-level method dispatch in
PyList_Sort.PyList_Sortdowncasts toPyListand then callsvm.call_method(list.as_object(), "sort", ()), which resolves"sort"viaPyMethod::getagainst the object’s runtime class and instance__dict__. That allows alistsubclass override (or an instance attribute namedsort) to run user code, unlike CPython’sPyList_Sortbehavior. Resolve/invoke thesortimplementation from the exact built-inlisttype (or call the internal list sort routine) instead of genericcall_method.🤖 Prompt for AI Agents