~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to profile_imports.py

  • Committer: Martin Pool
  • Date: 2010-02-07 16:45:17 UTC
  • mto: This revision was merged to the branch mainline in revision 5026.
  • Revision ID: mbp@canoncial.com-20100207164517-9i4s0c407fcz2f5o
timed_import copes with only one argument, eg when called as __import__(name)

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
 
103
103
_real_import = __import__
104
104
 
105
 
def timed_import(name, globals, locals, fromlist, level=None):
 
105
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
106
106
    """Wrap around standard importer to log import time"""
 
107
    # normally there are 4, but if this is called as __import__ eg by
 
108
    # /usr/lib/python2.6/email/__init__.py then there may be only one
 
109
    # parameter
107
110
    # level is only passed by python2.6
108
111
 
109
 
    scope_name = globals.get('__name__', None)
110
 
    if scope_name is None:
111
 
        scope_name = globals.get('__file__', None)
112
 
    if scope_name is None:
113
 
        scope_name = globals.keys()
 
112
    if globals is None:
 
113
        # can't determine the scope name afaics; we could peek up the stack to
 
114
        # see where this is being called from, but it should be a rare case.
 
115
        scope_name = None
114
116
    else:
115
 
        # Trim out paths before bzrlib
116
 
        loc = scope_name.find('bzrlib')
117
 
        if loc != -1:
118
 
            scope_name = scope_name[loc:]
119
 
        # For stdlib, trim out early paths
120
 
        loc = scope_name.find('python2.4')
121
 
        if loc != -1:
122
 
            scope_name = scope_name[loc:]
 
117
        scope_name = globals.get('__name__', None)
 
118
        if scope_name is None:
 
119
            scope_name = globals.get('__file__', None)
 
120
        if scope_name is None:
 
121
            scope_name = globals.keys()
 
122
        else:
 
123
            # Trim out paths before bzrlib
 
124
            loc = scope_name.find('bzrlib')
 
125
            if loc != -1:
 
126
                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:]
123
131
 
124
132
    # Figure out the frame that is doing the importing
125
133
    frame = sys._getframe(1)