gh-148268: Fix debug assertion in unsafe_*_compare sort helpers#151820
Closed
timurmamedov1 wants to merge 1 commit into
Closed
gh-148268: Fix debug assertion in unsafe_*_compare sort helpers#151820timurmamedov1 wants to merge 1 commit into
timurmamedov1 wants to merge 1 commit into
Conversation
The assert in unsafe_latin_compare, unsafe_long_compare, and unsafe_float_compare assumed PyObject_RichCompareBool always succeeds. It can return -1 on error (e.g. recursion depth), triggering a false assertion failure. Account for the error case.
Author
|
Closing — #148309 is already approved and covers the same fix. |
There was a problem hiding this comment.
Pull request overview
This PR fixes debug-build assertions in CPython’s list sort “unsafe_*_compare” helpers so they no longer assume PyObject_RichCompareBool() cannot fail, preventing false assertion failures when it returns -1 on error.
Changes:
- Updates
unsafe_latin_compare,unsafe_long_compare, andunsafe_float_compareto toleratePyObject_RichCompareBool()returning-1in#ifndef NDEBUGchecks. - Adds a NEWS fragment documenting the debug assertion fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
Objects/listobject.c |
Adjusts debug-only assertions in sort compare helpers to handle PyObject_RichCompareBool() error returns. |
Misc/NEWS.d/next/Core_and_Builtins/2026-06-20-12-00-00.gh-issue-148268.SrtAsFx.rst |
Documents the fix in the NEWS fragments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2833
to
+2836
| #ifndef NDEBUG | ||
| int cmp = PyObject_RichCompareBool(v, w, Py_LT); | ||
| assert(cmp < 0 || res == cmp); | ||
| #endif |
Comment on lines
+2861
to
+2864
| #ifndef NDEBUG | ||
| int cmp = PyObject_RichCompareBool(v, w, Py_LT); | ||
| assert(cmp < 0 || res == cmp); | ||
| #endif |
Comment on lines
+2879
to
+2882
| #ifndef NDEBUG | ||
| int cmp = PyObject_RichCompareBool(v, w, Py_LT); | ||
| assert(cmp < 0 || res == cmp); | ||
| #endif |
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.
The
assertinunsafe_latin_compare,unsafe_long_compare, andunsafe_float_compareassumedPyObject_RichCompareBoolalways succeeds. It can return-1on error (like recursion depth), causing a false assertion failure in debug builds.Using the pattern suggested by Tim Peters and StanFromIreland:
This preserves the debug check while allowing the error return.
unsafe_{latin,long,float}_comparehave anassertthat assumesPyObject_RichCompareBoolcan't fail #148268