Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-94485: Set line number of module's RESUME instruction to 0, as specified by PEP 626 #94552

Merged
merged 6 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def _write_atomic(path, data, mode=0o666):
# 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

Expand All @@ -423,7 +424,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 = (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

Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1032,8 +1032,8 @@ def test_uses_slice_instructions(self):
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():
Expand Down Expand Up @@ -1090,6 +1090,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:
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def bug42562():
expr_str = "x + 1"

dis_expr_str = """\
RESUME 0
0 RESUME 0

1 LOAD_NAME 0 (x)
LOAD_CONST 0 (1)
Expand All @@ -278,7 +278,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)
Expand All @@ -297,7 +297,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)
Expand Down Expand Up @@ -335,7 +335,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)
Expand Down Expand Up @@ -1092,7 +1092,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)
Expand All @@ -1110,7 +1110,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)
Expand All @@ -1130,7 +1130,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__)
Expand All @@ -1144,7 +1144,7 @@ def test_load_attr_specialize(self):
@cpython_only
def test_call_specialize(self):
call_quicken = """\
RESUME_QUICK 0
0 RESUME_QUICK 0

1 PUSH_NULL
LOAD_NAME 0 (str)
Expand Down Expand Up @@ -1718,7 +1718,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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Line number of a module's ``RESUME`` instruction is set to 0 as specified in
:pep:`626`.
22 changes: 11 additions & 11 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,14 +1759,17 @@ 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))
return 0;
}
ADDOP_I(c, RESUME, 0);

if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
c->u->u_loc.lineno = -1;
}
return 1;
}

Expand Down