From 518bb343e01fbb4f1e89e858d4e7bb323b18d30a Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Wed, 22 Feb 2017 10:35:42 -0800 Subject: [PATCH] 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). --- vmprof/show.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vmprof/show.py b/vmprof/show.py index 68d17010..240e22cf 100644 --- a/vmprof/show.py +++ b/vmprof/show.py @@ -5,6 +5,7 @@ import os import six import sys +import tokenize import vmprof import argparse @@ -231,7 +232,11 @@ def show_func(self, filename, start_lineno, func_name, timings, stream=None, str # Clear the cache to ensure that we get up-to-date results. linecache.clearcache() all_lines = linecache.getlines(filename) - sublines = inspect.getblock(all_lines[start_lineno-1:]) + try: + sublines = inspect.getblock(all_lines[start_lineno-1:]) + except tokenize.TokenError: + # inspect.getblock fails on multi line dictionary comprehensions + sublines = all_lines[start_lineno-1:max(linenos)] else: stream.write("\n") stream.write("Could not find file %s\n" % filename)