Skip to content

TracebackException.format_exception_only() mutates attributes on keyword typo suggestion #154544

Description

@tonghuaroot

Bug description

TracebackException.format_exception_only() is not idempotent when _find_keyword_typos() finds a suggestion. The method directly mutates self.text, self.offset, self.end_offset, self.lineno, self.end_lineno, and self.msg, so a second call produces different output (e.g. wrong line number).

import traceback

code = "fr x in range(10):\n    pass\n"
try:
    compile(code, "<test>", "exec")
except SyntaxError as exc:
    te = traceback.TracebackException(type(exc), exc, None)
    r1 = list(te.format_exception_only())  # correct
    r2 = list(te.format_exception_only())  # different output
    assert r1 == r2  # fails

_find_keyword_typos was added in gh-132449.

CPython versions tested on

main

Operating systems tested on

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions