From 0c37ea9abad2eae146ce117eca0503aaedc96c0f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 31 Jan 2023 23:53:14 +0900 Subject: =?UTF-8?q?[3.11]=20gh-101400:=20Fix=20incorrect=20lineno=20in=20e?= =?UTF-8?q?xception=20message=20on=20contin=E2=80=A6=20(gh-101447)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/test/test_syntax.py | 30 ++++++++++++---------- .../2023-01-30-08-59-47.gh-issue-101400.Di_ZFm.rst | 2 ++ Python/compile.c | 16 ++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-01-30-08-59-47.gh-issue-101400.Di_ZFm.rst diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 42d36e0..50168d9 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -1907,9 +1907,6 @@ class SyntaxTestCase(unittest.TestCase): """ 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") @@ -1938,20 +1935,27 @@ class SyntaxTestCase(unittest.TestCase): "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", diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-30-08-59-47.gh-issue-101400.Di_ZFm.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-30-08-59-47.gh-issue-101400.Di_ZFm.rst new file mode 100644 index 0000000..f3dd783 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-30-08-59-47.gh-issue-101400.Di_ZFm.rst @@ -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. diff --git a/Python/compile.c b/Python/compile.c index f4555b3..17d1df2 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3259,12 +3259,20 @@ static int compiler_break(struct compiler *c) { struct fblockinfo *loop = NULL; + int u_lineno = c->u->u_lineno; + int u_col_offset = c->u->u_col_offset; + int u_end_lineno = c->u->u_end_lineno; + int u_end_col_offset = c->u->u_end_col_offset; /* Emit instruction with line number */ ADDOP(c, NOP); if (!compiler_unwind_fblock_stack(c, 0, &loop)) { return 0; } if (loop == NULL) { + c->u->u_lineno = u_lineno; + c->u->u_col_offset = u_col_offset; + c->u->u_end_lineno = u_end_lineno; + c->u->u_end_col_offset = u_end_col_offset; return compiler_error(c, "'break' outside loop"); } if (!compiler_unwind_fblock(c, loop, 0)) { @@ -3278,12 +3286,20 @@ static int compiler_continue(struct compiler *c) { struct fblockinfo *loop = NULL; + int u_lineno = c->u->u_lineno; + int u_col_offset = c->u->u_col_offset; + int u_end_lineno = c->u->u_end_lineno; + int u_end_col_offset = c->u->u_end_col_offset; /* Emit instruction with line number */ ADDOP(c, NOP); if (!compiler_unwind_fblock_stack(c, 0, &loop)) { return 0; } if (loop == NULL) { + c->u->u_lineno = u_lineno; + c->u->u_col_offset = u_col_offset; + c->u->u_end_lineno = u_end_lineno; + c->u->u_end_col_offset = u_end_col_offset; return compiler_error(c, "'continue' not properly in loop"); } ADDOP_JUMP(c, JUMP, loop->fb_block); -- cgit v0.12