Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c37ea9

Browse files
authored
[3.11] gh-101400: Fix incorrect lineno in exception message on contin… (gh-101447)
1 parent 43af2db commit 0c37ea9

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,12 +3259,20 @@ static int
32593259
compiler_break(struct compiler *c)
32603260
{
32613261
struct fblockinfo *loop = NULL;
3262+
int u_lineno = c->u->u_lineno;
3263+
int u_col_offset = c->u->u_col_offset;
3264+
int u_end_lineno = c->u->u_end_lineno;
3265+
int u_end_col_offset = c->u->u_end_col_offset;
32623266
/* Emit instruction with line number */
32633267
ADDOP(c, NOP);
32643268
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
32653269
return 0;
32663270
}
32673271
if (loop == NULL) {
3272+
c->u->u_lineno = u_lineno;
3273+
c->u->u_col_offset = u_col_offset;
3274+
c->u->u_end_lineno = u_end_lineno;
3275+
c->u->u_end_col_offset = u_end_col_offset;
32683276
return compiler_error(c, "'break' outside loop");
32693277
}
32703278
if (!compiler_unwind_fblock(c, loop, 0)) {
@@ -3278,12 +3286,20 @@ static int
32783286
compiler_continue(struct compiler *c)
32793287
{
32803288
struct fblockinfo *loop = NULL;
3289+
int u_lineno = c->u->u_lineno;
3290+
int u_col_offset = c->u->u_col_offset;
3291+
int u_end_lineno = c->u->u_end_lineno;
3292+
int u_end_col_offset = c->u->u_end_col_offset;
32813293
/* Emit instruction with line number */
32823294
ADDOP(c, NOP);
32833295
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
32843296
return 0;
32853297
}
32863298
if (loop == NULL) {
3299+
c->u->u_lineno = u_lineno;
3300+
c->u->u_col_offset = u_col_offset;
3301+
c->u->u_end_lineno = u_end_lineno;
3302+
c->u->u_end_col_offset = u_end_col_offset;
32873303
return compiler_error(c, "'continue' not properly in loop");
32883304
}
32893305
ADDOP_JUMP(c, JUMP, loop->fb_block);

0 commit comments

Comments
 (0)