/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lsprof.py

  • Committer: Jelmer Vernooij
  • Date: 2017-11-21 00:06:54 UTC
  • mfrom: (6816.3.4 py3_lsprof)
  • Revision ID: jelmer@jelmer.uk-20171121000654-74rn9g7pondybwcc
Merge lp:~jelmer/brz/py3_lsprof.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
from __future__ import absolute_import
7
7
 
8
 
import cPickle
 
8
import codecs
 
9
try:
 
10
    import cPickle as pickle
 
11
except ImportError:
 
12
    import pickle
 
13
import operator
9
14
import os
10
15
import sys
11
 
import thread
 
16
try:
 
17
    import _thread
 
18
except ImportError:
 
19
    import thread as _thread
12
20
import threading
13
21
from _lsprof import Profiler, profiler_entry
14
22
 
107
115
    def _thread_profile(self, f, *args, **kwds):
108
116
        # we lose the first profile point for a new thread in order to
109
117
        # trampoline a new Profile object into place
110
 
        thr = thread.get_ident()
 
118
        thr = _thread.get_ident()
111
119
        self._g_threadmap[thr] = p = Profiler()
112
120
        # this overrides our sys.setprofile hook:
113
121
        p.enable(subcalls=True, builtins=True)
125
133
        self.data = data
126
134
        self.threads = threads
127
135
 
128
 
    def sort(self, crit="inlinetime"):
 
136
    def sort(self, crit="inlinetime", reverse=True):
129
137
        """Sort the data by the supplied critera.
130
138
 
131
139
        :param crit: the data attribute used as the sort key."""
132
 
        if crit not in profiler_entry.__dict__:
 
140
        if crit not in profiler_entry.__dict__ or crit == 'code':
133
141
            raise ValueError("Can't sort by %s" % crit)
134
 
        self.data.sort(lambda b, a: cmp(getattr(a, crit),
135
 
                                        getattr(b, crit)))
 
142
 
 
143
        key_func = operator.attrgetter(crit)
 
144
        self.data.sort(key=key_func, reverse=reverse)
 
145
 
136
146
        for e in self.data:
137
147
            if e.calls:
138
 
                e.calls.sort(lambda b, a: cmp(getattr(a, crit),
139
 
                                              getattr(b, crit)))
 
148
                e.calls.sort(key=key_func, reverse=reverse)
140
149
 
141
150
    def pprint(self, top=None, file=None):
142
151
        """Pretty-print the data as plain text for human consumption.
204
213
                ext = os.path.splitext(filename)[1]
205
214
                if len(ext) > 1:
206
215
                    format = ext[1:]
207
 
        outfile = open(filename, 'wb')
208
 
        try:
 
216
        with open(filename, 'wb') as outfile:
209
217
            if format == "callgrind":
210
 
                self.calltree(outfile)
 
218
                # The callgrind format states it is 'ASCII based':
 
219
                # <http://valgrind.org/docs/manual/cl-format.html>
 
220
                # But includes filenames so lets ignore and use UTF-8.
 
221
                self.calltree(codecs.getwriter('utf-8')(outfile))
211
222
            elif format == "txt":
212
 
                self.pprint(file=outfile)
 
223
                self.pprint(file=codecs.getwriter('utf-8')(outfile))
213
224
            else:
214
225
                self.freeze()
215
 
                cPickle.dump(self, outfile, 2)
216
 
        finally:
217
 
            outfile.close()
 
226
                pickle.dump(self, outfile, 2)
218
227
 
219
228
 
220
229
class _CallTreeFilter(object):
314
323
        return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
315
324
 
316
325
 
317
 
if __name__ == '__main__':
318
 
    import os
 
326
def main():
319
327
    sys.argv = sys.argv[1:]
320
328
    if not sys.argv:
321
329
        sys.stderr.write("usage: lsprof.py <script> <arguments...>\n")
322
330
        sys.exit(2)
323
 
    sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
324
 
    stats = sorted(profile(execfile, sys.argv[0], globals(), locals()))
 
331
    import runpy
 
332
    result, stats = profile(runpy.run_path, sys.argv[0], run_name='__main__')
 
333
    stats.sort()
325
334
    stats.pprint()
 
335
 
 
336
 
 
337
if __name__ == '__main__':
 
338
    main()