Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/exception-long-context-lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`.
22 changes: 14 additions & 8 deletions posthog/exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def get_lines_from_file(
loader=None, # type: Optional[Any]
module=None, # type: Optional[str]
):
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
# type: (...) -> Tuple[List[str], Optional[str], List[str]]
context_lines = 5
source = _get_source_lines(filename, loader, module)

Expand All @@ -423,14 +423,20 @@ def get_lines_from_file(
lower_bound = max(0, lineno - context_lines)
upper_bound = min(lineno + 1 + context_lines, len(source))

def strip_line(line):
# type: (str) -> str
# strip_string wraps a truncated line in an AnnotatedValue, which the
# event payload can't serialize, so keep only the truncated text.
stripped = strip_string(line.strip("\r\n"), max_length=max_length)
if isinstance(stripped, AnnotatedValue):
return cast(str, stripped.value)
return stripped

try:
pre_context = [
strip_string(line.strip("\r\n"), max_length=max_length)
for line in source[lower_bound:lineno]
]
context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
pre_context = [strip_line(line) for line in source[lower_bound:lineno]]
context_line = strip_line(source[lineno])
post_context = [
strip_string(line.strip("\r\n"), max_length=max_length)
strip_line(line)
for line in source[(lineno + 1) : upper_bound] # noqa: E203
]
return pre_context, context_line, post_context
Expand Down Expand Up @@ -464,7 +470,7 @@ def get_source_context(
tb_lineno, # type: int
max_value_length=None, # type: Optional[int]
):
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
# type: (...) -> Tuple[List[str], Optional[str], List[str]]
try:
abs_path = frame.f_code.co_filename # type: Optional[str]
except Exception:
Expand Down
17 changes: 17 additions & 0 deletions posthog/test/test_exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def unavailable(module):
) == ([], "current", ["after"])


def test_source_context_truncates_long_lines_to_strings(tmp_path):
long_line = "x" * 1100
source = tmp_path / "application.py"
source.write_text("\n".join([long_line, long_line, long_line]))

pre_context, context_line, post_context = exception_utils.get_lines_from_file(
str(source), 1
)

truncated = "x" * 1021 + "..."
assert (pre_context, context_line, post_context) == (
[truncated],
truncated,
[truncated],
)


def test_source_context_handles_changed_files(tmp_path):
source = tmp_path / "application.py"
source.write_text("current\n")
Expand Down
Loading