|
669 | 669 | "source": [ |
670 | 670 | "#| export\n", |
671 | 671 | "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__)" |
674 | 674 | ] |
675 | 675 | }, |
676 | 676 | { |
|
708 | 708 | " if r is not None and not o.quiet:\n", |
709 | 709 | " res.append(_mk_out(*fmt.format(r), 'execute_result'))\n", |
710 | 710 | " 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", |
712 | 713 | " return res" |
713 | 714 | ] |
714 | 715 | }, |
|
749 | 750 | { |
750 | 751 | "data": { |
751 | 752 | "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']}]" |
756 | 754 | ] |
757 | 755 | }, |
758 | 756 | "execution_count": null, |
|
781 | 779 | "text/plain": [ |
782 | 780 | "[{'name': 'stdout',\n", |
783 | 781 | " '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", |
787 | 784 | " {'data': {'text/plain': ['2']},\n", |
788 | 785 | " 'metadata': {},\n", |
789 | 786 | " 'output_type': 'execute_result',\n", |
|
869 | 866 | " 'File \\x1b[0;32m<ipython-input-1-9e1622b385b6>:1\\x1b[0m\\n',\n", |
870 | 867 | " '\\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", |
871 | 868 | " '\\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", |
874 | 870 | " {'ename': 'ZeroDivisionError',\n", |
875 | 871 | " 'evalue': 'division by zero',\n", |
876 | 872 | " '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']}]" |
879 | 877 | ] |
880 | 878 | }, |
881 | 879 | "execution_count": null, |
|
896 | 894 | { |
897 | 895 | "data": { |
898 | 896 | "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']}]" |
903 | 898 | ] |
904 | 899 | }, |
905 | 900 | "execution_count": null, |
|
1025 | 1020 | "File <ipython-input-1-a5c3817716b6>:1\n", |
1026 | 1021 | "----> 1 import time; time.sleep(1.1)\n", |
1027 | 1022 | "\n", |
1028 | | - "Cell In[143], line 7, in run_cell.<locals>.handler(*args)\n", |
| 1023 | + "Cell In[58], line 7, in run_cell.<locals>.handler(*args)\n", |
1029 | 1024 | "----> 7 def handler(*args): raise TimeoutError()\n", |
1030 | 1025 | "\n", |
1031 | 1026 | "TimeoutError: \n", |
|
1064 | 1059 | "File <span class=\"ansi-green-fg\"><ipython-input-1-a5c3817716b6>:1</span>\n", |
1065 | 1060 | "<span class=\"ansi-green-fg\">----> 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", |
1066 | 1061 | "\n", |
1067 | | - "Cell <span class=\"ansi-green-fg\">In[143], line 7</span>, in <span class=\"ansi-cyan-fg\">run_cell.<locals>.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.<locals>.handler</span><span class=\"ansi-blue-fg\">(*args)</span>\n", |
1068 | 1063 | "<span class=\"ansi-green-fg\">----> 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", |
1069 | 1064 | "\n", |
1070 | 1065 | "<span class=\"ansi-red-fg\">TimeoutError</span>: \n", |
|
1139 | 1134 | " 'File \\x1b[0;32m<ipython-input-1-01648acb07bd>:1\\x1b[0m\\n',\n", |
1140 | 1135 | " '\\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", |
1141 | 1136 | " '\\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", |
1144 | 1138 | " {'ename': 'Exception',\n", |
1145 | 1139 | " 'evalue': 'Oops',\n", |
1146 | 1140 | " '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']}]" |
1149 | 1145 | ] |
1150 | 1146 | }, |
1151 | 1147 | "execution_count": null, |
|
1242 | 1238 | { |
1243 | 1239 | "data": { |
1244 | 1240 | "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", |
1249 | 1242 | " {'data': {'text/plain': ['2']},\n", |
1250 | 1243 | " 'metadata': {},\n", |
1251 | 1244 | " 'output_type': 'execute_result',\n", |
|
1521 | 1514 | " 'File \\x1b[0;32m<ipython-input-1-1c97c1d317ab>:1\\x1b[0m\\n',\n", |
1522 | 1515 | " '\\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", |
1523 | 1516 | " '\\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", |
1526 | 1518 | " {'ename': 'Exception',\n", |
1527 | 1519 | " 'evalue': 'Oopsie!',\n", |
1528 | 1520 | " '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']}]" |
1531 | 1525 | ] |
1532 | 1526 | }, |
1533 | 1527 | "execution_count": null, |
|
1616 | 1610 | { |
1617 | 1611 | "data": { |
1618 | 1612 | "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", |
1623 | 1614 | " {'data': {'text/plain': ['2']},\n", |
1624 | 1615 | " 'metadata': {},\n", |
1625 | 1616 | " 'output_type': 'execute_result',\n", |
|
1686 | 1677 | "name": "stdout", |
1687 | 1678 | "output_type": "stream", |
1688 | 1679 | "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" |
1690 | 1681 | ] |
1691 | 1682 | } |
1692 | 1683 | ], |
|
1747 | 1738 | "===========================================================================\n", |
1748 | 1739 | "\n", |
1749 | 1740 | "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", |
1767 | 1742 | "\n" |
1768 | 1743 | ] |
1769 | 1744 | } |
|
1850 | 1825 | "#|hide\n", |
1851 | 1826 | "# `display_data` flushes stdout and stderr - in that order\n", |
1852 | 1827 | "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'}])" |
1856 | 1831 | ] |
1857 | 1832 | }, |
1858 | 1833 | { |
|
1864 | 1839 | "#|hide\n", |
1865 | 1840 | "# Consecutive writes to stdout are grouped\n", |
1866 | 1841 | "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']}])" |
1868 | 1843 | ] |
1869 | 1844 | }, |
1870 | 1845 | { |
|
1876 | 1851 | "#|hide\n", |
1877 | 1852 | "# Consecutive writes to stdout are grouped despite an intermediate write to stderr, which is captured after\n", |
1878 | 1853 | "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!']}])" |
1881 | 1856 | ] |
1882 | 1857 | }, |
1883 | 1858 | { |
|
1889 | 1864 | "#|hide\n", |
1890 | 1865 | "# `execute_result` is always last\n", |
1891 | 1866 | "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", |
1895 | 1870 | " {'data': {'text/plain': ['1']}, 'metadata': {}, 'output_type': 'execute_result', 'execution_count': None}])" |
1896 | 1871 | ] |
1897 | 1872 | }, |
|
1954 | 1929 | " 'execution_count': None,\n", |
1955 | 1930 | " 'id': 'ea528db5',\n", |
1956 | 1931 | " '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", |
1961 | 1933 | " 'source': 'print(a)',\n", |
1962 | 1934 | " 'idx_': 1}]" |
1963 | 1935 | ] |
|
2112 | 2084 | "s.complete('a.b')" |
2113 | 2085 | ] |
2114 | 2086 | }, |
| 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 | + }, |
2115 | 2108 | { |
2116 | 2109 | "cell_type": "markdown", |
2117 | 2110 | "metadata": {}, |
|
0 commit comments