From a388f88d9e886ff4703f873fffee0415817fc812 Mon Sep 17 00:00:00 2001 From: Brij <97006829+bkap123@users.noreply.github.com> Date: Fri, 20 Mar 2026 06:31:26 -0400 Subject: [PATCH 1/5] gh-146199: Fix error handling in `code_richcompare` when `PyObject_RichCompareBool` fails --- Lib/test/test_code.py | 11 +++++++++++ Objects/codeobject.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 19fa387cd7b271..7bcfd60846225a 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -1164,6 +1164,17 @@ 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!") + def __hash__(self): return str.__hash__(self) + + 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/Objects/codeobject.c b/Objects/codeobject.c index fbf0985e9050dd..e2ea0e1a661ee4 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -2606,7 +2606,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; From 1d128a61c56f5114072796a61a4a6236efb2070f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 20 Mar 2026 12:26:26 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst b/Misc/NEWS.d/next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst new file mode 100644 index 00000000000000..0611a0d6a6d65a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst @@ -0,0 +1 @@ +Comparison of code objects now handles errors correctly. From 7f1d736bcd22a922f813cca6c202fdff7e72013b Mon Sep 17 00:00:00 2001 From: Brij Kapadia <97006829+bkap123@users.noreply.github.com> Date: Fri, 20 Mar 2026 14:19:14 -0400 Subject: [PATCH 3/5] Update Lib/test/test_code.py Co-authored-by: Victor Stinner --- Lib/test/test_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 7bcfd60846225a..9a8d3e3347cfac 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -1174,7 +1174,7 @@ def __hash__(self): return str.__hash__(self) c2 = c1.replace(co_name=BadStr("poison")) c3 = compile("pass", "poison", "exec") with self.assertRaises(RuntimeError): - _ = c2 == c3 + c2 == c3 def isinterned(s): return s is sys.intern(('_' + s + '_')[1:-1]) From ef70ca53f41227a67e3a0a1e76196cf4272d0d0d Mon Sep 17 00:00:00 2001 From: Brij <97006829+bkap123@users.noreply.github.com> Date: Fri, 20 Mar 2026 16:06:21 -0400 Subject: [PATCH 4/5] Update __hash__ in test --- Lib/test/test_code.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 9a8d3e3347cfac..fac7e9148f1502 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -1168,7 +1168,8 @@ def test_code_richcompare_raise_exception(self): class BadStr(str): def __eq__(self, _): raise RuntimeError("Poison!") - def __hash__(self): return str.__hash__(self) + + __hash__ = str.__hash__ c1 = compile("pass", "test", "exec") c2 = c1.replace(co_name=BadStr("poison")) From bc1ecf68caaaf021b8ffde302c1c80f55611dccc Mon Sep 17 00:00:00 2001 From: Brij <97006829+bkap123@users.noreply.github.com> Date: Sat, 21 Mar 2026 06:19:15 -0400 Subject: [PATCH 5/5] Move NEWS to Core_and_Builtins --- .../2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Library => Core_and_Builtins}/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst (100%) diff --git a/Misc/NEWS.d/next/Library/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 similarity index 100% rename from Misc/NEWS.d/next/Library/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst rename to Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-12-26-24.gh-issue-146199.vV8V9s.rst