Skip to content
This repository

Parse user code to AST using compiler flags. #784

Merged
merged 2 commits into from over 2 years ago

2 participants

Thomas Kluyver Fernando Perez
Thomas Kluyver
Collaborator

Closes gh-777.

IPython/core/compilerop.py
... ...
@@ -77,6 +78,16 @@ class CachingCompiler(codeop.Compile):
77 78
         # stdlib that call it outside our control go through our codepath
78 79
         # (otherwise we'd lose our tracebacks).
79 80
         linecache.checkcache = self.check_cache
  81
+        
  82
+    def ast_parse(self, source, filename='<unknown>', symbol='exec'):
  83
+        """Parse code to an AST with the current compiler flags active."""
1
Fernando Perez Owner
fperez added a note September 12, 2011

Indicate in the docstring that all arguments are the same as those passed to the builtin compile() call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

Other than my tiny inline comment, this looks perfect. Once that's fixed, go ahead and merge. Thanks!

Thomas Kluyver takluyver merged commit ca3e756 into from September 12, 2011
Thomas Kluyver takluyver closed this September 12, 2011
Thomas Kluyver
Collaborator

Rebased and merged. Thanks, Fernando.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
14  IPython/core/compilerop.py
@@ -28,6 +28,7 @@
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
@@ -77,6 +78,19 @@ def __init__(self):
77 78
         # stdlib that call it outside our control go through our codepath
78 79
         # (otherwise we'd lose our tracebacks).
79 80
         linecache.checkcache = self.check_cache
  81
+        
  82
+    def ast_parse(self, source, filename='<unknown>', symbol='exec'):
  83
+        """Parse code to an AST with the current compiler flags active.
  84
+        
  85
+        Arguments are exactly the same as ast.parse (in the standard library),
  86
+        and are passed to the built-in compile function."""
  87
+        return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
  88
+    
  89
+    def reset_compiler_flags(self):
  90
+        """Reset compiler flags to default state."""
  91
+        # This value is copied from codeop.Compile.__init__, so if that ever
  92
+        # changes, it will need to be updated.
  93
+        self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
80 94
 
81 95
     @property
82 96
     def compiler_flags(self):
2  IPython/core/interactiveshell.py
@@ -2281,7 +2281,7 @@ def run_cell(self, raw_cell, store_history=True):
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
11  IPython/core/tests/test_interactiveshell.py
@@ -122,3 +122,14 @@ def __repr__(self):
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()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.