Skip to content
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

WiP: Handle CTRL-C interrupts from terminal to shutdown Mu gracefully #1324

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions mu/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import os
import platform
import sys
import signal

from PyQt5.QtCore import (
Qt,
QEventLoop,
QThread,
QObject,
pyqtSignal,
QTimer,
)
from PyQt5.QtWidgets import QApplication, QSplashScreen

Expand Down Expand Up @@ -108,6 +110,41 @@ def excepthook(*exc_args):
sys.exit(1)


# The following function handles a problem with using CTRL+C
# in terminal to terminate Qt applications.
# From:
# https://coldfix.eu/2016/11/08/pyqt-boilerplate/#keyboardinterrupt-ctrl-c
def setup_interrupt_handling(editor_quit):
"""Setup handling of KeyboardInterrupt (Ctrl-C) for PyQt."""

# Define this as a global function to make sure it is not garbage
# collected when going out of scope:
global _interrupt_handler

def _interrupt_handler(signum, frame):
"""Handle KeyboardInterrupt: quit application."""
editor_quit()

def safe_timer(timeout, func, *args, **kwargs):
"""
Create a timer that is safe against garbage collection and overlapping
calls. See: http://ralsina.me/weblog/posts/BB974.html
"""

def timer_event():
try:
func(*args, **kwargs)
finally:
QTimer.singleShot(timeout, timer_event)

QTimer.singleShot(timeout, timer_event)

signal.signal(signal.SIGINT, _interrupt_handler)
# Regularly run some (any) python code, so the signal handler gets a
# chance to be executed:
safe_timer(50, lambda: None)


def setup_logging():
"""
Configure logging.
Expand Down Expand Up @@ -253,6 +290,9 @@ def load_theme(theme):
editor_window.connect_toggle_comments(editor.toggle_comments, "Ctrl+K")
editor.connect_to_status_bar(editor_window.status_bar)

# Make CTRL-C quit Mu
setup_interrupt_handling(editor.quit)

# Restore the previous session along with files passed by the os
editor.restore_session(sys.argv[1:])

Expand Down