Skip to content

Commit 52f46df

Browse files
author
Maor Marcus
committed
Extract IPython code lines when extracting tracebacks (fix #558)
1 parent 9e7ed8c commit 52f46df

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: backslash/contrib/utils.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import linecache
3+
import ast
34

45
_SPECIAL_DIRS = ('.git', '.hg', '.svn')
56

@@ -19,12 +20,20 @@ def normalize_file_path(path):
1920
dirname = os.path.dirname(dirname)
2021
return path
2122

23+
def _is_ipython_frame(frame):
24+
return 'ipython-input' in frame['filename'] and frame['lineno'] == 1
25+
2226
def distill_slash_traceback(err, line_radius=5):
2327
returned = err.traceback.to_list()
2428
for frame in returned:
25-
lines = linecache.getlines(frame['filename'])
26-
lineno = frame['lineno']
27-
frame['code_lines_before'], _, frame['code_lines_after'] = _splice_lines(lines, lineno - 1, line_radius)
29+
if _is_ipython_frame(frame):
30+
commands = ast.literal_eval(frame['locals']['In']['value'])
31+
frame['code_lines_before'], frame['code_line'], frame['code_lines_after'] = _splice_lines(commands, len(commands) -1, line_radius)
32+
frame['lineno'] = len(commands) - 1
33+
else:
34+
lines = linecache.getlines(frame['filename'])
35+
lineno = frame['lineno']
36+
frame['code_lines_before'], _, frame['code_lines_after'] = _splice_lines(lines, lineno - 1, line_radius)
2837
return returned
2938

3039
def _splice_lines(lines, pivot, margin):

0 commit comments

Comments
 (0)