vpelletier / pprofile

Line-granularity, thread-aware deterministic and statistic pure-python profiler
GNU General Public License v2.0
445 stars 28 forks source link

Do not apply active `__future__` imports to profiled code #9

Closed sth closed 8 years ago

sth commented 8 years ago

Having dont_inherit=0 as default in the call to compile() causes the compiled code to be parsed with from __future__ import print_function active, since this is used in pprofile.py. This results in syntax errors if the profiled script uses old non-function print statements:

$ cat tst.py
print "Hello world"
$ pprofile tst.py
Command line: ['tst.py']
Total duration: 0s
Traceback (most recent call last):
  File "/home/stephan/.local/bin/pprofile", line 9, in <module>
    load_entry_point('pprofile==1.9', 'console_scripts', 'pprofile')()
  File "/home/stephan/.local/lib/python2.7/site-packages/pprofile.py", line 974, in main
    runner.runpath(options.script, args)
  File "/home/stephan/.local/lib/python2.7/site-packages/pprofile.py", line 555, in runpath
    return self.runfile(open(path, 'rb'), argv, fd_name=path)
  File "/home/stephan/.local/lib/python2.7/site-packages/pprofile.py", line 539, in runfile
    dont_inherit=dont_inherit)
  File "tst.py", line 2
    print "Hello world"
                      ^
SyntaxError: invalid syntax

Applying the __future__ imports from pprofile.py to the profiled code doesn't seem useful. Having dont_inherit=1, avoiding that issue, seems to be the better default. With this change the above test script can be profiled without errors.

vpelletier commented 8 years ago

Yes, this was not intended. Thanks for the report and the patch. Pushed & released as 1.9.1 .