Skip to content

Commit 740050a

Browse files
authored
[3.10] gh-101400: Fix incorrect lineno in exception message on contin… (gh-101448)
1 parent 71db9c9 commit 740050a

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

Lib/test/test_syntax.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -1410,9 +1410,6 @@ def error2():
14101410
"""
14111411
self._check_error(source, "parameter and nonlocal", lineno=3)
14121412

1413-
def test_break_outside_loop(self):
1414-
self._check_error("break", "outside loop")
1415-
14161413
def test_yield_outside_function(self):
14171414
self._check_error("if 0: yield", "outside function")
14181415
self._check_error("if 0: yield\nelse: x=1", "outside function")
@@ -1441,20 +1438,27 @@ def test_return_outside_function(self):
14411438
"outside function")
14421439

14431440
def test_break_outside_loop(self):
1444-
self._check_error("if 0: break", "outside loop")
1445-
self._check_error("if 0: break\nelse: x=1", "outside loop")
1446-
self._check_error("if 1: pass\nelse: break", "outside loop")
1447-
self._check_error("class C:\n if 0: break", "outside loop")
1441+
msg = "outside loop"
1442+
self._check_error("break", msg, lineno=1)
1443+
self._check_error("if 0: break", msg, lineno=1)
1444+
self._check_error("if 0: break\nelse: x=1", msg, lineno=1)
1445+
self._check_error("if 1: pass\nelse: break", msg, lineno=2)
1446+
self._check_error("class C:\n if 0: break", msg, lineno=2)
14481447
self._check_error("class C:\n if 1: pass\n else: break",
1449-
"outside loop")
1448+
msg, lineno=3)
1449+
self._check_error("with object() as obj:\n break",
1450+
msg, lineno=2)
14501451

14511452
def test_continue_outside_loop(self):
1452-
self._check_error("if 0: continue", "not properly in loop")
1453-
self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1454-
self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1455-
self._check_error("class C:\n if 0: continue", "not properly in loop")
1453+
msg = "not properly in loop"
1454+
self._check_error("if 0: continue", msg, lineno=1)
1455+
self._check_error("if 0: continue\nelse: x=1", msg, lineno=1)
1456+
self._check_error("if 1: pass\nelse: continue", msg, lineno=2)
1457+
self._check_error("class C:\n if 0: continue", msg, lineno=2)
14561458
self._check_error("class C:\n if 1: pass\n else: continue",
1457-
"not properly in loop")
1459+
msg, lineno=3)
1460+
self._check_error("with object() as obj:\n continue",
1461+
msg, lineno=2)
14581462

14591463
def test_unexpected_indent(self):
14601464
self._check_error("foo()\n bar()\n", "unexpected indent",
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

+4
Original file line numberDiff line numberDiff line change
@@ -3030,12 +3030,14 @@ static int
30303030
compiler_break(struct compiler *c)
30313031
{
30323032
struct fblockinfo *loop = NULL;
3033+
int origin_loc = c->u->u_lineno;
30333034
/* Emit instruction with line number */
30343035
ADDOP(c, NOP);
30353036
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
30363037
return 0;
30373038
}
30383039
if (loop == NULL) {
3040+
c->u->u_lineno = origin_loc;
30393041
return compiler_error(c, "'break' outside loop");
30403042
}
30413043
if (!compiler_unwind_fblock(c, loop, 0)) {
@@ -3050,12 +3052,14 @@ static int
30503052
compiler_continue(struct compiler *c)
30513053
{
30523054
struct fblockinfo *loop = NULL;
3055+
int origin_loc = c->u->u_lineno;
30533056
/* Emit instruction with line number */
30543057
ADDOP(c, NOP);
30553058
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
30563059
return 0;
30573060
}
30583061
if (loop == NULL) {
3062+
c->u->u_lineno = origin_loc;
30593063
return compiler_error(c, "'continue' not properly in loop");
30603064
}
30613065
ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);

0 commit comments

Comments
 (0)