Skip to content

Commit 4e1f5bf

Browse files
committed
[3.11] gh-101400: Fix incorrect lineno in exception message on continue/break which are not in a loop (GH-101413).
(cherry picked from commit e867c1b) Co-authored-by: Dong-hee Na <donghee.na@python.org>
1 parent faf8068 commit 4e1f5bf

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

Lib/test/test_syntax.py

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

1910-
def test_break_outside_loop(self):
1911-
self._check_error("break", "outside loop")
1912-
19131910
def test_yield_outside_function(self):
19141911
self._check_error("if 0: yield", "outside function")
19151912
self._check_error("if 0: yield\nelse: x=1", "outside function")
@@ -1938,20 +1935,27 @@ def test_return_outside_function(self):
19381935
"outside function")
19391936

19401937
def test_break_outside_loop(self):
1941-
self._check_error("if 0: break", "outside loop")
1942-
self._check_error("if 0: break\nelse: x=1", "outside loop")
1943-
self._check_error("if 1: pass\nelse: break", "outside loop")
1944-
self._check_error("class C:\n if 0: break", "outside loop")
1938+
msg = "outside loop"
1939+
self._check_error("break", msg, lineno=1)
1940+
self._check_error("if 0: break", msg, lineno=1)
1941+
self._check_error("if 0: break\nelse: x=1", msg, lineno=1)
1942+
self._check_error("if 1: pass\nelse: break", msg, lineno=2)
1943+
self._check_error("class C:\n if 0: break", msg, lineno=2)
19451944
self._check_error("class C:\n if 1: pass\n else: break",
1946-
"outside loop")
1945+
msg, lineno=3)
1946+
self._check_error("with object() as obj:\n break",
1947+
msg, lineno=2)
19471948

19481949
def test_continue_outside_loop(self):
1949-
self._check_error("if 0: continue", "not properly in loop")
1950-
self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1951-
self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1952-
self._check_error("class C:\n if 0: continue", "not properly in loop")
1950+
msg = "not properly in loop"
1951+
self._check_error("if 0: continue", msg, lineno=1)
1952+
self._check_error("if 0: continue\nelse: x=1", msg, lineno=1)
1953+
self._check_error("if 1: pass\nelse: continue", msg, lineno=2)
1954+
self._check_error("class C:\n if 0: continue", msg, lineno=2)
19531955
self._check_error("class C:\n if 1: pass\n else: continue",
1954-
"not properly in loop")
1956+
msg, lineno=3)
1957+
self._check_error("with object() as obj:\n continue",
1958+
msg, lineno=2)
19551959

19561960
def test_unexpected_indent(self):
19571961
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,12 +3259,14 @@ static int
32593259
compiler_break(struct compiler *c)
32603260
{
32613261
struct fblockinfo *loop = NULL;
3262+
int origin_loc = c->u->u_lineno;
32623263
/* Emit instruction with line number */
32633264
ADDOP(c, NOP);
32643265
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
32653266
return 0;
32663267
}
32673268
if (loop == NULL) {
3269+
c->u->u_lineno = origin_loc;
32683270
return compiler_error(c, "'break' outside loop");
32693271
}
32703272
if (!compiler_unwind_fblock(c, loop, 0)) {
@@ -3278,12 +3280,14 @@ static int
32783280
compiler_continue(struct compiler *c)
32793281
{
32803282
struct fblockinfo *loop = NULL;
3283+
int origin_loc = c->u->u_lineno;
32813284
/* Emit instruction with line number */
32823285
ADDOP(c, NOP);
32833286
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
32843287
return 0;
32853288
}
32863289
if (loop == NULL) {
3290+
c->u->u_lineno = origin_loc;
32873291
return compiler_error(c, "'continue' not properly in loop");
32883292
}
32893293
ADDOP_JUMP(c, JUMP, loop->fb_block);

0 commit comments

Comments
 (0)