Skip to content

Commit

Permalink
Merge pull request #5712 from minrk/inspect.getsource-context
Browse files Browse the repository at this point in the history
monkeypatch inspect.findsource in a context manager
  • Loading branch information
takluyver committed Apr 25, 2014
2 parents a7c5cad + 3aa6121 commit 3361b7e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions IPython/core/ultratb.py
Expand Up @@ -220,8 +220,17 @@ def findsource(object):
return lines, lnum
raise IOError('could not find code object')

# Monkeypatch inspect to apply our bugfix. This code only works with Python >= 2.5
inspect.findsource = findsource
# Monkeypatch inspect to apply our bugfix.
def with_patch_inspect(f):
"""decorator for monkeypatching inspect.findsource"""
def wrapped(*args, **kwargs):
save_findsource = inspect.findsource
inspect.findsource = findsource
try:
return f(*args, **kwargs)
finally:
inspect.findsource = save_findsource
return wrapped

def fix_frame_records_filenames(records):
"""Try to fix the filenames in each record from inspect.getinnerframes().
Expand All @@ -243,6 +252,7 @@ def fix_frame_records_filenames(records):
return fixed_records


@with_patch_inspect
def _fixed_getinnerframes(etb, context=1,tb_offset=0):
LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5

Expand Down

0 comments on commit 3361b7e

Please sign in to comment.