Fix IndexError in traceback formatter#305
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
|
Hi @kiri11 can you please run git commit --amend -s to add a sign off to your commit? Thanks! |
sarahmonod
approved these changes
May 19, 2026
Contributor
sarahmonod
left a comment
There was a problem hiding this comment.
LGTM, thanks @kiri11!
8009395 to
4a83268
Compare
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 |
211ddd7 to
2a546cc
Compare
4 tasks
e6faa60 to
42c8ce1
Compare
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>
49234ad to
67596b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #191
Describe your changes
Fix
IndexError: list index out of rangeinformat_framewhen the source file on disk has fewer lines than thelinenorecorded 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. Whenlinenois out of range, the source line is skipped but the rest of the frame (arguments, locals) is still displayed.Cases handled:
lines[-1]Testing performed
Added
test_traceback_formatter_with_source_lineno_out_of_rangewhich constructs a frame pointing to line 13 of a file that is empty on disk, and verifies thatformat_framedoes 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 indexlines[-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_framesto remove the bogus source lines that shim frames (lineno=0) were displaying. The old code indexedlines[0 - 1]which Python resolved tolines[-1], silently showing the last line of the file as "source" for the shim frame. The test was asserting this incorrect behavior.