]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94485: Set line number of module's RESUME instruction to 0, as specified by PEP...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 5 Jul 2022 12:38:44 +0000 (13:38 +0100)
committerGitHub <noreply@github.com>
Tue, 5 Jul 2022 12:38:44 +0000 (14:38 +0200)
Co-authored-by: Mark Shannon <mark@hotpy.org>
Lib/importlib/_bootstrap_external.py
Lib/test/test_code.py
Lib/test/test_compile.py
Lib/test/test_dis.py
Misc/NEWS.d/next/Core and Builtins/2022-07-01-20-00-19.gh-issue-94485.mo5st7.rst [new file with mode: 0644]
Programs/test_frozenmain.h
Python/compile.c

index 6cd3538624cb4f012294e8af020775fb6a3820da..007127b9fd42672925fe0b6b0eb67d2284cb4491 100644 (file)
@@ -410,6 +410,7 @@ _code_type = type(_write_atomic.__code__)
 #     Python 3.12a1 3504 (Merge LOAD_METHOD back into LOAD_ATTR)
 #     Python 3.12a1 3505 (Specialization/Cache for FOR_ITER)
 #     Python 3.12a1 3506 (Add BINARY_SLICE and STORE_SLICE instructions)
+#     Python 3.12a1 3507 (Set lineno of module's RESUME to 0)
 
 #     Python 3.13 will start with 3550
 
@@ -423,7 +424,7 @@ _code_type = type(_write_atomic.__code__)
 # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
 # in PC/launcher.c must also be updated.
 
-MAGIC_NUMBER = (3506).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3507).to_bytes(2, 'little') + b'\r\n'
 
 _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little')  # For import.c
 
index 308141aaf10e557919bf79a0418319e1a66f6719..fd68f6dee7915a583433469aaeab9bbfd8f0ff4a 100644 (file)
@@ -376,7 +376,6 @@ class CodeTest(unittest.TestCase):
                 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
index 46b16e753ceee2f7b9ece957e09426b1a7796f54..ab1685d3e5b8a99ea6b18ef9138e9edbb58cfd68 100644 (file)
@@ -161,7 +161,7 @@ if 1:
         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):
@@ -1032,8 +1032,8 @@ if 1:
         def check_op_count(func, op, expected):
             actual = 0
             for instr in dis.Bytecode(func):
-                 if instr.opname == op:
-                     actual += 1
+                if instr.opname == op:
+                    actual += 1
             self.assertEqual(actual, expected)
 
         def load():
@@ -1090,6 +1090,8 @@ class TestSourcePositions(unittest.TestCase):
 
         # 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:
index 07f1203ff068876394a0ae7b08190bc12cfc71c4..256004855d4a6e2dcea833b641fcc58dbeffccdb 100644 (file)
@@ -267,7 +267,7 @@ Disassembly of g:
 expr_str = "x + 1"
 
 dis_expr_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_NAME                0 (x)
            LOAD_CONST               0 (1)
@@ -278,7 +278,7 @@ dis_expr_str = """\
 simple_stmt_str = "x = x + 1"
 
 dis_simple_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_NAME                0 (x)
            LOAD_CONST               0 (1)
@@ -297,7 +297,7 @@ lst[fun(0)]: int = 1
 # leading newline is for a reason (tests lineno)
 
 dis_annot_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   2        SETUP_ANNOTATIONS
            LOAD_CONST               0 (1)
@@ -335,7 +335,7 @@ while 1:
 # Trailing newline has been deliberately omitted
 
 dis_compound_stmt_str = """\
-           RESUME                   0
+  0        RESUME                   0
 
   1        LOAD_CONST               0 (0)
            STORE_NAME               0 (x)
@@ -1092,7 +1092,7 @@ class DisTests(DisTestBase):
     @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)
@@ -1110,7 +1110,7 @@ class DisTests(DisTestBase):
         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)
@@ -1130,7 +1130,7 @@ class DisTests(DisTestBase):
     @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__)
@@ -1144,7 +1144,7 @@ class DisTests(DisTestBase):
     @cpython_only
     def test_call_specialize(self):
         call_quicken = """\
-           RESUME_QUICK             0
+  0        RESUME_QUICK             0
 
   1        PUSH_NULL
            LOAD_NAME                0 (str)
@@ -1718,7 +1718,7 @@ class InstructionTests(InstructionTestCase):
             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 (file)
index 0000000..14d90b7
--- /dev/null
@@ -0,0 +1,2 @@
+Line number of a module's ``RESUME`` instruction is set to 0 as specified in
+:pep:`626`.
index 55d461c8beb8096cdaad19c86696e502f282d748..677ce180f95fddd8352aa84744a9aff0da529a10 100644 (file)
@@ -28,15 +28,15 @@ unsigned char M_test_frozenmain[] = {
     3,0,0,0,218,3,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,145,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,212,0,27,208,
-    0,27,216,0,5,128,5,128,106,144,35,151,40,145,40,212,
-    0,27,208,0,27,216,9,38,208,9,26,215,9,38,209,9,
-    38,212,9,40,168,24,212,9,50,128,6,240,2,6,12,2,
-    240,0,7,1,42,241,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,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,0,0,1,0,0,0,115,149,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,
+    212,0,27,208,0,27,216,0,5,128,5,128,106,144,35,151,
+    40,145,40,212,0,27,208,0,27,216,9,38,208,9,26,215,
+    9,38,209,9,38,212,9,40,168,24,212,9,50,128,6,240,
+    2,6,12,2,240,0,7,1,42,241,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,212,4,
+    41,208,4,41,208,4,41,240,15,7,1,42,240,0,7,1,
+    42,114,16,0,0,0,
 };
index 5ae1e345201ce4eac170250fcb01274ea2be0db9..77176893f60c5cc91215107f1265d6ec4e80a697 100644 (file)
@@ -1759,7 +1759,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_loc.lineno = -1;
+        c->u->u_loc.lineno = 0;
     }
     else {
         if (!compiler_set_qualname(c))
@@ -1767,6 +1767,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_loc.lineno = -1;
+    }
     return 1;
 }