From ba8af4d6beedab3aa499bb739c0a69ea38c5a139 2015-05-31 13:30:08 From: Tianhui Michael Li Date: 2015-05-31 13:30:08 Subject: [PATCH] safe_execfile_ipy optionally raises exception in user code --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index c8f406f..d006279 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2664,7 +2664,7 @@ class InteractiveShell(SingletonConfigurable): # tb offset is 2 because we wrap execfile self.showtraceback(tb_offset=2) - def safe_execfile_ipy(self, fname, shell_futures=False): + def safe_execfile_ipy(self, fname, raise_exceptions=False, shell_futures=False): """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. Parameters @@ -2672,6 +2672,8 @@ class InteractiveShell(SingletonConfigurable): fname : str The name of the file to execute. The filename must have a .ipy or .ipynb extension. + raise_exceptions : bool (False) + If True raise exceptions everywhere. Meant for testing. shell_futures : bool (False) If True, the code will share future statements with the interactive shell. It will both be affected by previous __future__ imports, and @@ -2711,12 +2713,13 @@ class InteractiveShell(SingletonConfigurable): with prepended_to_syspath(dname): try: for cell in get_cells(): - # self.run_cell currently captures all exceptions - # raised in user code. It would be nice if there were - # versions of run_cell that did raise, so - # we could catch the errors. result = self.run_cell(cell, silent=True, shell_futures=shell_futures) - if not result.success: + if not result.success and raise_exceptions: + if result.error_before_exec is not None: + raise result.error_before_exec + else: + raise result.error_in_exec + elif not result.success: break except: self.showtraceback()