Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 1 | """RPC Implemention, originally written for the Python Idle IDE |
| 2 | |
| 3 | For security reasons, GvR requested that Idle's Python execution server process |
| 4 | connect to the Idle process, which listens for the connection. Since Idle has |
Terry Jan Reedy | c30b7b1 | 2013-03-11 17:57:08 -0400 | [diff] [blame] | 5 | only one client per server, this was not a limitation. |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 6 | |
| 7 | +---------------------------------+ +-------------+ |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 8 | | socketserver.BaseRequestHandler | | SocketIO | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 9 | +---------------------------------+ +-------------+ |
| 10 | ^ | register() | |
| 11 | | | unregister()| |
| 12 | | +-------------+ |
| 13 | | ^ ^ |
| 14 | | | | |
| 15 | | + -------------------+ | |
| 16 | | | | |
| 17 | +-------------------------+ +-----------------+ |
| 18 | | RPCHandler | | RPCClient | |
| 19 | | [attribute of RPCServer]| | | |
| 20 | +-------------------------+ +-----------------+ |
| 21 | |
| 22 | The RPCServer handler class is expected to provide register/unregister methods. |
| 23 | RPCHandler inherits the mix-in class SocketIO, which provides these methods. |
| 24 | |
| 25 | See the Idle run.main() docstring for further information on how this was |
| 26 | accomplished in Idle. |
| 27 | |
| 28 | """ |
| 29 | |
| 30 | import sys |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 31 | import os |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 32 | import socket |
| 33 | import select |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 34 | import socketserver |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 35 | import struct |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 36 | import pickle |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 37 | import threading |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 38 | import queue |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 39 | import traceback |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 40 | import copyreg |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 41 | import types |
| 42 | import marshal |
Andrew Svetlov | cd49d53 | 2012-03-25 11:43:02 +0300 | [diff] [blame] | 43 | import builtins |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 44 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 45 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 46 | def unpickle_code(ms): |
| 47 | co = marshal.loads(ms) |
| 48 | assert isinstance(co, types.CodeType) |
| 49 | return co |
| 50 | |
| 51 | def pickle_code(co): |
| 52 | assert isinstance(co, types.CodeType) |
| 53 | ms = marshal.dumps(co) |
| 54 | return unpickle_code, (ms,) |
| 55 | |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 56 | # XXX KBK 24Aug02 function pickling capability not used in Idle |
| 57 | # def unpickle_function(ms): |
| 58 | # return ms |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 59 | |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 60 | # def pickle_function(fn): |
| 61 | # assert isinstance(fn, type.FunctionType) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 62 | # return repr(fn) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 63 | |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 64 | copyreg.pickle(types.CodeType, pickle_code, unpickle_code) |
| 65 | # copyreg.pickle(types.FunctionType, pickle_function, unpickle_function) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 66 | |
| 67 | BUFSIZE = 8*1024 |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 68 | LOCALHOST = '127.0.0.1' |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 69 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 70 | class RPCServer(socketserver.TCPServer): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 71 | |
| 72 | def __init__(self, addr, handlerclass=None): |
| 73 | if handlerclass is None: |
| 74 | handlerclass = RPCHandler |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 75 | socketserver.TCPServer.__init__(self, addr, handlerclass) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 76 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 77 | def server_bind(self): |
| 78 | "Override TCPServer method, no bind() phase for connecting entity" |
| 79 | pass |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 80 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 81 | def server_activate(self): |
| 82 | """Override TCPServer method, connect() instead of listen() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 83 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 84 | Due to the reversed connection, self.server_address is actually the |
| 85 | address of the Idle Client to which we are connecting. |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 86 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 87 | """ |
| 88 | self.socket.connect(self.server_address) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 89 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 90 | def get_request(self): |
| 91 | "Override TCPServer method, return already connected socket" |
| 92 | return self.socket, self.server_address |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 93 | |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 94 | def handle_error(self, request, client_address): |
Kurt B. Kaiser | e51529d | 2003-03-22 19:15:58 +0000 | [diff] [blame] | 95 | """Override TCPServer method |
| 96 | |
| 97 | Error message goes to __stderr__. No error message if exiting |
| 98 | normally or socket raised EOF. Other exceptions not handled in |
| 99 | server code will cause os._exit. |
| 100 | |
| 101 | """ |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 102 | try: |
| 103 | raise |
| 104 | except SystemExit: |
| 105 | raise |
Kurt B. Kaiser | e51529d | 2003-03-22 19:15:58 +0000 | [diff] [blame] | 106 | except: |
Kurt B. Kaiser | 0529377 | 2003-03-22 20:11:14 +0000 | [diff] [blame] | 107 | erf = sys.__stderr__ |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 108 | print('\n' + '-'*40, file=erf) |
| 109 | print('Unhandled server exception!', file=erf) |
Amaury Forgeot d'Arc | bed1710 | 2008-11-29 01:48:47 +0000 | [diff] [blame] | 110 | print('Thread: %s' % threading.current_thread().name, file=erf) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 111 | print('Client Address: ', client_address, file=erf) |
| 112 | print('Request: ', repr(request), file=erf) |
Kurt B. Kaiser | e51529d | 2003-03-22 19:15:58 +0000 | [diff] [blame] | 113 | traceback.print_exc(file=erf) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 114 | print('\n*** Unrecoverable, server exiting!', file=erf) |
| 115 | print('-'*40, file=erf) |
Kurt B. Kaiser | 0529377 | 2003-03-22 20:11:14 +0000 | [diff] [blame] | 116 | os._exit(0) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 117 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 118 | #----------------- end class RPCServer -------------------- |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 119 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 120 | objecttable = {} |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 121 | request_queue = queue.Queue(0) |
| 122 | response_queue = queue.Queue(0) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 123 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 124 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 125 | class SocketIO(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 126 | |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 127 | nextseq = 0 |
| 128 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 129 | def __init__(self, sock, objtable=None, debugging=None): |
Benjamin Peterson | b03ca4b | 2008-06-13 02:00:47 +0000 | [diff] [blame] | 130 | self.sockthread = threading.current_thread() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 131 | if debugging is not None: |
| 132 | self.debugging = debugging |
| 133 | self.sock = sock |
| 134 | if objtable is None: |
| 135 | objtable = objecttable |
| 136 | self.objtable = objtable |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 137 | self.responses = {} |
| 138 | self.cvars = {} |
| 139 | |
| 140 | def close(self): |
| 141 | sock = self.sock |
| 142 | self.sock = None |
| 143 | if sock is not None: |
| 144 | sock.close() |
| 145 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 146 | def exithook(self): |
| 147 | "override for specific exit action" |
Roger Serwy | 71c9e1a | 2013-03-31 01:00:15 -0500 | [diff] [blame] | 148 | os._exit(0) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 149 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 150 | def debug(self, *args): |
| 151 | if not self.debugging: |
| 152 | return |
Amaury Forgeot d'Arc | bed1710 | 2008-11-29 01:48:47 +0000 | [diff] [blame] | 153 | s = self.location + " " + str(threading.current_thread().name) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 154 | for a in args: |
| 155 | s = s + " " + str(a) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 156 | print(s, file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 157 | |
| 158 | def register(self, oid, object): |
| 159 | self.objtable[oid] = object |
| 160 | |
| 161 | def unregister(self, oid): |
| 162 | try: |
| 163 | del self.objtable[oid] |
| 164 | except KeyError: |
| 165 | pass |
| 166 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 167 | def localcall(self, seq, request): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 168 | self.debug("localcall:", request) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 169 | try: |
| 170 | how, (oid, methodname, args, kwargs) = request |
| 171 | except TypeError: |
| 172 | return ("ERROR", "Bad request format") |
Guido van Rossum | 811c4e0 | 2006-08-22 15:45:46 +0000 | [diff] [blame] | 173 | if oid not in self.objtable: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 174 | return ("ERROR", "Unknown object id: %r" % (oid,)) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 175 | obj = self.objtable[oid] |
| 176 | if methodname == "__methods__": |
| 177 | methods = {} |
| 178 | _getmethods(obj, methods) |
| 179 | return ("OK", methods) |
| 180 | if methodname == "__attributes__": |
| 181 | attributes = {} |
| 182 | _getattributes(obj, attributes) |
| 183 | return ("OK", attributes) |
| 184 | if not hasattr(obj, methodname): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 185 | return ("ERROR", "Unsupported method name: %r" % (methodname,)) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 186 | method = getattr(obj, methodname) |
| 187 | try: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 188 | if how == 'CALL': |
| 189 | ret = method(*args, **kwargs) |
| 190 | if isinstance(ret, RemoteObject): |
| 191 | ret = remoteref(ret) |
| 192 | return ("OK", ret) |
| 193 | elif how == 'QUEUE': |
| 194 | request_queue.put((seq, (method, args, kwargs))) |
| 195 | return("QUEUED", None) |
| 196 | else: |
| 197 | return ("ERROR", "Unsupported message type: %s" % how) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 198 | except SystemExit: |
| 199 | raise |
Andrew Svetlov | 05bab93 | 2012-03-14 13:22:12 -0700 | [diff] [blame] | 200 | except KeyboardInterrupt: |
| 201 | raise |
Kurt B. Kaiser | 9ac783d | 2003-03-10 20:42:24 +0000 | [diff] [blame] | 202 | except socket.error: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 203 | raise |
Andrew Svetlov | 05bab93 | 2012-03-14 13:22:12 -0700 | [diff] [blame] | 204 | except Exception as ex: |
| 205 | return ("CALLEXC", ex) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 206 | except: |
Kurt B. Kaiser | e852c19 | 2004-12-23 04:39:55 +0000 | [diff] [blame] | 207 | msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\ |
| 208 | " Object: %s \n Method: %s \n Args: %s\n" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 209 | print(msg % (oid, method, args), file=sys.__stderr__) |
Kurt B. Kaiser | 86bc464 | 2003-02-27 23:04:17 +0000 | [diff] [blame] | 210 | traceback.print_exc(file=sys.__stderr__) |
Kurt B. Kaiser | 8cd0def | 2003-01-31 05:06:43 +0000 | [diff] [blame] | 211 | return ("EXCEPTION", None) |
| 212 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 213 | def remotecall(self, oid, methodname, args, kwargs): |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 214 | self.debug("remotecall:asynccall: ", oid, methodname) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 215 | seq = self.asynccall(oid, methodname, args, kwargs) |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 216 | return self.asyncreturn(seq) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 217 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 218 | def remotequeue(self, oid, methodname, args, kwargs): |
| 219 | self.debug("remotequeue:asyncqueue: ", oid, methodname) |
| 220 | seq = self.asyncqueue(oid, methodname, args, kwargs) |
| 221 | return self.asyncreturn(seq) |
| 222 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 223 | def asynccall(self, oid, methodname, args, kwargs): |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 224 | request = ("CALL", (oid, methodname, args, kwargs)) |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 225 | seq = self.newseq() |
Benjamin Peterson | b03ca4b | 2008-06-13 02:00:47 +0000 | [diff] [blame] | 226 | if threading.current_thread() != self.sockthread: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 227 | cvar = threading.Condition() |
| 228 | self.cvars[seq] = cvar |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 229 | self.debug(("asynccall:%d:" % seq), oid, methodname, args, kwargs) |
| 230 | self.putmessage((seq, request)) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 231 | return seq |
| 232 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 233 | def asyncqueue(self, oid, methodname, args, kwargs): |
| 234 | request = ("QUEUE", (oid, methodname, args, kwargs)) |
| 235 | seq = self.newseq() |
Benjamin Peterson | b03ca4b | 2008-06-13 02:00:47 +0000 | [diff] [blame] | 236 | if threading.current_thread() != self.sockthread: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 237 | cvar = threading.Condition() |
| 238 | self.cvars[seq] = cvar |
| 239 | self.debug(("asyncqueue:%d:" % seq), oid, methodname, args, kwargs) |
| 240 | self.putmessage((seq, request)) |
| 241 | return seq |
| 242 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 243 | def asyncreturn(self, seq): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 244 | self.debug("asyncreturn:%d:call getresponse(): " % seq) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 245 | response = self.getresponse(seq, wait=0.05) |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 246 | self.debug(("asyncreturn:%d:response: " % seq), response) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 247 | return self.decoderesponse(response) |
| 248 | |
| 249 | def decoderesponse(self, response): |
| 250 | how, what = response |
| 251 | if how == "OK": |
| 252 | return what |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 253 | if how == "QUEUED": |
| 254 | return None |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 255 | if how == "EXCEPTION": |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 256 | self.debug("decoderesponse: EXCEPTION") |
| 257 | return None |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 258 | if how == "EOF": |
| 259 | self.debug("decoderesponse: EOF") |
| 260 | self.decode_interrupthook() |
| 261 | return None |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 262 | if how == "ERROR": |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 263 | self.debug("decoderesponse: Internal ERROR:", what) |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 264 | raise RuntimeError(what) |
Andrew Svetlov | 05bab93 | 2012-03-14 13:22:12 -0700 | [diff] [blame] | 265 | if how == "CALLEXC": |
| 266 | self.debug("decoderesponse: Call Exception:", what) |
| 267 | raise what |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 268 | raise SystemError(how, what) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 269 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 270 | def decode_interrupthook(self): |
| 271 | "" |
| 272 | raise EOFError |
| 273 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 274 | def mainloop(self): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 275 | """Listen on socket until I/O not ready or EOF |
| 276 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 277 | pollresponse() will loop looking for seq number None, which |
Kurt B. Kaiser | 94afd30 | 2003-03-12 20:52:00 +0000 | [diff] [blame] | 278 | never comes, and exit on EOFError. |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 279 | |
| 280 | """ |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 281 | try: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 282 | self.getresponse(myseq=None, wait=0.05) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 283 | except EOFError: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 284 | self.debug("mainloop:return") |
| 285 | return |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 286 | |
Kurt B. Kaiser | 94afd30 | 2003-03-12 20:52:00 +0000 | [diff] [blame] | 287 | def getresponse(self, myseq, wait): |
| 288 | response = self._getresponse(myseq, wait) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 289 | if response is not None: |
| 290 | how, what = response |
| 291 | if how == "OK": |
| 292 | response = how, self._proxify(what) |
| 293 | return response |
| 294 | |
| 295 | def _proxify(self, obj): |
| 296 | if isinstance(obj, RemoteProxy): |
| 297 | return RPCProxy(self, obj.oid) |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 298 | if isinstance(obj, list): |
Kurt B. Kaiser | 66aaf74 | 2007-08-09 18:00:23 +0000 | [diff] [blame] | 299 | return list(map(self._proxify, obj)) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 300 | # XXX Check for other types -- not currently needed |
| 301 | return obj |
| 302 | |
Kurt B. Kaiser | 94afd30 | 2003-03-12 20:52:00 +0000 | [diff] [blame] | 303 | def _getresponse(self, myseq, wait): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 304 | self.debug("_getresponse:myseq:", myseq) |
Benjamin Peterson | b03ca4b | 2008-06-13 02:00:47 +0000 | [diff] [blame] | 305 | if threading.current_thread() is self.sockthread: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 306 | # this thread does all reading of requests or responses |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 307 | while 1: |
Kurt B. Kaiser | 94afd30 | 2003-03-12 20:52:00 +0000 | [diff] [blame] | 308 | response = self.pollresponse(myseq, wait) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 309 | if response is not None: |
| 310 | return response |
| 311 | else: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 312 | # wait for notification from socket handling thread |
| 313 | cvar = self.cvars[myseq] |
| 314 | cvar.acquire() |
Guido van Rossum | 811c4e0 | 2006-08-22 15:45:46 +0000 | [diff] [blame] | 315 | while myseq not in self.responses: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 316 | cvar.wait() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 317 | response = self.responses[myseq] |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 318 | self.debug("_getresponse:%s: thread woke up: response: %s" % |
| 319 | (myseq, response)) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 320 | del self.responses[myseq] |
| 321 | del self.cvars[myseq] |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 322 | cvar.release() |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 323 | return response |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 324 | |
| 325 | def newseq(self): |
| 326 | self.nextseq = seq = self.nextseq + 2 |
| 327 | return seq |
| 328 | |
| 329 | def putmessage(self, message): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 330 | self.debug("putmessage:%d:" % message[0]) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 331 | try: |
| 332 | s = pickle.dumps(message) |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 333 | except pickle.PicklingError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 334 | print("Cannot pickle:", repr(message), file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 335 | raise |
| 336 | s = struct.pack("<i", len(s)) + s |
| 337 | while len(s) > 0: |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 338 | try: |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 339 | r, w, x = select.select([], [self.sock], []) |
| 340 | n = self.sock.send(s[:BUFSIZE]) |
Kurt B. Kaiser | 935ea9a | 2005-05-10 03:44:24 +0000 | [diff] [blame] | 341 | except (AttributeError, TypeError): |
Terry Jan Reedy | ba6c0d3 | 2013-06-08 00:22:45 -0400 | [diff] [blame] | 342 | raise OSError("socket no longer exists") |
Kurt B. Kaiser | 935ea9a | 2005-05-10 03:44:24 +0000 | [diff] [blame] | 343 | except socket.error: |
| 344 | raise |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 345 | else: |
| 346 | s = s[n:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 347 | |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 348 | buff = b'' |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 349 | bufneed = 4 |
| 350 | bufstate = 0 # meaning: 0 => reading count; 1 => reading data |
| 351 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 352 | def pollpacket(self, wait): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 353 | self._stage0() |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 354 | if len(self.buff) < self.bufneed: |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 355 | r, w, x = select.select([self.sock.fileno()], [], [], wait) |
| 356 | if len(r) == 0: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 357 | return None |
| 358 | try: |
| 359 | s = self.sock.recv(BUFSIZE) |
| 360 | except socket.error: |
| 361 | raise EOFError |
| 362 | if len(s) == 0: |
| 363 | raise EOFError |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 364 | self.buff += s |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 365 | self._stage0() |
| 366 | return self._stage1() |
| 367 | |
| 368 | def _stage0(self): |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 369 | if self.bufstate == 0 and len(self.buff) >= 4: |
| 370 | s = self.buff[:4] |
| 371 | self.buff = self.buff[4:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 372 | self.bufneed = struct.unpack("<i", s)[0] |
| 373 | self.bufstate = 1 |
| 374 | |
| 375 | def _stage1(self): |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 376 | if self.bufstate == 1 and len(self.buff) >= self.bufneed: |
| 377 | packet = self.buff[:self.bufneed] |
| 378 | self.buff = self.buff[self.bufneed:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 379 | self.bufneed = 4 |
| 380 | self.bufstate = 0 |
| 381 | return packet |
| 382 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 383 | def pollmessage(self, wait): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 384 | packet = self.pollpacket(wait) |
| 385 | if packet is None: |
| 386 | return None |
| 387 | try: |
| 388 | message = pickle.loads(packet) |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 389 | except pickle.UnpicklingError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 390 | print("-----------------------", file=sys.__stderr__) |
| 391 | print("cannot unpickle packet:", repr(packet), file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 392 | traceback.print_stack(file=sys.__stderr__) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 393 | print("-----------------------", file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 394 | raise |
| 395 | return message |
| 396 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 397 | def pollresponse(self, myseq, wait): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 398 | """Handle messages received on the socket. |
| 399 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 400 | Some messages received may be asynchronous 'call' or 'queue' requests, |
| 401 | and some may be responses for other threads. |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 402 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 403 | 'call' requests are passed to self.localcall() with the expectation of |
| 404 | immediate execution, during which time the socket is not serviced. |
| 405 | |
| 406 | 'queue' requests are used for tasks (which may block or hang) to be |
| 407 | processed in a different thread. These requests are fed into |
| 408 | request_queue by self.localcall(). Responses to queued requests are |
| 409 | taken from response_queue and sent across the link with the associated |
| 410 | sequence numbers. Messages in the queues are (sequence_number, |
| 411 | request/response) tuples and code using this module removing messages |
| 412 | from the request_queue is responsible for returning the correct |
| 413 | sequence number in the response_queue. |
| 414 | |
| 415 | pollresponse() will loop until a response message with the myseq |
| 416 | sequence number is received, and will save other responses in |
| 417 | self.responses and notify the owning thread. |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 418 | |
| 419 | """ |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 420 | while 1: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 421 | # send queued response if there is one available |
| 422 | try: |
| 423 | qmsg = response_queue.get(0) |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 424 | except queue.Empty: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 425 | pass |
| 426 | else: |
| 427 | seq, response = qmsg |
| 428 | message = (seq, ('OK', response)) |
| 429 | self.putmessage(message) |
| 430 | # poll for message on link |
| 431 | try: |
| 432 | message = self.pollmessage(wait) |
| 433 | if message is None: # socket not ready |
| 434 | return None |
| 435 | except EOFError: |
| 436 | self.handle_EOF() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 437 | return None |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 438 | except AttributeError: |
| 439 | return None |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 440 | seq, resq = message |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 441 | how = resq[0] |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 442 | self.debug("pollresponse:%d:myseq:%s" % (seq, myseq)) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 443 | # process or queue a request |
| 444 | if how in ("CALL", "QUEUE"): |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 445 | self.debug("pollresponse:%d:localcall:call:" % seq) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 446 | response = self.localcall(seq, resq) |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 447 | self.debug("pollresponse:%d:localcall:response:%s" |
| 448 | % (seq, response)) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 449 | if how == "CALL": |
| 450 | self.putmessage((seq, response)) |
| 451 | elif how == "QUEUE": |
| 452 | # don't acknowledge the 'queue' request! |
| 453 | pass |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 454 | continue |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 455 | # return if completed message transaction |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 456 | elif seq == myseq: |
| 457 | return resq |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 458 | # must be a response for a different thread: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 459 | else: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 460 | cv = self.cvars.get(seq, None) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 461 | # response involving unknown sequence number is discarded, |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 462 | # probably intended for prior incarnation of server |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 463 | if cv is not None: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 464 | cv.acquire() |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 465 | self.responses[seq] = resq |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 466 | cv.notify() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 467 | cv.release() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 468 | continue |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 469 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 470 | def handle_EOF(self): |
| 471 | "action taken upon link being closed by peer" |
| 472 | self.EOFhook() |
| 473 | self.debug("handle_EOF") |
| 474 | for key in self.cvars: |
| 475 | cv = self.cvars[key] |
| 476 | cv.acquire() |
| 477 | self.responses[key] = ('EOF', None) |
| 478 | cv.notify() |
| 479 | cv.release() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 480 | # call our (possibly overridden) exit function |
| 481 | self.exithook() |
| 482 | |
| 483 | def EOFhook(self): |
| 484 | "Classes using rpc client/server can override to augment EOF action" |
| 485 | pass |
| 486 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 487 | #----------------- end class SocketIO -------------------- |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 488 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 489 | class RemoteObject(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 490 | # Token mix-in class |
| 491 | pass |
| 492 | |
| 493 | def remoteref(obj): |
| 494 | oid = id(obj) |
| 495 | objecttable[oid] = obj |
| 496 | return RemoteProxy(oid) |
| 497 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 498 | class RemoteProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 499 | |
| 500 | def __init__(self, oid): |
| 501 | self.oid = oid |
| 502 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 503 | class RPCHandler(socketserver.BaseRequestHandler, SocketIO): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 504 | |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 505 | debugging = False |
| 506 | location = "#S" # Server |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 507 | |
| 508 | def __init__(self, sock, addr, svr): |
| 509 | svr.current_handler = self ## cgt xxx |
| 510 | SocketIO.__init__(self, sock) |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 511 | socketserver.BaseRequestHandler.__init__(self, sock, addr, svr) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 512 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 513 | def handle(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 514 | "handle() method required by socketserver" |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 515 | self.mainloop() |
| 516 | |
| 517 | def get_remote_proxy(self, oid): |
| 518 | return RPCProxy(self, oid) |
| 519 | |
| 520 | class RPCClient(SocketIO): |
| 521 | |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 522 | debugging = False |
| 523 | location = "#C" # Client |
| 524 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 525 | nextseq = 1 # Requests coming from the client are odd numbered |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 526 | |
| 527 | def __init__(self, address, family=socket.AF_INET, type=socket.SOCK_STREAM): |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 528 | self.listening_sock = socket.socket(family, type) |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 529 | self.listening_sock.bind(address) |
| 530 | self.listening_sock.listen(1) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 531 | |
| 532 | def accept(self): |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 533 | working_sock, address = self.listening_sock.accept() |
Kurt B. Kaiser | 74d93c8 | 2002-12-23 22:51:03 +0000 | [diff] [blame] | 534 | if self.debugging: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 535 | print("****** Connection request from ", address, file=sys.__stderr__) |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 536 | if address[0] == LOCALHOST: |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 537 | SocketIO.__init__(self, working_sock) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 538 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 539 | print("** Invalid host: ", address, file=sys.__stderr__) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 540 | raise socket.error |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 541 | |
| 542 | def get_remote_proxy(self, oid): |
| 543 | return RPCProxy(self, oid) |
| 544 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 545 | class RPCProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 546 | |
| 547 | __methods = None |
| 548 | __attributes = None |
| 549 | |
| 550 | def __init__(self, sockio, oid): |
| 551 | self.sockio = sockio |
| 552 | self.oid = oid |
| 553 | |
| 554 | def __getattr__(self, name): |
| 555 | if self.__methods is None: |
| 556 | self.__getmethods() |
| 557 | if self.__methods.get(name): |
| 558 | return MethodProxy(self.sockio, self.oid, name) |
| 559 | if self.__attributes is None: |
| 560 | self.__getattributes() |
Guido van Rossum | 811c4e0 | 2006-08-22 15:45:46 +0000 | [diff] [blame] | 561 | if name in self.__attributes: |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 562 | value = self.sockio.remotecall(self.oid, '__getattribute__', |
| 563 | (name,), {}) |
| 564 | return value |
| 565 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 566 | raise AttributeError(name) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 567 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 568 | def __getattributes(self): |
| 569 | self.__attributes = self.sockio.remotecall(self.oid, |
| 570 | "__attributes__", (), {}) |
| 571 | |
| 572 | def __getmethods(self): |
| 573 | self.__methods = self.sockio.remotecall(self.oid, |
| 574 | "__methods__", (), {}) |
| 575 | |
| 576 | def _getmethods(obj, methods): |
| 577 | # Helper to get a list of methods from an object |
| 578 | # Adds names to dictionary argument 'methods' |
| 579 | for name in dir(obj): |
| 580 | attr = getattr(obj, name) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 581 | if callable(attr): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 582 | methods[name] = 1 |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 583 | if isinstance(obj, type): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 584 | for super in obj.__bases__: |
| 585 | _getmethods(super, methods) |
| 586 | |
| 587 | def _getattributes(obj, attributes): |
| 588 | for name in dir(obj): |
| 589 | attr = getattr(obj, name) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 590 | if not callable(attr): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 591 | attributes[name] = 1 |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 592 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 593 | class MethodProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 594 | |
| 595 | def __init__(self, sockio, oid, name): |
| 596 | self.sockio = sockio |
| 597 | self.oid = oid |
| 598 | self.name = name |
| 599 | |
| 600 | def __call__(self, *args, **kwargs): |
| 601 | value = self.sockio.remotecall(self.oid, self.name, args, kwargs) |
| 602 | return value |
| 603 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 604 | |
Kurt B. Kaiser | 62685d3 | 2003-09-10 02:42:18 +0000 | [diff] [blame] | 605 | # XXX KBK 09Sep03 We need a proper unit test for this module. Previously |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 606 | # existing test code was removed at Rev 1.27 (r34098). |
Andrew Svetlov | cd49d53 | 2012-03-25 11:43:02 +0300 | [diff] [blame] | 607 | |
| 608 | def displayhook(value): |
| 609 | """Override standard display hook to use non-locale encoding""" |
| 610 | if value is None: |
| 611 | return |
| 612 | # Set '_' to None to avoid recursion |
| 613 | builtins._ = None |
| 614 | text = repr(value) |
| 615 | try: |
| 616 | sys.stdout.write(text) |
| 617 | except UnicodeEncodeError: |
| 618 | # let's use ascii while utf8-bmp codec doesn't present |
| 619 | encoding = 'ascii' |
| 620 | bytes = text.encode(encoding, 'backslashreplace') |
| 621 | text = bytes.decode(encoding, 'strict') |
| 622 | sys.stdout.write(text) |
| 623 | sys.stdout.write("\n") |
| 624 | builtins._ = value |