Current Path: > > opt > alt > python27 > lib64 > python2.7 > idlelib
Operation : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64 Software : Apache Server IP : 162.0.232.56 | Your IP: 216.73.216.111 Domains : 1034 Domain(s) Permission : [ 0755 ]
Name | Type | Size | Last Modified | Actions |
---|---|---|---|---|
Icons | Directory | - | - | |
idle_test | Directory | - | - | |
AutoComplete.py | File | 8964 bytes | January 08 2025 10:43:10. | |
AutoComplete.pyc | File | 8181 bytes | January 08 2025 10:43:10. | |
AutoComplete.pyo | File | 8181 bytes | January 08 2025 10:43:10. | |
AutoCompleteWindow.py | File | 17318 bytes | January 08 2025 10:43:09. | |
AutoCompleteWindow.pyc | File | 12714 bytes | January 08 2025 10:43:10. | |
AutoCompleteWindow.pyo | File | 12655 bytes | January 08 2025 10:43:11. | |
AutoExpand.py | File | 3395 bytes | January 08 2025 10:43:10. | |
AutoExpand.pyc | File | 3580 bytes | January 08 2025 10:43:11. | |
AutoExpand.pyo | File | 3580 bytes | January 08 2025 10:43:11. | |
Bindings.py | File | 2976 bytes | January 08 2025 10:43:09. | |
Bindings.pyc | File | 4708 bytes | January 08 2025 10:43:09. | |
Bindings.pyo | File | 4708 bytes | January 08 2025 10:43:09. | |
CREDITS.txt | File | 1866 bytes | January 08 2025 10:43:11. | |
CallTipWindow.py | File | 6065 bytes | January 08 2025 10:43:09. | |
CallTipWindow.pyc | File | 6292 bytes | January 08 2025 10:43:09. | |
CallTipWindow.pyo | File | 6292 bytes | January 08 2025 10:43:09. | |
CallTips.py | File | 7740 bytes | January 08 2025 10:43:09. | |
CallTips.pyc | File | 8354 bytes | January 08 2025 10:43:10. | |
CallTips.pyo | File | 8354 bytes | January 08 2025 10:43:10. | |
ChangeLog | File | 56393 bytes | January 08 2025 10:43:11. | |
ClassBrowser.py | File | 6999 bytes | January 08 2025 10:43:11. | |
ClassBrowser.pyc | File | 9890 bytes | January 08 2025 10:43:10. | |
ClassBrowser.pyo | File | 9890 bytes | January 08 2025 10:43:10. | |
CodeContext.py | File | 8342 bytes | January 08 2025 10:43:09. | |
CodeContext.pyc | File | 6788 bytes | January 08 2025 10:43:11. | |
CodeContext.pyo | File | 6744 bytes | January 08 2025 10:43:09. | |
ColorDelegator.py | File | 9762 bytes | January 08 2025 10:43:09. | |
ColorDelegator.pyc | File | 9119 bytes | January 08 2025 10:43:10. | |
ColorDelegator.pyo | File | 9119 bytes | January 08 2025 10:43:10. | |
Debugger.py | File | 18236 bytes | January 08 2025 10:43:10. | |
Debugger.pyc | File | 18144 bytes | January 08 2025 10:43:10. | |
Debugger.pyo | File | 18144 bytes | January 08 2025 10:43:10. | |
Delegator.py | File | 665 bytes | January 08 2025 10:43:10. | |
Delegator.pyc | File | 1346 bytes | January 08 2025 10:43:09. | |
Delegator.pyo | File | 1346 bytes | January 08 2025 10:43:09. | |
EditorWindow.py | File | 65498 bytes | January 08 2025 10:43:09. | |
EditorWindow.pyc | File | 58522 bytes | January 08 2025 10:43:09. | |
EditorWindow.pyo | File | 58420 bytes | January 08 2025 10:43:10. | |
FileList.py | File | 3718 bytes | January 08 2025 10:43:10. | |
FileList.pyc | File | 4168 bytes | January 08 2025 10:43:11. | |
FileList.pyo | File | 4135 bytes | January 08 2025 10:43:10. | |
FormatParagraph.py | File | 7287 bytes | January 08 2025 10:43:11. | |
FormatParagraph.pyc | File | 7311 bytes | January 08 2025 10:43:09. | |
FormatParagraph.pyo | File | 7311 bytes | January 08 2025 10:43:09. | |
GrepDialog.py | File | 5145 bytes | January 08 2025 10:43:11. | |
GrepDialog.pyc | File | 6607 bytes | January 08 2025 10:43:11. | |
GrepDialog.pyo | File | 6607 bytes | January 08 2025 10:43:11. | |
HISTORY.txt | File | 10317 bytes | January 08 2025 10:43:09. | |
HyperParser.py | File | 10497 bytes | January 08 2025 10:43:09. | |
HyperParser.pyc | File | 6803 bytes | January 08 2025 10:43:09. | |
HyperParser.pyo | File | 6803 bytes | January 08 2025 10:43:09. | |
IOBinding.py | File | 21911 bytes | January 08 2025 10:43:09. | |
IOBinding.pyc | File | 19068 bytes | January 08 2025 10:43:11. | |
IOBinding.pyo | File | 19068 bytes | January 08 2025 10:43:11. | |
IdleHistory.py | File | 4052 bytes | January 08 2025 10:43:11. | |
IdleHistory.pyc | File | 4151 bytes | January 08 2025 10:43:11. | |
IdleHistory.pyo | File | 4151 bytes | January 08 2025 10:43:11. | |
MultiCall.py | File | 17701 bytes | January 08 2025 10:43:10. | |
MultiCall.pyc | File | 16793 bytes | January 08 2025 10:43:09. | |
MultiCall.pyo | File | 16719 bytes | January 08 2025 10:43:09. | |
MultiStatusBar.py | File | 1348 bytes | January 08 2025 10:43:09. | |
MultiStatusBar.pyc | File | 2357 bytes | January 08 2025 10:43:11. | |
MultiStatusBar.pyo | File | 2357 bytes | January 08 2025 10:43:11. | |
NEWS.txt | File | 47247 bytes | January 08 2025 10:43:10. | |
ObjectBrowser.py | File | 4376 bytes | January 08 2025 10:43:10. | |
ObjectBrowser.pyc | File | 7431 bytes | January 08 2025 10:43:10. | |
ObjectBrowser.pyo | File | 7431 bytes | January 08 2025 10:43:10. | |
OutputWindow.py | File | 4576 bytes | January 08 2025 10:43:11. | |
OutputWindow.pyc | File | 5427 bytes | January 08 2025 10:43:10. | |
OutputWindow.pyo | File | 5427 bytes | January 08 2025 10:43:10. | |
ParenMatch.py | File | 6714 bytes | January 08 2025 10:43:10. | |
ParenMatch.pyc | File | 7350 bytes | January 08 2025 10:43:11. | |
ParenMatch.pyo | File | 7350 bytes | January 08 2025 10:43:11. | |
PathBrowser.py | File | 3009 bytes | January 08 2025 10:43:09. | |
PathBrowser.pyc | File | 4681 bytes | January 08 2025 10:43:11. | |
PathBrowser.pyo | File | 4681 bytes | January 08 2025 10:43:11. | |
Percolator.py | File | 3222 bytes | January 08 2025 10:43:11. | |
Percolator.pyc | File | 4803 bytes | January 08 2025 10:43:11. | |
Percolator.pyo | File | 4619 bytes | January 08 2025 10:43:09. | |
PyParse.py | File | 19510 bytes | January 08 2025 10:43:10. | |
PyParse.pyc | File | 10226 bytes | January 08 2025 10:43:09. | |
PyParse.pyo | File | 9788 bytes | January 08 2025 10:43:10. | |
PyShell.py | File | 58872 bytes | January 08 2025 10:43:09. | |
PyShell.pyc | File | 54269 bytes | January 08 2025 10:43:10. | |
PyShell.pyo | File | 54167 bytes | January 08 2025 10:43:10. | |
README.txt | File | 7890 bytes | January 08 2025 10:43:10. | |
RemoteDebugger.py | File | 11632 bytes | January 08 2025 10:43:09. | |
RemoteDebugger.pyc | File | 17131 bytes | January 08 2025 10:43:10. | |
RemoteDebugger.pyo | File | 16977 bytes | January 08 2025 10:43:09. | |
RemoteObjectBrowser.py | File | 942 bytes | January 08 2025 10:43:09. | |
RemoteObjectBrowser.pyc | File | 2280 bytes | January 08 2025 10:43:10. | |
RemoteObjectBrowser.pyo | File | 2280 bytes | January 08 2025 10:43:10. | |
ReplaceDialog.py | File | 6639 bytes | January 08 2025 10:43:10. | |
ReplaceDialog.pyc | File | 8016 bytes | January 08 2025 10:43:09. | |
ReplaceDialog.pyo | File | 8016 bytes | January 08 2025 10:43:09. | |
RstripExtension.py | File | 1050 bytes | January 08 2025 10:43:10. | |
RstripExtension.pyc | File | 1665 bytes | January 08 2025 10:43:10. | |
RstripExtension.pyo | File | 1665 bytes | January 08 2025 10:43:10. | |
ScriptBinding.py | File | 8459 bytes | January 08 2025 10:43:10. | |
ScriptBinding.pyc | File | 8370 bytes | January 08 2025 10:43:09. | |
ScriptBinding.pyo | File | 8370 bytes | January 08 2025 10:43:09. | |
ScrolledList.py | File | 4373 bytes | January 08 2025 10:43:10. | |
ScrolledList.pyc | File | 6768 bytes | January 08 2025 10:43:09. | |
ScrolledList.pyo | File | 6768 bytes | January 08 2025 10:43:09. | |
SearchDialog.py | File | 2630 bytes | January 08 2025 10:43:09. | |
SearchDialog.pyc | File | 4139 bytes | January 08 2025 10:43:09. | |
SearchDialog.pyo | File | 4139 bytes | January 08 2025 10:43:09. | |
SearchDialogBase.py | File | 7094 bytes | January 08 2025 10:43:10. | |
SearchDialogBase.pyc | File | 8631 bytes | January 08 2025 10:43:11. | |
SearchDialogBase.pyo | File | 8631 bytes | January 08 2025 10:43:11. | |
SearchEngine.py | File | 7463 bytes | January 08 2025 10:43:11. | |
SearchEngine.pyc | File | 8575 bytes | January 08 2025 10:43:10. | |
SearchEngine.pyo | File | 8575 bytes | January 08 2025 10:43:10. | |
StackViewer.py | File | 4431 bytes | January 08 2025 10:43:11. | |
StackViewer.pyc | File | 6677 bytes | January 08 2025 10:43:09. | |
StackViewer.pyo | File | 6677 bytes | January 08 2025 10:43:09. | |
TODO.txt | File | 8478 bytes | January 08 2025 10:43:09. | |
ToolTip.py | File | 3173 bytes | January 08 2025 10:43:11. | |
ToolTip.pyc | File | 4890 bytes | January 08 2025 10:43:10. | |
ToolTip.pyo | File | 4890 bytes | January 08 2025 10:43:10. | |
TreeWidget.py | File | 15037 bytes | January 08 2025 10:43:09. | |
TreeWidget.pyc | File | 18383 bytes | January 08 2025 10:43:10. | |
TreeWidget.pyo | File | 18383 bytes | January 08 2025 10:43:10. | |
UndoDelegator.py | File | 10787 bytes | January 08 2025 10:43:09. | |
UndoDelegator.pyc | File | 14218 bytes | January 08 2025 10:43:09. | |
UndoDelegator.pyo | File | 14218 bytes | January 08 2025 10:43:09. | |
WidgetRedirector.py | File | 6906 bytes | January 08 2025 10:43:09. | |
WidgetRedirector.pyc | File | 7951 bytes | January 08 2025 10:43:11. | |
WidgetRedirector.pyo | File | 7951 bytes | January 08 2025 10:43:11. | |
WindowList.py | File | 2473 bytes | January 08 2025 10:43:09. | |
WindowList.pyc | File | 3831 bytes | January 08 2025 10:43:10. | |
WindowList.pyo | File | 3831 bytes | January 08 2025 10:43:10. | |
ZoomHeight.py | File | 1300 bytes | January 08 2025 10:43:11. | |
ZoomHeight.pyc | File | 1711 bytes | January 08 2025 10:43:09. | |
ZoomHeight.pyo | File | 1711 bytes | January 08 2025 10:43:09. | |
__init__.py | File | 288 bytes | January 08 2025 10:43:09. | |
__init__.pyc | File | 444 bytes | January 08 2025 10:43:11. | |
__init__.pyo | File | 444 bytes | January 08 2025 10:43:11. | |
aboutDialog.py | File | 7014 bytes | January 08 2025 10:43:09. | |
aboutDialog.pyc | File | 7017 bytes | January 08 2025 10:43:09. | |
aboutDialog.pyo | File | 7017 bytes | January 08 2025 10:43:09. | |
config-extensions.def | File | 2965 bytes | January 08 2025 10:43:10. | |
config-highlight.def | File | 2515 bytes | January 08 2025 10:43:11. | |
config-keys.def | File | 7777 bytes | January 08 2025 10:43:10. | |
config-main.def | File | 2561 bytes | January 08 2025 10:43:09. | |
configDialog.py | File | 65958 bytes | January 08 2025 10:43:09. | |
configDialog.pyc | File | 54435 bytes | January 08 2025 10:43:11. | |
configDialog.pyo | File | 54435 bytes | January 08 2025 10:43:11. | |
configHandler.py | File | 32485 bytes | January 08 2025 10:43:10. | |
configHandler.pyc | File | 29985 bytes | January 08 2025 10:43:11. | |
configHandler.pyo | File | 29985 bytes | January 08 2025 10:43:11. | |
configHelpSourceEdit.py | File | 6686 bytes | January 08 2025 10:43:10. | |
configHelpSourceEdit.pyc | File | 6712 bytes | January 08 2025 10:43:10. | |
configHelpSourceEdit.pyo | File | 6712 bytes | January 08 2025 10:43:10. | |
configSectionNameDialog.py | File | 4040 bytes | January 08 2025 10:43:09. | |
configSectionNameDialog.pyc | File | 4510 bytes | January 08 2025 10:43:11. | |
configSectionNameDialog.pyo | File | 4510 bytes | January 08 2025 10:43:11. | |
dynOptionMenuWidget.py | File | 1984 bytes | January 08 2025 10:43:09. | |
dynOptionMenuWidget.pyc | File | 2868 bytes | January 08 2025 10:43:09. | |
dynOptionMenuWidget.pyo | File | 2868 bytes | January 08 2025 10:43:09. | |
extend.txt | File | 3642 bytes | January 08 2025 10:43:11. | |
help.html | File | 42414 bytes | January 08 2025 10:43:10. | |
help.py | File | 11035 bytes | January 08 2025 10:43:10. | |
help.pyc | File | 12543 bytes | January 08 2025 10:43:09. | |
help.pyo | File | 12543 bytes | January 08 2025 10:43:09. | |
help.txt | File | 12144 bytes | January 08 2025 10:43:09. | |
idle.py | File | 453 bytes | January 08 2025 10:43:09. | |
idle.pyc | File | 423 bytes | January 08 2025 10:43:09. | |
idle.pyo | File | 423 bytes | January 08 2025 10:43:09. | |
idle.pyw | File | 563 bytes | January 08 2025 10:43:10. | |
idlever.py | File | 415 bytes | January 08 2025 10:43:10. | |
idlever.pyc | File | 591 bytes | January 08 2025 10:43:10. | |
idlever.pyo | File | 591 bytes | January 08 2025 10:43:10. | |
keybindingDialog.py | File | 12468 bytes | January 08 2025 10:43:09. | |
keybindingDialog.pyc | File | 12368 bytes | January 08 2025 10:43:10. | |
keybindingDialog.pyo | File | 12368 bytes | January 08 2025 10:43:10. | |
macosxSupport.py | File | 8435 bytes | January 08 2025 10:43:09. | |
macosxSupport.pyc | File | 8585 bytes | January 08 2025 10:43:10. | |
macosxSupport.pyo | File | 8447 bytes | January 08 2025 10:43:09. | |
rpc.py | File | 20150 bytes | January 08 2025 10:43:10. | |
rpc.pyc | File | 22495 bytes | January 08 2025 10:43:10. | |
rpc.pyo | File | 22389 bytes | January 08 2025 10:43:09. | |
run.py | File | 12917 bytes | January 08 2025 10:43:10. | |
run.pyc | File | 13770 bytes | January 08 2025 10:43:11. | |
run.pyo | File | 13712 bytes | January 08 2025 10:43:09. | |
tabbedpages.py | File | 18439 bytes | January 08 2025 10:43:09. | |
tabbedpages.pyc | File | 19081 bytes | January 08 2025 10:43:11. | |
tabbedpages.pyo | File | 19081 bytes | January 08 2025 10:43:11. | |
textView.py | File | 3520 bytes | January 08 2025 10:43:09. | |
textView.pyc | File | 4116 bytes | January 08 2025 10:43:10. | |
textView.pyo | File | 4116 bytes | January 08 2025 10:43:10. |
"""RPC Implementation, originally written for the Python Idle IDE For security reasons, GvR requested that Idle's Python execution server process connect to the Idle process, which listens for the connection. Since Idle has only one client per server, this was not a limitation. +---------------------------------+ +-------------+ | SocketServer.BaseRequestHandler | | SocketIO | +---------------------------------+ +-------------+ ^ | register() | | | unregister()| | +-------------+ | ^ ^ | | | | + -------------------+ | | | | +-------------------------+ +-----------------+ | RPCHandler | | RPCClient | | [attribute of RPCServer]| | | +-------------------------+ +-----------------+ The RPCServer handler class is expected to provide register/unregister methods. RPCHandler inherits the mix-in class SocketIO, which provides these methods. See the Idle run.main() docstring for further information on how this was accomplished in Idle. """ import sys import os import socket import select import SocketServer import struct import cPickle as pickle import threading import Queue import traceback import copy_reg import types import marshal def unpickle_code(ms): co = marshal.loads(ms) assert isinstance(co, types.CodeType) return co def pickle_code(co): assert isinstance(co, types.CodeType) ms = marshal.dumps(co) return unpickle_code, (ms,) # XXX KBK 24Aug02 function pickling capability not used in Idle # def unpickle_function(ms): # return ms # def pickle_function(fn): # assert isinstance(fn, type.FunctionType) # return repr(fn) copy_reg.pickle(types.CodeType, pickle_code, unpickle_code) # copy_reg.pickle(types.FunctionType, pickle_function, unpickle_function) BUFSIZE = 8*1024 LOCALHOST = '127.0.0.1' class RPCServer(SocketServer.TCPServer): def __init__(self, addr, handlerclass=None): if handlerclass is None: handlerclass = RPCHandler SocketServer.TCPServer.__init__(self, addr, handlerclass) def server_bind(self): "Override TCPServer method, no bind() phase for connecting entity" pass def server_activate(self): """Override TCPServer method, connect() instead of listen() Due to the reversed connection, self.server_address is actually the address of the Idle Client to which we are connecting. """ self.socket.connect(self.server_address) def get_request(self): "Override TCPServer method, return already connected socket" return self.socket, self.server_address def handle_error(self, request, client_address): """Override TCPServer method Error message goes to __stderr__. No error message if exiting normally or socket raised EOF. Other exceptions not handled in server code will cause os._exit. """ try: raise except SystemExit: raise except: erf = sys.__stderr__ print>>erf, '\n' + '-'*40 print>>erf, 'Unhandled server exception!' print>>erf, 'Thread: %s' % threading.currentThread().getName() print>>erf, 'Client Address: ', client_address print>>erf, 'Request: ', repr(request) traceback.print_exc(file=erf) print>>erf, '\n*** Unrecoverable, server exiting!' print>>erf, '-'*40 os._exit(0) #----------------- end class RPCServer -------------------- objecttable = {} request_queue = Queue.Queue(0) response_queue = Queue.Queue(0) class SocketIO(object): nextseq = 0 def __init__(self, sock, objtable=None, debugging=None): self.sockthread = threading.currentThread() if debugging is not None: self.debugging = debugging self.sock = sock if objtable is None: objtable = objecttable self.objtable = objtable self.responses = {} self.cvars = {} def close(self): sock = self.sock self.sock = None if sock is not None: sock.close() def exithook(self): "override for specific exit action" os._exit(0) def debug(self, *args): if not self.debugging: return s = self.location + " " + str(threading.currentThread().getName()) for a in args: s = s + " " + str(a) print>>sys.__stderr__, s def register(self, oid, object): self.objtable[oid] = object def unregister(self, oid): try: del self.objtable[oid] except KeyError: pass def localcall(self, seq, request): self.debug("localcall:", request) try: how, (oid, methodname, args, kwargs) = request except TypeError: return ("ERROR", "Bad request format") if oid not in self.objtable: return ("ERROR", "Unknown object id: %r" % (oid,)) obj = self.objtable[oid] if methodname == "__methods__": methods = {} _getmethods(obj, methods) return ("OK", methods) if methodname == "__attributes__": attributes = {} _getattributes(obj, attributes) return ("OK", attributes) if not hasattr(obj, methodname): return ("ERROR", "Unsupported method name: %r" % (methodname,)) method = getattr(obj, methodname) try: if how == 'CALL': ret = method(*args, **kwargs) if isinstance(ret, RemoteObject): ret = remoteref(ret) return ("OK", ret) elif how == 'QUEUE': request_queue.put((seq, (method, args, kwargs))) return("QUEUED", None) else: return ("ERROR", "Unsupported message type: %s" % how) except SystemExit: raise except socket.error: raise except: msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\ " Object: %s \n Method: %s \n Args: %s\n" print>>sys.__stderr__, msg % (oid, method, args) traceback.print_exc(file=sys.__stderr__) return ("EXCEPTION", None) def remotecall(self, oid, methodname, args, kwargs): self.debug("remotecall:asynccall: ", oid, methodname) seq = self.asynccall(oid, methodname, args, kwargs) return self.asyncreturn(seq) def remotequeue(self, oid, methodname, args, kwargs): self.debug("remotequeue:asyncqueue: ", oid, methodname) seq = self.asyncqueue(oid, methodname, args, kwargs) return self.asyncreturn(seq) def asynccall(self, oid, methodname, args, kwargs): request = ("CALL", (oid, methodname, args, kwargs)) seq = self.newseq() if threading.currentThread() != self.sockthread: cvar = threading.Condition() self.cvars[seq] = cvar self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs) self.putmessage((seq, request)) return seq def asyncqueue(self, oid, methodname, args, kwargs): request = ("QUEUE", (oid, methodname, args, kwargs)) seq = self.newseq() if threading.currentThread() != self.sockthread: cvar = threading.Condition() self.cvars[seq] = cvar self.debug(("asyncqueue:%d:" % seq), oid, methodname, args, kwargs) self.putmessage((seq, request)) return seq def asyncreturn(self, seq): self.debug("asyncreturn:%d:call getresponse(): " % seq) response = self.getresponse(seq, wait=0.05) self.debug(("asyncreturn:%d:response: " % seq), response) return self.decoderesponse(response) def decoderesponse(self, response): how, what = response if how == "OK": return what if how == "QUEUED": return None if how == "EXCEPTION": self.debug("decoderesponse: EXCEPTION") return None if how == "EOF": self.debug("decoderesponse: EOF") self.decode_interrupthook() return None if how == "ERROR": self.debug("decoderesponse: Internal ERROR:", what) raise RuntimeError, what raise SystemError, (how, what) def decode_interrupthook(self): "" raise EOFError def mainloop(self): """Listen on socket until I/O not ready or EOF pollresponse() will loop looking for seq number None, which never comes, and exit on EOFError. """ try: self.getresponse(myseq=None, wait=0.05) except EOFError: self.debug("mainloop:return") return def getresponse(self, myseq, wait): response = self._getresponse(myseq, wait) if response is not None: how, what = response if how == "OK": response = how, self._proxify(what) return response def _proxify(self, obj): if isinstance(obj, RemoteProxy): return RPCProxy(self, obj.oid) if isinstance(obj, types.ListType): return map(self._proxify, obj) # XXX Check for other types -- not currently needed return obj def _getresponse(self, myseq, wait): self.debug("_getresponse:myseq:", myseq) if threading.currentThread() is self.sockthread: # this thread does all reading of requests or responses while 1: response = self.pollresponse(myseq, wait) if response is not None: return response else: # wait for notification from socket handling thread cvar = self.cvars[myseq] cvar.acquire() while myseq not in self.responses: cvar.wait() response = self.responses[myseq] self.debug("_getresponse:%s: thread woke up: response: %s" % (myseq, response)) del self.responses[myseq] del self.cvars[myseq] cvar.release() return response def newseq(self): self.nextseq = seq = self.nextseq + 2 return seq def putmessage(self, message): self.debug("putmessage:%d:" % message[0]) try: s = pickle.dumps(message) except pickle.PicklingError: print >>sys.__stderr__, "Cannot pickle:", repr(message) raise s = struct.pack("<i", len(s)) + s while len(s) > 0: try: r, w, x = select.select([], [self.sock], []) n = self.sock.send(s[:BUFSIZE]) except (AttributeError, TypeError): raise IOError, "socket no longer exists" s = s[n:] buffer = "" bufneed = 4 bufstate = 0 # meaning: 0 => reading count; 1 => reading data def pollpacket(self, wait): self._stage0() if len(self.buffer) < self.bufneed: r, w, x = select.select([self.sock.fileno()], [], [], wait) if len(r) == 0: return None try: s = self.sock.recv(BUFSIZE) except socket.error: raise EOFError if len(s) == 0: raise EOFError self.buffer += s self._stage0() return self._stage1() def _stage0(self): if self.bufstate == 0 and len(self.buffer) >= 4: s = self.buffer[:4] self.buffer = self.buffer[4:] self.bufneed = struct.unpack("<i", s)[0] self.bufstate = 1 def _stage1(self): if self.bufstate == 1 and len(self.buffer) >= self.bufneed: packet = self.buffer[:self.bufneed] self.buffer = self.buffer[self.bufneed:] self.bufneed = 4 self.bufstate = 0 return packet def pollmessage(self, wait): packet = self.pollpacket(wait) if packet is None: return None try: message = pickle.loads(packet) except pickle.UnpicklingError: print >>sys.__stderr__, "-----------------------" print >>sys.__stderr__, "cannot unpickle packet:", repr(packet) traceback.print_stack(file=sys.__stderr__) print >>sys.__stderr__, "-----------------------" raise return message def pollresponse(self, myseq, wait): """Handle messages received on the socket. Some messages received may be asynchronous 'call' or 'queue' requests, and some may be responses for other threads. 'call' requests are passed to self.localcall() with the expectation of immediate execution, during which time the socket is not serviced. 'queue' requests are used for tasks (which may block or hang) to be processed in a different thread. These requests are fed into request_queue by self.localcall(). Responses to queued requests are taken from response_queue and sent across the link with the associated sequence numbers. Messages in the queues are (sequence_number, request/response) tuples and code using this module removing messages from the request_queue is responsible for returning the correct sequence number in the response_queue. pollresponse() will loop until a response message with the myseq sequence number is received, and will save other responses in self.responses and notify the owning thread. """ while 1: # send queued response if there is one available try: qmsg = response_queue.get(0) except Queue.Empty: pass else: seq, response = qmsg message = (seq, ('OK', response)) self.putmessage(message) # poll for message on link try: message = self.pollmessage(wait) if message is None: # socket not ready return None except EOFError: self.handle_EOF() return None except AttributeError: return None seq, resq = message how = resq[0] self.debug("pollresponse:%d:myseq:%s" % (seq, myseq)) # process or queue a request if how in ("CALL", "QUEUE"): self.debug("pollresponse:%d:localcall:call:" % seq) response = self.localcall(seq, resq) self.debug("pollresponse:%d:localcall:response:%s" % (seq, response)) if how == "CALL": self.putmessage((seq, response)) elif how == "QUEUE": # don't acknowledge the 'queue' request! pass continue # return if completed message transaction elif seq == myseq: return resq # must be a response for a different thread: else: cv = self.cvars.get(seq, None) # response involving unknown sequence number is discarded, # probably intended for prior incarnation of server if cv is not None: cv.acquire() self.responses[seq] = resq cv.notify() cv.release() continue def handle_EOF(self): "action taken upon link being closed by peer" self.EOFhook() self.debug("handle_EOF") for key in self.cvars: cv = self.cvars[key] cv.acquire() self.responses[key] = ('EOF', None) cv.notify() cv.release() # call our (possibly overridden) exit function self.exithook() def EOFhook(self): "Classes using rpc client/server can override to augment EOF action" pass #----------------- end class SocketIO -------------------- class RemoteObject(object): # Token mix-in class pass def remoteref(obj): oid = id(obj) objecttable[oid] = obj return RemoteProxy(oid) class RemoteProxy(object): def __init__(self, oid): self.oid = oid class RPCHandler(SocketServer.BaseRequestHandler, SocketIO): debugging = False location = "#S" # Server def __init__(self, sock, addr, svr): svr.current_handler = self ## cgt xxx SocketIO.__init__(self, sock) SocketServer.BaseRequestHandler.__init__(self, sock, addr, svr) def handle(self): "handle() method required by SocketServer" self.mainloop() def get_remote_proxy(self, oid): return RPCProxy(self, oid) class RPCClient(SocketIO): debugging = False location = "#C" # Client nextseq = 1 # Requests coming from the client are odd numbered def __init__(self, address, family=socket.AF_INET, type=socket.SOCK_STREAM): self.listening_sock = socket.socket(family, type) self.listening_sock.bind(address) self.listening_sock.listen(1) def accept(self): working_sock, address = self.listening_sock.accept() if self.debugging: print>>sys.__stderr__, "****** Connection request from ", address if address[0] == LOCALHOST: SocketIO.__init__(self, working_sock) else: print>>sys.__stderr__, "** Invalid host: ", address raise socket.error def get_remote_proxy(self, oid): return RPCProxy(self, oid) class RPCProxy(object): __methods = None __attributes = None def __init__(self, sockio, oid): self.sockio = sockio self.oid = oid def __getattr__(self, name): if self.__methods is None: self.__getmethods() if self.__methods.get(name): return MethodProxy(self.sockio, self.oid, name) if self.__attributes is None: self.__getattributes() if name in self.__attributes: value = self.sockio.remotecall(self.oid, '__getattribute__', (name,), {}) return value else: raise AttributeError, name def __getattributes(self): self.__attributes = self.sockio.remotecall(self.oid, "__attributes__", (), {}) def __getmethods(self): self.__methods = self.sockio.remotecall(self.oid, "__methods__", (), {}) def _getmethods(obj, methods): # Helper to get a list of methods from an object # Adds names to dictionary argument 'methods' for name in dir(obj): attr = getattr(obj, name) if hasattr(attr, '__call__'): methods[name] = 1 if type(obj) == types.InstanceType: _getmethods(obj.__class__, methods) if type(obj) == types.ClassType: for super in obj.__bases__: _getmethods(super, methods) def _getattributes(obj, attributes): for name in dir(obj): attr = getattr(obj, name) if not hasattr(attr, '__call__'): attributes[name] = 1 class MethodProxy(object): def __init__(self, sockio, oid, name): self.sockio = sockio self.oid = oid self.name = name def __call__(self, *args, **kwargs): value = self.sockio.remotecall(self.oid, self.name, args, kwargs) return value # XXX KBK 09Sep03 We need a proper unit test for this module. Previously # existing test code was removed at Rev 1.27 (r34098).
SILENT KILLER Tool