@@ -6960,6 +6960,36 @@ insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6960
6960
return 0 ;
6961
6961
}
6962
6962
6963
+ /* Make sure that all returns have a line number, even if early passes
6964
+ * have failed to propagate a correct line number.
6965
+ * The resulting line number may not be correct according to PEP 626,
6966
+ * but should be "good enough", and no worse than in older versions. */
6967
+ static void
6968
+ guarantee_lineno_for_exits (struct assembler * a , int firstlineno ) {
6969
+ int lineno = firstlineno ;
6970
+ assert (lineno > 0 );
6971
+ for (basicblock * b = a -> a_entry ; b != NULL ; b = b -> b_next ) {
6972
+ if (b -> b_iused == 0 ) {
6973
+ continue ;
6974
+ }
6975
+ struct instr * last = & b -> b_instr [b -> b_iused - 1 ];
6976
+ if (last -> i_lineno < 0 ) {
6977
+ /* A return in a block by itself can't have a linenumber, or
6978
+ * we can mess up frame.setlineno */
6979
+ if (last -> i_opcode == RETURN_VALUE )
6980
+ {
6981
+ for (int i = 0 ; i < b -> b_iused ; i ++ ) {
6982
+ assert (b -> b_instr [i ].i_lineno < 0 );
6983
+ b -> b_instr [i ].i_lineno = lineno ;
6984
+ }
6985
+ }
6986
+ }
6987
+ else {
6988
+ lineno = last -> i_lineno ;
6989
+ }
6990
+ }
6991
+ }
6992
+
6963
6993
static PyCodeObject *
6964
6994
assemble (struct compiler * c , int addNone )
6965
6995
{
@@ -7022,6 +7052,7 @@ assemble(struct compiler *c, int addNone)
7022
7052
if (optimize_cfg (c , & a , consts )) {
7023
7053
goto error ;
7024
7054
}
7055
+ guarantee_lineno_for_exits (& a , c -> u -> u_firstlineno );
7025
7056
7026
7057
/* Can't modify the bytecode after computing jump offsets. */
7027
7058
assemble_jump_offsets (& a , c );
0 commit comments