/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 profile_imports.py

  • Committer: Martin
  • Date: 2018-11-16 16:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7172.
  • Revision ID: gzlist@googlemail.com-20181116163822-yg1h1cdng6w7w9kn
Make --profile-imports work on Python 3

Also tweak heading to line up correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
import re
20
23
import sys
21
24
import time
22
25
 
23
26
 
24
 
if sys.version_info < (2, 5, 0):
25
 
    import sre
26
 
    re = sre
27
 
else:
28
 
    import re
29
 
 
30
 
 
31
27
_parent_stack = []
32
28
_total_stack = {}
33
29
_info = {}
65
61
def log_stack_info(out_file, sorted=True, hide_fast=True):
66
62
    # Find all of the roots with import = 0
67
63
    out_file.write('%5s %5s %-40s @ %s:%s\n'
68
 
        % ('cum', 'inline', 'name', 'file', 'line'))
69
 
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
 
64
        % ('cum', 'local', 'name', 'file', 'line'))
 
65
    todo = [(value[-1], key) for key,value in _info.items() if value[0] == 0]
70
66
 
71
67
    if sorted:
72
68
        todo.sort()
104
100
 
105
101
_real_import = __import__
106
102
 
107
 
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
 
103
def timed_import(name, globals=None, locals=None, fromlist=None, level=0):
108
104
    """Wrap around standard importer to log import time"""
109
105
    # normally there are 4, but if this is called as __import__ eg by
110
106
    # /usr/lib/python2.6/email/__init__.py then there may be only one
111
107
    # parameter
112
 
    # level is only passed by python2.6
 
108
    # level has different default between Python 2 and 3, but codebase
 
109
    # uses `from __future__ import absolute_import` so can just use 0.
113
110
 
114
111
    if globals is None:
115
112
        # can't determine the scope name afaics; we could peek up the stack to
122
119
        if scope_name is None:
123
120
            scope_name = globals.keys()
124
121
        else:
125
 
            # Trim out paths before bzrlib
126
 
            loc = scope_name.find('bzrlib')
127
 
            if loc != -1:
128
 
                scope_name = scope_name[loc:]
129
 
            # For stdlib, trim out early paths
130
 
            loc = scope_name.find('python2.4')
 
122
            # Trim out paths before breezy
 
123
            loc = scope_name.find('breezy')
131
124
            if loc != -1:
132
125
                scope_name = scope_name[loc:]
133
126
 
154
147
    tstart = _timer()
155
148
    try:
156
149
        # Do the import
157
 
        mod = _real_import(name, globals, locals, fromlist)
 
150
        return _real_import(name, globals, locals, fromlist, level=level)
158
151
    finally:
159
152
        tload = _timer()-tstart
160
153
        stack_finish(this, tload)
161
154
 
162
 
    return mod
 
155
 
 
156
def _repr_regexp(pattern, max_len=30):
 
157
    """Present regexp pattern for logging, truncating if over max_len."""
 
158
    if len(pattern) > max_len:
 
159
        return repr(pattern[:max_len-3]) + "..."
 
160
    return repr(pattern)
163
161
 
164
162
 
165
163
_real_compile = re._compile
179
177
        frame = sys._getframe(5)
180
178
        frame_name = frame.f_globals.get('__name__', '<unknown>')
181
179
    frame_lineno = frame.f_lineno
182
 
    this = stack_add(extra+repr(args[0]), frame_name, frame_lineno)
 
180
    this = stack_add(extra+_repr_regexp(args[0]), frame_name, frame_lineno)
183
181
 
184
182
    tstart = _timer()
185
183
    try: