https://github.com/python/cpython/commit/c03f94e7fe2fe96827ed96ce78aed0fc88fb8358
commit: c03f94e7fe2fe96827ed96ce78aed0fc88fb8358
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: ericsnowcurrently <ericsnowcurren...@gmail.com>
date: 2025-06-18T23:57:18Z
summary:

[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 15f2bac02c5e, AKA gh-135466)

Co-authored-by: Peter Bierma <zintensity...@gmail.com>

files:
M Lib/test/test_interpreters/test_api.py
M Objects/codeobject.c

diff --git a/Lib/test/test_interpreters/test_api.py 
b/Lib/test/test_interpreters/test_api.py
index 612e8240603f03..0ee4582b5d1568 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 34b50ef97d544b..3f53d4cfeb20ac 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -1999,7 +1999,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";
     }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to