eric6/DebugClients/Python/PyProfile.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8243
cc717c2ae956
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
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 # secok 13 import pickle # secok
14 import contextlib
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.
49 Private method to restore the timing data from the timing cache. 50 Private method to restore the timing data from the timing cache.
50 """ 51 """
51 if not os.path.exists(self.timingCache): 52 if not os.path.exists(self.timingCache):
52 return 53 return
53 54
54 try: 55 with contextlib.suppress(OSError, EOFError, ValueError, TypeError), \
55 with open(self.timingCache, 'rb') as cache: 56 open(self.timingCache, 'rb') as cache:
56 timings = marshal.load(cache) # secok 57 timings = marshal.load(cache) # secok
57 if isinstance(timings, dict): 58 if isinstance(timings, dict):
58 self.timings = timings 59 self.timings = timings
59 except (OSError, EOFError, ValueError, TypeError):
60 pass
61 60
62 def save(self): 61 def save(self):
63 """ 62 """
64 Public method to store the collected profile data. 63 Public method to store the collected profile data.
65 """ 64 """
66 # dump the raw timing data 65 # dump the raw timing data
67 try: 66 with contextlib.suppress(OSError), \
68 with open(self.timingCache, 'wb') as cache: 67 open(self.timingCache, 'wb') as cache:
69 marshal.dump(self.timings, cache) 68 marshal.dump(self.timings, cache)
70 except OSError:
71 pass
72 69
73 # dump the profile data 70 # dump the profile data
74 self.dump_stats(self.profileCache) 71 self.dump_stats(self.profileCache)
75 72
76 def dump_stats(self, file): 73 def dump_stats(self, file):
78 Public method to dump the statistics data. 75 Public method to dump the statistics data.
79 76
80 @param file name of the file to write to (string) 77 @param file name of the file to write to (string)
81 """ 78 """
82 self.create_stats() 79 self.create_stats()
83 try: 80 with contextlib.suppress(OSError, pickle.PickleError), \
84 with open(file, 'wb') as f: 81 open(file, 'wb') as f:
85 pickle.dump(self.stats, f, 4) 82 pickle.dump(self.stats, f, 4)
86 except (OSError, pickle.PickleError):
87 pass
88 83
89 def erase(self): 84 def erase(self):
90 """ 85 """
91 Public method to erase the collected timing data. 86 Public method to erase the collected timing data.
92 """ 87 """
141 @return flag indicating a successful handling (boolean) 136 @return flag indicating a successful handling (boolean)
142 """ 137 """
143 if self.cur and frame.f_back is not self.cur[-2]: 138 if self.cur and frame.f_back is not self.cur[-2]:
144 rpt, rit, ret, rfn, rframe, rcur = self.cur 139 rpt, rit, ret, rfn, rframe, rcur = self.cur
145 if not isinstance(rframe, profile.Profile.fake_frame): 140 if not isinstance(rframe, profile.Profile.fake_frame):
146 assert rframe.f_back is frame.f_back, ("Bad call", rfn, 141 assert rframe.f_back is frame.f_back, ( # secok
147 # secok 142 "Bad call", rfn, rframe, rframe.f_back,
148 rframe, rframe.f_back, 143 frame, frame.f_back)
149 frame, frame.f_back)
150 self.trace_dispatch_return(rframe, 0) 144 self.trace_dispatch_return(rframe, 0)
151 assert (self.cur is None or # secok 145 assert (self.cur is None or # secok
152 frame.f_back is self.cur[-2]), ("Bad call", 146 frame.f_back is self.cur[-2]), ("Bad call",
153 self.cur[-3]) 147 self.cur[-3])
154 fcode = frame.f_code 148 fcode = frame.f_code
155 fn = (self.fix_frame_filename(frame), 149 fn = (self.fix_frame_filename(frame),
156 fcode.co_firstlineno, fcode.co_name) 150 fcode.co_firstlineno, fcode.co_name)

eric ide

mercurial