/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: Jelmer Vernooij
  • Date: 2018-11-16 18:35:30 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7178.
  • Revision ID: jelmer@jelmer.uk-20181116183530-ue8yx60h5tinl2wy
Merge more-cleanups.

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
 
import re
25
 
 
26
 
 
27
27
_parent_stack = []
28
28
_total_stack = {}
29
29
_info = {}
62
62
def log_stack_info(out_file, sorted=True, hide_fast=True):
63
63
    # Find all of the roots with import = 0
64
64
    out_file.write(
65
 
        '%5s %5s %-40s @ %s:%s\n' % ('cum', 'inline', 'name', 'file', 'line'))
66
 
    todo = [(value[-1], key) for key, value in _info.iteritems()
67
 
            if value[0] == 0]
 
65
        '%5s %5s %-40s @ %s:%s\n'
 
66
        % ('cum', 'local', 'name', 'file', 'line'))
 
67
    todo = [(value[-1], key) for key, value in _info.items() if value[0] == 0]
68
68
 
69
69
    if sorted:
70
70
        todo.sort()
102
102
 
103
103
_real_import = __import__
104
104
 
105
 
def timed_import(name, globals=None, locals=None, fromlist=None, level=-1):
 
105
def timed_import(name, globals=None, locals=None, fromlist=None, level=0):
106
106
    """Wrap around standard importer to log import time"""
107
107
    # normally there are 4, but if this is called as __import__ eg by
108
108
    # /usr/lib/python2.6/email/__init__.py then there may be only one
109
109
    # parameter
110
 
    # level is only passed by python2.6
 
110
    # level has different default between Python 2 and 3, but codebase
 
111
    # uses `from __future__ import absolute_import` so can just use 0.
111
112
 
112
113
    if globals is None:
113
114
        # can't determine the scope name afaics; we could peek up the stack to
124
125
            loc = scope_name.find('breezy')
125
126
            if loc != -1:
126
127
                scope_name = scope_name[loc:]
127
 
            # For stdlib, trim out early paths
128
 
            loc = scope_name.find('python2.4')
129
 
            if loc != -1:
130
 
                scope_name = scope_name[loc:]
131
128
 
132
129
    # Figure out the frame that is doing the importing
133
130
    frame = sys._getframe(1)