14 import RemoteObjectBrowser
20 LOCALHOST
= '127.0.0.1'
27 def idle_formatwarning_subproc(message
, category
, filename
, lineno
,
28 file=None, line
=None):
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
is None else line
36 s
+= "%s: %s\n" % (category
.__name
__, message
)
38 warnings
.formatwarning
= idle_formatwarning_subproc
40 # Thread shared globals: Establish a queue between a subthread (which handles
41 # the socket) and the main thread (which runs user code), plus global
42 # completion, exit and interruptable (the main thread) flags:
48 def main(del_exitfunc
=False):
49 """Start the Python execution server in a subprocess
51 In the Python subprocess, RPCServer is instantiated with handlerclass
52 MyHandler, which inherits register/unregister methods from RPCHandler via
53 the mix-in class SocketIO.
55 When the RPCServer 'server' is instantiated, the TCPServer initialization
56 creates an instance of run.MyHandler and calls its handle() method.
57 handle() instantiates a run.Executive object, passing it a reference to the
58 MyHandler object. That reference is saved as attribute rpchandler of the
59 Executive instance. The Executive methods have access to the reference and
60 can pass it on to entities that they command
61 (e.g. RemoteDebugger.Debugger.start_debugger()). The latter, in turn, can
62 call MyHandler(SocketIO) register/unregister methods via the reference to
63 register and unregister themselves.
69 no_exitfunc
= del_exitfunc
71 #time.sleep(15) # test subprocess not responding
73 port
= int(sys
.argv
[1])
75 sockthread
= threading
.Thread(target
=manage_socket
,
77 args
=((LOCALHOST
, port
),))
78 sockthread
.setDaemon(True)
85 except KeyboardInterrupt:
86 # exiting but got an extra KBI? Try again!
89 seq
, request
= rpc
.request_queue
.get(block
=True, timeout
=0.05)
92 method
, args
, kwargs
= request
93 ret
= method(*args
, **kwargs
)
94 rpc
.response_queue
.put((seq
, ret
))
95 except KeyboardInterrupt:
102 type, value
, tb
= sys
.exc_info()
105 rpc
.response_queue
.put((seq
, None))
107 # Link didn't work, print same exception to __stderr__
108 traceback
.print_exception(type, value
, tb
, file=sys
.__stderr
__)
113 def manage_socket(address
):
117 server
= MyRPCServer(address
, MyHandler
)
119 except socket
.error
, err
:
120 print>>sys
.__stderr
__,"IDLE Subprocess: socket error: "\
121 + err
[1] + ", retrying...."
123 print>>sys
.__stderr
__, "IDLE Subprocess: Connection to "\
124 "IDLE GUI failed, exiting."
125 show_socket_error(err
, address
)
129 server
.handle_request() # A single request only
131 def show_socket_error(err
, address
):
136 if err
[0] == 61: # connection refused
137 msg
= "IDLE's subprocess can't connect to %s:%d. This may be due "\
138 "to your personal firewall configuration. It is safe to "\
139 "allow this internal connection because no data is visible on "\
140 "external ports." % address
141 tkMessageBox
.showerror("IDLE Subprocess Error", msg
, parent
=root
)
143 tkMessageBox
.showerror("IDLE Subprocess Error", "Socket Error: %s" % err
[1])
146 def print_exception():
148 linecache
.checkcache()
151 typ
, val
, tb
= excinfo
= sys
.exc_info()
152 sys
.last_type
, sys
.last_value
, sys
.last_traceback
= excinfo
153 tbe
= traceback
.extract_tb(tb
)
154 print>>efile
, '\nTraceback (most recent call last):'
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
)
163 def cleanup_traceback(tb
, exclude
):
164 "Remove excluded traces from beginning/end of tb; get cached lines"
167 for rpcfile
in exclude
:
168 if tb
[0][0].count(rpcfile
):
169 break # found an exclude, break for: and delete tb[0]
171 break # no excludes, have left RPC code, break while:
174 for rpcfile
in exclude
:
175 if tb
[-1][0].count(rpcfile
):
181 # exception was in IDLE internals, don't prune!
183 print>>sys
.stderr
, "** IDLE Internal Exception: "
184 rpchandler
= rpc
.objecttable
['exec'].rpchandler
185 for i
in range(len(tb
)):
186 fn
, ln
, nm
, line
= tb
[i
]
189 if not line
and fn
.startswith("<pyshell#"):
190 line
= rpchandler
.remotecall('linecache', 'getline',
192 tb
[i
] = fn
, ln
, nm
, line
196 if sys
.stdout
.softspace
:
197 sys
.stdout
.softspace
= 0
198 sys
.stdout
.write("\n")
199 except (AttributeError, EOFError):
203 """Exit subprocess, possibly after first deleting sys.exitfunc
205 If config-main.cfg/.def 'General' 'delete-exitfunc' is True, then any
206 sys.exitfunc will be removed before exiting. (VPython support)
212 except AttributeError:
216 class MyRPCServer(rpc
.RPCServer
):
218 def handle_error(self
, request
, client_address
):
219 """Override RPCServer method for IDLE
221 Interrupt the MainThread and exit server if link is dropped.
232 thread
.interrupt_main()
235 print>>erf
, '\n' + '-'*40
236 print>>erf
, 'Unhandled server exception!'
237 print>>erf
, 'Thread: %s' % threading
.currentThread().getName()
238 print>>erf
, 'Client Address: ', client_address
239 print>>erf
, 'Request: ', repr(request
)
240 traceback
.print_exc(file=erf
)
241 print>>erf
, '\n*** Unrecoverable, server exiting!'
244 thread
.interrupt_main()
247 class MyHandler(rpc
.RPCHandler
):
250 """Override base method"""
251 executive
= Executive(self
)
252 self
.register("exec", executive
)
253 sys
.stdin
= self
.console
= self
.get_remote_proxy("stdin")
254 sys
.stdout
= self
.get_remote_proxy("stdout")
255 sys
.stderr
= self
.get_remote_proxy("stderr")
257 sys
.stdin
.encoding
= sys
.stdout
.encoding
= \
258 sys
.stderr
.encoding
= IOBinding
.encoding
259 self
.interp
= self
.get_remote_proxy("interp")
260 rpc
.RPCHandler
.getresponse(self
, myseq
=None, wait
=0.05)
263 "override SocketIO method - wait for MainThread to shut us down"
267 "Override SocketIO method - terminate wait on callback and exit thread"
270 thread
.interrupt_main()
272 def decode_interrupthook(self
):
273 "interrupt awakened thread"
276 thread
.interrupt_main()
279 class Executive(object):
281 def __init__(self
, rpchandler
):
282 self
.rpchandler
= rpchandler
283 self
.locals = __main__
.__dict
__
284 self
.calltip
= CallTips
.CallTips()
285 self
.autocomplete
= AutoComplete
.AutoComplete()
287 def runcode(self
, code
):
290 self
.usr_exc_info
= None
293 exec code
in self
.locals
295 interruptable
= False
297 self
.usr_exc_info
= sys
.exc_info()
300 # even print a user code SystemExit exception, continue
302 jit
= self
.rpchandler
.console
.getvar("<<toggle-jit-stack-viewer>>")
304 self
.rpchandler
.interp
.open_remote_stack_viewer()
308 def interrupt_the_server(self
):
310 thread
.interrupt_main()
312 def start_the_debugger(self
, gui_adap_oid
):
313 return RemoteDebugger
.start_debugger(self
.rpchandler
, gui_adap_oid
)
315 def stop_the_debugger(self
, idb_adap_oid
):
316 "Unregister the Idb Adapter. Link objects and Idb then subject to GC"
317 self
.rpchandler
.unregister(idb_adap_oid
)
319 def get_the_calltip(self
, name
):
320 return self
.calltip
.fetch_tip(name
)
322 def get_the_completion_list(self
, what
, mode
):
323 return self
.autocomplete
.fetch_completions(what
, mode
)
325 def stackviewer(self
, flist_oid
=None):
326 if self
.usr_exc_info
:
327 typ
, val
, tb
= self
.usr_exc_info
331 if flist_oid
is not None:
332 flist
= self
.rpchandler
.get_remote_proxy(flist_oid
)
333 while tb
and tb
.tb_frame
.f_globals
["__name__"] in ["rpc", "run"]:
337 item
= StackViewer
.StackTreeItem(flist
, tb
)
338 return RemoteObjectBrowser
.remote_object_tree_item(item
)