-
-
Notifications
You must be signed in to change notification settings - Fork 31k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Negative line number in stacktrace exception #101400
Comments
python
with object() as obj:
break
Example:
bash
$ echo 'with object() as obj:\n\tbreak' > main.py
$ python main.py
File "/home/kartz/main.py", line -1
SyntaxError: 'break' outside loop
This is reproducible with Python 3.10, 3.11 and latest [master](https://github.com/python/cpython/commit/666c0840dcac9941fa41ec619fef8d45cd849a0b). I could not reproduce this on Python versions older than <3.10.
The bug is triggered from this line: Line 2069 in 666c084
with object() as obj:
continue
# File "/Users/user/oss/cpython/gh-101400.py", line -1
# SyntaxError: 'continue' not properly in loop Same issue with continue |
This kind of fix will solve the issue, but I am not sure this is the right approach. diff --git a/Python/compile.c b/Python/compile.c
index c31f08c0a1..18cf1382f5 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3248,9 +3248,10 @@ compiler_break(struct compiler *c, location loc)
struct fblockinfo *loop = NULL;
+ location origin_loc = loc;
/* Emit instruction with line number */
ADDOP(c, loc, NOP);
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
if (loop == NULL) {
- return compiler_error(c, loc, "'break' outside loop");
+ return compiler_error(c, origin_loc, "'break' outside loop");
}
RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
@@ -3261,11 +3262,12 @@ static int
compiler_continue(struct compiler *c, location loc)
{
struct fblockinfo *loop = NULL;
+ location origin_loc = loc;
/* Emit instruction with line number */
ADDOP(c, loc, NOP);
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
if (loop == NULL) {
- return compiler_error(c, loc, "'continue' not properly in loop");
+ return compiler_error(c, origin_loc, "'continue' not properly in loop");
}
ADDOP_JUMP(c, loc, JUMP, loop->fb_block);
return SUCCESS; |
@corona10 your fix looks right. Do you want to make a PR? We would need a couple of tests for this. |
I will submit the patch ;) |
…k which are not in a loop (#101413)
…continue/break which are not in a loop (pythonGH-101413). (cherry picked from commit e867c1b) Co-authored-by: Dong-hee Na <[email protected]>
…continue/break which are not in a loop (pythonGH-101413). (cherry picked from commit e867c1b) Co-authored-by: Dong-hee Na <[email protected]>
…continue/break which are not in a loop (pythonGH-101413). (cherry picked from commit e867c1b) Co-authored-by: Dong-hee Na <[email protected]>
Now we can close the issue. Please let me know if we have to reopen the issue. Thanks for the report @Kartatz |
…e/break which are not in a loop (python#101413)
The following code snippet generates a stacktrace exception pointing to a invalid line number (-1):
Example:
This is reproducible with Python 3.10, 3.11 and latest master. I could not reproduce this on Python versions older than <3.10.
Linked PRs
The text was updated successfully, but these errors were encountered: