diff Python/compile.c @ 45151:643c1397f954 legacy-trunk

#1750076: Debugger did not step on every iteration of a while statement. The mapping between bytecode offsets and source lines (lnotab) did not contain an entry for the beginning of the loop. Now it does, and the lnotab can be a bit larger: in particular, several statements on the same line generate several entries. However, this does not bother the settrace function, which will trigger only one 'line' event. The lnotab seems to be exactly the same as with python2.4.
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Mon, 04 Feb 2008 21:45:05 +0000
parents 3f16ac915214
children 3f8f86493daa
line wrap: on
line diff
--- a/Python/compile.c	Mon Feb 04 20:53:14 2008 +0000
+++ b/Python/compile.c	Mon Feb 04 21:45:05 2008 +0000
@@ -638,11 +638,16 @@
 	return b->b_iused++;
 }
 
-/* Set the i_lineno member of the instruction at offse off if the
-   line number for the current expression/statement (?) has not
+/* Set the i_lineno member of the instruction at offset off if the
+   line number for the current expression/statement has not
    already been set.  If it has been set, the call has no effect.
 
-   Every time a new node is b
+   The line number is reset in the following cases:
+   - when entering a new scope
+   - on each statement
+   - on each expression that start a new line
+   - before the "except" clause
+   - before the "for" and "while" expressions
 */
 
 static void
@@ -1611,9 +1616,8 @@
 	VISIT(c, expr, s->v.For.iter);
 	ADDOP(c, GET_ITER);
 	compiler_use_next_block(c, start);
-	/* XXX(nnorwitz): is there a better way to handle this?
-	   for loops are special, we want to be able to trace them
-	   each time around, so we need to set an extra line number. */
+	/* for expressions must be traced on each iteration,
+	   so we need to set an extra line number. */
 	c->u->u_lineno_set = false;
 	ADDOP_JREL(c, FOR_ITER, cleanup);
 	VISIT(c, expr, s->v.For.target);
@@ -1660,6 +1664,9 @@
 	if (!compiler_push_fblock(c, LOOP, loop))
 		return 0;
 	if (constant == -1) {
+		/* while expressions must be traced on each iteration,
+		   so we need to set an extra line number. */
+		c->u->u_lineno_set = false;
 		VISIT(c, expr, s->v.While.test);
 		ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
 		ADDOP(c, POP_TOP);
@@ -1840,8 +1847,8 @@
 						s->v.TryExcept.handlers, i);
 		if (!handler->type && i < n-1)
 		    return compiler_error(c, "default 'except:' must be last");
-	c->u->u_lineno_set = false;
-	c->u->u_lineno = handler->lineno;
+		c->u->u_lineno_set = false;
+		c->u->u_lineno = handler->lineno;
 		except = compiler_new_block(c);
 		if (except == NULL)
 			return 0;
@@ -3553,12 +3560,6 @@
 	assert(d_bytecode >= 0);
 	assert(d_lineno >= 0);
 
-	/* XXX(nnorwitz): is there a better way to handle this?
-	   for loops are special, we want to be able to trace them
-	   each time around, so we need to set an extra line number. */
-	if (d_lineno == 0 && i->i_opcode != FOR_ITER)
-		return 1;
-
 	if (d_bytecode > 255) {
 		int j, nbytes, ncodes = d_bytecode / 255;
 		nbytes = a->a_lnotab_off + 2 * ncodes;