@@ -142,8 +142,6 @@ struct compiler_unit {
142
142
int u_firstlineno ; /* the first lineno of the block */
143
143
int u_lineno ; /* the lineno for the current stmt */
144
144
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 */
147
145
};
148
146
149
147
/* This struct captures the global state of a compilation.
@@ -614,7 +612,6 @@ compiler_enter_scope(struct compiler *c, identifier name,
614
612
u -> u_firstlineno = lineno ;
615
613
u -> u_lineno = 0 ;
616
614
u -> u_col_offset = 0 ;
617
- u -> u_lineno_set = 0 ;
618
615
u -> u_consts = PyDict_New ();
619
616
if (!u -> u_consts ) {
620
617
compiler_unit_free (u );
@@ -849,28 +846,18 @@ compiler_next_instr(basicblock *b)
849
846
return b -> b_iused ++ ;
850
847
}
851
848
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.
855
850
856
851
The line number is reset in the following cases:
857
852
- when entering a new scope
858
853
- on each statement
859
- - on each expression that start a new line
854
+ - on each expression and sub-expression
860
855
- before the "except" and "finally" clauses
861
- - before the "for" and "while" expressions
862
856
*/
863
857
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;
874
861
875
862
/* Return the stack effect of opcode with argument oparg.
876
863
@@ -1172,7 +1159,7 @@ compiler_addop(struct compiler *c, int opcode)
1172
1159
i -> i_oparg = 0 ;
1173
1160
if (opcode == RETURN_VALUE )
1174
1161
b -> b_return = 1 ;
1175
- compiler_set_lineno ( c , off ) ;
1162
+ i -> i_lineno = c -> u -> u_lineno ;
1176
1163
return 1 ;
1177
1164
}
1178
1165
@@ -1407,7 +1394,7 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1407
1394
i = & c -> u -> u_curblock -> b_instr [off ];
1408
1395
i -> i_opcode = opcode ;
1409
1396
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 ;
1411
1398
return 1 ;
1412
1399
}
1413
1400
@@ -1433,7 +1420,7 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1433
1420
i -> i_jabs = 1 ;
1434
1421
else
1435
1422
i -> i_jrel = 1 ;
1436
- compiler_set_lineno ( c , off ) ;
1423
+ i -> i_lineno = c -> u -> u_lineno ;
1437
1424
return 1 ;
1438
1425
}
1439
1426
@@ -1706,7 +1693,6 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1706
1693
int saved_lineno = c -> u -> u_lineno ;
1707
1694
VISIT_SEQ (c , stmt , info -> fb_datum );
1708
1695
c -> u -> u_lineno = saved_lineno ;
1709
- c -> u -> u_lineno_set = 0 ;
1710
1696
if (preserve_tos ) {
1711
1697
compiler_pop_fblock (c , POP_VALUE , NULL );
1712
1698
}
@@ -1805,10 +1791,9 @@ compiler_body(struct compiler *c, asdl_seq *stmts)
1805
1791
This way line number for SETUP_ANNOTATIONS will always
1806
1792
coincide with the line number of first "real" statement in module.
1807
1793
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 )) {
1810
1795
st = (stmt_ty )asdl_seq_GET (stmts , 0 );
1811
- c -> u -> u_lineno = st -> lineno ;
1796
+ SET_LOC ( c , st ) ;
1812
1797
}
1813
1798
/* Every annotated class and module should have __annotations__. */
1814
1799
if (find_ann (stmts )) {
@@ -3043,9 +3028,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
3043
3028
s -> v .Try .handlers , i );
3044
3029
if (!handler -> v .ExceptHandler .type && i < n - 1 )
3045
3030
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 );
3049
3032
except = compiler_new_block (c );
3050
3033
if (except == NULL )
3051
3034
return 0 ;
@@ -3345,9 +3328,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
3345
3328
Py_ssize_t i , n ;
3346
3329
3347
3330
/* 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 );
3351
3332
3352
3333
switch (s -> kind ) {
3353
3334
case FunctionDef_kind :
@@ -5095,24 +5076,11 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
5095
5076
static int
5096
5077
compiler_visit_expr (struct compiler * c , expr_ty e )
5097
5078
{
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
- */
5101
5079
int old_lineno = c -> u -> u_lineno ;
5102
5080
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 );
5110
5082
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 ;
5116
5084
c -> u -> u_col_offset = old_col_offset ;
5117
5085
return res ;
5118
5086
}
@@ -5590,14 +5558,14 @@ assemble_lnotab(struct assembler *a, struct instr *i)
5590
5558
Py_ssize_t len ;
5591
5559
unsigned char * lnotab ;
5592
5560
5593
- d_bytecode = (a -> a_offset - a -> a_lineno_off ) * sizeof (_Py_CODEUNIT );
5594
5561
d_lineno = i -> i_lineno - a -> a_lineno ;
5562
+ if (d_lineno == 0 ) {
5563
+ return 1 ;
5564
+ }
5595
5565
5566
+ d_bytecode = (a -> a_offset - a -> a_lineno_off ) * sizeof (_Py_CODEUNIT );
5596
5567
assert (d_bytecode >= 0 );
5597
5568
5598
- if (d_bytecode == 0 && d_lineno == 0 )
5599
- return 1 ;
5600
-
5601
5569
if (d_bytecode > 255 ) {
5602
5570
int j , nbytes , ncodes = d_bytecode / 255 ;
5603
5571
nbytes = a -> a_lnotab_off + 2 * ncodes ;
0 commit comments