gh-146199: Fix error handling in code_richcompare when PyObject_RichCompareBool fails (#146200)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Brij Kapadia 2026-03-23 18:41:53 -04:00 committed by GitHub
parent 7dc2f52a6f
commit e017971eb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 1 deletions

View file

@ -1164,6 +1164,18 @@ def test_stateless(self):
with self.assertRaises(Exception):
_testinternalcapi.verify_stateless_code(func)
def test_code_richcompare_raise_exception(self):
class BadStr(str):
def __eq__(self, _):
raise RuntimeError("Poison!")
__hash__ = str.__hash__
c1 = compile("pass", "test", "exec")
c2 = c1.replace(co_name=BadStr("poison"))
c3 = compile("pass", "poison", "exec")
with self.assertRaises(RuntimeError):
c2 == c3
def isinterned(s):
return s is sys.intern(('_' + s + '_')[1:-1])

View file

@ -0,0 +1 @@
Comparison of code objects now handles errors correctly.

View file

@ -2607,7 +2607,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
cp = (PyCodeObject *)other;
eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
if (!eq) goto unequal;
if (eq <= 0) goto unequal;
eq = co->co_argcount == cp->co_argcount;
if (!eq) goto unequal;
eq = co->co_posonlyargcount == cp->co_posonlyargcount;