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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,9 +1907,6 @@ def error2():
"""
self._check_error(source, "parameter and nonlocal", lineno=3)

def test_break_outside_loop(self):
self._check_error("break", "outside loop")

def test_yield_outside_function(self):
self._check_error("if 0: yield", "outside function")
self._check_error("if 0: yield\nelse: x=1", "outside function")
Expand Down Expand Up @@ -1938,20 +1935,27 @@ def test_return_outside_function(self):
"outside function")

def test_break_outside_loop(self):
self._check_error("if 0: break", "outside loop")
self._check_error("if 0: break\nelse: x=1", "outside loop")
self._check_error("if 1: pass\nelse: break", "outside loop")
self._check_error("class C:\n if 0: break", "outside loop")
msg = "outside loop"
self._check_error("break", msg, lineno=1)
self._check_error("if 0: break", msg, lineno=1)
self._check_error("if 0: break\nelse: x=1", msg, lineno=1)
self._check_error("if 1: pass\nelse: break", msg, lineno=2)
self._check_error("class C:\n if 0: break", msg, lineno=2)
self._check_error("class C:\n if 1: pass\n else: break",
"outside loop")
msg, lineno=3)
self._check_error("with object() as obj:\n break",
msg, lineno=2)

def test_continue_outside_loop(self):
self._check_error("if 0: continue", "not properly in loop")
self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
self._check_error("if 1: pass\nelse: continue", "not properly in loop")
self._check_error("class C:\n if 0: continue", "not properly in loop")
msg = "not properly in loop"
self._check_error("if 0: continue", msg, lineno=1)
self._check_error("if 0: continue\nelse: x=1", msg, lineno=1)
self._check_error("if 1: pass\nelse: continue", msg, lineno=2)
self._check_error("class C:\n if 0: continue", msg, lineno=2)
self._check_error("class C:\n if 1: pass\n else: continue",
"not properly in loop")
msg, lineno=3)
self._check_error("with object() as obj:\n continue",
msg, lineno=2)

def test_unexpected_indent(self):
self._check_error("foo()\n bar()\n", "unexpected indent",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix wrong lineno in exception message on :keyword:`continue` or
:keyword:`break` which are not in a loop. Patch by Dong-hee Na.
12 changes: 12 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,12 @@ compiler_next_instr(basicblock *b)
(c)->u->u_end_lineno = -1; \
(c)->u->u_end_col_offset = -1;

#define COPY_COMPILER_UNIT_LOC(old, new) \
(new)->u_lineno = (old)->u_lineno; \
(new)->u_col_offset = (old)->u_col_offset; \
(new)->u_end_lineno = (old)->u_end_lineno; \
(new)->u_end_col_offset = (old)->u_end_col_offset;

#define COPY_INSTR_LOC(old, new) \
(new).i_lineno = (old).i_lineno; \
(new).i_col_offset = (old).i_col_offset; \
Expand Down Expand Up @@ -3259,12 +3265,15 @@ static int
compiler_break(struct compiler *c)
{
struct fblockinfo *loop = NULL;
struct compiler_unit origin_loc;
COPY_COMPILER_UNIT_LOC(c->u, &origin_loc);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
/* Emit instruction with line number */
ADDOP(c, NOP);
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
return 0;
}
if (loop == NULL) {
COPY_COMPILER_UNIT_LOC(&origin_loc, c->u);
return compiler_error(c, "'break' outside loop");
}
if (!compiler_unwind_fblock(c, loop, 0)) {
Expand All @@ -3278,12 +3287,15 @@ static int
compiler_continue(struct compiler *c)
{
struct fblockinfo *loop = NULL;
struct compiler_unit origin_loc;
COPY_COMPILER_UNIT_LOC(c->u, &origin_loc);
/* Emit instruction with line number */
ADDOP(c, NOP);
if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
return 0;
}
if (loop == NULL) {
COPY_COMPILER_UNIT_LOC(&origin_loc, c->u);
return compiler_error(c, "'continue' not properly in loop");
}
ADDOP_JUMP(c, JUMP, loop->fb_block);
Expand Down