7
7
import re
8
8
import signal
9
9
import urllib
10
+ import sys
11
+ import traceback
10
12
11
13
from powershell_kernel import subprocess_repl , powershell_proxy
12
14
from powershell_kernel .util import get_powershell
@@ -35,9 +37,9 @@ def banner(self):
35
37
'mimetype' : 'text/x-sh' ,
36
38
'file_extension' : '.ps1' }
37
39
38
- def __init__ ( self , ** kwargs ):
39
- Kernel . __init__ ( self , ** kwargs )
40
-
40
+ proxy = None
41
+
42
+ def __createProxy ( self ):
41
43
# powershell_command env variable is set by the kernel to allow both powershell and pwsh
42
44
# but on python2 we cannot pass it thru env variable, see https://github.com/vors/jupyter-powershell/issues/7
43
45
# TODO(python2): can we pass it somehow differently and still provide user-picked value on python2?
@@ -51,15 +53,30 @@ def __init__(self, **kwargs):
51
53
52
54
def do_execute (self , code , silent , store_history = True ,
53
55
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
+
55
70
return {'status' : 'ok' , 'execution_count' : self .execution_count ,
56
71
'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 )
63
72
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