Skip to content

Commit a388f88

Browse files
committed
pythongh-146199: Fix error handling in code_richcompare when PyObject_RichCompareBool fails
1 parent d357a7d commit a388f88

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_code.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,17 @@ def test_stateless(self):
11641164
with self.assertRaises(Exception):
11651165
_testinternalcapi.verify_stateless_code(func)
11661166

1167+
def test_code_richcompare_raise_exception(self):
1168+
class BadStr(str):
1169+
def __eq__(self, _):
1170+
raise RuntimeError("Poison!")
1171+
def __hash__(self): return str.__hash__(self)
1172+
1173+
c1 = compile("pass", "test", "exec")
1174+
c2 = c1.replace(co_name=BadStr("poison"))
1175+
c3 = compile("pass", "poison", "exec")
1176+
with self.assertRaises(RuntimeError):
1177+
_ = c2 == c3
11671178

11681179
def isinterned(s):
11691180
return s is sys.intern(('_' + s + '_')[1:-1])

Objects/codeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
26062606
cp = (PyCodeObject *)other;
26072607

26082608
eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
2609-
if (!eq) goto unequal;
2609+
if (eq <= 0) goto unequal;
26102610
eq = co->co_argcount == cp->co_argcount;
26112611
if (!eq) goto unequal;
26122612
eq = co->co_posonlyargcount == cp->co_posonlyargcount;

0 commit comments

Comments
 (0)