Skip to content

Negative line number in stacktrace exception #101400

Closed

Description

@Kartatz

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

Activity

  1. 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[/+] on Jan 29, 2023
  2. corona10 commented on Jan 29, 2023

    @corona10
    Member

    @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

  3. corona10 commented on Jan 29, 2023

    @corona10
    Member

    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;
  4. iritkatriel commented on Jan 29, 2023

    @iritkatriel
    Member

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

  5. corona10 commented on Jan 29, 2023

    @corona10
    Member

    I will submit the patch ;)

  6. added 2 commits that reference this issue on Jan 30, 2023
  7. added a commit that references this issue on Jan 30, 2023
  8. added 5 commits that reference this issue on Jan 31, 2023
  9. corona10 commented on Jan 31, 2023

    @corona10
    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 :)

  10. added a commit that references this issue on Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions