run.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. """ idlelib.run
  2. Simplified, pyshell.ModifiedInterpreter spawns a subprocess with
  3. f'''{sys.executable} -c "__import__('idlelib.run').run.main()"'''
  4. '.run' is needed because __import__ returns idlelib, not idlelib.run.
  5. """
  6. import functools
  7. import io
  8. import linecache
  9. import queue
  10. import sys
  11. import textwrap
  12. import time
  13. import traceback
  14. import _thread as thread
  15. import threading
  16. import warnings
  17. import idlelib # testing
  18. from idlelib import autocomplete # AutoComplete, fetch_encodings
  19. from idlelib import calltip # Calltip
  20. from idlelib import debugger_r # start_debugger
  21. from idlelib import debugobj_r # remote_object_tree_item
  22. from idlelib import iomenu # encoding
  23. from idlelib import rpc # multiple objects
  24. from idlelib import stackviewer # StackTreeItem
  25. import __main__
  26. import tkinter # Use tcl and, if startup fails, messagebox.
  27. if not hasattr(sys.modules['idlelib.run'], 'firstrun'):
  28. # Undo modifications of tkinter by idlelib imports; see bpo-25507.
  29. for mod in ('simpledialog', 'messagebox', 'font',
  30. 'dialog', 'filedialog', 'commondialog',
  31. 'ttk'):
  32. delattr(tkinter, mod)
  33. del sys.modules['tkinter.' + mod]
  34. # Avoid AttributeError if run again; see bpo-37038.
  35. sys.modules['idlelib.run'].firstrun = False
  36. LOCALHOST = '127.0.0.1'
  37. try:
  38. eof = 'Ctrl-D (end-of-file)'
  39. exit.eof = eof
  40. quit.eof = eof
  41. except NameError: # In case subprocess started with -S (maybe in future).
  42. pass
  43. def idle_formatwarning(message, category, filename, lineno, line=None):
  44. """Format warnings the IDLE way."""
  45. s = "\nWarning (from warnings module):\n"
  46. s += ' File \"%s\", line %s\n' % (filename, lineno)
  47. if line is None:
  48. line = linecache.getline(filename, lineno)
  49. line = line.strip()
  50. if line:
  51. s += " %s\n" % line
  52. s += "%s: %s\n" % (category.__name__, message)
  53. return s
  54. def idle_showwarning_subproc(
  55. message, category, filename, lineno, file=None, line=None):
  56. """Show Idle-format warning after replacing warnings.showwarning.
  57. The only difference is the formatter called.
  58. """
  59. if file is None:
  60. file = sys.stderr
  61. try:
  62. file.write(idle_formatwarning(
  63. message, category, filename, lineno, line))
  64. except OSError:
  65. pass # the file (probably stderr) is invalid - this warning gets lost.
  66. _warnings_showwarning = None
  67. def capture_warnings(capture):
  68. "Replace warning.showwarning with idle_showwarning_subproc, or reverse."
  69. global _warnings_showwarning
  70. if capture:
  71. if _warnings_showwarning is None:
  72. _warnings_showwarning = warnings.showwarning
  73. warnings.showwarning = idle_showwarning_subproc
  74. else:
  75. if _warnings_showwarning is not None:
  76. warnings.showwarning = _warnings_showwarning
  77. _warnings_showwarning = None
  78. capture_warnings(True)
  79. tcl = tkinter.Tcl()
  80. def handle_tk_events(tcl=tcl):
  81. """Process any tk events that are ready to be dispatched if tkinter
  82. has been imported, a tcl interpreter has been created and tk has been
  83. loaded."""
  84. tcl.eval("update")
  85. # Thread shared globals: Establish a queue between a subthread (which handles
  86. # the socket) and the main thread (which runs user code), plus global
  87. # completion, exit and interruptable (the main thread) flags:
  88. exit_now = False
  89. quitting = False
  90. interruptable = False
  91. def main(del_exitfunc=False):
  92. """Start the Python execution server in a subprocess
  93. In the Python subprocess, RPCServer is instantiated with handlerclass
  94. MyHandler, which inherits register/unregister methods from RPCHandler via
  95. the mix-in class SocketIO.
  96. When the RPCServer 'server' is instantiated, the TCPServer initialization
  97. creates an instance of run.MyHandler and calls its handle() method.
  98. handle() instantiates a run.Executive object, passing it a reference to the
  99. MyHandler object. That reference is saved as attribute rpchandler of the
  100. Executive instance. The Executive methods have access to the reference and
  101. can pass it on to entities that they command
  102. (e.g. debugger_r.Debugger.start_debugger()). The latter, in turn, can
  103. call MyHandler(SocketIO) register/unregister methods via the reference to
  104. register and unregister themselves.
  105. """
  106. global exit_now
  107. global quitting
  108. global no_exitfunc
  109. no_exitfunc = del_exitfunc
  110. #time.sleep(15) # test subprocess not responding
  111. try:
  112. assert(len(sys.argv) > 1)
  113. port = int(sys.argv[-1])
  114. except:
  115. print("IDLE Subprocess: no IP port passed in sys.argv.",
  116. file=sys.__stderr__)
  117. return
  118. capture_warnings(True)
  119. sys.argv[:] = [""]
  120. sockthread = threading.Thread(target=manage_socket,
  121. name='SockThread',
  122. args=((LOCALHOST, port),))
  123. sockthread.daemon = True
  124. sockthread.start()
  125. while 1:
  126. try:
  127. if exit_now:
  128. try:
  129. exit()
  130. except KeyboardInterrupt:
  131. # exiting but got an extra KBI? Try again!
  132. continue
  133. try:
  134. request = rpc.request_queue.get(block=True, timeout=0.05)
  135. except queue.Empty:
  136. request = None
  137. # Issue 32207: calling handle_tk_events here adds spurious
  138. # queue.Empty traceback to event handling exceptions.
  139. if request:
  140. seq, (method, args, kwargs) = request
  141. ret = method(*args, **kwargs)
  142. rpc.response_queue.put((seq, ret))
  143. else:
  144. handle_tk_events()
  145. except KeyboardInterrupt:
  146. if quitting:
  147. exit_now = True
  148. continue
  149. except SystemExit:
  150. capture_warnings(False)
  151. raise
  152. except:
  153. type, value, tb = sys.exc_info()
  154. try:
  155. print_exception()
  156. rpc.response_queue.put((seq, None))
  157. except:
  158. # Link didn't work, print same exception to __stderr__
  159. traceback.print_exception(type, value, tb, file=sys.__stderr__)
  160. exit()
  161. else:
  162. continue
  163. def manage_socket(address):
  164. for i in range(3):
  165. time.sleep(i)
  166. try:
  167. server = MyRPCServer(address, MyHandler)
  168. break
  169. except OSError as err:
  170. print("IDLE Subprocess: OSError: " + err.args[1] +
  171. ", retrying....", file=sys.__stderr__)
  172. socket_error = err
  173. else:
  174. print("IDLE Subprocess: Connection to "
  175. "IDLE GUI failed, exiting.", file=sys.__stderr__)
  176. show_socket_error(socket_error, address)
  177. global exit_now
  178. exit_now = True
  179. return
  180. server.handle_request() # A single request only
  181. def show_socket_error(err, address):
  182. "Display socket error from manage_socket."
  183. import tkinter
  184. from tkinter.messagebox import showerror
  185. root = tkinter.Tk()
  186. fix_scaling(root)
  187. root.withdraw()
  188. showerror(
  189. "Subprocess Connection Error",
  190. f"IDLE's subprocess can't connect to {address[0]}:{address[1]}.\n"
  191. f"Fatal OSError #{err.errno}: {err.strerror}.\n"
  192. "See the 'Startup failure' section of the IDLE doc, online at\n"
  193. "https://docs.python.org/3/library/idle.html#startup-failure",
  194. parent=root)
  195. root.destroy()
  196. def print_exception():
  197. import linecache
  198. linecache.checkcache()
  199. flush_stdout()
  200. efile = sys.stderr
  201. typ, val, tb = excinfo = sys.exc_info()
  202. sys.last_type, sys.last_value, sys.last_traceback = excinfo
  203. seen = set()
  204. def print_exc(typ, exc, tb):
  205. seen.add(id(exc))
  206. context = exc.__context__
  207. cause = exc.__cause__
  208. if cause is not None and id(cause) not in seen:
  209. print_exc(type(cause), cause, cause.__traceback__)
  210. print("\nThe above exception was the direct cause "
  211. "of the following exception:\n", file=efile)
  212. elif (context is not None and
  213. not exc.__suppress_context__ and
  214. id(context) not in seen):
  215. print_exc(type(context), context, context.__traceback__)
  216. print("\nDuring handling of the above exception, "
  217. "another exception occurred:\n", file=efile)
  218. if tb:
  219. tbe = traceback.extract_tb(tb)
  220. print('Traceback (most recent call last):', file=efile)
  221. exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
  222. "debugger_r.py", "bdb.py")
  223. cleanup_traceback(tbe, exclude)
  224. traceback.print_list(tbe, file=efile)
  225. lines = traceback.format_exception_only(typ, exc)
  226. for line in lines:
  227. print(line, end='', file=efile)
  228. print_exc(typ, val, tb)
  229. def cleanup_traceback(tb, exclude):
  230. "Remove excluded traces from beginning/end of tb; get cached lines"
  231. orig_tb = tb[:]
  232. while tb:
  233. for rpcfile in exclude:
  234. if tb[0][0].count(rpcfile):
  235. break # found an exclude, break for: and delete tb[0]
  236. else:
  237. break # no excludes, have left RPC code, break while:
  238. del tb[0]
  239. while tb:
  240. for rpcfile in exclude:
  241. if tb[-1][0].count(rpcfile):
  242. break
  243. else:
  244. break
  245. del tb[-1]
  246. if len(tb) == 0:
  247. # exception was in IDLE internals, don't prune!
  248. tb[:] = orig_tb[:]
  249. print("** IDLE Internal Exception: ", file=sys.stderr)
  250. rpchandler = rpc.objecttable['exec'].rpchandler
  251. for i in range(len(tb)):
  252. fn, ln, nm, line = tb[i]
  253. if nm == '?':
  254. nm = "-toplevel-"
  255. if not line and fn.startswith("<pyshell#"):
  256. line = rpchandler.remotecall('linecache', 'getline',
  257. (fn, ln), {})
  258. tb[i] = fn, ln, nm, line
  259. def flush_stdout():
  260. """XXX How to do this now?"""
  261. def exit():
  262. """Exit subprocess, possibly after first clearing exit functions.
  263. If config-main.cfg/.def 'General' 'delete-exitfunc' is True, then any
  264. functions registered with atexit will be removed before exiting.
  265. (VPython support)
  266. """
  267. if no_exitfunc:
  268. import atexit
  269. atexit._clear()
  270. capture_warnings(False)
  271. sys.exit(0)
  272. def fix_scaling(root):
  273. """Scale fonts on HiDPI displays."""
  274. import tkinter.font
  275. scaling = float(root.tk.call('tk', 'scaling'))
  276. if scaling > 1.4:
  277. for name in tkinter.font.names(root):
  278. font = tkinter.font.Font(root=root, name=name, exists=True)
  279. size = int(font['size'])
  280. if size < 0:
  281. font['size'] = round(-0.75*size)
  282. def fixdoc(fun, text):
  283. tem = (fun.__doc__ + '\n\n') if fun.__doc__ is not None else ''
  284. fun.__doc__ = tem + textwrap.fill(textwrap.dedent(text))
  285. RECURSIONLIMIT_DELTA = 30
  286. def install_recursionlimit_wrappers():
  287. """Install wrappers to always add 30 to the recursion limit."""
  288. # see: bpo-26806
  289. @functools.wraps(sys.setrecursionlimit)
  290. def setrecursionlimit(*args, **kwargs):
  291. # mimic the original sys.setrecursionlimit()'s input handling
  292. if kwargs:
  293. raise TypeError(
  294. "setrecursionlimit() takes no keyword arguments")
  295. try:
  296. limit, = args
  297. except ValueError:
  298. raise TypeError(f"setrecursionlimit() takes exactly one "
  299. f"argument ({len(args)} given)")
  300. if not limit > 0:
  301. raise ValueError(
  302. "recursion limit must be greater or equal than 1")
  303. return setrecursionlimit.__wrapped__(limit + RECURSIONLIMIT_DELTA)
  304. fixdoc(setrecursionlimit, f"""\
  305. This IDLE wrapper adds {RECURSIONLIMIT_DELTA} to prevent possible
  306. uninterruptible loops.""")
  307. @functools.wraps(sys.getrecursionlimit)
  308. def getrecursionlimit():
  309. return getrecursionlimit.__wrapped__() - RECURSIONLIMIT_DELTA
  310. fixdoc(getrecursionlimit, f"""\
  311. This IDLE wrapper subtracts {RECURSIONLIMIT_DELTA} to compensate
  312. for the {RECURSIONLIMIT_DELTA} IDLE adds when setting the limit.""")
  313. # add the delta to the default recursion limit, to compensate
  314. sys.setrecursionlimit(sys.getrecursionlimit() + RECURSIONLIMIT_DELTA)
  315. sys.setrecursionlimit = setrecursionlimit
  316. sys.getrecursionlimit = getrecursionlimit
  317. def uninstall_recursionlimit_wrappers():
  318. """Uninstall the recursion limit wrappers from the sys module.
  319. IDLE only uses this for tests. Users can import run and call
  320. this to remove the wrapping.
  321. """
  322. if (
  323. getattr(sys.setrecursionlimit, '__wrapped__', None) and
  324. getattr(sys.getrecursionlimit, '__wrapped__', None)
  325. ):
  326. sys.setrecursionlimit = sys.setrecursionlimit.__wrapped__
  327. sys.getrecursionlimit = sys.getrecursionlimit.__wrapped__
  328. sys.setrecursionlimit(sys.getrecursionlimit() - RECURSIONLIMIT_DELTA)
  329. class MyRPCServer(rpc.RPCServer):
  330. def handle_error(self, request, client_address):
  331. """Override RPCServer method for IDLE
  332. Interrupt the MainThread and exit server if link is dropped.
  333. """
  334. global quitting
  335. try:
  336. raise
  337. except SystemExit:
  338. raise
  339. except EOFError:
  340. global exit_now
  341. exit_now = True
  342. thread.interrupt_main()
  343. except:
  344. erf = sys.__stderr__
  345. print(textwrap.dedent(f"""
  346. {'-'*40}
  347. Unhandled exception in user code execution server!'
  348. Thread: {threading.current_thread().name}
  349. IDLE Client Address: {client_address}
  350. Request: {request!r}
  351. """), file=erf)
  352. traceback.print_exc(limit=-20, file=erf)
  353. print(textwrap.dedent(f"""
  354. *** Unrecoverable, server exiting!
  355. Users should never see this message; it is likely transient.
  356. If this recurs, report this with a copy of the message
  357. and an explanation of how to make it repeat.
  358. {'-'*40}"""), file=erf)
  359. quitting = True
  360. thread.interrupt_main()
  361. # Pseudofiles for shell-remote communication (also used in pyshell)
  362. class StdioFile(io.TextIOBase):
  363. def __init__(self, shell, tags, encoding='utf-8', errors='strict'):
  364. self.shell = shell
  365. self.tags = tags
  366. self._encoding = encoding
  367. self._errors = errors
  368. @property
  369. def encoding(self):
  370. return self._encoding
  371. @property
  372. def errors(self):
  373. return self._errors
  374. @property
  375. def name(self):
  376. return '<%s>' % self.tags
  377. def isatty(self):
  378. return True
  379. class StdOutputFile(StdioFile):
  380. def writable(self):
  381. return True
  382. def write(self, s):
  383. if self.closed:
  384. raise ValueError("write to closed file")
  385. s = str.encode(s, self.encoding, self.errors).decode(self.encoding, self.errors)
  386. return self.shell.write(s, self.tags)
  387. class StdInputFile(StdioFile):
  388. _line_buffer = ''
  389. def readable(self):
  390. return True
  391. def read(self, size=-1):
  392. if self.closed:
  393. raise ValueError("read from closed file")
  394. if size is None:
  395. size = -1
  396. elif not isinstance(size, int):
  397. raise TypeError('must be int, not ' + type(size).__name__)
  398. result = self._line_buffer
  399. self._line_buffer = ''
  400. if size < 0:
  401. while line := self.shell.readline():
  402. result += line
  403. else:
  404. while len(result) < size:
  405. line = self.shell.readline()
  406. if not line: break
  407. result += line
  408. self._line_buffer = result[size:]
  409. result = result[:size]
  410. return result
  411. def readline(self, size=-1):
  412. if self.closed:
  413. raise ValueError("read from closed file")
  414. if size is None:
  415. size = -1
  416. elif not isinstance(size, int):
  417. raise TypeError('must be int, not ' + type(size).__name__)
  418. line = self._line_buffer or self.shell.readline()
  419. if size < 0:
  420. size = len(line)
  421. eol = line.find('\n', 0, size)
  422. if eol >= 0:
  423. size = eol + 1
  424. self._line_buffer = line[size:]
  425. return line[:size]
  426. def close(self):
  427. self.shell.close()
  428. class MyHandler(rpc.RPCHandler):
  429. def handle(self):
  430. """Override base method"""
  431. executive = Executive(self)
  432. self.register("exec", executive)
  433. self.console = self.get_remote_proxy("console")
  434. sys.stdin = StdInputFile(self.console, "stdin",
  435. iomenu.encoding, iomenu.errors)
  436. sys.stdout = StdOutputFile(self.console, "stdout",
  437. iomenu.encoding, iomenu.errors)
  438. sys.stderr = StdOutputFile(self.console, "stderr",
  439. iomenu.encoding, "backslashreplace")
  440. sys.displayhook = rpc.displayhook
  441. # page help() text to shell.
  442. import pydoc # import must be done here to capture i/o binding
  443. pydoc.pager = pydoc.plainpager
  444. # Keep a reference to stdin so that it won't try to exit IDLE if
  445. # sys.stdin gets changed from within IDLE's shell. See issue17838.
  446. self._keep_stdin = sys.stdin
  447. install_recursionlimit_wrappers()
  448. self.interp = self.get_remote_proxy("interp")
  449. rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05)
  450. def exithook(self):
  451. "override SocketIO method - wait for MainThread to shut us down"
  452. time.sleep(10)
  453. def EOFhook(self):
  454. "Override SocketIO method - terminate wait on callback and exit thread"
  455. global quitting
  456. quitting = True
  457. thread.interrupt_main()
  458. def decode_interrupthook(self):
  459. "interrupt awakened thread"
  460. global quitting
  461. quitting = True
  462. thread.interrupt_main()
  463. class Executive:
  464. def __init__(self, rpchandler):
  465. self.rpchandler = rpchandler
  466. if idlelib.testing is False:
  467. self.locals = __main__.__dict__
  468. self.calltip = calltip.Calltip()
  469. self.autocomplete = autocomplete.AutoComplete()
  470. else:
  471. self.locals = {}
  472. def runcode(self, code):
  473. global interruptable
  474. try:
  475. self.user_exc_info = None
  476. interruptable = True
  477. try:
  478. exec(code, self.locals)
  479. finally:
  480. interruptable = False
  481. except SystemExit as e:
  482. if e.args: # SystemExit called with an argument.
  483. ob = e.args[0]
  484. if not isinstance(ob, (type(None), int)):
  485. print('SystemExit: ' + str(ob), file=sys.stderr)
  486. # Return to the interactive prompt.
  487. except:
  488. self.user_exc_info = sys.exc_info() # For testing, hook, viewer.
  489. if quitting:
  490. exit()
  491. if sys.excepthook is sys.__excepthook__:
  492. print_exception()
  493. else:
  494. try:
  495. sys.excepthook(*self.user_exc_info)
  496. except:
  497. self.user_exc_info = sys.exc_info() # For testing.
  498. print_exception()
  499. jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")
  500. if jit:
  501. self.rpchandler.interp.open_remote_stack_viewer()
  502. else:
  503. flush_stdout()
  504. def interrupt_the_server(self):
  505. if interruptable:
  506. thread.interrupt_main()
  507. def start_the_debugger(self, gui_adap_oid):
  508. return debugger_r.start_debugger(self.rpchandler, gui_adap_oid)
  509. def stop_the_debugger(self, idb_adap_oid):
  510. "Unregister the Idb Adapter. Link objects and Idb then subject to GC"
  511. self.rpchandler.unregister(idb_adap_oid)
  512. def get_the_calltip(self, name):
  513. return self.calltip.fetch_tip(name)
  514. def get_the_completion_list(self, what, mode):
  515. return self.autocomplete.fetch_completions(what, mode)
  516. def stackviewer(self, flist_oid=None):
  517. if self.user_exc_info:
  518. typ, val, tb = self.user_exc_info
  519. else:
  520. return None
  521. flist = None
  522. if flist_oid is not None:
  523. flist = self.rpchandler.get_remote_proxy(flist_oid)
  524. while tb and tb.tb_frame.f_globals["__name__"] in ["rpc", "run"]:
  525. tb = tb.tb_next
  526. sys.last_type = typ
  527. sys.last_value = val
  528. item = stackviewer.StackTreeItem(flist, tb)
  529. return debugobj_r.remote_object_tree_item(item)
  530. if __name__ == '__main__':
  531. from unittest import main
  532. main('idlelib.idle_test.test_run', verbosity=2)
  533. capture_warnings(False) # Make sure turned off; see bpo-18081.