]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146199: Fix error handling in `code_richcompare` when `PyObject_RichCompareBool...
authorBrij Kapadia <97006829+bkap123@users.noreply.github.com>
Mon, 23 Mar 2026 22:41:53 +0000 (18:41 -0400)
committerGitHub <noreply@github.com>
Mon, 23 Mar 2026 22:41:53 +0000 (23:41 +0100)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_code.py
Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst [new file with mode: 0644]
Objects/codeobject.c

index 19fa387cd7b2712f22d9993e331428471419fe10..fac7e9148f1502c609bfc21cc035a6e1e4d84705 100644 (file)
@@ -1164,6 +1164,18 @@ class CodeTest(unittest.TestCase):
                     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])
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst
new file mode 100644 (file)
index 0000000..0611a0d
--- /dev/null
@@ -0,0 +1 @@
+Comparison of code objects now handles errors correctly.
index 84a712b2b2c05d4e5022cf997d44303984938ac0..891aa4ee78c0f27103ae0c76bb56050a5ea6716c 100644 (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;