Skip to content

3.11 regression: traceback.format_list raises UnicodeDecodeError in certain scenarios #98744

Closed

Description

@sebastinas

Bug report

Take the following piece of code:

import sys, traceback
try:
    width
except:
    _, _, tb = sys.exc_info()

tblist = traceback.extract_tb(tb)
print(traceback.format_list(tblist))

With Python 3.10 and earlier versions, executing a file with this code produces:

['  File "/tmp/test.py", line 3, in <module>\n    width\n']

With Python 3.11, a UnicodeDecodeError is raised instead:

Traceback (most recent call last):
  File "/tmp/test.py", line 9, in <module>
    print(traceback.format_list(tblist))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/traceback.py", line 41, in format_list
    return StackSummary.from_list(extracted_list).format()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/traceback.py", line 531, in format
    formatted_frame = self.format_frame_summary(frame_summary)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/traceback.py", line 478, in format_frame_summary
    colno = _byte_offset_to_character_offset(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/traceback.py", line 566, in _byte_offset_to_character_offset
    return len(as_utf8[:offset + 1].decode("utf-8"))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 4: unexpected end of data

Your environment

  • CPython versions tested on: 3.10.8 and 3.11.0
  • Operating system and architecture: Debian unstable, amd64

Activity

  1. sebastinas commented on Oct 27, 2022

    @sebastinas
    Author

    Current main (d578aae) also has the same issue.

  2. serhiy-storchaka commented on Oct 28, 2022

    @serhiy-storchaka
    Member
  3. mdboom commented on Oct 28, 2022

    @mdboom
    Contributor

    Also cc @iritkatriel (related to source positions -- in this case a source position in the middle of a UTF-8 codepoint).

  4. isidentical commented on Oct 28, 2022

    @isidentical
    SponsorMember

    It seems like we are not mirroring the behaviour from the C implementation, which uses errors="replace" instead of the default strict. I'll try to send a PR soon. Thanks for the report @sebastinas!

  5. mdboom commented on Oct 28, 2022

    @mdboom
    Contributor

    @isidentical: I was just looking at this as well, and there is also an off-by-one difference between this and the C implementation.

  6. added a commit that references this issue on Oct 28, 2022
  7. isidentical commented on Oct 28, 2022

    @isidentical
    SponsorMember

    @mdboom you are right! That is also an issue (in the C Implementation, we do a lengths check to not to overflow; although it is not necessary here. Another problem is the caret positions which actually might differ from node's offsets). Both should now be resolved with #98824. Thanks for catching it.

  8. added a commit that references this issue on Oct 28, 2022
  9. added a commit that references this issue on Oct 29, 2022
  10. added a commit that references this issue on Oct 29, 2022
  11. added a commit that references this issue on Oct 29, 2022
  12. isidentical commented on Oct 29, 2022

    @isidentical
    SponsorMember

    Should be fixed now, thanks everyone!

  13. added
    3.11only security fixes
    3.12only security fixes
    and removed on Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixestopic-unicodetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions