aboutsummaryrefslogtreecommitdiff
path: root/tests/errors
diff options
context:
space:
mode:
authornatsuoto <[email protected]>2026-07-15 14:12:59 +0530
committernatsuoto <[email protected]>2026-07-15 14:12:59 +0530
commite6f7b668ce22b21a5c74beb52591b821f24a6d1c (patch)
tree65d05a618ab0b799d08b185ffb54414448ef6792 /tests/errors
parentf14a239abc99f3575bbc3ce26a6b4e3871891b0c (diff)
downloadedify-e6f7b668ce22b21a5c74beb52591b821f24a6d1c.tar.xz
edify-e6f7b668ce22b21a5c74beb52591b821f24a6d1c.zip
perf(builder): lazy source_line and cached co_positions/abspath cut chain-step allocation by ~4.6x
Diffstat (limited to 'tests/errors')
-rw-r--r--tests/errors/context.test.py19
-rw-r--r--tests/errors/formatting.test.py16
2 files changed, 29 insertions, 6 deletions
diff --git a/tests/errors/context.test.py b/tests/errors/context.test.py
index 6595e92..60e7add 100644
--- a/tests/errors/context.test.py
+++ b/tests/errors/context.test.py
@@ -1,5 +1,6 @@
"""Tests for the caller-source-location capture helpers in :mod:`edify.errors.context`."""
+import linecache
from unittest import mock
from edify.errors.context import CallerContext, capture_caller_context
@@ -10,7 +11,21 @@ def test_capture_caller_context_returns_none_when_every_frame_is_inside_edify():
assert capture_caller_context() is None
-def test_caller_context_dataclass_is_frozen_and_holds_all_five_fields():
- context = CallerContext(filename="a.py", lineno=1, colno=1, end_colno=5, source_line="x = 1")
+def test_caller_context_dataclass_is_frozen_and_holds_position_fields():
+ context = CallerContext(filename="a.py", lineno=1, colno=1, end_colno=5)
assert context.filename == "a.py"
+ assert context.lineno == 1
+ assert context.colno == 1
+ assert context.end_colno == 5
+
+
+def test_caller_context_source_line_property_reads_lazily_via_linecache():
+ filename = "/tmp/lazy_source_test.py"
+ linecache.cache[filename] = (5, None, ["x = 1\n"], filename)
+ context = CallerContext(filename=filename, lineno=1, colno=1, end_colno=5)
assert context.source_line == "x = 1"
+
+
+def test_caller_context_source_line_returns_empty_string_when_line_is_unreadable():
+ context = CallerContext(filename="/tmp/no_such_file.py", lineno=999, colno=1, end_colno=1)
+ assert context.source_line == ""
diff --git a/tests/errors/formatting.test.py b/tests/errors/formatting.test.py
index 314aff1..2455ecf 100644
--- a/tests/errors/formatting.test.py
+++ b/tests/errors/formatting.test.py
@@ -1,5 +1,6 @@
"""Tests for the message formatter helpers in :mod:`edify.errors.formatting`."""
+import linecache
from unittest import mock
from edify.errors.context import CallerContext
@@ -17,13 +18,19 @@ from edify.errors.formatting import (
)
+def _inject_source(filename: str, lineno: int, source_line: str) -> None:
+ lines = [""] * (lineno - 1) + [source_line + "\n"]
+ linecache.cache[filename] = (len(source_line) + 1, None, lines, filename)
+
+
def _context(source_line: str = " x = do_the_thing(1, 2, 3)", colno: int = 9) -> CallerContext:
+ filename = f"/tmp/user_code_{colno}_{hash(source_line) & 0xFFFF}.py"
+ _inject_source(filename, 42, source_line)
return CallerContext(
- filename="/tmp/user_code.py",
+ filename=filename,
lineno=42,
colno=colno,
end_colno=colno + 15,
- source_line=source_line,
)
@@ -45,17 +52,18 @@ def test_format_pointer_block_draws_caret_at_caller_column():
context = _context()
block = format_pointer_block(context, "here")
lines = block.splitlines()
- assert lines[0] == " --> /tmp/user_code.py:42:9"
+ assert lines[0].startswith(" -->")
+ assert lines[0].endswith(":42:9")
assert "^^^^^^^^^^^^^^^ here" in block
def test_format_pointer_block_has_at_least_one_caret_when_span_is_empty():
+ _inject_source("/tmp/x.py", 1, "abcde")
context = CallerContext(
filename="/tmp/x.py",
lineno=1,
colno=5,
end_colno=5,
- source_line="abcde",
)
block = format_pointer_block(context, "point")
assert "^ point" in block