Skip to content

Commit 324d019

Browse files
pythongh-94485: Set line number of module's RESUME instruction to 0, as specified by PEP 626 (pythonGH-94552)
Co-authored-by: Mark Shannon <[email protected]>
1 parent a2a3f2c commit 324d019

File tree

7 files changed

+33
-26
lines changed

7 files changed

+33
-26
lines changed

Lib/importlib/_bootstrap_external.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ def _write_atomic(path, data, mode=0o666):
410410
# Python 3.12a1 3504 (Merge LOAD_METHOD back into LOAD_ATTR)
411411
# Python 3.12a1 3505 (Specialization/Cache for FOR_ITER)
412412
# Python 3.12a1 3506 (Add BINARY_SLICE and STORE_SLICE instructions)
413+
# Python 3.12a1 3507 (Set lineno of module's RESUME to 0)
413414

414415
# Python 3.13 will start with 3550
415416

@@ -423,7 +424,7 @@ def _write_atomic(path, data, mode=0o666):
423424
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
424425
# in PC/launcher.c must also be updated.
425426

426-
MAGIC_NUMBER = (3506).to_bytes(2, 'little') + b'\r\n'
427+
MAGIC_NUMBER = (3507).to_bytes(2, 'little') + b'\r\n'
427428

428429
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
429430

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

Lib/test/test_compile.py

+5-3
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):
@@ -1032,8 +1032,8 @@ def test_uses_slice_instructions(self):
10321032
def check_op_count(func, op, expected):
10331033
actual = 0
10341034
for instr in dis.Bytecode(func):
1035-
if instr.opname == op:
1036-
actual += 1
1035+
if instr.opname == op:
1036+
actual += 1
10371037
self.assertEqual(actual, expected)
10381038

10391039
def load():
@@ -1090,6 +1090,8 @@ def generic_visit(self, node):
10901090

10911091
# Check against the positions in the code object.
10921092
for (line, end_line, col, end_col) in code.co_positions():
1093+
if line == 0:
1094+
continue # This is an artificial module-start line
10931095
# If the offset is not None (indicating missing data), ensure that
10941096
# it was part of one of the AST nodes.
10951097
if line is not None:

Lib/test/test_dis.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def bug42562():
267267
expr_str = "x + 1"
268268

269269
dis_expr_str = """\
270-
RESUME 0
270+
0 RESUME 0
271271
272272
1 LOAD_NAME 0 (x)
273273
LOAD_CONST 0 (1)
@@ -278,7 +278,7 @@ def bug42562():
278278
simple_stmt_str = "x = x + 1"
279279

280280
dis_simple_stmt_str = """\
281-
RESUME 0
281+
0 RESUME 0
282282
283283
1 LOAD_NAME 0 (x)
284284
LOAD_CONST 0 (1)
@@ -297,7 +297,7 @@ def bug42562():
297297
# leading newline is for a reason (tests lineno)
298298

299299
dis_annot_stmt_str = """\
300-
RESUME 0
300+
0 RESUME 0
301301
302302
2 SETUP_ANNOTATIONS
303303
LOAD_CONST 0 (1)
@@ -335,7 +335,7 @@ def bug42562():
335335
# Trailing newline has been deliberately omitted
336336

337337
dis_compound_stmt_str = """\
338-
RESUME 0
338+
0 RESUME 0
339339
340340
1 LOAD_CONST 0 (0)
341341
STORE_NAME 0 (x)
@@ -1092,7 +1092,7 @@ def test_super_instructions(self):
10921092
@cpython_only
10931093
def test_binary_specialize(self):
10941094
binary_op_quicken = """\
1095-
0 RESUME_QUICK 0
1095+
0 0 RESUME_QUICK 0
10961096
10971097
1 2 LOAD_NAME 0 (a)
10981098
4 LOAD_NAME 1 (b)
@@ -1110,7 +1110,7 @@ def test_binary_specialize(self):
11101110
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE 0 (+)", True)
11111111

11121112
binary_subscr_quicken = """\
1113-
0 RESUME_QUICK 0
1113+
0 0 RESUME_QUICK 0
11141114
11151115
1 2 LOAD_NAME 0 (a)
11161116
4 LOAD_CONST 0 (0)
@@ -1130,7 +1130,7 @@ def test_binary_specialize(self):
11301130
@cpython_only
11311131
def test_load_attr_specialize(self):
11321132
load_attr_quicken = """\
1133-
0 RESUME_QUICK 0
1133+
0 0 RESUME_QUICK 0
11341134
11351135
1 2 LOAD_CONST 0 ('a')
11361136
4 LOAD_ATTR_SLOT 0 (__class__)
@@ -1144,7 +1144,7 @@ def test_load_attr_specialize(self):
11441144
@cpython_only
11451145
def test_call_specialize(self):
11461146
call_quicken = """\
1147-
RESUME_QUICK 0
1147+
0 RESUME_QUICK 0
11481148
11491149
1 PUSH_NULL
11501150
LOAD_NAME 0 (str)
@@ -1718,7 +1718,7 @@ def test_co_positions(self):
17181718
for instr in dis.get_instructions(code)
17191719
]
17201720
expected = [
1721-
(None, None, None, None),
1721+
(0, 1, 0, 0),
17221722
(1, 1, 0, 1),
17231723
(1, 1, 0, 1),
17241724
(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`.

Programs/test_frozenmain.h

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

Python/compile.c

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

17611761
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1762-
c->u->u_loc.lineno = -1;
1762+
c->u->u_loc.lineno = 0;
17631763
}
17641764
else {
17651765
if (!compiler_set_qualname(c))
17661766
return 0;
17671767
}
17681768
ADDOP_I(c, RESUME, 0);
17691769

1770+
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1771+
c->u->u_loc.lineno = -1;
1772+
}
17701773
return 1;
17711774
}
17721775

0 commit comments

Comments
 (0)