Skip to content
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

Closed
Kartatz opened this issue Jan 29, 2023 · 5 comments
Closed

Negative line number in stacktrace exception #101400

Kartatz opened this issue Jan 29, 2023 · 5 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@Kartatz
Copy link

Kartatz commented Jan 29, 2023

The following code snippet generates a stacktrace exception pointing to a invalid line number (-1):

with object() as obj:
	break

Example:

$ 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. I could not reproduce this on Python versions older than <3.10.

Linked PRs

@Kartatz Kartatz added the type-bug An unexpected behavior, bug, or error label Jan 29, 2023
@Kartatz Kartatz changed the title The following code snippet generates a stacktrace exception pointing to a invalid line number (-1): 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. Negative line number in stacktrace exception Jan 29, 2023
@corona10
Copy link
Member

corona10 commented Jan 29, 2023

@iritkatriel

The bug is triggered from this line:

*ploc = NO_LOCATION;

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

@corona10
Copy link
Member

corona10 commented Jan 29, 2023

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;

@iritkatriel
Copy link
Member

@corona10 your fix looks right. Do you want to make a PR? We would need a couple of tests for this.

@corona10
Copy link
Member

I will submit the patch ;)

corona10 added a commit to corona10/cpython that referenced this issue Jan 30, 2023
corona10 added a commit to corona10/cpython that referenced this issue Jan 30, 2023
iritkatriel pushed a commit that referenced this issue Jan 30, 2023
corona10 added a commit to corona10/cpython that referenced this issue Jan 31, 2023
…continue/break which are not in a loop (pythonGH-101413).

(cherry picked from commit e867c1b)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
corona10 added a commit to corona10/cpython that referenced this issue Jan 31, 2023
…continue/break which are not in a loop (pythonGH-101413).

(cherry picked from commit e867c1b)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
corona10 added a commit to corona10/cpython that referenced this issue Jan 31, 2023
…continue/break which are not in a loop (pythonGH-101413).

(cherry picked from commit e867c1b)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
@corona10
Copy link
Member

Now we can close the issue. Please let me know if we have to reopen the issue.

Thanks for the report @Kartatz
And thanks for the kind reviews!! @iritkatriel :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants