##// END OF EJS Templates
Refactor run_cell for clarity.
Thomas Kluyver -
Show More
@@ -2632,13 +2632,40 b' class InteractiveShell(SingletonConfigurable):'
2632 if silent:
2632 if silent:
2633 store_history = False
2633 store_history = False
2634
2634
2635 prefilter_failed = False
2635 preprocessing_exc_tuple = None
2636 try:
2636 try:
2637 # Static input transformations
2637 self.input_transformer_manager.push(raw_cell)
2638 self.input_transformer_manager.push(raw_cell)
2639 cell = self.input_transformer_manager.source_reset()
2638 except SyntaxError:
2640 except SyntaxError:
2639 self.showtraceback()
2641 preprocessing_exc_tuple = sys.exc_info()
2640 prefilter_failed = True
2642 cell = raw_cell # cell has to exist so it can be stored/logged
2641 cell = self.input_transformer_manager.source_reset()
2643 else:
2644 if len(cell.splitlines()) == 1:
2645 # Dynamic transformations - only applied for single line commands
2646 with self.builtin_trap:
2647 try:
2648 # use prefilter_lines to handle trailing newlines
2649 # restore trailing newline for ast.parse
2650 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2651 except Exception:
2652 # don't allow prefilter errors to crash IPython
2653 preprocessing_exc_tuple = sys.exc_info()
2654
2655 # Store raw and processed history
2656 if store_history:
2657 self.history_manager.store_inputs(self.execution_count,
2658 cell, raw_cell)
2659 if not silent:
2660 self.logger.log(cell, raw_cell)
2661
2662 # Display the exception if input processing failed.
2663 if preprocessing_exc_tuple is not None:
2664 self.showtraceback(preprocessing_exc_tuple)
2665 del preprocessing_exc_tuple # Break reference cycle
2666 if store_history:
2667 self.execution_count += 1
2668 return
2642
2669
2643 # Our own compiler remembers the __future__ environment. If we want to
2670 # Our own compiler remembers the __future__ environment. If we want to
2644 # run code with a separate __future__ environment, use the default
2671 # run code with a separate __future__ environment, use the default
@@ -2646,72 +2673,53 b' class InteractiveShell(SingletonConfigurable):'
2646 compiler = self.compile if shell_futures else CachingCompiler()
2673 compiler = self.compile if shell_futures else CachingCompiler()
2647
2674
2648 with self.builtin_trap:
2675 with self.builtin_trap:
2649 if not prefilter_failed and len(cell.splitlines()) == 1:
2676 cell_name = self.compile.cache(cell, self.execution_count)
2650 try:
2651 # use prefilter_lines to handle trailing newlines
2652 # restore trailing newline for ast.parse
2653 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2654 except AliasError as e:
2655 error(e)
2656 prefilter_failed = True
2657 except Exception:
2658 # don't allow prefilter errors to crash IPython
2659 self.showtraceback()
2660 prefilter_failed = True
2661
2662 # Store raw and processed history
2663 if store_history:
2664 self.history_manager.store_inputs(self.execution_count,
2665 cell, raw_cell)
2666 if not silent:
2667 self.logger.log(cell, raw_cell)
2668
2677
2669 if not prefilter_failed:
2678 with self.display_trap:
2670 # don't run if prefilter failed
2679 # Compile to bytecode
2671 cell_name = self.compile.cache(cell, self.execution_count)
2680 try:
2672
2681 code_ast = compiler.ast_parse(cell, filename=cell_name)
2673 with self.display_trap:
2682 except IndentationError:
2683 self.showindentationerror()
2684 if store_history:
2685 self.execution_count += 1
2686 return None
2687 except (OverflowError, SyntaxError, ValueError, TypeError,
2688 MemoryError):
2689 self.showsyntaxerror()
2690 if store_history:
2691 self.execution_count += 1
2692 return None
2693
2694 # Apply AST transformations
2695 code_ast = self.transform_ast(code_ast)
2696
2697 # Execute the user code
2698 interactivity = "none" if silent else self.ast_node_interactivity
2699 self.run_ast_nodes(code_ast.body, cell_name,
2700 interactivity=interactivity, compiler=compiler)
2701
2702 # Execute any registered post-execution functions.
2703 # unless we are silent
2704 post_exec = [] if silent else iteritems(self._post_execute)
2705
2706 for func, status in post_exec:
2707 if self.disable_failing_post_execute and not status:
2708 continue
2674 try:
2709 try:
2675 code_ast = compiler.ast_parse(cell, filename=cell_name)
2710 func()
2676 except IndentationError:
2711 except KeyboardInterrupt:
2677 self.showindentationerror()
2712 print("\nKeyboardInterrupt", file=io.stderr)
2678 if store_history:
2713 except Exception:
2679 self.execution_count += 1
2714 # register as failing:
2680 return None
2715 self._post_execute[func] = False
2681 except (OverflowError, SyntaxError, ValueError, TypeError,
2716 self.showtraceback()
2682 MemoryError):
2717 print('\n'.join([
2683 self.showsyntaxerror()
2718 "post-execution function %r produced an error." % func,
2684 if store_history:
2719 "If this problem persists, you can disable failing post-exec functions with:",
2685 self.execution_count += 1
2720 "",
2686 return None
2721 " get_ipython().disable_failing_post_execute = True"
2687
2722 ]), file=io.stderr)
2688 code_ast = self.transform_ast(code_ast)
2689
2690 interactivity = "none" if silent else self.ast_node_interactivity
2691 self.run_ast_nodes(code_ast.body, cell_name,
2692 interactivity=interactivity, compiler=compiler)
2693
2694 # Execute any registered post-execution functions.
2695 # unless we are silent
2696 post_exec = [] if silent else iteritems(self._post_execute)
2697
2698 for func, status in post_exec:
2699 if self.disable_failing_post_execute and not status:
2700 continue
2701 try:
2702 func()
2703 except KeyboardInterrupt:
2704 print("\nKeyboardInterrupt", file=io.stderr)
2705 except Exception:
2706 # register as failing:
2707 self._post_execute[func] = False
2708 self.showtraceback()
2709 print('\n'.join([
2710 "post-execution function %r produced an error." % func,
2711 "If this problem persists, you can disable failing post-exec functions with:",
2712 "",
2713 " get_ipython().disable_failing_post_execute = True"
2714 ]), file=io.stderr)
2715
2723
2716 if store_history:
2724 if store_history:
2717 # Write output to the database. Does nothing unless
2725 # Write output to the database. Does nothing unless
General Comments 0
You need to be logged in to leave comments. Login now