~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to profile_imports.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-19 14:40:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1794.
  • Revision ID: john@arbash-meinel.com-20060619144019-873a4a8d252f7896
Refactor import stuff into separate functions. Update news

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import time
23
23
 
24
24
 
25
 
import_logfile = sys.stderr
26
 
compile_logfile = sys.stderr
27
 
 
28
 
 
29
25
_parent_stack = []
30
26
_total_stack = {}
31
27
_info = {}
100
96
def timed_import(name, globals, locals, fromlist):
101
97
    """Wrap around standard importer to log import time"""
102
98
 
103
 
    if import_logfile is None:
104
 
        return _real_import(name, globals, locals, fromlist)
105
 
 
106
99
    scope_name = globals.get('__name__', None)
107
100
    if scope_name is None:
108
101
        scope_name = globals.get('__file__', None)
148
141
 
149
142
def timed_compile(*args, **kwargs):
150
143
    """Log how long it takes to compile a regex"""
151
 
    if compile_logfile is None:
152
 
        return _real_compile(*args, **kwargs)
153
144
 
154
145
    # And who is requesting this?
155
146
    frame = sys._getframe(2)
167
158
        stack_finish(this, tcompile)
168
159
 
169
160
    return comp
 
161
 
 
162
 
 
163
def install():
 
164
    """Install the hooks for measuring import and regex compile time."""
 
165
    __builtins__['__import__'] = timed_import
 
166
    sre._compile = timed_compile
 
167
 
 
168
 
 
169
def uninstall():
 
170
    """Remove the import and regex compile timing hooks."""
 
171
    __builtins__['__import__'] = _real_import
 
172
    sre._compile = _real_compile
 
173