Skip to content

Commit 72ce40d

Browse files
Merge pull request #99 from dalthviz/spyder_12300
PR: Don't call os.kill on Windows if running inside Spyder
2 parents 34fcb8e + 67d1804 commit 72ce40d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

vpython/no_notebook.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@
2222
from .rate_control import rate
2323

2424

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+
2550
# Check for Ctrl+C. SIGINT will also be sent by our code if WServer is closed.
2651
def signal_handler(signal, frame):
2752
stop_server()
@@ -211,7 +236,7 @@ def onClose(self, wasClean, code, reason):
211236
# Only the main thread can properly call sys.exit, so have a signal
212237
# handler call it on the main thread's behalf.
213238
if platform.system() == 'Windows':
214-
if threading.main_thread().is_alive():
239+
if threading.main_thread().is_alive() and not _in_spyder:
215240
# On windows, if we get here then this signal won't be caught
216241
# by our signal handler. Just call it ourselves.
217242
os.kill(os.getpid(), signal.CTRL_C_EVENT)

0 commit comments

Comments
 (0)