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