##// END OF EJS Templates
Add shell_futures parameter to run_cell to allow running code without sharing __future__ imports.
Thomas Kluyver -
Show More
@@ -14,7 +14,6 b''
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from __future__ import with_statement
18 from __future__ import absolute_import
17 from __future__ import absolute_import
19 from __future__ import print_function
18 from __future__ import print_function
20
19
@@ -2514,7 +2513,7 b' class InteractiveShell(SingletonConfigurable):'
2514 # raised in user code. It would be nice if there were
2513 # raised in user code. It would be nice if there were
2515 # versions of runlines, execfile that did raise, so
2514 # versions of runlines, execfile that did raise, so
2516 # we could catch the errors.
2515 # we could catch the errors.
2517 self.run_cell(thefile.read(), store_history=False)
2516 self.run_cell(thefile.read(), store_history=False, shell_futures=False)
2518 except:
2517 except:
2519 self.showtraceback()
2518 self.showtraceback()
2520 warn('Unknown failure executing file: <%s>' % fname)
2519 warn('Unknown failure executing file: <%s>' % fname)
@@ -2548,7 +2547,7 b' class InteractiveShell(SingletonConfigurable):'
2548 self._current_cell_magic_body = None
2547 self._current_cell_magic_body = None
2549 return self.run_cell_magic(magic_name, line, cell)
2548 return self.run_cell_magic(magic_name, line, cell)
2550
2549
2551 def run_cell(self, raw_cell, store_history=False, silent=False):
2550 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2552 """Run a complete IPython cell.
2551 """Run a complete IPython cell.
2553
2552
2554 Parameters
2553 Parameters
@@ -2562,6 +2561,11 b' class InteractiveShell(SingletonConfigurable):'
2562 silent : bool
2561 silent : bool
2563 If True, avoid side-effects, such as implicit displayhooks and
2562 If True, avoid side-effects, such as implicit displayhooks and
2564 and logging. silent=True forces store_history=False.
2563 and logging. silent=True forces store_history=False.
2564 shell_futures : bool
2565 If True, the code will share future statements with the interactive
2566 shell. It will both be affected by previous __future__ imports, and
2567 any __future__ imports in the code will affect the shell. If False,
2568 __future__ imports are not shared in either direction.
2565 """
2569 """
2566 if (not raw_cell) or raw_cell.isspace():
2570 if (not raw_cell) or raw_cell.isspace():
2567 return
2571 return
@@ -2579,6 +2583,16 b' class InteractiveShell(SingletonConfigurable):'
2579 self._current_cell_magic_body = \
2583 self._current_cell_magic_body = \
2580 ''.join(self.input_splitter.cell_magic_parts)
2584 ''.join(self.input_splitter.cell_magic_parts)
2581 cell = self.input_splitter.source_reset()
2585 cell = self.input_splitter.source_reset()
2586
2587 # Our own compiler remembers the __future__ environment. If we want to
2588 # run code with a separate __future__ environment, use the default
2589 # compiler
2590 if shell_futures:
2591 compiler = self.compile
2592 ast_parse = self.compile.ast_parse
2593 else:
2594 compiler = compile
2595 ast_parse = ast.parse
2582
2596
2583 with self.builtin_trap:
2597 with self.builtin_trap:
2584 prefilter_failed = False
2598 prefilter_failed = False
@@ -2608,8 +2622,7 b' class InteractiveShell(SingletonConfigurable):'
2608
2622
2609 with self.display_trap:
2623 with self.display_trap:
2610 try:
2624 try:
2611 code_ast = self.compile.ast_parse(cell,
2625 code_ast = ast_parse(cell, filename=cell_name)
2612 filename=cell_name)
2613 except IndentationError:
2626 except IndentationError:
2614 self.showindentationerror()
2627 self.showindentationerror()
2615 if store_history:
2628 if store_history:
@@ -2626,7 +2639,7 b' class InteractiveShell(SingletonConfigurable):'
2626
2639
2627 interactivity = "none" if silent else self.ast_node_interactivity
2640 interactivity = "none" if silent else self.ast_node_interactivity
2628 self.run_ast_nodes(code_ast.body, cell_name,
2641 self.run_ast_nodes(code_ast.body, cell_name,
2629 interactivity=interactivity)
2642 interactivity=interactivity, compiler=compiler)
2630
2643
2631 # Execute any registered post-execution functions.
2644 # Execute any registered post-execution functions.
2632 # unless we are silent
2645 # unless we are silent
@@ -2682,7 +2695,8 b' class InteractiveShell(SingletonConfigurable):'
2682 return ast.fix_missing_locations(node)
2695 return ast.fix_missing_locations(node)
2683
2696
2684
2697
2685 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2698 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2699 compiler=compile):
2686 """Run a sequence of AST nodes. The execution mode depends on the
2700 """Run a sequence of AST nodes. The execution mode depends on the
2687 interactivity parameter.
2701 interactivity parameter.
2688
2702
@@ -2699,6 +2713,9 b' class InteractiveShell(SingletonConfigurable):'
2699 will run the last node interactively only if it is an expression (i.e.
2713 will run the last node interactively only if it is an expression (i.e.
2700 expressions in loops or other blocks are not displayed. Other values
2714 expressions in loops or other blocks are not displayed. Other values
2701 for this parameter will raise a ValueError.
2715 for this parameter will raise a ValueError.
2716 compiler : callable
2717 A function with the same interface as the built-in compile(), to turn
2718 the AST nodes into code objects. Default is the built-in compile().
2702 """
2719 """
2703 if not nodelist:
2720 if not nodelist:
2704 return
2721 return
@@ -2723,13 +2740,13 b' class InteractiveShell(SingletonConfigurable):'
2723 try:
2740 try:
2724 for i, node in enumerate(to_run_exec):
2741 for i, node in enumerate(to_run_exec):
2725 mod = ast.Module([node])
2742 mod = ast.Module([node])
2726 code = self.compile(mod, cell_name, "exec")
2743 code = compiler(mod, cell_name, "exec")
2727 if self.run_code(code):
2744 if self.run_code(code):
2728 return True
2745 return True
2729
2746
2730 for i, node in enumerate(to_run_interactive):
2747 for i, node in enumerate(to_run_interactive):
2731 mod = ast.Interactive([node])
2748 mod = ast.Interactive([node])
2732 code = self.compile(mod, cell_name, "single")
2749 code = compiler(mod, cell_name, "single")
2733 if self.run_code(code):
2750 if self.run_code(code):
2734 return True
2751 return True
2735
2752
General Comments 0
You need to be logged in to leave comments. Login now