|
22 | 22 | from .rate_control import rate
|
23 | 23 |
|
24 | 24 |
|
| 25 | +# Redefine `Thread.run` to not show a traceback for Spyder when stopping |
| 26 | +# the server by raising a KeyboardInterrupt or SystemExit. |
| 27 | +if _in_spyder: |
| 28 | + def install_thread_stopped_message(): |
| 29 | + """ |
| 30 | + Workaround to prevent showing a traceback when VPython server stops. |
| 31 | +
|
| 32 | + See: |
| 33 | + https://bugs.python.org/issue1230540 |
| 34 | + """ |
| 35 | + run_old = threading.Thread.run |
| 36 | + |
| 37 | + def run(*args, **kwargs): |
| 38 | + try: |
| 39 | + run_old(*args, **kwargs) |
| 40 | + except (KeyboardInterrupt, SystemExit): |
| 41 | + print("VPython server stopped.") |
| 42 | + except: |
| 43 | + raise |
| 44 | + threading.Thread.run = run |
| 45 | + |
| 46 | + install_thread_stopped_message() |
| 47 | + |
| 48 | + |
| 49 | + |
25 | 50 | # Check for Ctrl+C. SIGINT will also be sent by our code if WServer is closed.
|
26 | 51 | def signal_handler(signal, frame):
|
27 | 52 | stop_server()
|
@@ -211,7 +236,7 @@ def onClose(self, wasClean, code, reason):
|
211 | 236 | # Only the main thread can properly call sys.exit, so have a signal
|
212 | 237 | # handler call it on the main thread's behalf.
|
213 | 238 | if platform.system() == 'Windows':
|
214 |
| - if threading.main_thread().is_alive(): |
| 239 | + if threading.main_thread().is_alive() and not _in_spyder: |
215 | 240 | # On windows, if we get here then this signal won't be caught
|
216 | 241 | # by our signal handler. Just call it ourselves.
|
217 | 242 | os.kill(os.getpid(), signal.CTRL_C_EVENT)
|
|
0 commit comments