Skip to content

Wrong error location in traceback inside an assert #116034

Closed

Description

@solanav

Bug report

Bug description:

The location in the traceback for this failed assertion:

test = 3
assert test == 1 and test == 2, "Bug found?"

Seems to be wrong. It should show test == 1 as the source of the error:

Traceback (most recent call last):
  File "/home/.../test.py", line 3, in <module>
    assert test == 1 and test == 2, "Bug found?"
                         ^^^^^^^^^
AssertionError: Bug found?

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Activity

  1. Eclips4 commented on Feb 28, 2024

    @Eclips4
    Member

    Hello!
    Thanks for the report!
    Traceback for current 3.11 branch:

    Traceback (most recent call last):
      File "/Users/admin/Projects/cpython/example.py", line 2, in <module>
        assert test == 1 and test == 2, "Bug found?"
                             ^^^^^^^^^
    AssertionError: Bug found?

    Traceback for current 3.12 branch:

    Traceback (most recent call last):
      File "/Users/admin/Projects/cpython/example.py", line 2, in <module>
        assert test == 1 and test == 2, "Bug found?"
    AssertionError: Bug found?

    Traceback for current main branch:

    Traceback (most recent call last):
      File "/Users/admin/Projects/cpython/example.py", line 2, in <module>
        assert test == 1 and test == 2, "Bug found?"
               ^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: Bug found?

    cc @pablogsal

  2. pablogsal commented on Feb 28, 2024

    @pablogsal
    Member

    Error locations are set by the compiler so this is likely a compiler bug. CC @iritkatriel

  3. iritkatriel commented on Feb 28, 2024

    @iritkatriel
    Member

    in 3.13 it does this:

    >>> assert test == 1 and test == 2, "Bug found?"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        assert test == 1 and test == 2, "Bug found?"
               ^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: Bug found?
  4. pablogsal commented on Feb 28, 2024

    @pablogsal
    Member

    Shouldn't this be just one or the other?

  5. iritkatriel commented on Feb 28, 2024

    @iritkatriel
    Member

    Shouldn't this be just one or the other?

    Since the line numbers are calculated statically, that would require emitting a different RAISE instruction for each clause. Currently we don't.

  6. self-assigned this
    on Feb 28, 2024
  7. pablogsal commented on Feb 28, 2024

    @pablogsal
    Member

    Then it seems there isn't much we can do for 3.13, no? For 3.12 it seems that we don't emit line info

  8. iritkatriel commented on Feb 28, 2024

    @iritkatriel
    Member

    For 3.13 we could emit more instructions per assert, if we decide the tradeoff is worth it.

    I'm looking at 3.11 / 3.12 and will see if there is an easy fix.

  9. added 5 commits that reference this issue on Feb 28, 2024
  10. iritkatriel commented on Feb 29, 2024

    @iritkatriel
    Member

    I fixed 3.11 and 3.12 to behave like 3.13. I will close this issue as the problem it reports it resolved.

    I'm not sure narrowing the ranges is worth more code. If it ever comes up as an actual problem, we can revisit.

    Thank you @solanav .

  11. solanav commented on Feb 29, 2024

    @solanav
    Author

    Thank you all so much!

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