Skip to content

Commit 9ff100a

Browse files
authored
Handle None lineno in get_source_context (#3925)
Be more defensive in `get_source_context`. The current check makes no sense as we first try to decrement `tb_lineno` and then check the result against `None`: ```python lineno = tb_lineno - 1 if lineno is not None and abs_path: ``` So it looks like this was an oversight/got broken at some point. Closes #3924
1 parent 98d0415 commit 9ff100a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Diff for: sentry_sdk/utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def get_lines_from_file(
571571

572572
def get_source_context(
573573
frame, # type: FrameType
574-
tb_lineno, # type: int
574+
tb_lineno, # type: Optional[int]
575575
max_value_length=None, # type: Optional[int]
576576
):
577577
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
@@ -587,11 +587,13 @@ def get_source_context(
587587
loader = frame.f_globals["__loader__"]
588588
except Exception:
589589
loader = None
590-
lineno = tb_lineno - 1
591-
if lineno is not None and abs_path:
590+
591+
if tb_lineno is not None and abs_path:
592+
lineno = tb_lineno - 1
592593
return get_lines_from_file(
593594
abs_path, lineno, max_value_length, loader=loader, module=module
594595
)
596+
595597
return [], None, []
596598

597599

0 commit comments

Comments
 (0)