Repository navigation
Exception raised in traceback.StackSummary._should_show_carets exits interpreter: IndexError on tree.body[0] #122145
Copy link
Copy link
Closed
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Running the code sample from #122071 in 3.13.0b4 or main exits the interpreter due to traceback.StackSummary._should_show_carets raising an exception:
>>> exec(compile("tuple()[0]", "s", "exec"))
Traceback (most recent call last):
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
File "~\PycharmProjects\cpython\Lib\traceback.py", line 139, in _print_exception_bltin
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 130, in print_exception
te.print(file=file, chain=chain, colorize=colorize)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 1448, in print
for line in self.format(chain=chain, colorize=colorize):
File "~\PycharmProjects\cpython\Lib\traceback.py", line 1384, in format
yield from _ctx.emit(exc.stack.format(colorize=colorize))
File "~\PycharmProjects\cpython\Lib\traceback.py", line 747, in format
formatted_frame = self.format_frame_summary(frame_summary, colorize=colorize)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 583, in format_frame_summary
show_carets = self._should_show_carets(start_offset, end_offset, all_lines, anchors)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 701, in _should_show_carets
statement = tree.body[0]
IndexError: list index out of range
Traceback (most recent call last):
File "~\PycharmProjects\cpython\Lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<python-input-2>", line 1, in <module>
File "s", line 1, in <module>
IndexError: tuple index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~\PycharmProjects\cpython\Lib\runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
File "~\PycharmProjects\cpython\Lib\runpy.py", line 88, in _run_code
exec(code, run_globals)
File "~\PycharmProjects\cpython\Lib\_pyrepl\__main__.py", line 6, in <module>
__pyrepl_interactive_console()
File "~\PycharmProjects\cpython\Lib\_pyrepl\main.py", line 59, in interactive_console
run_multiline_interactive_console(console)
File "~\PycharmProjects\cpython\Lib\_pyrepl\simple_interact.py", line 156, in run_multiline_interactive_console
more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single") # type: ignore[call-arg]
File "~\PycharmProjects\cpython\Lib\code.py", line 303, in push
more = self.runsource(source, filename, symbol=_symbol)
File "~\PycharmProjects\cpython\Lib\_pyrepl\console.py", line 200, in runsource
self.runcode(code)
File "~\PycharmProjects\cpython\Lib\code.py", line 95, in runcode
self.showtraceback()
File "~\PycharmProjects\cpython\Lib\_pyrepl\console.py", line 168, in showtraceback
super().showtraceback(colorize=self.can_colorize)
File "~\PycharmProjects\cpython\Lib\code.py", line 147, in showtraceback
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 155, in format_exception
return list(te.format(chain=chain, colorize=colorize))
File "~\PycharmProjects\cpython\Lib\traceback.py", line 1384, in format
yield from _ctx.emit(exc.stack.format(colorize=colorize))
File "~\PycharmProjects\cpython\Lib\traceback.py", line 747, in format
formatted_frame = self.format_frame_summary(frame_summary, colorize=colorize)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 583, in format_frame_summary
show_carets = self._should_show_carets(start_offset, end_offset, all_lines, anchors)
File "~\PycharmProjects\cpython\Lib\traceback.py", line 701, in _should_show_carets
statement = tree.body[0]
IndexError: list index out of range
[Thread 31424.0x8ec0 exited with code 1]
[Thread 31424.0x15dc exited with code 1]
[Thread 31424.0x8b8c exited with code 1]
[Inferior 1 (process 31424) exited with code 01]We can either protect the _should_show_carets call with suppress(Exception) here:
Lines 581 to 583 in 2762c6c
| with suppress(Exception): | |
| anchors = _extract_caret_anchors_from_line_segment(segment) | |
| show_carets = self._should_show_carets(start_offset, end_offset, all_lines, anchors) |
Or guard against tree.body being empty here:
Lines 700 to 701 in 2762c6c
| tree = ast.parse('\n'.join(all_lines)) | |
| statement = tree.body[0] |
(or both?)
Should I submit a PR with one of the fixes above? Any preferences?
CPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
Windows, Linux
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
[-]Exception raised in traceback.StackSummary._should_show_carets exits interpreter on Windows[/-][+]Exception raised in traceback.StackSummary._should_show_carets exits interpreter[/+]gaogaotiantian commentedon Jul 23, 2024
In my opinion, we should figure out that issue first. This is probably something that should not happen - it's kind of equivalent to a comment only file generates an exception, that's just impossible.
devdanzin commentedon Jul 23, 2024
Indeed, ISTM that due to the linecache bug we try to parse "lines" consisting of only a comment then access the tree's body first node. So fixing that issue will fix this specific failure.
Maybe it would still be interesting to make the code more robust against exceptions in
_should_show_carets?sobolevn commentedon Jul 23, 2024
Looks like there are two bugs:
Lib/_pyrepl/__main__.py(this file is used for sources)tree.bodycan be empty in other cases as well, so why not guard it?After:
picnixz commentedon Jul 23, 2024
I've fixed the second issue and added a test for that one. I can move to fixing the first issue as well.
devdanzin commentedon Jul 23, 2024
Exactly. Another way to get an empty
tree.bodyis to run:Where you have a x.py file that consists of:
# Just a comment.devdanzin commentedon Jul 23, 2024
Yet another way to get an emptytree.body:EDIT: never mind, I had reverted the fix.
picnixz commentedon Jul 23, 2024
Errr... ok. I'll amend the NEWS entry in the PR since it only talks about comments.
EDIT: Actually, I think you can only have an empty AST body if you are either an empty string or if you are a comment.
devdanzin commentedon Jul 30, 2024
In
StackSummary.format_frame_summary,StackSummary._should_show_caretsis called without error suppression (like is done for_extract_caret_anchors_from_line_segment). This causes any errors in that method to exit the interpreter. I suggest we also ignore errors from_should_show_caretshere:cpython/Lib/traceback.py
Lines 581 to 583 in d27a53f
This time, an error in parsing a too deeply nested structure exits the interpreter:
If an even deeper nested structure is used,
MemoryErroris raised, but somehow doesn't exit the interpreter.Doesn't seem to affect 3.13. Should a new issue be created for this one?
vstinner commentedon Sep 17, 2024
IMO #122071 is a duplicate of this issue.
hroncok commentedon Sep 17, 2024
I think #122071 affects all the Python versions:
While this one only affects Python 3.13, e.g.:
I think they build on top of each other -- when #122071 is fixed, this one will likely also get fixed, but I am not quite sure.
[-]Exception raised in traceback.StackSummary._should_show_carets exits interpreter[/-][+]Exception raised in traceback.StackSummary._should_show_carets exits interpreter: IndexError on tree.body[0][/+]vstinner commentedon Sep 18, 2024
This bug is impacting Fedora Installer (Anaconda) and was marked as a Fedora 41 blocker issue: https://bugzilla.redhat.com/show_bug.cgi?id=2311907 So I also mark this bug as a blocker issue.
gh-122145: Handle an empty AST body when reporting tracebacks (#122161)
pythongh-122145: Handle an empty AST body when reporting tracebacks (p…
vstinner commentedon Sep 18, 2024
@Yhg1s explained me that if we add a bugfix after 3.13rc2, another rc3 is needed. I don't think that this bug should require a whole new rc3. So I removed the release-blocker label. The fix can wait for 3.13.1 even if it's bad.
For Fedora, we can have a downstream patch to unblock Fedora 41, until the fix is merged into the 3.13 branch.
vstinner commentedon Sep 18, 2024
Bug fixed by the change 5cd50cb. A backport to 3.13 will follow. I close the issue.
See #122071 for the second traceback bug.
00439: pythongh-122145: Handle an empty AST body when reporting trace…
pythongh-122145: Handle an empty AST body when reporting tracebacks (p…
[3.13] gh-122145: Handle an empty AST body when reporting tracebacks (G…