Skip to content

Commit 25d2788

Browse files
committed
Remove assertion in _PyCode_CheckNoExternalState()
1 parent 2b0c684 commit 25d2788

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_interpreters/test_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,22 @@ def test_created_with_capi(self):
944944
with self.assertRaisesRegex(InterpreterError, 'unrecognized'):
945945
interp.exec('raise Exception("it worked!")')
946946

947+
def test_list_comprehension(self):
948+
# gh-135450: List comprehensions caused an assertion failure
949+
# in _PyCode_CheckNoExternalState()
950+
import string
951+
r_interp, w_interp = self.pipe()
952+
953+
interp = interpreters.create()
954+
interp.exec(f"""if True:
955+
import os
956+
comp = [str(i) for i in range(10)]
957+
os.write({w_interp}, ''.join(comp).encode())
958+
""")
959+
self.assertEqual(os.read(r_interp, 10).decode(), string.digits)
960+
interp.close()
961+
962+
947963
# test__interpreters covers the remaining
948964
# Interpreter.exec() behavior.
949965

Objects/codeobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,6 @@ _PyCode_CheckNoExternalState(PyCodeObject *co, _PyCode_var_counts_t *counts,
19791979
const char **p_errmsg)
19801980
{
19811981
const char *errmsg = NULL;
1982-
assert(counts->locals.hidden.total == 0);
19831982
if (counts->numfree > 0) { // It's a closure.
19841983
errmsg = "closures not supported";
19851984
}

0 commit comments

Comments
 (0)