Skip to content

Commit d5a12b4

Browse files
authored
gh-121404: move calculation of module start location from compiler_body up to compiler_codegen (#122127)
1 parent 5716cc3 commit d5a12b4

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Python/compile.c

+28-20
Original file line numberDiff line numberDiff line change
@@ -1151,9 +1151,6 @@ compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
11511151
}
11521152
ADDOP_I(c, loc, RESUME, RESUME_AT_FUNC_START);
11531153

1154-
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1155-
loc.lineno = -1;
1156-
}
11571154
return SUCCESS;
11581155
}
11591156

@@ -1459,15 +1456,6 @@ compiler_leave_annotations_scope(struct compiler *c, location loc,
14591456
static int
14601457
compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
14611458
{
1462-
1463-
/* Set current line number to the line number of first statement.
1464-
This way line number for SETUP_ANNOTATIONS will always
1465-
coincide with the line number of first "real" statement in module.
1466-
If body is empty, then lineno will be set later in optimize_and_assemble. */
1467-
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
1468-
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
1469-
loc = LOC(st);
1470-
}
14711459
/* If from __future__ import annotations is active,
14721460
* every annotated class and module should have __annotations__.
14731461
* Else __annotate__ is created when necessary. */
@@ -1545,31 +1533,51 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
15451533
return SUCCESS;
15461534
}
15471535

1536+
static location
1537+
start_location(asdl_stmt_seq *stmts)
1538+
{
1539+
if (asdl_seq_LEN(stmts) > 0) {
1540+
/* Set current line number to the line number of first statement.
1541+
* This way line number for SETUP_ANNOTATIONS will always
1542+
* coincide with the line number of first "real" statement in module.
1543+
* If body is empty, then lineno will be set later in optimize_and_assemble.
1544+
*/
1545+
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
1546+
return LOC(st);
1547+
}
1548+
return LOCATION(1, 1, 0, 0);
1549+
}
1550+
15481551
static int
15491552
compiler_codegen(struct compiler *c, mod_ty mod)
15501553
{
1551-
location loc = LOCATION(1, 1, 0, 0);
1554+
assert(c->u->u_scope_type == COMPILER_SCOPE_MODULE);
15521555
switch (mod->kind) {
1553-
case Module_kind:
1554-
if (compiler_body(c, loc, mod->v.Module.body) < 0) {
1556+
case Module_kind: {
1557+
asdl_stmt_seq *stmts = mod->v.Module.body;
1558+
if (compiler_body(c, start_location(stmts), stmts) < 0) {
15551559
return ERROR;
15561560
}
15571561
break;
1558-
case Interactive_kind:
1562+
}
1563+
case Interactive_kind: {
15591564
c->c_interactive = 1;
1560-
if (compiler_body(c, loc, mod->v.Interactive.body) < 0) {
1565+
asdl_stmt_seq *stmts = mod->v.Interactive.body;
1566+
if (compiler_body(c, start_location(stmts), stmts) < 0) {
15611567
return ERROR;
15621568
}
15631569
break;
1564-
case Expression_kind:
1570+
}
1571+
case Expression_kind: {
15651572
VISIT(c, expr, mod->v.Expression.body);
15661573
break;
1567-
default:
1574+
}
1575+
default: {
15681576
PyErr_Format(PyExc_SystemError,
15691577
"module kind %d should not be possible",
15701578
mod->kind);
15711579
return ERROR;
1572-
}
1580+
}}
15731581
return SUCCESS;
15741582
}
15751583

0 commit comments

Comments
 (0)