Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 1 | import sys |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 2 | import linecache |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 3 | import time |
| 4 | import socket |
Kurt B. Kaiser | 86bc464 | 2003-02-27 23:04:17 +0000 | [diff] [blame] | 5 | import traceback |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 6 | import _thread as thread |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 7 | import threading |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 8 | import queue |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 9 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 10 | from idlelib import CallTips |
| 11 | from idlelib import AutoComplete |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 12 | |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 13 | from idlelib import RemoteDebugger |
| 14 | from idlelib import RemoteObjectBrowser |
| 15 | from idlelib import StackViewer |
| 16 | from idlelib import rpc |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 17 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 18 | import __main__ |
| 19 | |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 20 | LOCALHOST = '127.0.0.1' |
| 21 | |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 22 | try: |
| 23 | import warnings |
| 24 | except ImportError: |
| 25 | pass |
| 26 | else: |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 27 | def idle_formatwarning_subproc(message, category, filename, lineno, |
Guilherme Polo | 1fff008 | 2009-08-14 15:05:30 +0000 | [diff] [blame] | 28 | line=None): |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 29 | """Format warnings the IDLE way""" |
| 30 | s = "\nWarning (from warnings module):\n" |
| 31 | s += ' File \"%s\", line %s\n' % (filename, lineno) |
Guilherme Polo | 1fff008 | 2009-08-14 15:05:30 +0000 | [diff] [blame] | 32 | if line is None: |
| 33 | line = linecache.getline(filename, lineno) |
| 34 | line = line.strip() |
Kurt B. Kaiser | 49a5fe1 | 2004-07-04 01:25:56 +0000 | [diff] [blame] | 35 | if line: |
| 36 | s += " %s\n" % line |
| 37 | s += "%s: %s\n" % (category.__name__, message) |
| 38 | return s |
| 39 | warnings.formatwarning = idle_formatwarning_subproc |
| 40 | |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 41 | # Thread shared globals: Establish a queue between a subthread (which handles |
| 42 | # the socket) and the main thread (which runs user code), plus global |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 43 | # completion, exit and interruptable (the main thread) flags: |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 44 | |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 45 | exit_now = False |
| 46 | quitting = False |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 47 | interruptable = False |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 48 | |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 49 | def main(del_exitfunc=False): |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 50 | """Start the Python execution server in a subprocess |
| 51 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 52 | In the Python subprocess, RPCServer is instantiated with handlerclass |
| 53 | MyHandler, which inherits register/unregister methods from RPCHandler via |
| 54 | the mix-in class SocketIO. |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 55 | |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 56 | When the RPCServer 'server' is instantiated, the TCPServer initialization |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 57 | creates an instance of run.MyHandler and calls its handle() method. |
| 58 | handle() instantiates a run.Executive object, passing it a reference to the |
| 59 | MyHandler object. That reference is saved as attribute rpchandler of the |
| 60 | Executive instance. The Executive methods have access to the reference and |
| 61 | can pass it on to entities that they command |
| 62 | (e.g. RemoteDebugger.Debugger.start_debugger()). The latter, in turn, can |
| 63 | call MyHandler(SocketIO) register/unregister methods via the reference to |
| 64 | register and unregister themselves. |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 65 | |
| 66 | """ |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 67 | global exit_now |
| 68 | global quitting |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 69 | global no_exitfunc |
| 70 | no_exitfunc = del_exitfunc |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 71 | #time.sleep(15) # test subprocess not responding |
Kurt B. Kaiser | e67842a | 2009-04-04 20:13:23 +0000 | [diff] [blame] | 72 | try: |
| 73 | assert(len(sys.argv) > 1) |
| 74 | port = int(sys.argv[-1]) |
| 75 | except: |
Kurt B. Kaiser | 8fc98c3 | 2009-04-04 20:20:29 +0000 | [diff] [blame] | 76 | print("IDLE Subprocess: no IP port passed in sys.argv.", |
| 77 | file=sys.__stderr__) |
Kurt B. Kaiser | e67842a | 2009-04-04 20:13:23 +0000 | [diff] [blame] | 78 | return |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 79 | sys.argv[:] = [""] |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 80 | sockthread = threading.Thread(target=manage_socket, |
| 81 | name='SockThread', |
Kurt B. Kaiser | 24d7e0c | 2003-06-05 23:51:29 +0000 | [diff] [blame] | 82 | args=((LOCALHOST, port),)) |
Benjamin Peterson | 71088cc | 2008-09-19 21:49:37 +0000 | [diff] [blame] | 83 | sockthread.daemon = True |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 84 | sockthread.start() |
| 85 | while 1: |
| 86 | try: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 87 | if exit_now: |
| 88 | try: |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 89 | exit() |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 90 | except KeyboardInterrupt: |
| 91 | # exiting but got an extra KBI? Try again! |
| 92 | continue |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 93 | try: |
Kurt B. Kaiser | 20345fb | 2005-05-05 23:29:54 +0000 | [diff] [blame] | 94 | seq, request = rpc.request_queue.get(block=True, timeout=0.05) |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 95 | except queue.Empty: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 96 | continue |
| 97 | method, args, kwargs = request |
| 98 | ret = method(*args, **kwargs) |
| 99 | rpc.response_queue.put((seq, ret)) |
Kurt B. Kaiser | aa6b856 | 2003-05-14 18:15:40 +0000 | [diff] [blame] | 100 | except KeyboardInterrupt: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 101 | if quitting: |
| 102 | exit_now = True |
Kurt B. Kaiser | aa6b856 | 2003-05-14 18:15:40 +0000 | [diff] [blame] | 103 | continue |
Kurt B. Kaiser | a2792be | 2003-05-17 21:04:10 +0000 | [diff] [blame] | 104 | except SystemExit: |
| 105 | raise |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 106 | except: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 107 | type, value, tb = sys.exc_info() |
Kurt B. Kaiser | a2792be | 2003-05-17 21:04:10 +0000 | [diff] [blame] | 108 | try: |
| 109 | print_exception() |
| 110 | rpc.response_queue.put((seq, None)) |
| 111 | except: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 112 | # Link didn't work, print same exception to __stderr__ |
| 113 | traceback.print_exception(type, value, tb, file=sys.__stderr__) |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 114 | exit() |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 115 | else: |
| 116 | continue |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 117 | |
| 118 | def manage_socket(address): |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 119 | for i in range(3): |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 120 | time.sleep(i) |
| 121 | try: |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 122 | server = MyRPCServer(address, MyHandler) |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 123 | break |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 124 | except socket.error as err: |
Georg Brandl | 6464d47 | 2007-10-22 16:16:13 +0000 | [diff] [blame] | 125 | print("IDLE Subprocess: socket error: " + err.args[1] + |
| 126 | ", retrying....", file=sys.__stderr__) |
Amaury Forgeot d'Arc | efae8c4 | 2008-11-21 23:08:09 +0000 | [diff] [blame] | 127 | socket_error = err |
Kurt B. Kaiser | b417936 | 2002-07-26 00:06:42 +0000 | [diff] [blame] | 128 | else: |
Amaury Forgeot d'Arc | efae8c4 | 2008-11-21 23:08:09 +0000 | [diff] [blame] | 129 | print("IDLE Subprocess: Connection to " |
| 130 | "IDLE GUI failed, exiting.", file=sys.__stderr__) |
| 131 | show_socket_error(socket_error, address) |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 132 | global exit_now |
| 133 | exit_now = True |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 134 | return |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 135 | server.handle_request() # A single request only |
| 136 | |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 137 | def show_socket_error(err, address): |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 138 | import tkinter |
| 139 | import tkinter.messagebox as tkMessageBox |
| 140 | root = tkinter.Tk() |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 141 | root.withdraw() |
Georg Brandl | 6464d47 | 2007-10-22 16:16:13 +0000 | [diff] [blame] | 142 | if err.args[0] == 61: # connection refused |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 143 | msg = "IDLE's subprocess can't connect to %s:%d. This may be due "\ |
| 144 | "to your personal firewall configuration. It is safe to "\ |
| 145 | "allow this internal connection because no data is visible on "\ |
| 146 | "external ports." % address |
| 147 | tkMessageBox.showerror("IDLE Subprocess Error", msg, parent=root) |
| 148 | else: |
Georg Brandl | 6464d47 | 2007-10-22 16:16:13 +0000 | [diff] [blame] | 149 | tkMessageBox.showerror("IDLE Subprocess Error", |
| 150 | "Socket Error: %s" % err.args[1]) |
Kurt B. Kaiser | af3eb87 | 2004-01-21 18:54:30 +0000 | [diff] [blame] | 151 | root.destroy() |
| 152 | |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 153 | def print_exception(): |
Kurt B. Kaiser | e9802a3 | 2004-01-02 04:04:04 +0000 | [diff] [blame] | 154 | import linecache |
| 155 | linecache.checkcache() |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 156 | flush_stdout() |
| 157 | efile = sys.stderr |
Kurt B. Kaiser | 924f616 | 2003-11-19 04:52:32 +0000 | [diff] [blame] | 158 | typ, val, tb = excinfo = sys.exc_info() |
| 159 | sys.last_type, sys.last_value, sys.last_traceback = excinfo |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 160 | tbe = traceback.extract_tb(tb) |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 161 | print('Traceback (most recent call last):', file=efile) |
Georg Brandl | 15a51e3 | 2008-05-25 07:17:27 +0000 | [diff] [blame] | 162 | exclude = ("run.py", "rpc.py", "threading.py", "queue.py", |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 163 | "RemoteDebugger.py", "bdb.py") |
| 164 | cleanup_traceback(tbe, exclude) |
| 165 | traceback.print_list(tbe, file=efile) |
| 166 | lines = traceback.format_exception_only(typ, val) |
| 167 | for line in lines: |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 168 | print(line, end='', file=efile) |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 169 | |
| 170 | def cleanup_traceback(tb, exclude): |
| 171 | "Remove excluded traces from beginning/end of tb; get cached lines" |
| 172 | orig_tb = tb[:] |
| 173 | while tb: |
| 174 | for rpcfile in exclude: |
| 175 | if tb[0][0].count(rpcfile): |
| 176 | break # found an exclude, break for: and delete tb[0] |
| 177 | else: |
| 178 | break # no excludes, have left RPC code, break while: |
| 179 | del tb[0] |
| 180 | while tb: |
| 181 | for rpcfile in exclude: |
| 182 | if tb[-1][0].count(rpcfile): |
| 183 | break |
| 184 | else: |
| 185 | break |
| 186 | del tb[-1] |
| 187 | if len(tb) == 0: |
| 188 | # exception was in IDLE internals, don't prune! |
| 189 | tb[:] = orig_tb[:] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 190 | print("** IDLE Internal Exception: ", file=sys.stderr) |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 191 | rpchandler = rpc.objecttable['exec'].rpchandler |
| 192 | for i in range(len(tb)): |
| 193 | fn, ln, nm, line = tb[i] |
| 194 | if nm == '?': |
| 195 | nm = "-toplevel-" |
| 196 | if not line and fn.startswith("<pyshell#"): |
| 197 | line = rpchandler.remotecall('linecache', 'getline', |
| 198 | (fn, ln), {}) |
| 199 | tb[i] = fn, ln, nm, line |
| 200 | |
| 201 | def flush_stdout(): |
Guido van Rossum | 79139b2 | 2007-02-09 23:20:19 +0000 | [diff] [blame] | 202 | """XXX How to do this now?""" |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 203 | |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 204 | def exit(): |
Guido van Rossum | c76a250 | 2007-08-09 14:26:58 +0000 | [diff] [blame] | 205 | """Exit subprocess, possibly after first clearing exit functions. |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 206 | |
| 207 | If config-main.cfg/.def 'General' 'delete-exitfunc' is True, then any |
Guido van Rossum | c76a250 | 2007-08-09 14:26:58 +0000 | [diff] [blame] | 208 | functions registered with atexit will be removed before exiting. |
| 209 | (VPython support) |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 210 | |
| 211 | """ |
| 212 | if no_exitfunc: |
Guido van Rossum | c76a250 | 2007-08-09 14:26:58 +0000 | [diff] [blame] | 213 | import atexit |
| 214 | atexit._clear() |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 215 | sys.exit(0) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 216 | |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 217 | class MyRPCServer(rpc.RPCServer): |
| 218 | |
| 219 | def handle_error(self, request, client_address): |
| 220 | """Override RPCServer method for IDLE |
| 221 | |
| 222 | Interrupt the MainThread and exit server if link is dropped. |
| 223 | |
| 224 | """ |
Kurt B. Kaiser | e953511 | 2004-11-19 15:46:49 +0000 | [diff] [blame] | 225 | global quitting |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 226 | try: |
| 227 | raise |
| 228 | except SystemExit: |
| 229 | raise |
| 230 | except EOFError: |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 231 | global exit_now |
| 232 | exit_now = True |
Kurt B. Kaiser | 93e8e54 | 2003-06-13 22:03:43 +0000 | [diff] [blame] | 233 | thread.interrupt_main() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 234 | except: |
| 235 | erf = sys.__stderr__ |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 236 | print('\n' + '-'*40, file=erf) |
| 237 | print('Unhandled server exception!', file=erf) |
Amaury Forgeot d'Arc | bed1710 | 2008-11-29 01:48:47 +0000 | [diff] [blame] | 238 | print('Thread: %s' % threading.current_thread().name, file=erf) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 239 | print('Client Address: ', client_address, file=erf) |
| 240 | print('Request: ', repr(request), file=erf) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 241 | traceback.print_exc(file=erf) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 242 | print('\n*** Unrecoverable, server exiting!', file=erf) |
| 243 | print('-'*40, file=erf) |
Kurt B. Kaiser | e953511 | 2004-11-19 15:46:49 +0000 | [diff] [blame] | 244 | quitting = True |
| 245 | thread.interrupt_main() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 246 | |
| 247 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 248 | class MyHandler(rpc.RPCHandler): |
| 249 | |
| 250 | def handle(self): |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 251 | """Override base method""" |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 252 | executive = Executive(self) |
| 253 | self.register("exec", executive) |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 254 | sys.stdin = self.console = self.get_remote_proxy("stdin") |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 255 | sys.stdout = self.get_remote_proxy("stdout") |
| 256 | sys.stderr = self.get_remote_proxy("stderr") |
Kurt B. Kaiser | f609a34 | 2007-12-28 03:57:56 +0000 | [diff] [blame] | 257 | # page help() text to shell. |
| 258 | import pydoc # import must be done here to capture i/o binding |
| 259 | pydoc.pager = pydoc.plainpager |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 260 | from idlelib import IOBinding |
Martin v. Löwis | bcc651a | 2003-06-22 07:52:56 +0000 | [diff] [blame] | 261 | sys.stdin.encoding = sys.stdout.encoding = \ |
| 262 | sys.stderr.encoding = IOBinding.encoding |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 263 | self.interp = self.get_remote_proxy("interp") |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 264 | rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05) |
| 265 | |
| 266 | def exithook(self): |
| 267 | "override SocketIO method - wait for MainThread to shut us down" |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 268 | time.sleep(10) |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 269 | |
| 270 | def EOFhook(self): |
| 271 | "Override SocketIO method - terminate wait on callback and exit thread" |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 272 | global quitting |
| 273 | quitting = True |
Kurt B. Kaiser | 93e8e54 | 2003-06-13 22:03:43 +0000 | [diff] [blame] | 274 | thread.interrupt_main() |
Kurt B. Kaiser | a00050f | 2003-05-08 20:26:55 +0000 | [diff] [blame] | 275 | |
| 276 | def decode_interrupthook(self): |
| 277 | "interrupt awakened thread" |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 278 | global quitting |
| 279 | quitting = True |
Kurt B. Kaiser | 93e8e54 | 2003-06-13 22:03:43 +0000 | [diff] [blame] | 280 | thread.interrupt_main() |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 281 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 282 | |
Kurt B. Kaiser | dcba662 | 2004-12-21 22:10:32 +0000 | [diff] [blame] | 283 | class Executive(object): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 284 | |
| 285 | def __init__(self, rpchandler): |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 286 | self.rpchandler = rpchandler |
Kurt B. Kaiser | adc6384 | 2002-08-25 14:08:07 +0000 | [diff] [blame] | 287 | self.locals = __main__.__dict__ |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 288 | self.calltip = CallTips.CallTips() |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 289 | self.autocomplete = AutoComplete.AutoComplete() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 290 | |
| 291 | def runcode(self, code): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 292 | global interruptable |
Kurt B. Kaiser | 86bc464 | 2003-02-27 23:04:17 +0000 | [diff] [blame] | 293 | try: |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 294 | self.usr_exc_info = None |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 295 | interruptable = True |
| 296 | try: |
| 297 | exec(code, self.locals) |
| 298 | finally: |
| 299 | interruptable = False |
Kurt B. Kaiser | 86bc464 | 2003-02-27 23:04:17 +0000 | [diff] [blame] | 300 | except: |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 301 | self.usr_exc_info = sys.exc_info() |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 302 | if quitting: |
Kurt B. Kaiser | 62df044 | 2003-05-28 01:47:46 +0000 | [diff] [blame] | 303 | exit() |
Kurt B. Kaiser | 67fd0ea | 2003-05-24 20:59:15 +0000 | [diff] [blame] | 304 | # even print a user code SystemExit exception, continue |
| 305 | print_exception() |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 306 | jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>") |
| 307 | if jit: |
| 308 | self.rpchandler.interp.open_remote_stack_viewer() |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 309 | else: |
Kurt B. Kaiser | 9ec454e | 2003-05-12 02:33:47 +0000 | [diff] [blame] | 310 | flush_stdout() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 311 | |
Kurt B. Kaiser | 003091c | 2003-02-17 18:57:16 +0000 | [diff] [blame] | 312 | def interrupt_the_server(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 313 | if interruptable: |
| 314 | thread.interrupt_main() |
Kurt B. Kaiser | 11c53e2 | 2003-03-22 19:40:19 +0000 | [diff] [blame] | 315 | |
Kurt B. Kaiser | 0e3a577 | 2002-06-16 03:32:24 +0000 | [diff] [blame] | 316 | def start_the_debugger(self, gui_adap_oid): |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 317 | return RemoteDebugger.start_debugger(self.rpchandler, gui_adap_oid) |
| 318 | |
| 319 | def stop_the_debugger(self, idb_adap_oid): |
| 320 | "Unregister the Idb Adapter. Link objects and Idb then subject to GC" |
| 321 | self.rpchandler.unregister(idb_adap_oid) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 322 | |
Kurt B. Kaiser | 5afa1df | 2002-10-10 08:25:24 +0000 | [diff] [blame] | 323 | def get_the_calltip(self, name): |
| 324 | return self.calltip.fetch_tip(name) |
| 325 | |
Kurt B. Kaiser | b175445 | 2005-11-18 22:05:48 +0000 | [diff] [blame] | 326 | def get_the_completion_list(self, what, mode): |
| 327 | return self.autocomplete.fetch_completions(what, mode) |
| 328 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 329 | def stackviewer(self, flist_oid=None): |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 330 | if self.usr_exc_info: |
| 331 | typ, val, tb = self.usr_exc_info |
| 332 | else: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 333 | return None |
| 334 | flist = None |
| 335 | if flist_oid is not None: |
Kurt B. Kaiser | ffd3a42 | 2002-06-26 02:32:09 +0000 | [diff] [blame] | 336 | flist = self.rpchandler.get_remote_proxy(flist_oid) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 337 | while tb and tb.tb_frame.f_globals["__name__"] in ["rpc", "run"]: |
| 338 | tb = tb.tb_next |
Kurt B. Kaiser | 9f36609 | 2003-06-02 01:50:19 +0000 | [diff] [blame] | 339 | sys.last_type = typ |
| 340 | sys.last_value = val |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 341 | item = StackViewer.StackTreeItem(flist, tb) |
| 342 | return RemoteObjectBrowser.remote_object_tree_item(item) |