summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-05-13 13:11:41 (GMT)
committerGitHub <noreply@github.com>2021-05-13 13:11:41 (GMT)
commit0acdf255a51b836c0b44f3676797620322974af3 (patch)
treead0927988cd589b10696a0133815da196c5f45a9 /Python/compile.c
parent2d972b8e7cb5347ddf83dfcee461f550b59f0736 (diff)
downloadcpython-0acdf255a51b836c0b44f3676797620322974af3.zip
cpython-0acdf255a51b836c0b44f3676797620322974af3.tar.gz
cpython-0acdf255a51b836c0b44f3676797620322974af3.tar.bz2
[3.10] bpo-43933: Force RETURN_VALUE bytecodes to have line numbers (GH-26061)
* Guarantee that line number is set for returns.
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/Python/compile.c b/Python/compile.c
index bb88c06..3c69ce2 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -6960,6 +6960,34 @@ insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
return 0;
}
+/* Make sure that all returns have a line number, even if early passes
+ * have failed to propagate a correct line number.
+ * The resulting line number may not be correct according to PEP 626,
+ * but should be "good enough", and no worse than in older versions. */
+static void
+guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
+ int lineno = firstlineno;
+ assert(lineno > 0);
+ for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
+ if (b->b_iused == 0) {
+ continue;
+ }
+ struct instr *last = &b->b_instr[b->b_iused-1];
+ if (last->i_lineno < 0) {
+ if (last->i_opcode == RETURN_VALUE)
+ {
+ for (int i = 0; i < b->b_iused; i++) {
+ assert(b->b_instr[i].i_lineno < 0);
+ b->b_instr[i].i_lineno = lineno;
+ }
+ }
+ }
+ else {
+ lineno = last->i_lineno;
+ }
+ }
+}
+
static PyCodeObject *
assemble(struct compiler *c, int addNone)
{
@@ -7022,6 +7050,7 @@ assemble(struct compiler *c, int addNone)
if (optimize_cfg(c, &a, consts)) {
goto error;
}
+ guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(&a, c);