structio/structio/core/managers/signals/sigint.py

38 lines
1.1 KiB
Python
Raw Normal View History

from structio.abc import SignalManager
2023-05-15 18:25:02 +02:00
from types import FrameType
from structio.util.ki import currently_protected
from structio.core.run import current_loop
import warnings
import signal
class SigIntManager(SignalManager):
"""
Handles Ctrl+C
"""
def __init__(self):
self.installed = False
def handle(self, sig: int, frame: FrameType):
loop = current_loop()
if currently_protected():
loop.signal_notify(sig, frame)
else:
raise KeyboardInterrupt()
def install(self):
if signal.getsignal(signal.SIGINT) != signal.default_int_handler:
2023-05-22 09:22:37 +02:00
warnings.warn(
f"structio has detected a custom SIGINT handler and won't touch it: keep in mind"
f" this is likely to break KeyboardInterrupt delivery!"
)
2023-05-15 18:25:02 +02:00
return
signal.signal(signal.SIGINT, self.handle)
self.installed = True
def uninstall(self):
if self.installed:
signal.signal(signal.SIGINT, signal.default_int_handler)
self.installed = False