##// END OF EJS Templates
monkeypatch inspect.findsource in a context manager...
MinRK -
r16441:ba676161
parent child
Show More
@@ -83,6 +83,7 Inheritance diagram:
83 from __future__ import unicode_literals
83 from __future__ import unicode_literals
84 from __future__ import print_function
84 from __future__ import print_function
85
85
86 from contextlib import contextmanager
86 import inspect
87 import inspect
87 import keyword
88 import keyword
88 import linecache
89 import linecache
@@ -220,8 +221,23 def findsource(object):
220 return lines, lnum
221 return lines, lnum
221 raise IOError('could not find code object')
222 raise IOError('could not find code object')
222
223
223 # Monkeypatch inspect to apply our bugfix. This code only works with Python >= 2.5
224 # Monkeypatch inspect to apply our bugfix.
224 inspect.findsource = findsource
225 @contextmanager
226 def patch_inspect():
227 """context manager for monkeypatching inspect.findsource"""
228 save_findsource = inspect.findsource
229 inspect.findsource = findsource
230 try:
231 yield
232 finally:
233 inspect.findsource = save_findsource
234
235 def with_patch_inspect(f):
236 """decorator for monkeypatching inspect.findsource"""
237 def wrapped(*args, **kwargs):
238 with patch_inspect():
239 return f(*args, **kwargs)
240 return wrapped
225
241
226 def fix_frame_records_filenames(records):
242 def fix_frame_records_filenames(records):
227 """Try to fix the filenames in each record from inspect.getinnerframes().
243 """Try to fix the filenames in each record from inspect.getinnerframes().
@@ -243,6 +259,7 def fix_frame_records_filenames(records):
243 return fixed_records
259 return fixed_records
244
260
245
261
262 @with_patch_inspect
246 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
263 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
247 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
264 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
248
265
General Comments 0
You need to be logged in to leave comments. Login now