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 |
Terry Jan Reedy | ad0c57f | 2014-10-10 19:33:45 -0400 | [diff] [blame] | 32 | import io |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 33 | import socket |
| 34 | import select |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 35 | import socketserver |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 36 | import struct |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 37 | import pickle |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 38 | import threading |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 39 | import queue |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 40 | import traceback |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 41 | import copyreg |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 42 | import types |
| 43 | import marshal |
Andrew Svetlov | cd49d53 | 2012-03-25 11:43:02 +0300 | [diff] [blame] | 44 | import builtins |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 45 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 46 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 47 | def unpickle_code(ms): |
| 48 | co = marshal.loads(ms) |
| 49 | assert isinstance(co, types.CodeType) |
| 50 | return co |
| 51 | |
| 52 | def pickle_code(co): |
| 53 | assert isinstance(co, types.CodeType) |
| 54 | ms = marshal.dumps(co) |
| 55 | return unpickle_code, (ms,) |
| 56 | |
Terry Jan Reedy | ad0c57f | 2014-10-10 19:33:45 -0400 | [diff] [blame] | 57 | def dumps(obj, protocol=None): |
| 58 | f = io.BytesIO() |
| 59 | p = CodePickler(f, protocol) |
| 60 | p.dump(obj) |
| 61 | return f.getvalue() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 62 | |
Terry Jan Reedy | ad0c57f | 2014-10-10 19:33:45 -0400 | [diff] [blame] | 63 | class CodePickler(pickle.Pickler): |
| 64 | dispatch_table = {types.CodeType: pickle_code} |
| 65 | dispatch_table.update(copyreg.dispatch_table) |
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 |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 202 | except OSError: |
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: |
Terry Jan Reedy | ad0c57f | 2014-10-10 19:33:45 -0400 | [diff] [blame] | 332 | s = 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): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 342 | raise OSError("socket no longer exists") |
Serhiy Storchaka | ef94869 | 2015-05-20 16:15:02 +0300 | [diff] [blame^] | 343 | s = s[n:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 344 | |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 345 | buff = b'' |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 346 | bufneed = 4 |
| 347 | bufstate = 0 # meaning: 0 => reading count; 1 => reading data |
| 348 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 349 | def pollpacket(self, wait): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 350 | self._stage0() |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 351 | if len(self.buff) < self.bufneed: |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 352 | r, w, x = select.select([self.sock.fileno()], [], [], wait) |
| 353 | if len(r) == 0: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 354 | return None |
| 355 | try: |
| 356 | s = self.sock.recv(BUFSIZE) |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 357 | except OSError: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 358 | raise EOFError |
| 359 | if len(s) == 0: |
| 360 | raise EOFError |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 361 | self.buff += s |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 362 | self._stage0() |
| 363 | return self._stage1() |
| 364 | |
| 365 | def _stage0(self): |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 366 | if self.bufstate == 0 and len(self.buff) >= 4: |
| 367 | s = self.buff[:4] |
| 368 | self.buff = self.buff[4:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 369 | self.bufneed = struct.unpack("<i", s)[0] |
| 370 | self.bufstate = 1 |
| 371 | |
| 372 | def _stage1(self): |
Kurt B. Kaiser | 2d726df | 2007-08-22 21:33:27 +0000 | [diff] [blame] | 373 | if self.bufstate == 1 and len(self.buff) >= self.bufneed: |
| 374 | packet = self.buff[:self.bufneed] |
| 375 | self.buff = self.buff[self.bufneed:] |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 376 | self.bufneed = 4 |
| 377 | self.bufstate = 0 |
| 378 | return packet |
| 379 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 380 | def pollmessage(self, wait): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 381 | packet = self.pollpacket(wait) |
| 382 | if packet is None: |
| 383 | return None |
| 384 | try: |
| 385 | message = pickle.loads(packet) |
Kurt B. Kaiser | d6ab77d | 2004-01-21 19:21:11 +0000 | [diff] [blame] | 386 | except pickle.UnpicklingError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 387 | print("-----------------------", file=sys.__stderr__) |
| 388 | print("cannot unpickle packet:", repr(packet), file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 389 | traceback.print_stack(file=sys.__stderr__) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 390 | print("-----------------------", file=sys.__stderr__) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 391 | raise |
| 392 | return message |
| 393 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 394 | def pollresponse(self, myseq, wait): |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 395 | """Handle messages received on the socket. |
| 396 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 397 | Some messages received may be asynchronous 'call' or 'queue' requests, |
| 398 | and some may be responses for other threads. |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 399 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 400 | 'call' requests are passed to self.localcall() with the expectation of |
| 401 | immediate execution, during which time the socket is not serviced. |
| 402 | |
| 403 | 'queue' requests are used for tasks (which may block or hang) to be |
| 404 | processed in a different thread. These requests are fed into |
| 405 | request_queue by self.localcall(). Responses to queued requests are |
| 406 | taken from response_queue and sent across the link with the associated |
| 407 | sequence numbers. Messages in the queues are (sequence_number, |
| 408 | request/response) tuples and code using this module removing messages |
| 409 | from the request_queue is responsible for returning the correct |
| 410 | sequence number in the response_queue. |
| 411 | |
| 412 | pollresponse() will loop until a response message with the myseq |
| 413 | sequence number is received, and will save other responses in |
| 414 | self.responses and notify the owning thread. |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 415 | |
| 416 | """ |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 417 | while 1: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 418 | # send queued response if there is one available |
| 419 | try: |
| 420 | qmsg = response_queue.get(0) |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 421 | except queue.Empty: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 422 | pass |
| 423 | else: |
| 424 | seq, response = qmsg |
| 425 | message = (seq, ('OK', response)) |
| 426 | self.putmessage(message) |
| 427 | # poll for message on link |
| 428 | try: |
| 429 | message = self.pollmessage(wait) |
| 430 | if message is None: # socket not ready |
| 431 | return None |
| 432 | except EOFError: |
| 433 | self.handle_EOF() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 434 | return None |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 435 | except AttributeError: |
| 436 | return None |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 437 | seq, resq = message |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 438 | how = resq[0] |
Kurt B. Kaiser | 0a0e6c3 | 2003-01-25 03:26:35 +0000 | [diff] [blame] | 439 | self.debug("pollresponse:%d:myseq:%s" % (seq, myseq)) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 440 | # process or queue a request |
| 441 | if how in ("CALL", "QUEUE"): |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 442 | self.debug("pollresponse:%d:localcall:call:" % seq) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 443 | response = self.localcall(seq, resq) |
Kurt B. Kaiser | bc28613 | 2003-01-25 21:33:40 +0000 | [diff] [blame] | 444 | self.debug("pollresponse:%d:localcall:response:%s" |
| 445 | % (seq, response)) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 446 | if how == "CALL": |
| 447 | self.putmessage((seq, response)) |
| 448 | elif how == "QUEUE": |
| 449 | # don't acknowledge the 'queue' request! |
| 450 | pass |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 451 | continue |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 452 | # return if completed message transaction |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 453 | elif seq == myseq: |
| 454 | return resq |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 455 | # must be a response for a different thread: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 456 | else: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 457 | cv = self.cvars.get(seq, None) |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 458 | # response involving unknown sequence number is discarded, |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 459 | # probably intended for prior incarnation of server |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 460 | if cv is not None: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 461 | cv.acquire() |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 462 | self.responses[seq] = resq |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 463 | cv.notify() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 464 | cv.release() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 465 | continue |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 466 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 467 | def handle_EOF(self): |
| 468 | "action taken upon link being closed by peer" |
| 469 | self.EOFhook() |
| 470 | self.debug("handle_EOF") |
| 471 | for key in self.cvars: |
| 472 | cv = self.cvars[key] |
| 473 | cv.acquire() |
| 474 | self.responses[key] = ('EOF', None) |
| 475 | cv.notify() |
| 476 | cv.release() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 477 | # call our (possibly overridden) exit function |
| 478 | self.exithook() |
| 479 | |
| 480 | def EOFhook(self): |
| 481 | "Classes using rpc client/server can override to augment EOF action" |
| 482 | pass |
| 483 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 484 | #----------------- end class SocketIO -------------------- |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 485 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 486 | class RemoteObject(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 487 | # Token mix-in class |
| 488 | pass |
| 489 | |
| 490 | def remoteref(obj): |
| 491 | oid = id(obj) |
| 492 | objecttable[oid] = obj |
| 493 | return RemoteProxy(oid) |
| 494 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 495 | class RemoteProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 496 | |
| 497 | def __init__(self, oid): |
| 498 | self.oid = oid |
| 499 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 500 | class RPCHandler(socketserver.BaseRequestHandler, SocketIO): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 501 | |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 502 | debugging = False |
| 503 | location = "#S" # Server |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 504 | |
| 505 | def __init__(self, sock, addr, svr): |
| 506 | svr.current_handler = self ## cgt xxx |
| 507 | SocketIO.__init__(self, sock) |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 508 | socketserver.BaseRequestHandler.__init__(self, sock, addr, svr) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 509 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 510 | def handle(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 511 | "handle() method required by socketserver" |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 512 | self.mainloop() |
| 513 | |
| 514 | def get_remote_proxy(self, oid): |
| 515 | return RPCProxy(self, oid) |
| 516 | |
| 517 | class RPCClient(SocketIO): |
| 518 | |
Kurt B. Kaiser | 0930c43 | 2002-12-06 21:45:24 +0000 | [diff] [blame] | 519 | debugging = False |
| 520 | location = "#C" # Client |
| 521 | |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 522 | nextseq = 1 # Requests coming from the client are odd numbered |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 523 | |
| 524 | 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] | 525 | self.listening_sock = socket.socket(family, type) |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 526 | self.listening_sock.bind(address) |
| 527 | self.listening_sock.listen(1) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 528 | |
| 529 | def accept(self): |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 530 | working_sock, address = self.listening_sock.accept() |
Kurt B. Kaiser | 74d93c8 | 2002-12-23 22:51:03 +0000 | [diff] [blame] | 531 | if self.debugging: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 532 | print("****** Connection request from ", address, file=sys.__stderr__) |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 533 | if address[0] == LOCALHOST: |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 534 | SocketIO.__init__(self, working_sock) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 535 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 536 | print("** Invalid host: ", address, file=sys.__stderr__) |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 537 | raise OSError |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 538 | |
| 539 | def get_remote_proxy(self, oid): |
| 540 | return RPCProxy(self, oid) |
| 541 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 542 | class RPCProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 543 | |
| 544 | __methods = None |
| 545 | __attributes = None |
| 546 | |
| 547 | def __init__(self, sockio, oid): |
| 548 | self.sockio = sockio |
| 549 | self.oid = oid |
| 550 | |
| 551 | def __getattr__(self, name): |
| 552 | if self.__methods is None: |
| 553 | self.__getmethods() |
| 554 | if self.__methods.get(name): |
| 555 | return MethodProxy(self.sockio, self.oid, name) |
| 556 | if self.__attributes is None: |
| 557 | self.__getattributes() |
Guido van Rossum | 811c4e0 | 2006-08-22 15:45:46 +0000 | [diff] [blame] | 558 | if name in self.__attributes: |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 559 | value = self.sockio.remotecall(self.oid, '__getattribute__', |
| 560 | (name,), {}) |
| 561 | return value |
| 562 | else: |
Kurt B. Kaiser | ad66742 | 2007-08-23 01:06:15 +0000 | [diff] [blame] | 563 | raise AttributeError(name) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 564 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 565 | def __getattributes(self): |
| 566 | self.__attributes = self.sockio.remotecall(self.oid, |
| 567 | "__attributes__", (), {}) |
| 568 | |
| 569 | def __getmethods(self): |
| 570 | self.__methods = self.sockio.remotecall(self.oid, |
| 571 | "__methods__", (), {}) |
| 572 | |
| 573 | def _getmethods(obj, methods): |
| 574 | # Helper to get a list of methods from an object |
| 575 | # Adds names to dictionary argument 'methods' |
| 576 | for name in dir(obj): |
| 577 | attr = getattr(obj, name) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 578 | if callable(attr): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 579 | methods[name] = 1 |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 580 | if isinstance(obj, type): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 581 | for super in obj.__bases__: |
| 582 | _getmethods(super, methods) |
| 583 | |
| 584 | def _getattributes(obj, attributes): |
| 585 | for name in dir(obj): |
| 586 | attr = getattr(obj, name) |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 587 | if not callable(attr): |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 588 | attributes[name] = 1 |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 589 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 590 | class MethodProxy(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 591 | |
| 592 | def __init__(self, sockio, oid, name): |
| 593 | self.sockio = sockio |
| 594 | self.oid = oid |
| 595 | self.name = name |
| 596 | |
| 597 | def __call__(self, *args, **kwargs): |
| 598 | value = self.sockio.remotecall(self.oid, self.name, args, kwargs) |
| 599 | return value |
| 600 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 601 | |
Kurt B. Kaiser | 62685d3 | 2003-09-10 02:42:18 +0000 | [diff] [blame] | 602 | # 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] | 603 | # existing test code was removed at Rev 1.27 (r34098). |
Andrew Svetlov | cd49d53 | 2012-03-25 11:43:02 +0300 | [diff] [blame] | 604 | |
| 605 | def displayhook(value): |
| 606 | """Override standard display hook to use non-locale encoding""" |
| 607 | if value is None: |
| 608 | return |
| 609 | # Set '_' to None to avoid recursion |
| 610 | builtins._ = None |
| 611 | text = repr(value) |
| 612 | try: |
| 613 | sys.stdout.write(text) |
| 614 | except UnicodeEncodeError: |
| 615 | # let's use ascii while utf8-bmp codec doesn't present |
| 616 | encoding = 'ascii' |
| 617 | bytes = text.encode(encoding, 'backslashreplace') |
| 618 | text = bytes.decode(encoding, 'strict') |
| 619 | sys.stdout.write(text) |
| 620 | sys.stdout.write("\n") |
| 621 | builtins._ = value |