Skip to content

Commit e017971

Browse files
bkap123blurb-it[bot]vstinner
authored
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>
1 parent 7dc2f52 commit e017971

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/test/test_code.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,18 @@ 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+
1172+
__hash__ = str.__hash__
1173+
1174+
c1 = compile("pass", "test", "exec")
1175+
c2 = c1.replace(co_name=BadStr("poison"))
1176+
c3 = compile("pass", "poison", "exec")
1177+
with self.assertRaises(RuntimeError):
1178+
c2 == c3
11671179

11681180
def isinterned(s):
11691181
return s is sys.intern(('_' + s + '_')[1:-1])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Comparison of code objects now handles errors correctly.

Objects/codeobject.c

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

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

0 commit comments

Comments
 (0)