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