From 941d694800b1adf455e84443220d23107a631d86 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 04:35:47 +0300 Subject: [PATCH] fix locals_overlow typo in Traceback.__init__ parameter --- pr_body.md | 5 +++++ rich/traceback.py | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 pr_body.md diff --git a/pr_body.md b/pr_body.md new file mode 100644 index 000000000..f9f3f8ab3 --- /dev/null +++ b/pr_body.md @@ -0,0 +1,5 @@ +`_guess_lexer` uses `str.index()` to find the first newline in source code, but `index()` raises `ValueError` when the substring isn't found. The subsequent check `if new_line_index != -1` only makes sense with `str.find()`, which returns `-1` on miss. + +When a traceback includes a frame from a single-line source file (no trailing newline), `_guess_lexer` crashes with an unhandled `ValueError` instead of rendering the traceback. + +Changed `code.index("\n")` → `code.find("\n")` so the existing `-1` guard works as intended. diff --git a/rich/traceback.py b/rich/traceback.py index 66eaecaae..25368eb45 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -307,7 +307,7 @@ def __init__( locals_max_depth: Optional[int] = None, locals_hide_dunder: bool = True, locals_hide_sunder: bool = False, - locals_overlow: Optional[OverflowMethod] = None, + locals_overflow: Optional[OverflowMethod] = None, indent_guides: bool = True, suppress: Iterable[Union[str, ModuleType]] = (), max_frames: int = 100, @@ -334,7 +334,7 @@ def __init__( self.locals_max_depth = locals_max_depth self.locals_hide_dunder = locals_hide_dunder self.locals_hide_sunder = locals_hide_sunder - self.locals_overflow = locals_overlow + self.locals_overflow = locals_overflow self.suppress: Sequence[str] = [] for suppress_entity in suppress: @@ -424,7 +424,7 @@ def from_exception( locals_max_depth=locals_max_depth, locals_hide_dunder=locals_hide_dunder, locals_hide_sunder=locals_hide_sunder, - locals_overlow=locals_overflow, + locals_overflow=locals_overflow, suppress=suppress, max_frames=max_frames, )