https://github.com/python/cpython/commit/3643a26a01a97e396472ba1d99626cc4e911d266
commit: 3643a26a01a97e396472ba1d99626cc4e911d266
branch: 3.14
author: Victor Stinner <[email protected]>
committer: hugovk <[email protected]>
date: 2025-09-11T08:43:57+03:00
summary:

[3.14] gh-138349: Fix crash when combining module-level annotation and listcomp 
(#138363) (#138749)

* gh-138349: Fix crash when combining module-level annotation and listcomp 
(#138363)

(cherry picked from commit 7a6fd4a45deee23b827bf8e32bde172e14921ed6)

---------

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-13-54-43.gh-issue-138349.0fGmAi.rst
M Include/internal/pycore_magic_number.h
M Lib/test/test_importlib/test_util.py
M Lib/test/test_type_annotations.py
M Python/codegen.c

diff --git a/Include/internal/pycore_magic_number.h 
b/Include/internal/pycore_magic_number.h
index 5047517fdad701..f31951057121d9 100644
--- a/Include/internal/pycore_magic_number.h
+++ b/Include/internal/pycore_magic_number.h
@@ -279,6 +279,7 @@ Known values:
     Python 3.14b1 3624 (Don't optimize LOAD_FAST when local is killed by 
DELETE_FAST)
     Python 3.14b3 3625 (Fix handling of opcodes that may leave operands on the 
stack when optimizing LOAD_FAST)
     Python 3.14rc2 3626 (Fix missing exception handlers in logical expression)
+    Python 3.14rc3 3627 (Fix miscompilation of some module-level annotations)
 
     Python 3.15 will start with 3650
 
@@ -291,7 +292,7 @@ PC/launcher.c must also be updated.
 
 */
 
-#define PYC_MAGIC_NUMBER 3626
+#define PYC_MAGIC_NUMBER 3627
 /* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
    (little-endian) and then appending b'\r\n'. */
 #define PYC_MAGIC_NUMBER_TOKEN \
diff --git a/Lib/test/test_importlib/test_util.py 
b/Lib/test/test_importlib/test_util.py
index 1e87fcc8de3725..55dae82d0e4a43 100644
--- a/Lib/test/test_importlib/test_util.py
+++ b/Lib/test/test_importlib/test_util.py
@@ -635,7 +635,7 @@ def test_magic_number(self):
         # stakeholders such as OS package maintainers must be notified
         # in advance. Such exceptional releases will then require an
         # adjustment to this test case.
-        EXPECTED_MAGIC_NUMBER = 3626
+        EXPECTED_MAGIC_NUMBER = 3627
         actual = int.from_bytes(importlib.util.MAGIC_NUMBER[:2], 'little')
 
         msg = (
diff --git a/Lib/test/test_type_annotations.py 
b/Lib/test/test_type_annotations.py
index c66cb058552643..4c58fade1b4f26 100644
--- a/Lib/test/test_type_annotations.py
+++ b/Lib/test/test_type_annotations.py
@@ -835,3 +835,40 @@ def test_complex_comprehension_inlining_exec(self):
         genexp = annos["unique_name_2"][0]
         lamb = list(genexp)[0]
         self.assertEqual(lamb(), 42)
+
+    # gh-138349
+    def test_module_level_annotation_plus_listcomp(self):
+        cases = [
+            """
+            def report_error():
+                pass
+            try:
+                [0 for name_2 in unique_name_0 if (lambda: name_2)]
+            except:
+                pass
+            annotated_name: 0
+            """,
+            """
+            class Generic:
+                pass
+            try:
+                [0 for name_2 in unique_name_0 if (0 for unique_name_1 in 
unique_name_2 for unique_name_3 in name_2)]
+            except:
+                pass
+            annotated_name: 0
+            """,
+            """
+            class Generic:
+                pass
+            annotated_name: 0
+            try:
+                [0 for name_2 in [[0]] for unique_name_1 in unique_name_2 if 
(lambda: name_2)]
+            except:
+                pass
+            """,
+        ]
+        for code in cases:
+            with self.subTest(code=code):
+                mod = build_module(code)
+                annos = mod.__annotations__
+                self.assertEqual(annos, {"annotated_name": 0})
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-13-54-43.gh-issue-138349.0fGmAi.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-13-54-43.gh-issue-138349.0fGmAi.rst
new file mode 100644
index 00000000000000..9b94eec38d9625
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-13-54-43.gh-issue-138349.0fGmAi.rst
@@ -0,0 +1,2 @@
+Fix crash in certain cases where a module contains both a module-level
+annotation and a comprehension.
diff --git a/Python/codegen.c b/Python/codegen.c
index 7d62b41841ce81..bacd3460f2fa9c 100644
--- a/Python/codegen.c
+++ b/Python/codegen.c
@@ -5500,10 +5500,12 @@ codegen_annassign(compiler *c, stmt_ty s)
                 RETURN_IF_ERROR(_PyCompile_AddDeferredAnnotation(
                     c, s, &conditional_annotation_index));
                 if (conditional_annotation_index != NULL) {
-                    ADDOP_NAME(
-                        c, loc,
-                        SCOPE_TYPE(c) == COMPILE_SCOPE_CLASS ? LOAD_DEREF : 
LOAD_NAME,
-                        &_Py_ID(__conditional_annotations__), cellvars);
+                    if (SCOPE_TYPE(c) == COMPILE_SCOPE_CLASS) {
+                        ADDOP_NAME(c, loc, LOAD_DEREF, 
&_Py_ID(__conditional_annotations__), cellvars);
+                    }
+                    else {
+                        ADDOP_NAME(c, loc, LOAD_NAME, 
&_Py_ID(__conditional_annotations__), names);
+                    }
                     ADDOP_LOAD_CONST_NEW(c, loc, conditional_annotation_index);
                     ADDOP_I(c, loc, SET_ADD, 1);
                     ADDOP(c, loc, POP_TOP);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to