Skip to content

Commit 86f15e6

Browse files
committed
tree: Don't call linecache for the root node
After fixing the bug where an empty list would be returned if there were not enough lines before the cursor line in the file, another bug was revealed by the test suite: we've been calling `linecache.getlines()` even for the root node. That's only noticeable in the test suite because we mock the return value from that function. In a real run of the tree reporter, we call `linecache.getlines()` with an empty string as the file name, and it always returns an empty list in that case. Still, better to fix this to recognize the root node and not attempt to fetch lines for it at all. Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
1 parent 196ade2 commit 86f15e6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/memray/reporters/tree.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async def update_text_area(self) -> None:
9595

9696
text = self.query_one("#textarea", TextArea)
9797

98-
if self.frame.location is None:
98+
if self.frame.location in (None, ROOT_NODE):
9999
text.clear()
100100
return
101101

@@ -166,15 +166,16 @@ def watch_frame(self) -> None:
166166
def compose(self) -> ComposeResult:
167167
if self.frame is None:
168168
return
169+
169170
delta = 3
170171

171-
if self.frame.location is not None:
172+
if self.frame.location in (None, ROOT_NODE):
173+
lines = []
174+
selected_line = 0
175+
else:
172176
_, file, line = self.frame.location
173177
lines = linecache.getlines(file)[max(line - delta, 0) : line + delta]
174178
selected_line = line - 1 if delta >= line else delta - 1
175-
else:
176-
lines = []
177-
selected_line = 0
178179

179180
text = TextArea(
180181
"\n".join(lines), language="python", theme="dracula", id="textarea"

0 commit comments

Comments
 (0)