1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 |
2 |
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
3 # Copyright (c) 2002 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
4 |
4 |
5 """ |
5 """ |
6 Module defining additions to the standard Python profile.py. |
6 Module defining additions to the standard Python profile.py. |
7 """ |
7 """ |
8 |
8 |
9 import os |
9 import os |
10 import marshal |
10 import marshal |
11 import profile |
11 import profile |
12 import atexit |
12 import atexit |
13 import pickle |
13 import pickle |
|
14 import sys |
14 |
15 |
15 |
16 |
16 class PyProfile(profile.Profile): |
17 class PyProfile(profile.Profile): |
17 """ |
18 """ |
18 Class extending the standard Python profiler with additional methods. |
19 Class extending the standard Python profiler with additional methods. |
28 |
29 |
29 @param basename name of the script to be profiled (string) |
30 @param basename name of the script to be profiled (string) |
30 @param timer function defining the timing calculation |
31 @param timer function defining the timing calculation |
31 @param bias calibration value (float) |
32 @param bias calibration value (float) |
32 """ |
33 """ |
33 profile.Profile.__init__(self, timer, bias) |
34 try: |
|
35 profile.Profile.__init__(self, timer, bias) |
|
36 except TypeError: |
|
37 profile.Profile.__init__(self, timer) |
34 |
38 |
35 self.dispatch = self.__class__.dispatch |
39 self.dispatch = self.__class__.dispatch |
36 |
40 |
37 basename = os.path.splitext(basename)[0] |
41 basename = os.path.splitext(basename)[0] |
38 self.profileCache = "{0}.profile".format(basename) |
42 self.profileCache = "{0}.profile".format(basename) |
113 code over a network... This logic deals with that. |
117 code over a network... This logic deals with that. |
114 |
118 |
115 @param frame the frame object |
119 @param frame the frame object |
116 @return fixed up file name (string) |
120 @return fixed up file name (string) |
117 """ |
121 """ |
|
122 if sys.version_info[0] == 2: |
|
123 versionExt = '.py2' |
|
124 else: |
|
125 versionExt = '.py3' |
|
126 |
118 # get module name from __file__ |
127 # get module name from __file__ |
119 if not isinstance(frame, profile.Profile.fake_frame) and \ |
128 if not isinstance(frame, profile.Profile.fake_frame) and \ |
120 '__file__' in frame.f_globals: |
129 '__file__' in frame.f_globals: |
121 root, ext = os.path.splitext(frame.f_globals['__file__']) |
130 root, ext = os.path.splitext(frame.f_globals['__file__']) |
122 if ext in ['.pyc', '.py', '.py3', '.pyo']: |
131 if ext in ['.pyc', '.py', versionExt, '.pyo']: |
123 fixedName = root + '.py' |
132 fixedName = root + '.py' |
124 if os.path.exists(fixedName): |
133 if os.path.exists(fixedName): |
125 return fixedName |
134 return fixedName |
126 |
135 |
127 fixedName = root + '.py3' |
136 fixedName = root + versionExt |
128 if os.path.exists(fixedName): |
137 if os.path.exists(fixedName): |
129 return fixedName |
138 return fixedName |
130 |
139 |
131 return frame.f_code.co_filename |
140 return frame.f_code.co_filename |
132 |
141 |