-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vmprofshow breaks on multiline dictionary comprehensions #118
Comments
diff --git a/vmprof/show.py b/vmprof/show.py
index 68d1701..e35a456 100644
--- a/vmprof/show.py
+++ b/vmprof/show.py
@@ -224,7 +224,7 @@ class LinesPrinter(object):
return
stream.write("Total hits: %g s\n" % total_hits)
- if os.path.exists(filename) or filename.startswith("<ipython-input-"):
+ if (os.path.exists(filename) or filename.startswith("<ipython-input-")) and func_name != "<dictcomp>":
stream.write("File: %s\n" % filename)
stream.write("Function: %s at line %s\n" % (func_name, start_lineno))
if os.path.exists(filename): Fixes the issue for me, although there may be a better way. |
I think that your patch filters dict comprehensions, the error indicates up the following issue: inspect.getblock rightfully raises a tokenize error, because in the example you provided, it cannot parse a full python block. it is missing the starting bracket This means that multiline list comprehensions are also broken. I think a better fix would be to parse the whole file, and iterate each syntax element and check if the startline <= line <= endline, where line is the line you want to show. |
I tested a multi line list comprehension and it appears to work.
I am not sure how to get the endline, but here is a pull request that I think is close to what you describe: #119 |
Yes, that looks better. I tried to use the As I see this now, this gives a better result than previously, so lets merge it. |
@planrich thanks! |
Using cpython 2.7
test.py
:Specifically the line
i: i**i
takes the most time. But when handling a tokenize error the same way as a file missing I get:I tried playing around with
inspect.getblock
for dictionary comprehension by hand and was unable to get it to work properly.So I am not sure that multiline dictionary comprehensions are measured correctly per line.
At the very least we can prevent
vmprofshow --lines test.prof
from crashing by handling the tokenize error invmprof.show
.The text was updated successfully, but these errors were encountered: