Show More
@@ -28,13 +28,26 b' Authors' | |||
|
28 | 28 | from __future__ import print_function |
|
29 | 29 | |
|
30 | 30 | # Stdlib imports |
|
31 | import __future__ | |
|
31 | 32 | from ast import PyCF_ONLY_AST |
|
32 | 33 | import codeop |
|
34 | import functools | |
|
33 | 35 | import hashlib |
|
34 | 36 | import linecache |
|
37 | import operator | |
|
35 | 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 | 51 | # Local utilities |
|
39 | 52 | #----------------------------------------------------------------------------- |
|
40 | 53 |
@@ -34,7 +34,7 b' try:' | |||
|
34 | 34 | except: |
|
35 | 35 | from IPython.utils.nested_context import nested |
|
36 | 36 | |
|
37 | from IPython.core import ultratb | |
|
37 | from IPython.core import ultratb, compilerop | |
|
38 | 38 | from IPython.core.magic import Magics, magics_class, line_magic |
|
39 | 39 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell |
|
40 | 40 | from IPython.frontend.terminal.ipapp import load_default_config |
@@ -162,7 +162,7 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||
|
162 | 162 | print self.exit_msg |
|
163 | 163 | |
|
164 | 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 | 166 | """Embeds IPython into a running python program. |
|
167 | 167 | |
|
168 | 168 | Input: |
@@ -180,6 +180,11 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||
|
180 | 180 | the namespace from the intended level in the stack. By default (0) |
|
181 | 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 | 188 | Warning: it's possible to use this in a program which is being run by |
|
184 | 189 | IPython itself (via %run), but some funny things will happen (a few |
|
185 | 190 | globals get overwritten). In the future this will be cleaned up, as |
@@ -202,11 +207,15 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||
|
202 | 207 | if module is None: |
|
203 | 208 | global_ns = call_frame.f_globals |
|
204 | 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 | 214 | # Save original namespace and module so we can restore them after |
|
207 | 215 | # embedding; otherwise the shell doesn't shut down correctly. |
|
208 | 216 | orig_user_module = self.user_module |
|
209 | 217 | orig_user_ns = self.user_ns |
|
218 | orig_compile_flags = self.compile.flags | |
|
210 | 219 | |
|
211 | 220 | # Update namespaces and fire up interpreter |
|
212 | 221 | |
@@ -222,6 +231,9 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||
|
222 | 231 | self.user_ns = local_ns |
|
223 | 232 | self.init_user_ns() |
|
224 | 233 | |
|
234 | # Compiler flags | |
|
235 | self.compile.flags = compile_flags | |
|
236 | ||
|
225 | 237 | # Patch for global embedding to make sure that things don't overwrite |
|
226 | 238 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
227 | 239 | # FIXME. Test this a bit more carefully (the if.. is new) |
@@ -247,6 +259,7 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||
|
247 | 259 | # Restore original namespace so shell can shut down when we exit. |
|
248 | 260 | self.user_module = orig_user_module |
|
249 | 261 | self.user_ns = orig_user_ns |
|
262 | self.compile.flags = orig_compile_flags | |
|
250 | 263 | |
|
251 | 264 | _embedded_shell = None |
|
252 | 265 |
General Comments 0
You need to be logged in to leave comments.
Login now