Skip to content

Commit 306fd61

Browse files
committed
fixes #66
1 parent 565c59d commit 306fd61

File tree

2 files changed

+64
-70
lines changed

2 files changed

+64
-70
lines changed

execnb/shell.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def handler(*args): raise TimeoutError()
9999

100100
# %% ../nbs/02_shell.ipynb
101101
def format_exc(e):
102-
"Format exception `e` as a string"
103-
return ''.join(traceback.format_exception(type(e), e, e.__traceback__))
102+
"Format exception `e` as a string list"
103+
return traceback.format_exception(type(e), e, e.__traceback__)
104104

105105
# %% ../nbs/02_shell.ipynb
106106
def _out_stream(text, name): return dict(name=name, output_type='stream', text=text.splitlines(True))
@@ -131,7 +131,8 @@ def _out_nb(o, fmt):
131131
if r is not None and not o.quiet:
132132
res.append(_mk_out(*fmt.format(r), 'execute_result'))
133133
if 'execution_count' not in o: o['execution_count']=None
134-
for p in res: p['execution_count'] = o['execution_count']
134+
for p in res:
135+
if p["output_type"]=="execute_result": p['execution_count'] = o['execution_count']
135136
return res
136137

137138
# %% ../nbs/02_shell.ipynb

nbs/02_shell.ipynb

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@
669669
"source": [
670670
"#| export\n",
671671
"def format_exc(e):\n",
672-
" \"Format exception `e` as a string\"\n",
673-
" return ''.join(traceback.format_exception(type(e), e, e.__traceback__))"
672+
" \"Format exception `e` as a string list\"\n",
673+
" return traceback.format_exception(type(e), e, e.__traceback__)"
674674
]
675675
},
676676
{
@@ -708,7 +708,8 @@
708708
" if r is not None and not o.quiet:\n",
709709
" res.append(_mk_out(*fmt.format(r), 'execute_result'))\n",
710710
" if 'execution_count' not in o: o['execution_count']=None\n",
711-
" for p in res: p['execution_count'] = o['execution_count']\n",
711+
" for p in res:\n",
712+
" if p[\"output_type\"]==\"execute_result\": p['execution_count'] = o['execution_count']\n",
712713
" return res"
713714
]
714715
},
@@ -749,10 +750,7 @@
749750
{
750751
"data": {
751752
"text/plain": [
752-
"[{'name': 'stdout',\n",
753-
" 'output_type': 'stream',\n",
754-
" 'text': ['1\\n'],\n",
755-
" 'execution_count': None}]"
753+
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']}]"
756754
]
757755
},
758756
"execution_count": null,
@@ -781,9 +779,8 @@
781779
"text/plain": [
782780
"[{'name': 'stdout',\n",
783781
" 'output_type': 'stream',\n",
784-
" 'text': ['CPU times: user 2 us, sys: 1 us, total: 3 us\\n',\n",
785-
" 'Wall time: 4.29 us\\n'],\n",
786-
" 'execution_count': None},\n",
782+
" 'text': ['CPU times: user 4 us, sys: 1 us, total: 5 us\\n',\n",
783+
" 'Wall time: 9.06 us\\n']},\n",
787784
" {'data': {'text/plain': ['2']},\n",
788785
" 'metadata': {},\n",
789786
" 'output_type': 'execute_result',\n",
@@ -869,13 +866,14 @@
869866
" 'File \\x1b[0;32m<ipython-input-1-9e1622b385b6>:1\\x1b[0m\\n',\n",
870867
" '\\x1b[0;32m----> 1\\x1b[0m \\x1b[38;5;241m1\\x1b[39m\\x1b[38;5;241m/\\x1b[39m\\x1b[38;5;241m0\\x1b[39m\\n',\n",
871868
" '\\n',\n",
872-
" '\\x1b[0;31mZeroDivisionError\\x1b[0m: division by zero\\n'],\n",
873-
" 'execution_count': None},\n",
869+
" '\\x1b[0;31mZeroDivisionError\\x1b[0m: division by zero\\n']},\n",
874870
" {'ename': 'ZeroDivisionError',\n",
875871
" 'evalue': 'division by zero',\n",
876872
" 'output_type': 'error',\n",
877-
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\nZeroDivisionError: division by zero\\n',\n",
878-
" 'execution_count': None}]"
873+
" 'traceback': ['Traceback (most recent call last):\\n',\n",
874+
" ' File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n',\n",
875+
" ' File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\n',\n",
876+
" 'ZeroDivisionError: division by zero\\n']}]"
879877
]
880878
},
881879
"execution_count": null,
@@ -896,10 +894,7 @@
896894
{
897895
"data": {
898896
"text/plain": [
899-
"[{'name': 'stdout',\n",
900-
" 'output_type': 'stream',\n",
901-
" 'text': ['no timeout\\n'],\n",
902-
" 'execution_count': None}]"
897+
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['no timeout\\n']}]"
903898
]
904899
},
905900
"execution_count": null,
@@ -1025,7 +1020,7 @@
10251020
"File &lt;ipython-input-1-a5c3817716b6&gt;:1\n",
10261021
"----&gt; 1 import time; time.sleep(1.1)\n",
10271022
"\n",
1028-
"Cell In[143], line 7, in run_cell.&lt;locals&gt;.handler(*args)\n",
1023+
"Cell In[58], line 7, in run_cell.&lt;locals&gt;.handler(*args)\n",
10291024
"----&gt; 7 def handler(*args): raise TimeoutError()\n",
10301025
"\n",
10311026
"TimeoutError: \n",
@@ -1064,7 +1059,7 @@
10641059
"File <span class=\"ansi-green-fg\">&lt;ipython-input-1-a5c3817716b6&gt;:1</span>\n",
10651060
"<span class=\"ansi-green-fg\">----&gt; 1</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">import</span> <span class=\"ansi-bold\" style=\"color: rgb(0,0,255)\">time</span>; time<span style=\"color: rgb(98,98,98)\">.</span>sleep(<span style=\"color: rgb(98,98,98)\">1.1</span>)\n",
10661061
"\n",
1067-
"Cell <span class=\"ansi-green-fg\">In[143], line 7</span>, in <span class=\"ansi-cyan-fg\">run_cell.&lt;locals&gt;.handler</span><span class=\"ansi-blue-fg\">(*args)</span>\n",
1062+
"Cell <span class=\"ansi-green-fg\">In[58], line 7</span>, in <span class=\"ansi-cyan-fg\">run_cell.&lt;locals&gt;.handler</span><span class=\"ansi-blue-fg\">(*args)</span>\n",
10681063
"<span class=\"ansi-green-fg\">----&gt; 7</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">def</span> <span style=\"color: rgb(0,0,255)\">handler</span>(<span style=\"color: rgb(98,98,98)\">*</span>args): <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">raise</span> <span class=\"ansi-bold\" style=\"color: rgb(215,95,95)\">TimeoutError</span>()\n",
10691064
"\n",
10701065
"<span class=\"ansi-red-fg\">TimeoutError</span>: \n",
@@ -1139,13 +1134,14 @@
11391134
" 'File \\x1b[0;32m<ipython-input-1-01648acb07bd>:1\\x1b[0m\\n',\n",
11401135
" '\\x1b[0;32m----> 1\\x1b[0m \\x1b[38;5;28;01mraise\\x1b[39;00m \\x1b[38;5;167;01mException\\x1b[39;00m(\\x1b[38;5;124m\"\\x1b[39m\\x1b[38;5;124mOops\\x1b[39m\\x1b[38;5;124m\"\\x1b[39m)\\n',\n",
11411136
" '\\n',\n",
1142-
" '\\x1b[0;31mException\\x1b[0m: Oops\\n'],\n",
1143-
" 'execution_count': None},\n",
1137+
" '\\x1b[0;31mException\\x1b[0m: Oops\\n']},\n",
11441138
" {'ename': 'Exception',\n",
11451139
" 'evalue': 'Oops',\n",
11461140
" 'output_type': 'error',\n",
1147-
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-01648acb07bd>\", line 1, in <module>\\n raise Exception(\"Oops\")\\nException: Oops\\n',\n",
1148-
" 'execution_count': None}]"
1141+
" 'traceback': ['Traceback (most recent call last):\\n',\n",
1142+
" ' File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n',\n",
1143+
" ' File \"<ipython-input-1-01648acb07bd>\", line 1, in <module>\\n raise Exception(\"Oops\")\\n',\n",
1144+
" 'Exception: Oops\\n']}]"
11491145
]
11501146
},
11511147
"execution_count": null,
@@ -1242,10 +1238,7 @@
12421238
{
12431239
"data": {
12441240
"text/plain": [
1245-
"[{'name': 'stdout',\n",
1246-
" 'output_type': 'stream',\n",
1247-
" 'text': ['1\\n'],\n",
1248-
" 'execution_count': None},\n",
1241+
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n",
12491242
" {'data': {'text/plain': ['2']},\n",
12501243
" 'metadata': {},\n",
12511244
" 'output_type': 'execute_result',\n",
@@ -1521,13 +1514,14 @@
15211514
" 'File \\x1b[0;32m<ipython-input-1-1c97c1d317ab>:1\\x1b[0m\\n',\n",
15221515
" '\\x1b[0;32m----> 1\\x1b[0m \\x1b[38;5;28;01mraise\\x1b[39;00m \\x1b[38;5;167;01mException\\x1b[39;00m(\\x1b[38;5;124m\"\\x1b[39m\\x1b[38;5;124mOopsie!\\x1b[39m\\x1b[38;5;124m\"\\x1b[39m)\\n',\n",
15231516
" '\\n',\n",
1524-
" '\\x1b[0;31mException\\x1b[0m: Oopsie!\\n'],\n",
1525-
" 'execution_count': None},\n",
1517+
" '\\x1b[0;31mException\\x1b[0m: Oopsie!\\n']},\n",
15261518
" {'ename': 'Exception',\n",
15271519
" 'evalue': 'Oopsie!',\n",
15281520
" 'output_type': 'error',\n",
1529-
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-1c97c1d317ab>\", line 1, in <module>\\n raise Exception(\"Oopsie!\")\\nException: Oopsie!\\n',\n",
1530-
" 'execution_count': None}]"
1521+
" 'traceback': ['Traceback (most recent call last):\\n',\n",
1522+
" ' File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n',\n",
1523+
" ' File \"<ipython-input-1-1c97c1d317ab>\", line 1, in <module>\\n raise Exception(\"Oopsie!\")\\n',\n",
1524+
" 'Exception: Oopsie!\\n']}]"
15311525
]
15321526
},
15331527
"execution_count": null,
@@ -1616,10 +1610,7 @@
16161610
{
16171611
"data": {
16181612
"text/plain": [
1619-
"[{'name': 'stdout',\n",
1620-
" 'output_type': 'stream',\n",
1621-
" 'text': ['1\\n'],\n",
1622-
" 'execution_count': None},\n",
1613+
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n",
16231614
" {'data': {'text/plain': ['2']},\n",
16241615
" 'metadata': {},\n",
16251616
" 'output_type': 'execute_result',\n",
@@ -1686,7 +1677,7 @@
16861677
"name": "stdout",
16871678
"output_type": "stream",
16881679
"text": [
1689-
"[{'execution_count': None, 'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']}, {'data': {'text/plain': ['2']}, 'execution_count': None, 'metadata': {}, 'output_type': 'execute_result'}]\n"
1680+
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']}, {'data': {'text/plain': ['2']}, 'execution_count': None, 'metadata': {}, 'output_type': 'execute_result'}]\n"
16901681
]
16911682
}
16921683
],
@@ -1747,23 +1738,7 @@
17471738
"===========================================================================\n",
17481739
"\n",
17491740
"While Executing Cell #2:\n",
1750-
"Traceback (most recent call last):\n",
1751-
" File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_6273/1421292703.py\", line 3, in <module>\n",
1752-
" s.execute('../tests/error.ipynb', exc_stop=True)\n",
1753-
" File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_6273/3609882568.py\", line 18, in execute\n",
1754-
" self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,\n",
1755-
" File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_6273/3068237356.py\", line 19, in run_all\n",
1756-
" if self.exc and exc_stop: raise self.exc from None\n",
1757-
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
1758-
" File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\n",
1759-
" exec(code_obj, self.user_global_ns, self.user_ns)\n",
1760-
" File \"<ipython-input-1-b968a57a586e>\", line 3, in <module>\n",
1761-
" foo()\n",
1762-
" File \"/Users/jhoward/subs_aai/execnb/tests/err.py\", line 2, in foo\n",
1763-
" assert 13 == 98\n",
1764-
" ^^^^^^^^\n",
1765-
"AssertionError\n",
1766-
"\n",
1741+
"['Traceback (most recent call last):\\n', ' File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_23644/1421292703.py\", line 3, in <module>\\n s.execute(\\'../tests/error.ipynb\\', exc_stop=True)\\n', ' File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_23644/3609882568.py\", line 18, in execute\\n self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,\\n', ' File \"/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_23644/3068237356.py\", line 19, in run_all\\n if self.exc and exc_stop: raise self.exc from None\\n ^^^^^^^^^^^^^^^^^^^^^^^^\\n', ' File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n', ' File \"<ipython-input-1-b968a57a586e>\", line 3, in <module>\\n foo()\\n', ' File \"/Users/jhoward/subs_aai/execnb/tests/err.py\", line 2, in foo\\n assert 13 == 98\\n ^^^^^^^^\\n', 'AssertionError\\n']\n",
17671742
"\n"
17681743
]
17691744
}
@@ -1850,9 +1825,9 @@
18501825
"#|hide\n",
18511826
"# `display_data` flushes stdout and stderr - in that order\n",
18521827
"res = CaptureShell().run(\"import sys; sys.stderr.write('Error!'); print(1); display(1)\")\n",
1853-
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n'], 'execution_count': None},\n",
1854-
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!'], 'execution_count': None},\n",
1855-
" {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'display_data', 'execution_count': None}])"
1828+
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n",
1829+
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!']},\n",
1830+
" {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'display_data'}])"
18561831
]
18571832
},
18581833
{
@@ -1864,7 +1839,7 @@
18641839
"#|hide\n",
18651840
"# Consecutive writes to stdout are grouped\n",
18661841
"res = CaptureShell().run('print(1); print(2); print(3)')\n",
1867-
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n'], 'execution_count': None}])"
1842+
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n']}])"
18681843
]
18691844
},
18701845
{
@@ -1876,8 +1851,8 @@
18761851
"#|hide\n",
18771852
"# Consecutive writes to stdout are grouped despite an intermediate write to stderr, which is captured after\n",
18781853
"res = CaptureShell().run(\"import sys; print(1); print(2); sys.stderr.write('Error!'); print(3)\")\n",
1879-
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n'], 'execution_count': None},\n",
1880-
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!'], 'execution_count': None}])"
1854+
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n', '2\\n', '3\\n']},\n",
1855+
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!']}])"
18811856
]
18821857
},
18831858
{
@@ -1889,9 +1864,9 @@
18891864
"#|hide\n",
18901865
"# `execute_result` is always last\n",
18911866
"res = CaptureShell().run(\"import sys; sys.stderr.write('Error!'); print(1); display(1); 1\")\n",
1892-
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n'], 'execution_count': None},\n",
1893-
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!'], 'execution_count': None},\n",
1894-
" {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'display_data', 'execution_count': None},\n",
1867+
"test_eq(res, [{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n",
1868+
" {'name': 'stderr', 'output_type': 'stream', 'text': ['Error!']},\n",
1869+
" {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'display_data'},\n",
18951870
" {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'execute_result', 'execution_count': None}])"
18961871
]
18971872
},
@@ -1954,10 +1929,7 @@
19541929
" 'execution_count': None,\n",
19551930
" 'id': 'ea528db5',\n",
19561931
" 'metadata': {},\n",
1957-
" 'outputs': [{'name': 'stdout',\n",
1958-
" 'output_type': 'stream',\n",
1959-
" 'text': ['2\\n'],\n",
1960-
" 'execution_count': None}],\n",
1932+
" 'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': ['2\\n']}],\n",
19611933
" 'source': 'print(a)',\n",
19621934
" 'idx_': 1}]"
19631935
]
@@ -2112,6 +2084,27 @@
21122084
"s.complete('a.b')"
21132085
]
21142086
},
2087+
{
2088+
"cell_type": "code",
2089+
"execution_count": null,
2090+
"metadata": {},
2091+
"outputs": [
2092+
{
2093+
"data": {
2094+
"text/plain": [
2095+
"['flags=', 'pattern=']"
2096+
]
2097+
},
2098+
"execution_count": null,
2099+
"metadata": {},
2100+
"output_type": "execute_result"
2101+
}
2102+
],
2103+
"source": [
2104+
"s.run('import re')\n",
2105+
"s.complete('re.compile(')"
2106+
]
2107+
},
21152108
{
21162109
"cell_type": "markdown",
21172110
"metadata": {},

0 commit comments

Comments
 (0)