Пример #1
0
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
Пример #2
0
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
Пример #3
0
    def test_getsignal(self):
        """
        Test that signal.getsignal returns the currently installed handler.
        """
        from _signal import getsignal, signal, SIGINT, SIG_DFL, SIG_IGN

        def handler(*a):
            pass

        try:
            if not self.appdirect:
                assert getsignal(SIGINT) == SIG_DFL
            signal(SIGINT, SIG_DFL)
            assert getsignal(SIGINT) == SIG_DFL
            signal(SIGINT, SIG_IGN)
            assert getsignal(SIGINT) == SIG_IGN
            signal(SIGINT, handler)
            assert getsignal(SIGINT) is handler
        finally:
            signal(SIGINT, SIG_DFL)
Пример #4
0
    def test_check_signum(self):
        import sys
        from _signal import getsignal, signal, NSIG

        # signum out of range fails
        raises(ValueError, getsignal, NSIG)
        raises(ValueError, signal, NSIG, lambda *args: None)

        # on windows invalid signal within range should pass getsignal but fail signal
        if sys.platform == 'win32':
            assert getsignal(7) == None
            raises(ValueError, signal, 7, lambda *args: None)
Пример #5
0
def getsignal(signalnum):
    handler = _signal.getsignal(signalnum)
    return _int_to_enum(handler, Handlers)
Пример #6
0
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