@@ -63,32 +63,40 @@ def __init__(self, path:str|Path=None, mpl_format='retina', history=False, timeo
6363 if not IN_NOTEBOOK : InteractiveShell ._instance = self
6464 if set_matplotlib_formats :
6565 self .enable_matplotlib ("inline" )
66- self .run_cell ("from matplotlib_inline.backend_inline import set_matplotlib_formats" )
67- self .run_cell (f"set_matplotlib_formats('{ mpl_format } ')" )
66+ self ._run ("from matplotlib_inline.backend_inline import set_matplotlib_formats" )
67+ self ._run (f"set_matplotlib_formats('{ mpl_format } ')" )
6868
69- def run_cell (self , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
70- stdout = True , stderr = True , display = True , timeout :Optional [int ]= None ):
71- if not timeout : timeout = self .timeout
69+ def _run (self , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
70+ stdout = True , stderr = True , display = True ):
7271 # TODO what if there's a comment?
7372 semic = raw_cell .rstrip ().endswith (';' )
74- if timeout :
75- def handler (* args ): raise TimeoutError ()
76- signal .signal (signal .SIGALRM , handler )
77- signal .alarm (timeout )
7873 with capture_output (display = display , stdout = stdout , stderr = stderr ) as c :
7974 result = super ().run_cell (raw_cell , store_history , silent , shell_futures = shell_futures , cell_id = cell_id )
80- if timeout : signal .alarm (0 )
8175 return AttrDict (result = result , stdout = '' if semic else c .stdout , stderr = c .stderr ,
8276 display_objects = c .outputs , exception = result .error_in_exec , quiet = semic )
8377
8478 def set_path (self , path ):
8579 "Add `path` to python path, or `path.parent` if it's a file"
8680 path = Path (path )
8781 if path .is_file (): path = path .parent
88- self .run_cell (f"import sys; sys.path.insert(0, '{ path .as_posix ()} ')" )
82+ self ._run (f"import sys; sys.path.insert(0, '{ path .as_posix ()} ')" )
8983
9084 def enable_gui (self , gui = None ): pass
9185
86+ # %% ../nbs/02_shell.ipynb
87+ @patch
88+ def run_cell (self :CaptureShell , raw_cell , store_history = False , silent = False , shell_futures = True , cell_id = None ,
89+ stdout = True , stderr = True , display = True , timeout = None ):
90+ if not timeout : timeout = self .timeout
91+ if timeout :
92+ def handler (* args ): raise TimeoutError ()
93+ signal .signal (signal .SIGALRM , handler )
94+ signal .alarm (timeout )
95+ try : return self ._run (raw_cell , store_history , silent , shell_futures , cell_id = cell_id ,
96+ stdout = stdout , stderr = stderr , display = display )
97+ finally :
98+ if timeout : signal .alarm (0 )
99+
92100# %% ../nbs/02_shell.ipynb
93101def format_exc (e ):
94102 "Format exception `e` as a string"
0 commit comments