Skip to content

Commit

Permalink
pythongh-121404: move calculation of module start location from compi…
Browse files Browse the repository at this point in the history
…ler_body up to compiler_codegen (python#122127)
  • Loading branch information
iritkatriel authored Jul 22, 2024
1 parent 5716cc3 commit d5a12b4
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,9 +1151,6 @@ compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
}
ADDOP_I(c, loc, RESUME, RESUME_AT_FUNC_START);

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

Expand Down Expand Up @@ -1459,15 +1456,6 @@ compiler_leave_annotations_scope(struct compiler *c, location loc,
static int
compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
{

/* Set current line number to the line number of first statement.
This way line number for SETUP_ANNOTATIONS will always
coincide with the line number of first "real" statement in module.
If body is empty, then lineno will be set later in optimize_and_assemble. */
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
loc = LOC(st);
}
/* If from __future__ import annotations is active,
* every annotated class and module should have __annotations__.
* Else __annotate__ is created when necessary. */
Expand Down Expand Up @@ -1545,31 +1533,51 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
return SUCCESS;
}

static location
start_location(asdl_stmt_seq *stmts)
{
if (asdl_seq_LEN(stmts) > 0) {
/* Set current line number to the line number of first statement.
* This way line number for SETUP_ANNOTATIONS will always
* coincide with the line number of first "real" statement in module.
* If body is empty, then lineno will be set later in optimize_and_assemble.
*/
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
return LOC(st);
}
return LOCATION(1, 1, 0, 0);
}

static int
compiler_codegen(struct compiler *c, mod_ty mod)
{
location loc = LOCATION(1, 1, 0, 0);
assert(c->u->u_scope_type == COMPILER_SCOPE_MODULE);
switch (mod->kind) {
case Module_kind:
if (compiler_body(c, loc, mod->v.Module.body) < 0) {
case Module_kind: {
asdl_stmt_seq *stmts = mod->v.Module.body;
if (compiler_body(c, start_location(stmts), stmts) < 0) {
return ERROR;
}
break;
case Interactive_kind:
}
case Interactive_kind: {
c->c_interactive = 1;
if (compiler_body(c, loc, mod->v.Interactive.body) < 0) {
asdl_stmt_seq *stmts = mod->v.Interactive.body;
if (compiler_body(c, start_location(stmts), stmts) < 0) {
return ERROR;
}
break;
case Expression_kind:
}
case Expression_kind: {
VISIT(c, expr, mod->v.Expression.body);
break;
default:
}
default: {
PyErr_Format(PyExc_SystemError,
"module kind %d should not be possible",
mod->kind);
return ERROR;
}
}}
return SUCCESS;
}

Expand Down

0 comments on commit d5a12b4

Please sign in to comment.