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