~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to profile_imports.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-10 17:52:08 UTC
  • mfrom: (5021 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5023.
  • Revision ID: john@arbash-meinel.com-20100210175208-bubuwav4uqigu291
Merge bzr.dev 5021 to resolve NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006, 2008, 2009, 2010 by Canonical Ltd
2
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
20
 
import sre
21
20
import sys
22
21
import time
23
22
 
24
23
 
 
24
if sys.version_info < (2, 5, 0):
 
25
    import sre
 
26
    re = sre
 
27
else:
 
28
    import re
 
29
 
 
30
 
25
31
_parent_stack = []
26
32
_total_stack = {}
27
33
_info = {}
96
102
 
97
103
_real_import = __import__
98
104
 
99
 
def timed_import(name, globals, locals, fromlist):
 
105
def timed_import(name, globals, locals, fromlist, level=None):
100
106
    """Wrap around standard importer to log import time"""
 
107
    # level is only passed by python2.6
101
108
 
102
109
    scope_name = globals.get('__name__', None)
103
110
    if scope_name is None:
145
152
    return mod
146
153
 
147
154
 
148
 
_real_compile = sre._compile
 
155
_real_compile = re._compile
 
156
 
149
157
 
150
158
def timed_compile(*args, **kwargs):
151
159
    """Log how long it takes to compile a regex"""
177
185
def install():
178
186
    """Install the hooks for measuring import and regex compile time."""
179
187
    __builtins__['__import__'] = timed_import
180
 
    sre._compile = timed_compile
 
188
    re._compile = timed_compile
181
189
 
182
190
 
183
191
def uninstall():
184
192
    """Remove the import and regex compile timing hooks."""
185
193
    __builtins__['__import__'] = _real_import
186
 
    sre._compile = _real_compile
 
194
    re._compile = _real_compile
187
195