eric6/DebugClients/Python/PyProfile.py

branch
maintenance
changeset 7642
72721823d453
parent 7639
422fd05e9c91
child 7690
a59680062837
equal deleted inserted replaced
7608:f7cb83647621 7642:72721823d453
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
15 14
16 15
17 class PyProfile(profile.Profile): 16 class PyProfile(profile.Profile):
18 """ 17 """
19 Class extending the standard Python profiler with additional methods. 18 Class extending the standard Python profiler with additional methods.
52 if not os.path.exists(self.timingCache): 51 if not os.path.exists(self.timingCache):
53 return 52 return
54 53
55 try: 54 try:
56 cache = open(self.timingCache, 'rb') 55 cache = open(self.timingCache, 'rb')
57 timings = marshal.load(cache) 56 timings = marshal.load(cache) # secok
58 if isinstance(timings, dict): 57 if isinstance(timings, dict):
59 self.timings = timings 58 self.timings = timings
60 except Exception: 59 except (EnvironmentError, EOFError, ValueError, TypeError):
61 pass 60 pass
62 finally: 61 finally:
63 cache.close() 62 cache.close()
64 63
65 def save(self): 64 def save(self):
68 """ 67 """
69 # dump the raw timing data 68 # dump the raw timing data
70 try: 69 try:
71 cache = open(self.timingCache, 'wb') 70 cache = open(self.timingCache, 'wb')
72 marshal.dump(self.timings, cache) 71 marshal.dump(self.timings, cache)
73 except Exception: 72 except EnvironmentError:
74 pass 73 pass
75 finally: 74 finally:
76 cache.close() 75 cache.close()
77 76
78 # dump the profile data 77 # dump the profile data
85 @param file name of the file to write to (string) 84 @param file name of the file to write to (string)
86 """ 85 """
87 try: 86 try:
88 f = open(file, 'wb') 87 f = open(file, 'wb')
89 self.create_stats() 88 self.create_stats()
90 pickle.dump(self.stats, f, 2) 89 pickle.dump(self.stats, f, 4)
91 except (EnvironmentError, pickle.PickleError): 90 except (EnvironmentError, pickle.PickleError):
92 pass 91 pass
93 finally: 92 finally:
94 f.close() 93 f.close()
95 94
117 code over a network... This logic deals with that. 116 code over a network... This logic deals with that.
118 117
119 @param frame the frame object 118 @param frame the frame object
120 @return fixed up file name (string) 119 @return fixed up file name (string)
121 """ 120 """
122 if sys.version_info[0] == 2: 121 versionExt = '.py3'
123 versionExt = '.py2'
124 else:
125 versionExt = '.py3'
126 122
127 # get module name from __file__ 123 # get module name from __file__
128 if (not isinstance(frame, profile.Profile.fake_frame) and 124 if (not isinstance(frame, profile.Profile.fake_frame) and
129 '__file__' in frame.f_globals): 125 '__file__' in frame.f_globals):
130 root, ext = os.path.splitext(frame.f_globals['__file__']) 126 root, ext = os.path.splitext(frame.f_globals['__file__'])
152 """ 148 """
153 if self.cur and frame.f_back is not self.cur[-2]: 149 if self.cur and frame.f_back is not self.cur[-2]:
154 rpt, rit, ret, rfn, rframe, rcur = self.cur 150 rpt, rit, ret, rfn, rframe, rcur = self.cur
155 if not isinstance(rframe, profile.Profile.fake_frame): 151 if not isinstance(rframe, profile.Profile.fake_frame):
156 assert rframe.f_back is frame.f_back, ("Bad call", rfn, 152 assert rframe.f_back is frame.f_back, ("Bad call", rfn,
153 # secok
157 rframe, rframe.f_back, 154 rframe, rframe.f_back,
158 frame, frame.f_back) 155 frame, frame.f_back)
159 self.trace_dispatch_return(rframe, 0) 156 self.trace_dispatch_return(rframe, 0)
160 assert (self.cur is None or 157 assert (self.cur is None or # secok
161 frame.f_back is self.cur[-2]), ("Bad call", 158 frame.f_back is self.cur[-2]), ("Bad call",
162 self.cur[-3]) 159 self.cur[-3])
163 fcode = frame.f_code 160 fcode = frame.f_code
164 fn = (self.fix_frame_filename(frame), 161 fn = (self.fix_frame_filename(frame),
165 fcode.co_firstlineno, fcode.co_name) 162 fcode.co_firstlineno, fcode.co_name)
179 "c_call": profile.Profile.trace_dispatch_c_call, 176 "c_call": profile.Profile.trace_dispatch_c_call,
180 "c_exception": profile.Profile.trace_dispatch_return, 177 "c_exception": profile.Profile.trace_dispatch_return,
181 # the C function returned 178 # the C function returned
182 "c_return": profile.Profile.trace_dispatch_return, 179 "c_return": profile.Profile.trace_dispatch_return,
183 } 180 }
184
185 #
186 # eflag: noqa = M702

eric ide

mercurial