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

Skip to content

Commit 2aa4010

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 <[email protected]>
1 parent faf8068 commit 2aa4010

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,13 @@ compiler_next_instr(basicblock *b)
925925
(c)->u->u_end_lineno = -1; \
926926
(c)->u->u_end_col_offset = -1;
927927

928+
#define COPY_COMPILER_UNIT_LOC(old, new) \
929+
(new)->u_lineno = (old)->u_lineno; \
930+
(new)->u_col_offset = (old)->u_col_offset; \
931+
(new)->u_end_lineno = (old)->u_end_lineno; \
932+
(new)->u_end_col_offset = (old)->u_end_col_offset; \
933+
934+
928935
#define COPY_INSTR_LOC(old, new) \
929936
(new).i_lineno = (old).i_lineno; \
930937
(new).i_col_offset = (old).i_col_offset; \
@@ -3259,12 +3266,15 @@ static int
32593266
compiler_break(struct compiler *c)
32603267
{
32613268
struct fblockinfo *loop = NULL;
3269+
struct compiler_unit origin_loc;
3270+
COPY_COMPILER_UNIT_LOC(c->u, &origin_loc);
32623271
/* Emit instruction with line number */
32633272
ADDOP(c, NOP);
32643273
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
32653274
return 0;
32663275
}
32673276
if (loop == NULL) {
3277+
COPY_COMPILER_UNIT_LOC(&origin_loc, c->u);
32683278
return compiler_error(c, "'break' outside loop");
32693279
}
32703280
if (!compiler_unwind_fblock(c, loop, 0)) {
@@ -3278,12 +3288,15 @@ static int
32783288
compiler_continue(struct compiler *c)
32793289
{
32803290
struct fblockinfo *loop = NULL;
3291+
struct compiler_unit origin_loc;
3292+
COPY_COMPILER_UNIT_LOC(c->u, &origin_loc);
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+
COPY_COMPILER_UNIT_LOC(&origin_loc, c->u);
32873300
return compiler_error(c, "'continue' not properly in loop");
32883301
}
32893302
ADDOP_JUMP(c, JUMP, loop->fb_block);

0 commit comments

Comments
 (0)