From 25d2788b0e09651ce9b5c8695f79a16155beb537 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 13 Jun 2025 09:23:21 -0400 Subject: [PATCH] Remove assertion in _PyCode_CheckNoExternalState() --- Lib/test/test_interpreters/test_api.py | 16 ++++++++++++++++ Objects/codeobject.c | 1 - 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 1403cd145b6787..8ff4e1eb71aa68 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -944,6 +944,22 @@ def test_created_with_capi(self): with self.assertRaisesRegex(InterpreterError, 'unrecognized'): interp.exec('raise Exception("it worked!")') + def test_list_comprehension(self): + # gh-135450: List comprehensions caused an assertion failure + # in _PyCode_CheckNoExternalState() + import string + r_interp, w_interp = self.pipe() + + interp = interpreters.create() + interp.exec(f"""if True: + import os + comp = [str(i) for i in range(10)] + os.write({w_interp}, ''.join(comp).encode()) + """) + self.assertEqual(os.read(r_interp, 10).decode(), string.digits) + interp.close() + + # test__interpreters covers the remaining # Interpreter.exec() behavior. diff --git a/Objects/codeobject.c b/Objects/codeobject.c index ee869d991d93cd..0e887ee197c50c 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1979,7 +1979,6 @@ _PyCode_CheckNoExternalState(PyCodeObject *co, _PyCode_var_counts_t *counts, const char **p_errmsg) { const char *errmsg = NULL; - assert(counts->locals.hidden.total == 0); if (counts->numfree > 0) { // It's a closure. errmsg = "closures not supported"; }