Skip to content

Commit e867c1b

Browse files
authored
gh-101400: Fix incorrect lineno in exception message on continue/break which are not in a loop (#101413)
1 parent 28db978 commit e867c1b

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

Lib/test/test_syntax.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,9 +1957,6 @@ def error2():
19571957
"""
19581958
self._check_error(source, "parameter and nonlocal", lineno=3)
19591959

1960-
def test_break_outside_loop(self):
1961-
self._check_error("break", "outside loop")
1962-
19631960
def test_yield_outside_function(self):
19641961
self._check_error("if 0: yield", "outside function")
19651962
self._check_error("if 0: yield\nelse: x=1", "outside function")
@@ -1988,20 +1985,27 @@ def test_return_outside_function(self):
19881985
"outside function")
19891986

19901987
def test_break_outside_loop(self):
1991-
self._check_error("if 0: break", "outside loop")
1992-
self._check_error("if 0: break\nelse: x=1", "outside loop")
1993-
self._check_error("if 1: pass\nelse: break", "outside loop")
1994-
self._check_error("class C:\n if 0: break", "outside loop")
1988+
msg = "outside loop"
1989+
self._check_error("break", msg, lineno=1)
1990+
self._check_error("if 0: break", msg, lineno=1)
1991+
self._check_error("if 0: break\nelse: x=1", msg, lineno=1)
1992+
self._check_error("if 1: pass\nelse: break", msg, lineno=2)
1993+
self._check_error("class C:\n if 0: break", msg, lineno=2)
19951994
self._check_error("class C:\n if 1: pass\n else: break",
1996-
"outside loop")
1995+
msg, lineno=3)
1996+
self._check_error("with object() as obj:\n break",
1997+
msg, lineno=2)
19971998

19981999
def test_continue_outside_loop(self):
1999-
self._check_error("if 0: continue", "not properly in loop")
2000-
self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
2001-
self._check_error("if 1: pass\nelse: continue", "not properly in loop")
2002-
self._check_error("class C:\n if 0: continue", "not properly in loop")
2000+
msg = "not properly in loop"
2001+
self._check_error("if 0: continue", msg, lineno=1)
2002+
self._check_error("if 0: continue\nelse: x=1", msg, lineno=1)
2003+
self._check_error("if 1: pass\nelse: continue", msg, lineno=2)
2004+
self._check_error("class C:\n if 0: continue", msg, lineno=2)
20032005
self._check_error("class C:\n if 1: pass\n else: continue",
2004-
"not properly in loop")
2006+
msg, lineno=3)
2007+
self._check_error("with object() as obj:\n continue",
2008+
msg, lineno=2)
20052009

20062010
def test_unexpected_indent(self):
20072011
self._check_error("foo()\n bar()\n", "unexpected indent",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix wrong lineno in exception message on :keyword:`continue` or
2+
:keyword:`break` which are not in a loop. Patch by Dong-hee Na.

Python/compile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,11 +3246,12 @@ static int
32463246
compiler_break(struct compiler *c, location loc)
32473247
{
32483248
struct fblockinfo *loop = NULL;
3249+
location origin_loc = loc;
32493250
/* Emit instruction with line number */
32503251
ADDOP(c, loc, NOP);
32513252
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
32523253
if (loop == NULL) {
3253-
return compiler_error(c, loc, "'break' outside loop");
3254+
return compiler_error(c, origin_loc, "'break' outside loop");
32543255
}
32553256
RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
32563257
ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
@@ -3261,11 +3262,12 @@ static int
32613262
compiler_continue(struct compiler *c, location loc)
32623263
{
32633264
struct fblockinfo *loop = NULL;
3265+
location origin_loc = loc;
32643266
/* Emit instruction with line number */
32653267
ADDOP(c, loc, NOP);
32663268
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
32673269
if (loop == NULL) {
3268-
return compiler_error(c, loc, "'continue' not properly in loop");
3270+
return compiler_error(c, origin_loc, "'continue' not properly in loop");
32693271
}
32703272
ADDOP_JUMP(c, loc, JUMP, loop->fb_block);
32713273
return SUCCESS;

0 commit comments

Comments
 (0)