8000 Merge pull request #27 from corivera/createProxyInExecute · vors/jupyter-powershell@a61881c · GitHub
[go: up one dir, main page]

Skip to content

Commit a61881c

Browse files
authored
Merge pull request #27 from corivera/createProxyInExecute
Create the powershell proxy during execute in order to better handle exceptions.
2 parents 2f7bfe2 + 79c5c5e commit a61881c

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

powershell_kernel/kernel.py

+29-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import re
88
import signal
99
import urllib
10+
import sys
11+
import traceback
1012

1113
from powershell_kernel import subprocess_repl, powershell_proxy
1214
from powershell_kernel.util import get_powershell
@@ -35,9 +37,9 @@ def banner(self):
3537
'mimetype': 'text/x-sh',
3638
'file_extension': '.ps1'}
3739

38-
def __init__(self, **kwargs):
39-
Kernel.__init__(self, **kwargs)
40-
40+
proxy = None
41+
42+
def __createProxy(self):
4143
# powershell_command env variable is set by the kernel to allow both powershell and pwsh
4244
# but on python2 we cannot pass it thru env variable, see https://github.com/vors/jupyter-powershell/issues/7
4345
# TODO(python2): can we pass it somehow differently and still provide user-picked value on python2?
@@ -51,15 +53,30 @@ def __init__(self, **kwargs):
5153

5254
def do_execute(self, code, silent, store_history=True,
5355
user_expressions=None, allow_stdin=False):
54-
if not code.strip():
56+
try:
57+
if not code.strip():
58+
return {'status': 'ok', 'execution_count': self.execution_count,
59+
'payload': [], 'user_expressions': {}}
60+
61+
if not self.proxy:
62+
self.__createProxy()
63+
64+
self.proxy.send_input('. { ' + code + ' }')
65+
output = self.proxy.get_output()
66+
67+
message = {'name': 'stdout', 'text': output}
68+
self.send_response(self.iopub_socket, 'stream', message)
69+
5570
return {'status': 'ok', 'execution_count': self.execution_count,
5671
'payload': [], 'user_expressions': {}}
57-
58-
self.proxy.send_input('. { ' + code + ' }')
59-
output = self.proxy.get_output()
60-
61-
message = {'name': 'stdout', 'text': output}
62-
self.send_response(self.iopub_socket, 'stream', message)
6372

64-
return {'status': 'ok', 'execution_count': self.execution_count,
65-
'payload': [], 'user_expressions': {}}
73+
except Exception:
74+
excInfo = sys.exc_info()
75+
message = {
76+
'ename': str(excInfo[0].__name__),
77+
'evalue': str(excInfo[1]),
78+
'traceback': [traceback.format_exc()]
79+
}
80+
self.send_response(self.iopub_socket, 'error', message)
81+
message['status'] = 'error'
82+
return message

0 commit comments

Comments
 (0)
0