##// END OF EJS Templates
embed(): Default to the future compile flags of the calling frame.
Bradley M. Froehle -
Show More
@@ -28,13 +28,26 b' Authors'
28 from __future__ import print_function
28 from __future__ import print_function
29
29
30 # Stdlib imports
30 # Stdlib imports
31 import __future__
31 from ast import PyCF_ONLY_AST
32 from ast import PyCF_ONLY_AST
32 import codeop
33 import codeop
34 import functools
33 import hashlib
35 import hashlib
34 import linecache
36 import linecache
37 import operator
35 import time
38 import time
36
39
37 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Constants
42 #-----------------------------------------------------------------------------
43
44 # Roughtly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
45 # this is used as a bitmask to extract future-related code flags.
46 PyCF_MASK = functools.reduce(operator.or_,
47 (getattr(__future__, fname).compiler_flag
48 for fname in __future__.all_feature_names))
49
50 #-----------------------------------------------------------------------------
38 # Local utilities
51 # Local utilities
39 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
40
53
@@ -34,7 +34,7 b' try:'
34 except:
34 except:
35 from IPython.utils.nested_context import nested
35 from IPython.utils.nested_context import nested
36
36
37 from IPython.core import ultratb
37 from IPython.core import ultratb, compilerop
38 from IPython.core.magic import Magics, magics_class, line_magic
38 from IPython.core.magic import Magics, magics_class, line_magic
39 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
39 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
40 from IPython.frontend.terminal.ipapp import load_default_config
40 from IPython.frontend.terminal.ipapp import load_default_config
@@ -162,7 +162,7 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
162 print self.exit_msg
162 print self.exit_msg
163
163
164 def mainloop(self, local_ns=None, module=None, stack_depth=0,
164 def mainloop(self, local_ns=None, module=None, stack_depth=0,
165 display_banner=None, global_ns=None):
165 display_banner=None, global_ns=None, compile_flags=None):
166 """Embeds IPython into a running python program.
166 """Embeds IPython into a running python program.
167
167
168 Input:
168 Input:
@@ -180,6 +180,11 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
180 the namespace from the intended level in the stack. By default (0)
180 the namespace from the intended level in the stack. By default (0)
181 it will get its locals and globals from the immediate caller.
181 it will get its locals and globals from the immediate caller.
182
182
183 - compile_flags: A bit field identifying the __future__ features
184 that are enabled, as passed to the builtin `compile` function. If
185 given as None, they are automatically taken from the scope where the
186 shell was called.
187
183 Warning: it's possible to use this in a program which is being run by
188 Warning: it's possible to use this in a program which is being run by
184 IPython itself (via %run), but some funny things will happen (a few
189 IPython itself (via %run), but some funny things will happen (a few
185 globals get overwritten). In the future this will be cleaned up, as
190 globals get overwritten). In the future this will be cleaned up, as
@@ -202,11 +207,15 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
202 if module is None:
207 if module is None:
203 global_ns = call_frame.f_globals
208 global_ns = call_frame.f_globals
204 module = sys.modules[global_ns['__name__']]
209 module = sys.modules[global_ns['__name__']]
210 if compile_flags is None:
211 compile_flags = (call_frame.f_code.co_flags &
212 compilerop.PyCF_MASK)
205
213
206 # Save original namespace and module so we can restore them after
214 # Save original namespace and module so we can restore them after
207 # embedding; otherwise the shell doesn't shut down correctly.
215 # embedding; otherwise the shell doesn't shut down correctly.
208 orig_user_module = self.user_module
216 orig_user_module = self.user_module
209 orig_user_ns = self.user_ns
217 orig_user_ns = self.user_ns
218 orig_compile_flags = self.compile.flags
210
219
211 # Update namespaces and fire up interpreter
220 # Update namespaces and fire up interpreter
212
221
@@ -222,6 +231,9 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
222 self.user_ns = local_ns
231 self.user_ns = local_ns
223 self.init_user_ns()
232 self.init_user_ns()
224
233
234 # Compiler flags
235 self.compile.flags = compile_flags
236
225 # Patch for global embedding to make sure that things don't overwrite
237 # Patch for global embedding to make sure that things don't overwrite
226 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
238 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
227 # FIXME. Test this a bit more carefully (the if.. is new)
239 # FIXME. Test this a bit more carefully (the if.. is new)
@@ -247,6 +259,7 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
247 # Restore original namespace so shell can shut down when we exit.
259 # Restore original namespace so shell can shut down when we exit.
248 self.user_module = orig_user_module
260 self.user_module = orig_user_module
249 self.user_ns = orig_user_ns
261 self.user_ns = orig_user_ns
262 self.compile.flags = orig_compile_flags
250
263
251 _embedded_shell = None
264 _embedded_shell = None
252
265
General Comments 0
You need to be logged in to leave comments. Login now