Skip to content

gh-148268: Fix debug assertion in unsafe_*_compare sort helpers#151820

Closed
timurmamedov1 wants to merge 1 commit into
python:mainfrom
timurmamedov1:gh-148268-fix-sort-assert
Closed

gh-148268: Fix debug assertion in unsafe_*_compare sort helpers#151820
timurmamedov1 wants to merge 1 commit into
python:mainfrom
timurmamedov1:gh-148268-fix-sort-assert

Conversation

@timurmamedov1

@timurmamedov1 timurmamedov1 commented Jun 20, 2026

Copy link
Copy Markdown

The assert in unsafe_latin_compare, unsafe_long_compare, and unsafe_float_compare assumed PyObject_RichCompareBool always succeeds. It can return -1 on error (like recursion depth), causing a false assertion failure in debug builds.

Using the pattern suggested by Tim Peters and StanFromIreland:

#ifndef NDEBUG
    int cmp = PyObject_RichCompareBool(v, w, Py_LT);
    assert(cmp < 0 || res == cmp);
#endif

This preserves the debug check while allowing the error return.

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.
@timurmamedov1

Copy link
Copy Markdown
Author

Closing — #148309 is already approved and covers the same fix.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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, and unsafe_float_compare to tolerate PyObject_RichCompareBool() returning -1 in #ifndef NDEBUG checks.
  • 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 thread Objects/listobject.c
Comment on lines +2833 to +2836
#ifndef NDEBUG
int cmp = PyObject_RichCompareBool(v, w, Py_LT);
assert(cmp < 0 || res == cmp);
#endif
Comment thread Objects/listobject.c
Comment on lines +2861 to +2864
#ifndef NDEBUG
int cmp = PyObject_RichCompareBool(v, w, Py_LT);
assert(cmp < 0 || res == cmp);
#endif
Comment thread Objects/listobject.c
Comment on lines +2879 to +2882
#ifndef NDEBUG
int cmp = PyObject_RichCompareBool(v, w, Py_LT);
assert(cmp < 0 || res == cmp);
#endif
@timurmamedov1 timurmamedov1 deleted the gh-148268-fix-sort-assert branch June 20, 2026 23:26
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