From 3361b7eedc8cc4916ecba24981de3ee5bf35942f 2014-04-25 19:47:36
From: Thomas Kluyver <takowl@gmail.com>
Date: 2014-04-25 19:47:36
Subject: [PATCH] Merge pull request #5712 from minrk/inspect.getsource-context

monkeypatch inspect.findsource in a context manager
---

diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py
index 5b4eeac..b1cc185 100644
--- a/IPython/core/ultratb.py
+++ b/IPython/core/ultratb.py
@@ -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().
@@ -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