Sitelet https://github.com/python/cpython/issues/93662
Skip to content

Incorrect line number calculated in traceback #93662

Description

@pablogsal

Consider the following code:

def _run_with_file_output():
    example_report_generation_message= (
            """
            """
    ).strip()
    raise ValueError()

_run_with_file_output()

If you execute this with 3.11.0a7 you get (correct traceback):

Traceback (most recent call last):
  File "/home/pablogsal/github/python/3.11/lel.py", line 8, in <module>
    _run_with_file_output()
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pablogsal/github/python/3.11/lel.py", line 6, in _run_with_file_output
    raise ValueError()
    ^^^^^^^^^^^^^^^^^^
ValueError

but on 3.11.0b1 you get:

Traceback (most recent call last):
  File "/home/pablogsal/github/python/3.11/lel.py", line 8, in <module>
    _run_with_file_output()
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pablogsal/github/python/3.11/lel.py", line 255, in _run_with_file_output
ValueError

Notice that the last line number for _run_with_file_output is wrong (255 instead of 6).

Bisecting points at:

944fffee8916cb94321fa33cd3a43f4108717746 is the first bad commit
commit 944fffee8916cb94321fa33cd3a43f4108717746
Author: Mark Shannon <mark@hotpy.org>
Date:   Thu Apr 21 16:10:37 2022 +0100

    GH-88116: Use a compact format to represent end line and column offsets. (GH-91666)

    * Stores all location info in linetable to conform to PEP 626.

    * Remove column table from code objects.

    * Remove end-line table from code objects.

    * Document new location table format

Activity

  1. pablogsal commented on Jun 9, 2022

    @pablogsal
    MemberAuthor

    Originally noticed by @godlygeek

  2. pablogsal commented on Jun 9, 2022

    @pablogsal
    MemberAuthor

    Notice that if you change the input to be:

    def _run_with_file_output():
        _= (
                """
                """
        ).strip()
        raise ValueError()
    
    _run_with_file_output()
    

    Then the line numbers and the tracebacks are correct (likely due to the difference in column offset):

    Traceback (most recent call last):
      File "/home/pablogsal/github/python/3.11/lel.py", line 8, in <module>
        _run_with_file_output()
        ^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/pablogsal/github/python/3.11/lel.py", line 6, in _run_with_file_output
        raise ValueError()
        ^^^^^^^^^^^^^^^^^^
    ValueError
    
  3. pablogsal commented on Jun 9, 2022

    @pablogsal
    MemberAuthor

    May be related to #93249

  4. markshannon commented on Jun 10, 2022

    @markshannon
    Member

    It looks like the table is correct, but the parsing to get the line number is wrong.
    .__code__.co_positions() shows the correct positions, but .__code__.co_lines() shows the wrong line numbers.

  5. markshannon commented on Jun 10, 2022

    @markshannon
    Member

    It's actually a bug in compiler front-end. It produces an instruction with lineno == end_lineno but end_col_offset < col_offset

  6. markshannon commented on Jun 10, 2022

    @markshannon
    Member
  7. added a commit that references this issue on Jun 14, 2022
  8. added a commit that references this issue on Jun 16, 2022
  9. iritkatriel commented on Jun 16, 2022

    @iritkatriel
    Member

    Backport to 3.11: #93895

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions