1616from collections import deque
1717import os
1818import warnings
19-
19+ from encodings import utf_8 , ascii
2020try :
2121 import thread
2222except ImportError :
5050 from queue import Empty # Python 3
5151
5252DEBUG = os .environ .get ('DEBUG_DJAYAMANNE_IPYTHON' ) is not None
53- TEST = os .environ .get ('TEST_DJAYAMANNE_IPYTHON ' ) is not None
54- TEST = True
53+ TEST = os .environ .get ('PYTHON_DONJAYAMANNE_TEST ' ) is not None
54+
5555# The great "support IPython 2, 3, 4" strat begins
5656if not TEST :
5757 try :
@@ -146,7 +146,7 @@ def connect(self, port):
146146 _debug_write ('Connecting to socket port: ' + str (port ))
147147 self .conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
148148 self .conn .connect (('127.0.0.1' , port ))
149- _debug_write ('Connected to socket port: ' + str ( port ) )
149+ _debug_write ('Connected to socket' )
150150
151151 # perform the handshake
152152 with self .send_lock :
@@ -171,31 +171,25 @@ def start_processing(self):
171171 # the next command.
172172 self .flush ()
173173 self .conn .settimeout (10 )
174- print ('check bytes' )
175- _debug_write ('Read command bytes' )
176174 try :
177175 inp = read_bytes (self .conn , 4 )
178-
179- # self.conn.settimeout(None)
180- _debug_write ('Command bytes received: ' )
181-
176+ self .conn .settimeout (None )
182177 cmd = iPythonSocketServer ._COMMANDS .get (inp )
183178 if inp and cmd is not None :
184179 id = ""
185180 try :
186181 if iPythonSocketServer ._COMMANDS_WITH_IDS .get (inp ) == True :
187- id = read_string (self .conn )
188- if TEST :
189- self .replyWithError (inp , id )
190- else :
191- cmd (self , id )
182+ while True :
183+ try :
184+ id = read_string (self .conn )
185+ break
186+ except socket .timeout :
187+ pass
188+ cmd (self , id )
192189 else :
193- if TEST :
194- self .replyWithError (inp , id )
195- else :
196- cmd (self )
190+ cmd (self )
197191 except :
198- self .replyWithError (inp , id )
192+ self .replyWithError (cmd , id )
199193 else :
200194 if inp :
201195 print ('unknown command' , inp )
@@ -224,35 +218,35 @@ def start_processing(self):
224218 def check_for_exit_socket_loop (self ):
225219 return self .exit_requested
226220
227- def replyForTest (self , commandName , id ):
228- message = read_string (self .conn )
229- with self .send_lock :
230- _debug_write ('Test response for Command: ' + commandName )
231- write_bytes (self .conn , iPythonSocketServer ._TEST )
232- write_string (self .conn , commandName )
233- write_string (self .conn , "" if id is None else id )
234- write_string (self .conn , message )
235-
236221 def replyWithError (self , commandName , id ):
237222 with self .send_lock :
238- _debug_write ('Replying with error' )
223+ traceMessage = traceback .format_exc ()
224+ _debug_write ('Replying with error:' + traceMessage )
225+
239226 write_bytes (self .conn , iPythonSocketServer ._EROR )
240227 write_string (self .conn , commandName )
241228 write_string (self .conn , "" if id is None else id )
229+ write_string (self .conn , traceMessage )
242230
243231 def _cmd_exit (self ):
244232 """exits the interactive process"""
245233 self .exit_requested = True
246234 self .exit_process ()
247235
248- def _cmd_ping (self ):
236+ def _cmd_ping (self , id ):
249237 """ping"""
250238 _debug_write ('Ping received' )
251- message = read_string (self .conn )
239+ while True :
240+ try :
241+ message = read_string (self .conn )
242+ break
243+ except socket .timeout :
244+ pass
252245 with self .send_lock :
253246 _debug_write ('Pong response being sent out' )
254247 write_bytes (self .conn , iPythonSocketServer ._PONG )
255- write_string (self .conn , "pong received with message" + message )
248+ write_string (self .conn , id )
249+ write_string (self .conn , message )
256250
257251 def _cmd_lstk (self , id ):
258252 """List kernel specs"""
@@ -359,6 +353,7 @@ def flush(self):
359353
360354 _COMMANDS_WITH_IDS = {
361355 to_bytes ('lstk' ): True ,
356+ to_bytes ('ping' ): True ,
362357 }
363358
364359
@@ -414,6 +409,9 @@ def watch(self):
414409 _debug_write ('Socket port received: ' + str (port ))
415410 server = iPythonSocketServer ()
416411 server .connect (port )
412+ sys .__stdout__ .write ('Started' )
413+ sys .__stdout__ .write ("\n " )
414+ sys .__stdout__ .flush ()
417415 while True :
418416 try :
419417 self ._process_request (self ._input .readline ())
0 commit comments