Exemplo n.º 1
0
 def test_context_manager_installs_default_handlers(self):
     with self.assertLogs('wrapitup') as logcm, self.catch_signals():
         self.assertFalse(requested())
         self.suicide(KILL1)
         self.assertTrue(requested())
     self.assertFalse(requested())  # The context manager cleans up
     self.assertFalse(self.handler_called)
     self.assert_logging(logcm.output)
Exemplo n.º 2
0
    def test_catch_both_signals_forward(self):
        with self.assertLogs('wrapitup') as logcm, self.catch_signals():
            self.assertFalse(requested())
            self.suicide(KILL1)
            self.assertTrue(requested())

            self.assertEqual(signal.getsignal(SIG2), signal.SIG_DFL)
        self.assert_logging(logcm.output)
        self.assertIn(SIG1.name, logcm.output[1])
Exemplo n.º 3
0
    def test_handler_reset_after_other_signals(self):
        with self.assertLogs('wrapitup') as logcm, self.catch_signals():
            self.assertFalse(requested())
            self.suicide(KILL2)
            self.assertTrue(requested())

            self.assertFalse(self.handler_called)
            self.suicide(KILL1)
            self.assertTrue(self.handler_called)
        self.assert_logging(logcm.output)
Exemplo n.º 4
0
    def test_catch_both_signals_backward(self):
        with self.assertLogs('wrapitup') as logcm, self.catch_signals():
            self.assertFalse(requested())
            self.suicide(KILL2)
            self.assertTrue(requested())

            self.assertFalse(self.handler_called)
            self.suicide(KILL1)
            self.assertTrue(self.handler_called)
        self.assert_logging(logcm.output)
        self.assertIn(SIG2.name, logcm.output[1])
Exemplo n.º 5
0
    def assert_context_manager_callbacks(self, error: bool):
        callback_args = None

        class Exc(Exception):
            pass

        def callback(signum: signal.Signals,
                     stack_frame: types.FrameType) -> None:
            nonlocal callback_args
            self.assertIsInstance(signum, signal.Signals)
            callback_args = (signum, stack_frame)
            if error:
                raise Exc

        def callback_star_args(*args):
            return callback(args[0], args[1])

        def callback_args_defaults(a=None, b=None, c=None):
            return callback(a, b)

        def callback_args_partial(a, *args):
            return callback(a, args[0])

        callbacks = (
            callback,
            callback_star_args,
            callback_args_defaults,
            callback_args_partial,
        )
        for cb in callbacks:
            self.setUp()
            callback_args = None
            with self.subTest(callback=cb.__name__):
                with self.assertLogs('wrapitup') as logcm, self.catch_signals(
                        cb):
                    self.assertFalse(requested())
                    if error:
                        with self.assertRaises(Exc):
                            self.suicide(KILL2)
                    else:
                        self.suicide(KILL2)
                    self.assertTrue(requested())

                    self.assertFalse(self.handler_called)
                    self.suicide(KILL1)
                    self.assertTrue(self.handler_called)
                self.assert_logging(logcm.output, default_callback=False)
                self.assertEqual(callback_args[0], SIG2)
                self.assertIsInstance(callback_args[1], types.FrameType)
Exemplo n.º 6
0
    def test_handler_restored_after_nested_catch_signals(self):
        self.assertEqual(signal.getsignal(SIG2), signal.SIG_DFL)
        catch = self.catch_signals()
        with self.assertLogs('wrapitup'), catch:
            signal.signal(SIG2, self.handler)
            self.assertFalse(requested())
            with catch:
                self.assertFalse(requested())
                self.suicide(KILL2)
                self.assertTrue(requested())

                self.assertFalse(self.handler_called)
                self.suicide(KILL2)
                self.assertTrue(self.handler_called)
            self.assertFalse(requested())
            self.assertEqual(signal.getsignal(SIG2), self.handler)

            self.assertFalse(requested())
            self.suicide(KILL1)
            self.assertTrue(requested())

            self.handler_called = False
            self.suicide(KILL1)
            self.assertTrue(self.handler_called)
        self.assertFalse(requested())
        self.assertEqual(signal.getsignal(SIG2), signal.SIG_DFL)
Exemplo n.º 7
0
    def test_catch_signals_resets_requests(self):
        with self.assertLogs('wrapitup') as logcm, self.catch_signals():
            self.assertFalse(requested())
            self.suicide(KILL1)
            self.assertTrue(requested())
        self.assertFalse(requested())  # The context manager cleans up
        self.assertFalse(self.handler_called)
        self.assert_logging(logcm.output)

        # Do not overwrite existing request
        request()
        with self.assertLogs('wrapitup') as logcm, self.catch_signals():
            self.assertTrue(requested())
            self.suicide(KILL1)
            self.assertTrue(requested())
        self.assertTrue(requested())
        self.assertFalse(self.handler_called)
        self.assert_logging(logcm.output)
Exemplo n.º 8
0
 def test_request(self):
     self.assertFalse(requested())
     request()
     self.assertTrue(requested())
     reset()
     self.assertFalse(requested())