Skip to content

Commit 881dbef

Browse files
committed
refactor
1 parent 3e81c35 commit 881dbef

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

execnb/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'execnb.nbio.write_nb': ('nbio.html#write_nb', 'execnb/nbio.py')},
2323
'execnb.shell': { 'execnb.shell.CaptureShell': ('shell.html#captureshell', 'execnb/shell.py'),
2424
'execnb.shell.CaptureShell.__init__': ('shell.html#captureshell.__init__', 'execnb/shell.py'),
25+
'execnb.shell.CaptureShell._run': ('shell.html#captureshell._run', 'execnb/shell.py'),
2526
'execnb.shell.CaptureShell.cell': ('shell.html#captureshell.cell', 'execnb/shell.py'),
2627
'execnb.shell.CaptureShell.complete': ('shell.html#captureshell.complete', 'execnb/shell.py'),
2728
'execnb.shell.CaptureShell.enable_gui': ('shell.html#captureshell.enable_gui', 'execnb/shell.py'),

execnb/shell.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
93101
def format_exc(e):
94102
"Format exception `e` as a string"

0 commit comments

Comments
 (0)