Skip to content

Commit 1bfe83a

Browse files
authored
[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 324d019)
1 parent 0615e7f commit 1bfe83a

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

Diff for: Lib/importlib/_bootstrap_external.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ def _write_atomic(path, data, mode=0o666):
402402
# add JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual)
403403
# Python 3.11a7 3492 (make POP_JUMP_IF_NONE/NOT_NONE/TRUE/FALSE relative)
404404
# Python 3.11a7 3493 (Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative)
405-
# Python 3.11a7 3494 (New location info table)
405+
# Python 3.11a7 3494 (New location info table)
406+
# Python 3.11b4 3495 (Set line number of module's RESUME instr to 0 per PEP 626)
406407
# Python 3.12 will start with magic number 3500
407408

408409

@@ -416,7 +417,7 @@ def _write_atomic(path, data, mode=0o666):
416417
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
417418
# in PC/launcher.c must also be updated.
418419

419-
MAGIC_NUMBER = (3494).to_bytes(2, 'little') + b'\r\n'
420+
MAGIC_NUMBER = (3495).to_bytes(2, 'little') + b'\r\n'
420421

421422
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
422423

Diff for: Lib/test/test_code.py

-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ def test_co_positions_artificial_instructions(self):
376376
for instruction in artificial_instructions
377377
],
378378
[
379-
('RESUME', 0),
380379
("PUSH_EXC_INFO", None),
381380
("LOAD_CONST", None), # artificial 'None'
382381
("STORE_NAME", "e"), # XX: we know the location for this

Diff for: Lib/test/test_compile.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def test_leading_newlines(self):
161161
co = compile(s256, 'fn', 'exec')
162162
self.assertEqual(co.co_firstlineno, 1)
163163
lines = list(co.co_lines())
164-
self.assertEqual(lines[0][2], None)
164+
self.assertEqual(lines[0][2], 0)
165165
self.assertEqual(lines[1][2], 257)
166166

167167
def test_literals_with_leading_zeroes(self):
@@ -1054,6 +1054,8 @@ def generic_visit(self, node):
10541054

10551055
# Check against the positions in the code object.
10561056
for (line, end_line, col, end_col) in code.co_positions():
1057+
if line == 0:
1058+
continue # This is an artificial module-start line
10571059
# If the offset is not None (indicating missing data), ensure that
10581060
# it was part of one of the AST nodes.
10591061
if line is not None:

Diff for: Lib/test/test_dis.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def bug42562():
272272
expr_str = "x + 1"
273273

274274
dis_expr_str = """\
275-
RESUME 0
275+
0 RESUME 0
276276
277277
1 LOAD_NAME 0 (x)
278278
LOAD_CONST 0 (1)
@@ -283,7 +283,7 @@ def bug42562():
283283
simple_stmt_str = "x = x + 1"
284284

285285
dis_simple_stmt_str = """\
286-
RESUME 0
286+
0 RESUME 0
287287
288288
1 LOAD_NAME 0 (x)
289289
LOAD_CONST 0 (1)
@@ -302,7 +302,7 @@ def bug42562():
302302
# leading newline is for a reason (tests lineno)
303303

304304
dis_annot_stmt_str = """\
305-
RESUME 0
305+
0 RESUME 0
306306
307307
2 SETUP_ANNOTATIONS
308308
LOAD_CONST 0 (1)
@@ -342,7 +342,7 @@ def bug42562():
342342
# Trailing newline has been deliberately omitted
343343

344344
dis_compound_stmt_str = """\
345-
RESUME 0
345+
0 RESUME 0
346346
347347
1 LOAD_CONST 0 (0)
348348
STORE_NAME 0 (x)
@@ -954,7 +954,7 @@ def test_super_instructions(self):
954954
@cpython_only
955955
def test_binary_specialize(self):
956956
binary_op_quicken = """\
957-
0 RESUME_QUICK 0
957+
0 0 RESUME_QUICK 0
958958
959959
1 2 LOAD_NAME 0 (a)
960960
4 LOAD_NAME 1 (b)
@@ -972,7 +972,7 @@ def test_binary_specialize(self):
972972
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE 0 (+)", True)
973973

974974
binary_subscr_quicken = """\
975-
0 RESUME_QUICK 0
975+
0 0 RESUME_QUICK 0
976976
977977
1 2 LOAD_NAME 0 (a)
978978
4 LOAD_CONST 0 (0)
@@ -992,7 +992,7 @@ def test_binary_specialize(self):
992992
@cpython_only
993993
def test_load_attr_specialize(self):
994994
load_attr_quicken = """\
995-
0 RESUME_QUICK 0
995+
0 0 RESUME_QUICK 0
996996
997997
1 2 LOAD_CONST 0 ('a')
998998
4 LOAD_ATTR_SLOT 0 (__class__)
@@ -1006,7 +1006,7 @@ def test_load_attr_specialize(self):
10061006
@cpython_only
10071007
def test_call_specialize(self):
10081008
call_quicken = """\
1009-
0 RESUME_QUICK 0
1009+
0 0 RESUME_QUICK 0
10101010
10111011
1 2 PUSH_NULL
10121012
4 LOAD_NAME 0 (str)
@@ -1595,7 +1595,7 @@ def test_co_positions(self):
15951595
for instr in dis.get_instructions(code)
15961596
]
15971597
expected = [
1598-
(None, None, None, None),
1598+
(0, 1, 0, 0),
15991599
(1, 1, 0, 1),
16001600
(1, 1, 0, 1),
16011601
(2, 2, 2, 3),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Line number of a module's ``RESUME`` instruction is set to 0 as specified in
2+
:pep:`626`.

Diff for: Programs/test_frozenmain.h

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Python/compile.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1803,14 +1803,17 @@ compiler_enter_scope(struct compiler *c, identifier name,
18031803
c->u->u_curblock = block;
18041804

18051805
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1806-
c->u->u_lineno = -1;
1806+
c->u->u_lineno = 0;
18071807
}
18081808
else {
18091809
if (!compiler_set_qualname(c))
18101810
return 0;
18111811
}
18121812
ADDOP_I(c, RESUME, 0);
18131813

1814+
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1815+
c->u->u_lineno = -1;
1816+
}
18141817
return 1;
18151818
}
18161819

0 commit comments

Comments
 (0)