def test_alarm2(): try: import _signal except ImportError: import signal as _signal import time triggered = None def handler(signal, frame): nonlocal triggered caller_code = sys._getframe(1).f_code assert caller_code == test_alarm2.__code__, "expected: '%s' but was '%s'" % ( test_alarm2.__code__, caller_code) triggered = (signal, frame) oldhandler = _signal.signal(_signal.SIGALRM, handler) assert oldhandler == _signal.SIG_DFL, "oldhandler != SIG_DFL" assert _signal.getsignal( _signal.SIGALRM) is handler, "getsignal handler != handler" _signal.alarm(1) while not triggered: time.sleep(0.5) assert triggered[0] == _signal.SIGALRM assert triggered[1].f_code.co_name == "test_alarm2", triggered[1].f_code
def test_alarm2(): try: import _signal except ImportError: import signal as _signal import time triggered = None def handler(signal, frame): nonlocal triggered triggered = (signal, frame) oldhandler = _signal.signal(_signal.SIGALRM, handler) assert oldhandler == _signal.SIG_DFL, "oldhandler != SIG_DFL" assert _signal.getsignal( _signal.SIGALRM) is handler, "getsignal handler != handler" _signal.alarm(1) while not triggered: time.sleep(0.5) assert triggered[0] == _signal.SIGALRM assert triggered[1].f_code.co_name == "test_alarm2", triggered[1].f_code
def test_alarm_raise(self): try: from _signal import alarm, signal, SIG_DFL, SIGALRM except ImportError: skip("no SIGALRM on this platform") import _socket class Alarm(Exception): pass def handler(*a): raise Alarm() s = _socket.socket() s.listen(1) try: signal(SIGALRM, handler) alarm(1) try: s._accept() except Alarm: pass else: raise Exception("should have raised Alarm") alarm(0) finally: signal(SIGALRM, SIG_DFL)
def test_pep475_retry(self): import select, time import _signal as signal def foo(*args): signalled.append("ALARM") # a list of functions that will do nothing more than sleep for 3 # seconds cases = [(select.select, [], [], [], 3.0)] if hasattr(select, 'poll'): import posix poll = select.poll() cases.append((poll.poll, 3000)) # milliseconds if hasattr(select, 'epoll'): epoll = select.epoll() cases.append((epoll.poll, 3.0)) if hasattr(select, 'kqueue'): kqueue = select.kqueue() cases.append((kqueue.control, [], 1, 3.0)) if hasattr(select, 'devpoll'): raise NotImplementedError("write this test if we have devpoll") for wait_for_three_seconds in cases: print(wait_for_three_seconds[0]) signalled = [] signal.signal(signal.SIGALRM, foo) try: t1 = time.time() signal.alarm(1) wait_for_three_seconds[0](*wait_for_three_seconds[1:]) t2 = time.time() finally: signal.signal(signal.SIGALRM, signal.SIG_DFL) print("result: signalled = %r in %s seconds" % (signalled, t2 - t1)) assert signalled != [] assert t2 - t1 > 2.99
def test_alarm(self): try: from _signal import alarm, signal, SIG_DFL, SIGALRM except: skip('no alarm on this platform') import time l = [] def handler(*a): l.append(42) try: signal(SIGALRM, handler) alarm(1) time.sleep(2) assert l == [42] alarm(0) assert l == [42] finally: signal(SIGALRM, SIG_DFL)
def test_pep475_retry_sleep(self): import time import _signal as signal if not hasattr(signal, 'SIGALRM'): skip("SIGALRM available only under Unix") signalled = [] def foo(*args): signalled.append("ALARM") signal.signal(signal.SIGALRM, foo) try: t1 = time.time() signal.alarm(1) time.sleep(3.0) t2 = time.time() finally: signal.signal(signal.SIGALRM, signal.SIG_DFL) assert signalled != [] assert t2 - t1 > 2.99
def skip_test_alarm(): # (tfel): this test is very brittle, because it is supposed to work with our # first, very primitive implementation of signal handlers, which does not # allow Python code to run in the handler. So we rely on a side-effect on an # open file descriptor instead. try: import _signal except ImportError: import signal as _signal import posix import time import sys # first, we start opening files until the fd is the same as SIGALRM fds = [] dupd_fd = None fd = None try: fd = posix.open(__file__, posix.O_RDONLY) while fd < _signal.SIGALRM: fds.append(fd) fd = posix.open(__file__, posix.O_RDONLY) if fd > _signal.SIGALRM: dupd_fd = posix.dup(_signal.SIGALRM) posix.close(_signal.SIGALRM) fd = posix.open(__file__, posix.O_RDONLY) # close the unneeded fds for oldfd in fds: posix.close(oldfd) assert fd == _signal.SIGALRM, "fd not equal to SIGALRM" # temporary: graalpython doesn't check the argcount for the handler atm if sys.implementation.name == "graalpython": handler = posix.close else: handler = lambda s, f: posix.close(s) oldhandler = _signal.signal(_signal.SIGALRM, handler) assert oldhandler == _signal.SIG_DFL, "oldhandler != SIG_DFL" assert _signal.getsignal( _signal.SIGALRM) is handler, "getsignal handler != handler" # schedule the alarm signal, that will trigger the handler, which # will in turn close our file _signal.alarm(1) # wait for the signal to come in and be handled time.sleep(1.5) # check for the side-effect try: posix.read(fd, 1) except OSError: assert True else: assert False, "file is still open" finally: if dupd_fd is not None: try: posix.close(fd) except OSError: pass posix.dup(dupd_fd) # duplicates back into just free'd fd