Skip to content

gh-121404: move calculation of module start location from compiler_body up to compiler_codegen #122127

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

Merged
merged 2 commits into from
Jul 22, 2024
Merged
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
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;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not doing anything - loc is a local variable and we're exiting the function.

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
Loading