From 1bfe83a114da3939c00746fc44dc5da7f56f525f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Langa?= <lukasz@langa.pl>
Date: Tue, 5 Jul 2022 16:01:24 +0200
Subject: [PATCH] [3.11] gh-94485: Set line number of module's RESUME
 instruction to 0 as specified by PEP 626 (GH-94552) (GH-94562)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Mark Shannon <mark@hotpy.org>

(cherry picked from commit 324d01944d16868b07df9e8eef6987766a31a36d)
---
 Lib/importlib/_bootstrap_external.py          |  5 +++--
 Lib/test/test_code.py                         |  1 -
 Lib/test/test_compile.py                      |  4 +++-
 Lib/test/test_dis.py                          | 18 +++++++--------
 ...2-07-01-20-00-19.gh-issue-94485.mo5st7.rst |  2 ++
 Programs/test_frozenmain.h                    | 22 +++++++++----------
 Python/compile.c                              |  5 ++++-
 7 files changed, 32 insertions(+), 25 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-07-01-20-00-19.gh-issue-94485.mo5st7.rst

diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index 0061ad091c2..446d7981acb 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -402,7 +402,8 @@ def _write_atomic(path, data, mode=0o666):
 #                         add JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual)
 #     Python 3.11a7 3492 (make POP_JUMP_IF_NONE/NOT_NONE/TRUE/FALSE relative)
 #     Python 3.11a7 3493 (Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative)
-#    Python 3.11a7 3494 (New location info table)
+#     Python 3.11a7 3494 (New location info table)
+#     Python 3.11b4 3495 (Set line number of module's RESUME instr to 0 per PEP 626)
 #     Python 3.12 will start with magic number 3500
 
 
@@ -416,7 +417,7 @@ def _write_atomic(path, data, mode=0o666):
 # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
 # in PC/launcher.c must also be updated.
 
-MAGIC_NUMBER = (3494).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3495).to_bytes(2, 'little') + b'\r\n'
 
 _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little')  # For import.c
 
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 308141aaf10..fd68f6dee79 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -376,7 +376,6 @@ def test_co_positions_artificial_instructions(self):
                 for instruction in artificial_instructions
             ],
             [
-                ('RESUME', 0),
                 ("PUSH_EXC_INFO", None),
                 ("LOAD_CONST", None), # artificial 'None'
                 ("STORE_NAME", "e"),  # XX: we know the location for this
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index d7c536e4ab2..d113a00088f 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -161,7 +161,7 @@ def test_leading_newlines(self):
         co = compile(s256, 'fn', 'exec')
         self.assertEqual(co.co_firstlineno, 1)
         lines = list(co.co_lines())
-        self.assertEqual(lines[0][2], None)
+        self.assertEqual(lines[0][2], 0)
         self.assertEqual(lines[1][2], 257)
 
     def test_literals_with_leading_zeroes(self):
@@ -1054,6 +1054,8 @@ def generic_visit(self, node):
 
         # Check against the positions in the code object.
         for (line, end_line, col, end_col) in code.co_positions():
+            if line == 0:
+                continue # This is an artificial module-start line
             # If the offset is not None (indicating missing data), ensure that
             # it was part of one of the AST nodes.
             if line is not None:
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index e3e4a3768e7..4283c30e0a1 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -272,7 +272,7 @@ def bug42562():
 expr_str = "x + 1"
 
 dis_expr_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_NAME                0 (x)
            LOAD_CONST               0 (1)
@@ -283,7 +283,7 @@ def bug42562():
 simple_stmt_str = "x = x + 1"
 
 dis_simple_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_NAME                0 (x)
            LOAD_CONST               0 (1)
@@ -302,7 +302,7 @@ def bug42562():
 # leading newline is for a reason (tests lineno)
 
 dis_annot_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   2        SETUP_ANNOTATIONS
            LOAD_CONST               0 (1)
@@ -342,7 +342,7 @@ def bug42562():
 # Trailing newline has been deliberately omitted
 
 dis_compound_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_CONST               0 (0)
            STORE_NAME               0 (x)
@@ -954,7 +954,7 @@ def test_super_instructions(self):
     @cpython_only
     def test_binary_specialize(self):
         binary_op_quicken = """\
-              0 RESUME_QUICK             0
+  0           0 RESUME_QUICK             0
 
   1           2 LOAD_NAME                0 (a)
               4 LOAD_NAME                1 (b)
@@ -972,7 +972,7 @@ def test_binary_specialize(self):
         self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE     0 (+)", True)
 
         binary_subscr_quicken = """\
-              0 RESUME_QUICK             0
+  0           0 RESUME_QUICK             0
 
   1           2 LOAD_NAME                0 (a)
               4 LOAD_CONST               0 (0)
@@ -992,7 +992,7 @@ def test_binary_specialize(self):
     @cpython_only
     def test_load_attr_specialize(self):
         load_attr_quicken = """\
-              0 RESUME_QUICK             0
+  0           0 RESUME_QUICK             0
 
   1           2 LOAD_CONST               0 ('a')
               4 LOAD_ATTR_SLOT           0 (__class__)
@@ -1006,7 +1006,7 @@ def test_load_attr_specialize(self):
     @cpython_only
     def test_call_specialize(self):
         call_quicken = """\
-              0 RESUME_QUICK             0
+  0           0 RESUME_QUICK             0
 
   1           2 PUSH_NULL
               4 LOAD_NAME                0 (str)
@@ -1595,7 +1595,7 @@ def test_co_positions(self):
             for instr in dis.get_instructions(code)
         ]
         expected = [
-            (None, None, None, None),
+            (0, 1, 0, 0),
             (1, 1, 0, 1),
             (1, 1, 0, 1),
             (2, 2, 2, 3),
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-01-20-00-19.gh-issue-94485.mo5st7.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-01-20-00-19.gh-issue-94485.mo5st7.rst
new file mode 100644
index 00000000000..14d90b7e764
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-01-20-00-19.gh-issue-94485.mo5st7.rst	
@@ -0,0 +1,2 @@
+Line number of a module's ``RESUME`` instruction is set to 0 as specified in
+:pep:`626`.
diff --git a/Programs/test_frozenmain.h b/Programs/test_frozenmain.h
index eec2e0cc604..0b658000d44 100644
--- a/Programs/test_frozenmain.h
+++ b/Programs/test_frozenmain.h
@@ -28,15 +28,15 @@ unsigned char M_test_frozenmain[] = {
     107,101,121,169,0,243,0,0,0,0,250,18,116,101,115,116,
     95,102,114,111,122,101,110,109,97,105,110,46,112,121,250,8,
     60,109,111,100,117,108,101,62,114,18,0,0,0,1,0,0,
-    0,115,152,0,0,0,248,240,6,0,1,11,128,10,128,10,
-    128,10,216,0,24,208,0,24,208,0,24,208,0,24,224,0,
-    5,128,5,208,6,26,209,0,27,212,0,27,208,0,27,216,
-    0,5,128,5,128,106,144,35,148,40,209,0,27,212,0,27,
-    208,0,27,216,9,38,208,9,26,212,9,38,209,9,40,212,
-    9,40,168,24,212,9,50,128,6,240,2,6,12,2,240,0,
-    7,1,42,240,0,7,1,42,128,67,240,14,0,5,10,128,
-    69,208,10,40,144,67,208,10,40,208,10,40,152,54,160,35,
-    156,59,208,10,40,208,10,40,209,4,41,212,4,41,208,4,
-    41,208,4,41,240,15,7,1,42,240,0,7,1,42,114,16,
-    0,0,0,
+    0,115,156,0,0,0,240,3,1,1,1,240,8,0,1,11,
+    128,10,128,10,128,10,216,0,24,208,0,24,208,0,24,208,
+    0,24,224,0,5,128,5,208,6,26,209,0,27,212,0,27,
+    208,0,27,216,0,5,128,5,128,106,144,35,148,40,209,0,
+    27,212,0,27,208,0,27,216,9,38,208,9,26,212,9,38,
+    209,9,40,212,9,40,168,24,212,9,50,128,6,240,2,6,
+    12,2,240,0,7,1,42,240,0,7,1,42,128,67,240,14,
+    0,5,10,128,69,208,10,40,144,67,208,10,40,208,10,40,
+    152,54,160,35,156,59,208,10,40,208,10,40,209,4,41,212,
+    4,41,208,4,41,208,4,41,240,15,7,1,42,240,0,7,
+    1,42,114,16,0,0,0,
 };
diff --git a/Python/compile.c b/Python/compile.c
index 1bb0d96207d..cfe4b6ec04f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1803,7 +1803,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
     c->u->u_curblock = block;
 
     if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
-        c->u->u_lineno = -1;
+        c->u->u_lineno = 0;
     }
     else {
         if (!compiler_set_qualname(c))
@@ -1811,6 +1811,9 @@ compiler_enter_scope(struct compiler *c, identifier name,
     }
     ADDOP_I(c, RESUME, 0);
 
+    if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
+        c->u->u_lineno = -1;
+    }
     return 1;
 }
 
-- 
GitLab