diff --git a/python/helpers/profiler/prof_util.py b/python/helpers/profiler/prof_util.py index 03afa98ed8cc..e52faad8c036 100644 --- a/python/helpers/profiler/prof_util.py +++ b/python/helpers/profiler/prof_util.py @@ -2,9 +2,47 @@ __author__ = 'traff' import threading import os +import sys import tempfile from _prof_imports import Stats, FuncStat, Function +try: + execfile=execfile #Not in Py3k +except NameError: + #We must redefine it in Py3k if it's not already there + def execfile(file, glob=None, loc=None): + if glob is None: + import sys + glob = sys._getframe().f_back.f_globals + if loc is None: + loc = glob + + # It seems that the best way is using tokenize.open(): http://code.activestate.com/lists/python-dev/131251/ + import tokenize + stream = tokenize.open(file) # @UndefinedVariable + try: + contents = stream.read() + finally: + stream.close() + + #execute the script (note: it's important to compile first to have the filename set in debug mode) + exec(compile(contents+"\n", file, 'exec'), glob, loc) + + +def save_main_module(file, module_name): + sys.modules[module_name] = sys.modules['__main__'] + sys.modules[module_name].__name__ = module_name + from imp import new_module + + m = new_module('__main__') + sys.modules['__main__'] = m + if hasattr(sys.modules[module_name], '__loader__'): + setattr(m, '__loader__', getattr(sys.modules[module_name], '__loader__')) + m.__file__ = file + + return m + + class ProfDaemonThread(threading.Thread): def __init__(self): super(ProfDaemonThread, self).__init__() diff --git a/python/helpers/profiler/run_profiler.py b/python/helpers/profiler/run_profiler.py index 8593c6268e4e..9daa3c5a9b53 100644 --- a/python/helpers/profiler/run_profiler.py +++ b/python/helpers/profiler/run_profiler.py @@ -2,15 +2,13 @@ import os import sys import time import traceback -from _pydev_bundle import pydev_imports -from _pydevd_bundle.pydevd_utils import save_main_module from socket import AF_INET from socket import SOCK_STREAM from socket import socket from _prof_imports import ProfilerResponse from prof_io import ProfWriter, ProfReader -from prof_util import generate_snapshot_filepath, stats_to_response, get_snapshot_basepath +from prof_util import generate_snapshot_filepath, stats_to_response, get_snapshot_basepath, save_main_module, execfile base_snapshot_path = os.getenv('PYCHARM_SNAPSHOT_PATH') remote_run = bool(os.getenv('PYCHARM_REMOTE_RUN', '')) @@ -88,7 +86,7 @@ class Profiler(object): self.start_profiling() try: - pydev_imports.execfile(file, globals, globals) # execute the script + execfile(file, globals, globals) # execute the script finally: self.stop_profiling() self.save_snapshot(0, generate_snapshot_filepath(base_snapshot_path, remote_run, self.snapshot_extension()), remote_run)