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