Skip to content

Commit 518bb34

Browse files
committed
Fallback in case of TokenError during vmprofshow
multiline dictionary comprehensions cause inspect.getblock to fail, so in case of a TokenError, guess the last line based on max(linenos).
1 parent 755c869 commit 518bb34

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

vmprof/show.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import six
77
import sys
8+
import tokenize
89

910
import vmprof
1011
import argparse
@@ -231,7 +232,11 @@ def show_func(self, filename, start_lineno, func_name, timings, stream=None, str
231232
# Clear the cache to ensure that we get up-to-date results.
232233
linecache.clearcache()
233234
all_lines = linecache.getlines(filename)
234-
sublines = inspect.getblock(all_lines[start_lineno-1:])
235+
try:
236+
sublines = inspect.getblock(all_lines[start_lineno-1:])
237+
except tokenize.TokenError:
238+
# inspect.getblock fails on multi line dictionary comprehensions
239+
sublines = all_lines[start_lineno-1:max(linenos)]
235240
else:
236241
stream.write("\n")
237242
stream.write("Could not find file %s\n" % filename)

0 commit comments

Comments
 (0)