def test_function_dead(self):
        def func(arg):
            return arg * 2

        f = weak_method.WeakCallable(func)
        self.assertFalse(f.dead())
        del func
        self.assertTrue(f.dead())
    def test_weakref_function(self):
        def func(arg):
            return arg * 2

        f = weak_method.WeakCallable(func)
        self.assertEqual(f(1234), 2468)
        del func
        self.assertRaises(TypeError, f, 1234)
    def test_weakref_function(self):
        def func(arg):
            return arg * 2

        f = weak_method.WeakCallable(func)
        self.assertEqual(f(1234), 2468)
        del func
        self.assertRaises(InvalidCallbackException, f, 1234)
Beispiel #4
0
 def deregister_callback(self, function):
     comparable_unwrapped = function
     comparable_wrapped = weak_method.WeakCallable(function)
     to_remove = [(callback, params)
                  for callback, params in self._callbacks_with_params
                  if (callback == comparable_unwrapped
                      or callback == comparable_wrapped)]
     for pair in to_remove:
         self._callbacks_with_params.remove(pair)
    def test_method_dead(self):
        class Test(object):
            def func(self, arg):
                return arg * 2

        instance = Test()
        f = weak_method.WeakCallable(instance.func)
        self.assertFalse(f.dead())
        del instance
        self.assertTrue(f.dead())
    def test_weakref_method(self):
        class Test(object):
            def func(self, arg):
                return arg * 2

        instance = Test()
        f = weak_method.WeakCallable(instance.func)
        self.assertEqual(f(1234), 2468)
        del instance
        self.assertRaises(TypeError, f, 1234)
    def test_deregister_callback_wrapped(self):
        command_class = CTICommandClass(self.class_name, None, None)
        command = command_class.from_dict({'class': 'callback_test'})

        function_1 = lambda: None
        function_2 = lambda: None

        command_class.register_callback_params(function_1)
        command_class.register_callback_params(function_2)
        command_class.deregister_callback(command.callbacks_with_params()[0][0])

        callbacks = command.callbacks_with_params()
        self.assertEqual(len(callbacks), 1)
        self.assertEqual(weak_method.WeakCallable(function_2), callbacks[0][0])
Beispiel #8
0
 def register_callback_params(self, function, params=None):
     if not params:
         params = []
     self._callbacks_with_params.append((weak_method.WeakCallable(function), params))