Skip to content

Commit 593df6d

Browse files
committed
Handle extra error code pystata glitch
1 parent 5568859 commit 593df6d

File tree

2 files changed

+115
-8
lines changed

2 files changed

+115
-8
lines changed

nbs/14_kernel.ipynb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@
174174
" }"
175175
]
176176
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"#| hide\n",
184+
"#| eval: False\n",
185+
"from nbstata.config import launch_stata\n",
186+
"from nbstata.stata import run_direct"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"#| hide\n",
196+
"#| eval: False\n",
197+
"launch_stata(splash=False)"
198+
]
199+
},
177200
{
178201
"cell_type": "code",
179202
"execution_count": null,
@@ -183,6 +206,8 @@
183206
"#| export\n",
184207
"def print_stata_error(text):\n",
185208
" lines = text.splitlines()\n",
209+
" if len(lines) >= 2 and lines[-2] == lines[-1]:\n",
210+
" lines.pop(-1) # remove duplicate error code glitch in pystata.stata.run multi-line (ex. below)\n",
186211
" if len(lines) > 2:\n",
187212
" print(\"\\n\".join(lines[:-2]))\n",
188213
" print_red(\"\\n\".join(lines[-2:]))"
@@ -197,6 +222,68 @@
197222
"from textwrap import dedent"
198223
]
199224
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": null,
228+
"metadata": {},
229+
"outputs": [
230+
{
231+
"name": "stdout",
232+
"output_type": "stream",
233+
"text": [
234+
"\n",
235+
". disp 1\n",
236+
"1\n",
237+
"\n",
238+
". disp error\n",
239+
"error not found\n",
240+
"r(111);\n",
241+
"r(111);\n",
242+
"\n"
243+
]
244+
}
245+
],
246+
"source": [
247+
"#| hide\n",
248+
"#| eval: False\n",
249+
"try:\n",
250+
" run_direct(dedent(\"\"\"\\\n",
251+
" disp 1\n",
252+
" disp error\"\"\"), echo=False)\n",
253+
"except SystemError as err:\n",
254+
" print(str(err))"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"name": "stdout",
264+
"output_type": "stream",
265+
"text": [
266+
"\n",
267+
". disp 1\n",
268+
"1\n",
269+
"\n",
270+
". disp error\n",
271+
"\u001b[31merror not found\n",
272+
"r(111);\u001b[0m\n"
273+
]
274+
}
275+
],
276+
"source": [
277+
"#| hide\n",
278+
"#| eval: False\n",
279+
"try:\n",
280+
" run_direct(dedent(\"\"\"\\\n",
281+
" disp 1\n",
282+
" disp error\"\"\"), echo=False)\n",
283+
"except SystemError as err:\n",
284+
" print_stata_error(str(err))"
285+
]
286+
},
200287
{
201288
"cell_type": "code",
202289
"execution_count": null,
@@ -220,6 +307,24 @@
220307
" \"\"\"))"
221308
]
222309
},
310+
{
311+
"cell_type": "code",
312+
"execution_count": null,
313+
"metadata": {},
314+
"outputs": [
315+
{
316+
"name": "stdout",
317+
"output_type": "stream",
318+
"text": [
319+
"\u001b[31mone line\u001b[0m\n"
320+
]
321+
}
322+
],
323+
"source": [
324+
"#| hide\n",
325+
"print_stata_error(\"one line\")"
326+
]
327+
},
223328
{
224329
"cell_type": "code",
225330
"execution_count": null,

nbstata/kernel.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ def _handle_stata_import_error(err, silent, execution_count):
7878
'execution_count': execution_count,
7979
}
8080

81-
# %% ../nbs/14_kernel.ipynb 11
81+
# %% ../nbs/14_kernel.ipynb 13
8282
def print_stata_error(text):
8383
lines = text.splitlines()
84+
if len(lines) >= 2 and lines[-2] == lines[-1]:
85+
lines.pop(-1) # remove duplicate error code glitch in pystata.stata.run multi-line (ex. below)
8486
if len(lines) > 2:
8587
print("\n".join(lines[:-2]))
8688
print_red("\n".join(lines[-2:]))
8789

88-
# %% ../nbs/14_kernel.ipynb 14
90+
# %% ../nbs/14_kernel.ipynb 19
8991
def _handle_stata_error(err, silent, execution_count):
9092
reply_content = {
9193
"traceback": [],
@@ -105,12 +107,12 @@ def _handle_stata_error(err, silent, execution_count):
105107
})
106108
return reply_content
107109

108-
# %% ../nbs/14_kernel.ipynb 15
110+
# %% ../nbs/14_kernel.ipynb 20
109111
@patch_to(PyStataKernel)
110112
def post_do_hook(self):
111113
self.inspect_output = ""
112114

113-
# %% ../nbs/14_kernel.ipynb 16
115+
# %% ../nbs/14_kernel.ipynb 21
114116
@patch_to(PyStataKernel)
115117
def do_execute(self, code, silent,
116118
store_history=True, user_expressions=None, allow_stdin=False):
@@ -134,7 +136,7 @@ def do_execute(self, code, silent,
134136
'user_expressions': {},
135137
}
136138

137-
# %% ../nbs/14_kernel.ipynb 17
139+
# %% ../nbs/14_kernel.ipynb 22
138140
@patch_to(PyStataKernel)
139141
def do_complete(self, code, cursor_pos):
140142
"""Provide context-aware suggestions"""
@@ -154,13 +156,13 @@ def do_complete(self, code, cursor_pos):
154156
'matches': matches,
155157
}
156158

157-
# %% ../nbs/14_kernel.ipynb 18
159+
# %% ../nbs/14_kernel.ipynb 23
158160
@patch_to(PyStataKernel)
159161
def do_is_complete(self, code):
160162
"""Overrides IPythonKernel with kernelbase default"""
161163
return {"status": "unknown"}
162164

163-
# %% ../nbs/14_kernel.ipynb 19
165+
# %% ../nbs/14_kernel.ipynb 24
164166
@patch_to(PyStataKernel)
165167
def do_inspect(self, code, cursor_pos, detail_level=0, omit_sections=()):
166168
"""Display Stata 'describe' output regardless of cursor position"""
@@ -169,7 +171,7 @@ def do_inspect(self, code, cursor_pos, detail_level=0, omit_sections=()):
169171
data = {'text/plain': self.inspect_output}
170172
return {"status": "ok", "data": data, "metadata": {}, "found": True}
171173

172-
# %% ../nbs/14_kernel.ipynb 20
174+
# %% ../nbs/14_kernel.ipynb 25
173175
@patch_to(PyStataKernel)
174176
def do_history(
175177
self,

0 commit comments

Comments
 (0)