Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c03f94e

Browse files
[3.14] gh-135450: Remove assertion in _PyCode_CheckNoExternalState (gh-135694)
The assertion reflected a misunderstanding of situations where "hidden" variables might exist, namely generator expressions and comprehensions. (cherry picked from commit 15f2bac, AKA gh-135466) Co-authored-by: Peter Bierma <[email protected]>
1 parent 47ee2ae commit c03f94e

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
@@ -1999,7 +1999,6 @@ _PyCode_CheckNoExternalState(PyCodeObject *co, _PyCode_var_counts_t *counts,
19991999
const char **p_errmsg)
20002000
{
20012001
const char *errmsg = NULL;
2002-
assert(counts->locals.hidden.total == 0);
20032002
if (counts->numfree > 0) { // It's a closure.
20042003
errmsg = "closures not supported";
20052004
}

0 commit comments

Comments
 (0)