gh-148268: Avoid debug assertion in specialized list compares (#148309)

Co-authored-by: Stan Ulbrych <stan@python.org>
This commit is contained in:
Asish KumarandStan Ulbrych authored and GitHub committed 2026-10-21 18:12:42 +00:00
1 parent 65b7e9444a
commit a13d6dbe03
1 file changed
+23 -3
+23 -3
View File
@@ -2863,6 +2863,26 @@ unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
return res;
}
#ifndef NDEBUG
static void
assert_compare_consistent(PyObject *v, PyObject *w, int res)
{
int cmp = PyObject_RichCompareBool(v, w, Py_LT);
if (cmp < 0) {
/* This debug-only cross-check can hit the C stack guard even when
* the specialized comparison itself is safe. Keep the optimized path
* side-effect free by discarding that exception.
*/
assert(PyErr_ExceptionMatches(PyExc_RecursionError));
PyErr_Clear();
return;
}
assert(res == cmp);
}
#else
# define assert_compare_consistent(v, w, res) ((void)0)
#endif
/* Latin string compare: safe for any two latin (one byte per char) strings. */
static int
unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
@@ -2883,7 +2903,7 @@ unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
res < 0 :
PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
assert_compare_consistent(v, w, res);
return res;
}
@@ -2908,7 +2928,7 @@ unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
w0 = _PyLong_CompactValue(wl);
res = v0 < w0;
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
assert_compare_consistent(v, w, res);
return res;
}
@@ -2923,7 +2943,7 @@ unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
assert(Py_IS_TYPE(w, &PyFloat_Type));
res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
assert_compare_consistent(v, w, res);
return res;
}