Skip to content

3.13 traceback module print / format functions fail if incorrect source is found by linecache #124060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jpe opened this issue Sep 13, 2024 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@jpe
Copy link

jpe commented Sep 13, 2024

Bug report

Bug description:

Functions in the traceback module such as format_exc() fail when linecache.getlines() returns a comment or other incorrect source. The following fails with an IndexError: list index out of range exception raised in the format_exc() call.

import linecache
import traceback

pseudo_filename = 'pseudo filename'
src = '1/0'
co = compile(src, filename=pseudo_filename, mode='exec')
linecache._register_code(pseudo_filename, '# a comment', pseudo_filename)
try:
    exec(co)
except Exception as exc:
    lines = traceback.format_exc()

I know linecache.._register_code() is not public and code that does something like this is buggy, but the traceback module is used to report bugs and shouldn't fail itself.

The fix is probably to check the len(tree.body) in StackSummary._should_show_carets() in traceback.py before accessing tree.body[0]

CPython versions tested on:

3.13

Operating systems tested on:

No response

@jpe jpe added the type-bug An unexpected behavior, bug, or error label Sep 13, 2024
@picnixz
Copy link
Member

picnixz commented Sep 13, 2024

Thank you for the report but it's a known issue: #122071 and #122145. So I'm closing it as a duplicate (see also the discussion on #122161 for why we don't want (yet) fixing it like this).

@picnixz picnixz closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2024
@jpe
Copy link
Author

jpe commented Sep 13, 2024

OK, though I wish the simple fix to the traceback module had been applied. It's important that tools for reporting exceptions don't fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants