##// END OF EJS Templates
Parse user code to AST using compiler flags....
Thomas Kluyver -
Show More
@@ -28,6 +28,7 b' Authors'
28 28 from __future__ import print_function
29 29
30 30 # Stdlib imports
31 from ast import PyCF_ONLY_AST
31 32 import codeop
32 33 import hashlib
33 34 import linecache
@@ -78,6 +79,16 b' class CachingCompiler(codeop.Compile):'
78 79 # (otherwise we'd lose our tracebacks).
79 80 linecache.checkcache = self.check_cache
80 81
82 def ast_parse(self, source, filename='<unknown>', symbol='exec'):
83 """Parse code to an AST with the current compiler flags active."""
84 return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
85
86 def reset_compiler_flags(self):
87 """Reset compiler flags to default state."""
88 # This value is copied from codeop.Compile.__init__, so if that ever
89 # changes, it will need to be updated.
90 self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
91
81 92 @property
82 93 def compiler_flags(self):
83 94 """Flags currently active in the compilation process.
@@ -2281,7 +2281,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
2281 2281
2282 2282 with self.display_trap:
2283 2283 try:
2284 code_ast = ast.parse(cell, filename=cell_name)
2284 code_ast = self.compile.ast_parse(cell, filename=cell_name)
2285 2285 except IndentationError:
2286 2286 self.showindentationerror()
2287 2287 self.execution_count += 1
@@ -122,3 +122,14 b' class InteractiveShellTestCase(unittest.TestCase):'
122 122 import IPython.core.formatters
123 123 f = IPython.core.formatters.PlainTextFormatter()
124 124 f([Spam(),Spam()])
125
126 def test_future_flags(self):
127 """Check that future flags are used for parsing code (gh-777)"""
128 ip = get_ipython()
129 ip.run_cell('from __future__ import print_function')
130 try:
131 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
132 assert 'prfunc_return_val' in ip.user_ns
133 finally:
134 # Reset compiler flags so we don't mess up other tests.
135 ip.compile.reset_compiler_flags()
General Comments 0
You need to be logged in to leave comments. Login now