Skip to content

Fail gracefully when line number is out-of-bounds and negative - #10868

Closed
jakevdp wants to merge 2 commits into
pytest-dev:mainfrom
jakevdp:fix-lineno
Closed

Fail gracefully when line number is out-of-bounds and negative#10868
jakevdp wants to merge 2 commits into
pytest-dev:mainfrom
jakevdp:fix-lineno

Conversation

@jakevdp

@jakevdp jakevdp commented Apr 5, 2023

Copy link
Copy Markdown
Contributor

This fixes an issue I ran into that is similar to #752.

I don't have a self-contained repro for this failure, but an example of the failure is in the nightly CI for the JAX project: https://github.com/google/jax/actions/runs/4607513902/jobs/8142126075. The last few lines of the traceback looks like this:

INTERNALERROR> E                 reprtraceback = self.repr_traceback(excinfo_)
INTERNALERROR> E                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 871, in repr_traceback
INTERNALERROR> E                 reprentry = self.repr_traceback_entry(entry, einfo)
INTERNALERROR> E                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 822, in repr_traceback_entry
INTERNALERROR> E                 s = self.get_source(source, line_index, excinfo, short=short)
INTERNALERROR> E                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> E               File "/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/_pytest/_code/code.py", line 751, in get_source
INTERNALERROR> E                 lines.append(space_prefix + source.lines[line_index].strip())
INTERNALERROR> E                                             ~~~~~~~~~~~~^^^^^^^^^^^^
INTERNALERROR> E             IndexError: list index out of range

Reading through the source, I see that positive out-of-bound indices are already handled gracefully:

if source is None or line_index >= len(source.lines):
source = Source("???")
line_index = 0

Given this, I suspect the only possible way to hit the error I'm seeing is if the list index is out of range and negative.

@nicoddemus

Copy link
Copy Markdown
Member

Seems similar/duplicate to #10840.

@jakevdp

jakevdp commented Apr 5, 2023

Copy link
Copy Markdown
Contributor Author

Yep, looks like a duplicate. Consider this a +1 for getting that PR in ASAP!

@jakevdp jakevdp closed this Apr 5, 2023
@jakevdp
jakevdp deleted the fix-lineno branch April 5, 2023 00:14
@jakevdp

jakevdp commented Apr 5, 2023

Copy link
Copy Markdown
Contributor Author

It looks like that PR is stalled on lack of tests – would it help to pull in the tests I wrote here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants