Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a0e6c3

Browse files
committed
1. Eliminate putrequest(): only used in asynccall(), merge it there.
2. Add additional debugging statements and enhance others. 3. Clarify comments. 4. Move SocketIO.nextseq class attribute to beginning of class.
1 parent 8d81a01 commit 0a0e6c3

1 file changed

Lines changed: 36 additions & 19 deletions

File tree

Lib/idlelib/rpc.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def get_request(self):
9090

9191
class SocketIO:
9292

93+
nextseq = 0
94+
9395
def __init__(self, sock, objtable=None, debugging=None):
9496
self.mainthread = threading.currentThread()
9597
if debugging is not None:
@@ -174,27 +176,29 @@ def localcall(self, request):
174176
return ("EXCEPTION", (mod, name, args, tb))
175177

176178
def remotecall(self, oid, methodname, args, kwargs):
177-
self.debug("remotecall:")
179+
self.debug("calling asynccall via remotecall")
178180
seq = self.asynccall(oid, methodname, args, kwargs)
179181
return self.asyncreturn(seq)
180182

181183
def asynccall(self, oid, methodname, args, kwargs):
182184
request = ("call", (oid, methodname, args, kwargs))
183-
seq = self.putrequest(request)
184-
self.debug(("asyncall:%d:" % seq), oid, methodname, args, kwargs)
185+
seq = self.newseq()
186+
self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs)
187+
self.putmessage((seq, request))
185188
return seq
186189

187190
def asyncreturn(self, seq):
191+
self.debug("asyncreturn:%d:call getresponse(): " % seq)
188192
response = self.getresponse(seq)
189-
self.debug(("asyncreturn:%d:" % seq), response)
193+
self.debug(("asyncreturn:%d:response: " % seq), response)
190194
return self.decoderesponse(response)
191195

192196
def decoderesponse(self, response):
193197
how, what = response
194198
if how == "OK":
195199
return what
196200
if how == "EXCEPTION":
197-
self.debug("decoderesponse: Internal EXCEPTION:", what)
201+
self.debug("decoderesponse: EXCEPTION:", what)
198202
mod, name, args, tb = what
199203
self.traceback = tb
200204
if mod: # not string exception
@@ -220,6 +224,12 @@ def decoderesponse(self, response):
220224
raise SystemError, (how, what)
221225

222226
def mainloop(self):
227+
"""Listen on socket until I/O not ready or EOF
228+
229+
pollpacket() will loop looking for seq number None, which never
230+
comes. The loop will exit when self.ioready() returns 0.
231+
232+
"""
223233
try:
224234
self.getresponse(None)
225235
except EOFError:
@@ -242,8 +252,10 @@ def _proxify(self, obj):
242252
return obj
243253

244254
def _getresponse(self, myseq):
255+
self.debug("_getresponse:myseq:", myseq)
245256
if threading.currentThread() is self.mainthread:
246-
# Main thread: does all reading of requests and responses
257+
# Main thread: does all reading of requests or responses
258+
# Loop here until there is message traffic on the socket
247259
while 1:
248260
response = self.pollresponse(myseq, None)
249261
if response is not None:
@@ -259,21 +271,14 @@ def _getresponse(self, myseq):
259271
del self.responses[myseq]
260272
del self.cvars[myseq]
261273
self.statelock.release()
262-
return response
263-
264-
def putrequest(self, request):
265-
seq = self.newseq()
266-
self.putmessage((seq, request))
267-
return seq
268-
269-
nextseq = 0
274+
return response # might be None
270275

271276
def newseq(self):
272277
self.nextseq = seq = self.nextseq + 2
273278
return seq
274279

275280
def putmessage(self, message):
276-
##self.debug("putmessage: ", message)
281+
self.debug("putmessage:%d:" % message[0])
277282
try:
278283
s = pickle.dumps(message)
279284
except:
@@ -337,16 +342,28 @@ def pollmessage(self, wait=0.0):
337342
return message
338343

339344
def pollresponse(self, myseq, wait=0.0):
340-
# Loop while there's no more buffered input or until specific response
345+
"""Handle messages received on the socket.
346+
347+
Some messages received may be asynchronous 'call' commands, and
348+
some may be responses intended for other threads.
349+
350+
Loop until message with myseq sequence number is received. Save others
351+
in self.responses and notify the owning thread, except that 'call'
352+
commands are handed off to localcall() and the response sent back
353+
across the link with the appropriate sequence number.
354+
355+
"""
341356
while 1:
342357
message = self.pollmessage(wait)
343-
if message is None:
358+
if message is None: # socket not ready
344359
return None
345360
wait = 0.0
346361
seq, resq = message
362+
self.debug("pollresponse:%d:myseq:%s" % (seq, myseq))
347363
if resq[0] == "call":
348-
self.debug("call_localcall:%d:" % seq)
364+
self.debug("pollresponse:%d:call_localcall" % seq)
349365
response = self.localcall(resq)
366+
self.debug("pollresponse:%d:response:%s" % (seq, response))
350367
self.putmessage((seq, response))
351368
continue
352369
elif seq == myseq:
@@ -410,7 +427,7 @@ def __init__(self, address, family=socket.AF_INET, type=socket.SOCK_STREAM):
410427
def accept(self):
411428
working_sock, address = self.listening_sock.accept()
412429
if self.debugging:
413-
print>>sys.__stderr__, "** Connection request from ", address
430+
print>>sys.__stderr__, "****** Connection request from ", address
414431
if address[0] == '127.0.0.1':
415432
SocketIO.__init__(self, working_sock)
416433
else:

0 commit comments

Comments
 (0)