From 06edb0ca99fd9704120ac40373d75a59a9e45ce3 2013-01-17 22:14:09 From: Thomas Kluyver Date: 2013-01-17 22:14:09 Subject: [PATCH] Better support compiling cells with separate __future__ environments --- diff --git a/IPython/core/compilerop.py b/IPython/core/compilerop.py index ee0de31..e39ded6 100644 --- a/IPython/core/compilerop.py +++ b/IPython/core/compilerop.py @@ -90,7 +90,7 @@ class CachingCompiler(codeop.Compile): # Now, we must monkeypatch the linecache directly so that parts of the # stdlib that call it outside our control go through our codepath # (otherwise we'd lose our tracebacks). - linecache.checkcache = self.check_cache + linecache.checkcache = check_linecache_ipython def ast_parse(self, source, filename='', symbol='exec'): """Parse code to an AST with the current compiler flags active. @@ -134,11 +134,11 @@ class CachingCompiler(codeop.Compile): linecache._ipython_cache[name] = entry return name - def check_cache(self, *args): - """Call linecache.checkcache() safely protecting our cached values. - """ - # First call the orignal checkcache as intended - linecache._checkcache_ori(*args) - # Then, update back the cache with our data, so that tracebacks related - # to our compiled codes can be produced. - linecache.cache.update(linecache._ipython_cache) +def check_linecache_ipython(*args): + """Call linecache.checkcache() safely protecting our cached values. + """ + # First call the orignal checkcache as intended + linecache._checkcache_ori(*args) + # Then, update back the cache with our data, so that tracebacks related + # to our compiled codes can be produced. + linecache.cache.update(linecache._ipython_cache) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index f9fdc55..bc0b4f8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -41,7 +41,7 @@ from IPython.core import ultratb from IPython.core.alias import AliasManager, AliasError from IPython.core.autocall import ExitAutocall from IPython.core.builtin_trap import BuiltinTrap -from IPython.core.compilerop import CachingCompiler +from IPython.core.compilerop import CachingCompiler, check_linecache_ipython from IPython.core.display_trap import DisplayTrap from IPython.core.displayhook import DisplayHook from IPython.core.displaypub import DisplayPublisher @@ -1532,7 +1532,7 @@ class InteractiveShell(SingletonConfigurable): self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', color_scheme='NoColor', tb_offset = 1, - check_cache=self.compile.check_cache) + check_cache=check_linecache_ipython) # The instance will store a pointer to the system-wide exception hook, # so that runtime code (such as magics) can access it. This is because @@ -2587,12 +2587,7 @@ class InteractiveShell(SingletonConfigurable): # Our own compiler remembers the __future__ environment. If we want to # run code with a separate __future__ environment, use the default # compiler - if shell_futures: - compiler = self.compile - ast_parse = self.compile.ast_parse - else: - compiler = compile - ast_parse = ast.parse + compiler = self.compile if shell_futures else CachingCompiler() with self.builtin_trap: prefilter_failed = False @@ -2622,7 +2617,7 @@ class InteractiveShell(SingletonConfigurable): with self.display_trap: try: - code_ast = ast_parse(cell, filename=cell_name) + code_ast = compiler.ast_parse(cell, filename=cell_name) except IndentationError: self.showindentationerror() if store_history: diff --git a/IPython/core/tests/test_compilerop.py b/IPython/core/tests/test_compilerop.py index f586c33..125a567 100644 --- a/IPython/core/tests/test_compilerop.py +++ b/IPython/core/tests/test_compilerop.py @@ -67,7 +67,7 @@ def test_compiler_check_cache(): cp = compilerop.CachingCompiler() cp.cache('x=1', 99) # Ensure now that after clearing the cache, our entries survive - cp.check_cache() + linecache.checkcache() for k in linecache.cache: if k.startswith('