Skip to content

Commit 61cb3d0

Browse files
bpo-39987: Simplify setting lineno in the compiler. (GH-19037)
1 parent eb886db commit 61cb3d0

File tree

4 files changed

+3970
-4006
lines changed

4 files changed

+3970
-4006
lines changed

Python/compile.c

+18-50
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ struct compiler_unit {
142142
int u_firstlineno; /* the first lineno of the block */
143143
int u_lineno; /* the lineno for the current stmt */
144144
int u_col_offset; /* the offset of the current stmt */
145-
int u_lineno_set; /* boolean to indicate whether instr
146-
has been generated with current lineno */
147145
};
148146

149147
/* This struct captures the global state of a compilation.
@@ -614,7 +612,6 @@ compiler_enter_scope(struct compiler *c, identifier name,
614612
u->u_firstlineno = lineno;
615613
u->u_lineno = 0;
616614
u->u_col_offset = 0;
617-
u->u_lineno_set = 0;
618615
u->u_consts = PyDict_New();
619616
if (!u->u_consts) {
620617
compiler_unit_free(u);
@@ -849,28 +846,18 @@ compiler_next_instr(basicblock *b)
849846
return b->b_iused++;
850847
}
851848

852-
/* Set the i_lineno member of the instruction at offset off if the
853-
line number for the current expression/statement has not
854-
already been set. If it has been set, the call has no effect.
849+
/* Set the line number and column offset for the following instructions.
855850
856851
The line number is reset in the following cases:
857852
- when entering a new scope
858853
- on each statement
859-
- on each expression that start a new line
854+
- on each expression and sub-expression
860855
- before the "except" and "finally" clauses
861-
- before the "for" and "while" expressions
862856
*/
863857

864-
static void
865-
compiler_set_lineno(struct compiler *c, int off)
866-
{
867-
basicblock *b;
868-
if (c->u->u_lineno_set)
869-
return;
870-
c->u->u_lineno_set = 1;
871-
b = c->u->u_curblock;
872-
b->b_instr[off].i_lineno = c->u->u_lineno;
873-
}
858+
#define SET_LOC(c, x) \
859+
(c)->u->u_lineno = (x)->lineno; \
860+
(c)->u->u_col_offset = (x)->col_offset;
874861

875862
/* Return the stack effect of opcode with argument oparg.
876863
@@ -1172,7 +1159,7 @@ compiler_addop(struct compiler *c, int opcode)
11721159
i->i_oparg = 0;
11731160
if (opcode == RETURN_VALUE)
11741161
b->b_return = 1;
1175-
compiler_set_lineno(c, off);
1162+
i->i_lineno = c->u->u_lineno;
11761163
return 1;
11771164
}
11781165

@@ -1407,7 +1394,7 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
14071394
i = &c->u->u_curblock->b_instr[off];
14081395
i->i_opcode = opcode;
14091396
i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1410-
compiler_set_lineno(c, off);
1397+
i->i_lineno = c->u->u_lineno;
14111398
return 1;
14121399
}
14131400

@@ -1433,7 +1420,7 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
14331420
i->i_jabs = 1;
14341421
else
14351422
i->i_jrel = 1;
1436-
compiler_set_lineno(c, off);
1423+
i->i_lineno = c->u->u_lineno;
14371424
return 1;
14381425
}
14391426

@@ -1706,7 +1693,6 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
17061693
int saved_lineno = c->u->u_lineno;
17071694
VISIT_SEQ(c, stmt, info->fb_datum);
17081695
c->u->u_lineno = saved_lineno;
1709-
c->u->u_lineno_set = 0;
17101696
if (preserve_tos) {
17111697
compiler_pop_fblock(c, POP_VALUE, NULL);
17121698
}
@@ -1805,10 +1791,9 @@ compiler_body(struct compiler *c, asdl_seq *stmts)
18051791
This way line number for SETUP_ANNOTATIONS will always
18061792
coincide with the line number of first "real" statement in module.
18071793
If body is empty, then lineno will be set later in assemble. */
1808-
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1809-
!c->u->u_lineno && asdl_seq_LEN(stmts)) {
1794+
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
18101795
st = (stmt_ty)asdl_seq_GET(stmts, 0);
1811-
c->u->u_lineno = st->lineno;
1796+
SET_LOC(c, st);
18121797
}
18131798
/* Every annotated class and module should have __annotations__. */
18141799
if (find_ann(stmts)) {
@@ -3043,9 +3028,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
30433028
s->v.Try.handlers, i);
30443029
if (!handler->v.ExceptHandler.type && i < n-1)
30453030
return compiler_error(c, "default 'except:' must be last");
3046-
c->u->u_lineno_set = 0;
3047-
c->u->u_lineno = handler->lineno;
3048-
c->u->u_col_offset = handler->col_offset;
3031+
SET_LOC(c, handler);
30493032
except = compiler_new_block(c);
30503033
if (except == NULL)
30513034
return 0;
@@ -3345,9 +3328,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
33453328
Py_ssize_t i, n;
33463329

33473330
/* Always assign a lineno to the next instruction for a stmt. */
3348-
c->u->u_lineno = s->lineno;
3349-
c->u->u_col_offset = s->col_offset;
3350-
c->u->u_lineno_set = 0;
3331+
SET_LOC(c, s);
33513332

33523333
switch (s->kind) {
33533334
case FunctionDef_kind:
@@ -5095,24 +5076,11 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
50955076
static int
50965077
compiler_visit_expr(struct compiler *c, expr_ty e)
50975078
{
5098-
/* If expr e has a different line number than the last expr/stmt,
5099-
set a new line number for the next instruction.
5100-
*/
51015079
int old_lineno = c->u->u_lineno;
51025080
int old_col_offset = c->u->u_col_offset;
5103-
if (e->lineno != c->u->u_lineno) {
5104-
c->u->u_lineno = e->lineno;
5105-
c->u->u_lineno_set = 0;
5106-
}
5107-
/* Updating the column offset is always harmless. */
5108-
c->u->u_col_offset = e->col_offset;
5109-
5081+
SET_LOC(c, e);
51105082
int res = compiler_visit_expr1(c, e);
5111-
5112-
if (old_lineno != c->u->u_lineno) {
5113-
c->u->u_lineno = old_lineno;
5114-
c->u->u_lineno_set = 0;
5115-
}
5083+
c->u->u_lineno = old_lineno;
51165084
c->u->u_col_offset = old_col_offset;
51175085
return res;
51185086
}
@@ -5590,14 +5558,14 @@ assemble_lnotab(struct assembler *a, struct instr *i)
55905558
Py_ssize_t len;
55915559
unsigned char *lnotab;
55925560

5593-
d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
55945561
d_lineno = i->i_lineno - a->a_lineno;
5562+
if (d_lineno == 0) {
5563+
return 1;
5564+
}
55955565

5566+
d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
55965567
assert(d_bytecode >= 0);
55975568

5598-
if(d_bytecode == 0 && d_lineno == 0)
5599-
return 1;
5600-
56015569
if (d_bytecode > 255) {
56025570
int j, nbytes, ncodes = d_bytecode / 255;
56035571
nbytes = a->a_lnotab_off + 2 * ncodes;

0 commit comments

Comments
 (0)