diff Python/compile.c @ 40044:9e0431fd674a 2.5

Fix #1569998: no break inside try statement (outside loop) allowed. (backport from rev. 52129)
author Georg Brandl <georg@python.org>
date Sun, 08 Oct 2006 07:12:23 +0000
parents ed88d6b7c1f9
children 9acc1775d571
line wrap: on
line diff
--- a/Python/compile.c	Sun Oct 08 07:06:29 2006 +0000
+++ b/Python/compile.c	Sun Oct 08 07:12:23 2006 +0000
@@ -187,6 +187,8 @@
 				basicblock *);
 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
 				basicblock *);
+/* Returns true if there is a loop on the fblock stack. */
+static int compiler_in_loop(struct compiler *);
 
 static int inplace_binop(struct compiler *, operator_ty);
 static int expr_constant(expr_ty e);
@@ -2764,7 +2766,7 @@
 	case Pass_kind:
 		break;
 	case Break_kind:
-		if (!c->u->u_nfblocks)
+                if (!compiler_in_loop(c))
 			return compiler_error(c, "'break' outside loop");
 		ADDOP(c, BREAK_LOOP);
 		break;
@@ -3754,6 +3756,16 @@
 	assert(u->u_fblock[u->u_nfblocks].fb_block == b);
 }
 
+static int
+compiler_in_loop(struct compiler *c) {
+        int i;
+        struct compiler_unit *u = c->u;
+        for (i = 0; i < u->u_nfblocks; ++i) {
+                if (u->u_fblock[i].fb_type == LOOP)
+                        return 1;
+        }
+        return 0;
+}
 /* Raises a SyntaxError and returns 0.
    If something goes wrong, a different exception may be raised.
 */