Skip to content

PR: Don't call os.kill on Windows if running inside Spyder #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion vpython/no_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@
from .rate_control import rate


# Redefine `Thread.run` to not show a traceback for Spyder when stopping
# the server by raising a KeyboardInterrupt or SystemExit.
if _in_spyder:
def install_thread_stopped_message():
"""
Workaround to prevent showing a traceback when VPython server stops.

See:
https://bugs.python.org/issue1230540
"""
run_old = threading.Thread.run

def run(*args, **kwargs):
try:
run_old(*args, **kwargs)
except (KeyboardInterrupt, SystemExit):
print("VPython server stopped.")
except:
raise
threading.Thread.run = run

install_thread_stopped_message()



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