Skip to content

Fix IndexError in traceback formatter#305

Merged
godlygeek merged 4 commits into
bloomberg:mainfrom
kiri11:fix-traceback-formatter
May 19, 2026
Merged

Fix IndexError in traceback formatter#305
godlygeek merged 4 commits into
bloomberg:mainfrom
kiri11:fix-traceback-formatter

Conversation

@kiri11
Copy link
Copy Markdown
Contributor

@kiri11 kiri11 commented May 19, 2026

Closes: #191

Describe your changes

Fix IndexError: list index out of range in format_frame when the source file on disk has fewer lines than the lineno recorded in the target process's frame. This can happen when the code object's line numbers don't correspond to the file's actual line count — for example, Jinja2 compiled templates use the template filename but with line numbers from the generated Python source, or when a file has been modified on disk since the process compiled it.

The fix adds a bounds check (1 <= lineno <= len(lines)) before indexing into the read lines. When lineno is out of range, the source line is skipped but the rest of the frame (arguments, locals) is still displayed.

Cases handled:

  • Jinja2 mismatched line numbers (lineno 13, file has 7 lines) — skips source display
  • Truncated/empty files (lineno > 0, file has 0 lines) — skips source display
  • Shim frames (lineno = 0) — skips source display instead of silently showing lines[-1]
  • Valid line numbers — unchanged behavior

Testing performed

Added test_traceback_formatter_with_source_lineno_out_of_range which constructs a frame pointing to line 13 of a file that is empty on disk, and verifies that format_frame does not raise and still renders the frame header, arguments, and locals.

Additional context

This also fixes a latent bug where lineno = 0 (used by shim frames) would silently index lines[-1], displaying the last line of the file as if it were the source — a misleading but non-crashing behavior.

Updated test_native_traceback_with_shim_frames to remove the bogus source lines that shim frames (lineno=0) were displaying. The old code indexed lines[0 - 1] which Python resolved to lines[-1], silently showing the last line of the file as "source" for the shim frame. The test was asserting this incorrect behavior.

@kiri11 kiri11 marked this pull request as ready for review May 19, 2026 15:41
@kiri11 kiri11 changed the title Fix traceback formatter Fix IndexError in traceback formatter May 19, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.22%. Comparing base (b87b6b6) to head (67596b4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #305      +/-   ##
==========================================
+ Coverage   79.19%   79.22%   +0.02%     
==========================================
  Files          51       51              
  Lines        5542     5550       +8     
  Branches      580      581       +1     
==========================================
+ Hits         4389     4397       +8     
  Misses       1153     1153              
Flag Coverage Δ
cpp 79.22% <100.00%> (+0.02%) ⬆️
python 79.22% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sarahmonod sarahmonod added bug Something isn't working skip news labels May 19, 2026
@sarahmonod
Copy link
Copy Markdown
Contributor

Hi @kiri11 can you please run git commit --amend -s to add a sign off to your commit? Thanks!

Copy link
Copy Markdown
Contributor

@sarahmonod sarahmonod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @kiri11!

@kiri11 kiri11 force-pushed the fix-traceback-formatter branch from 8009395 to 4a83268 Compare May 19, 2026 20:54
@sarahmonod sarahmonod enabled auto-merge (rebase) May 19, 2026 21:04
@sarahmonod sarahmonod disabled auto-merge May 19, 2026 21:04
@sarahmonod
Copy link
Copy Markdown
Contributor

@kiri11 please rebase this branch on top of main instead of adding a merge commit. This keeps history clean and follows the project's conventions

@kiri11 kiri11 force-pushed the fix-traceback-formatter branch 3 times, most recently from 211ddd7 to 2a546cc Compare May 19, 2026 21:15
@kiri11 kiri11 force-pushed the fix-traceback-formatter branch from e6faa60 to 42c8ce1 Compare May 19, 2026 21:48
kiri11 added 4 commits May 19, 2026 15:04
Signed-off-by: Kirill Ignatev <kiri11@users.noreply.github.com>
Signed-off-by: Kirill Ignatev <kiri11@users.noreply.github.com>
Signed-off-by: Kirill Ignatev <kiri11@users.noreply.github.com>
Signed-off-by: Kirill Ignatev <kiri11@users.noreply.github.com>
@kiri11 kiri11 force-pushed the fix-traceback-formatter branch from 49234ad to 67596b4 Compare May 19, 2026 22:07
Copy link
Copy Markdown
Contributor

@godlygeek godlygeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @kiri11 !

@godlygeek godlygeek merged commit bc2f920 into bloomberg:main May 19, 2026
30 checks passed
@kiri11 kiri11 deleted the fix-traceback-formatter branch May 20, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jinja templating seems to be crashing PyStack with an IndexError

4 participants