Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #168 from FynnBe/master
Browse files Browse the repository at this point in the history
delay keyboard interrupt only if running in main thread
  • Loading branch information
constantinpape authored Mar 26, 2019
2 parents 4bb6bc3 + 133015b commit 5158e7d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions inferno/utils/python_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import inspect
import os

from threading import current_thread, main_thread


def ensure_dir(directory):
"""ensure the existence of e directory at a given path
Expand Down Expand Up @@ -96,17 +98,19 @@ class delayed_keyboard_interrupt(object):
"""
# PEP8: Context manager class in lowercase
def __enter__(self):
self.signal_received = False
self.old_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, self.handler)
if current_thread() is main_thread():
self.signal_received = False
self.old_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, self.handler)

def handler(self, sig, frame):
self.signal_received = (sig, frame)

def __exit__(self, type, value, traceback):
signal.signal(signal.SIGINT, self.old_handler)
if self.signal_received:
self.old_handler(*self.signal_received)
if current_thread() is main_thread():
signal.signal(signal.SIGINT, self.old_handler)
if self.signal_received:
self.old_handler(*self.signal_received)


def get_config_for_name(config, name):
Expand Down

0 comments on commit 5158e7d

Please sign in to comment.