def test_weak_method_func():
    from kivy.weakmethod import WeakMethod

    def do_something():
        pass

    weak_method = WeakMethod(do_something)

    assert not weak_method.is_dead()
    assert weak_method() == do_something
    assert weak_method == WeakMethod(do_something)

    del do_something
    gc.collect()

    assert not weak_method.is_dead()
    assert weak_method() is not None
def test_weak_method_on_obj():
    from kivy.weakmethod import WeakMethod

    class SomeClass:
        def do_something(self):
            pass

    obj = SomeClass()
    weak_method = WeakMethod(obj.do_something)

    assert not weak_method.is_dead()
    assert weak_method() == obj.do_something
    assert weak_method == WeakMethod(obj.do_something)
    assert weak_method != WeakMethod(SomeClass().do_something)

    del obj
    gc.collect()

    assert weak_method.is_dead()
    assert weak_method() is None
    assert weak_method != WeakMethod(SomeClass().do_something)
Пример #3
0
class _Event(object):

    def __init__(self, loop, callback, timeout, starttime):
        self.loop = loop
        self.callback = WeakMethod(callback)
        self.timeout = timeout
        self._last_dt = starttime
        self._dt = 0.

    def do(self, dt):
        if self.callback.is_dead():
            return False
        self.callback()(dt)

    def tick(self, curtime):
        # timeout happen ?
        if curtime - self._last_dt < self.timeout:
            return True

        # calculate current timediff for this event
        self._dt = curtime - self._last_dt
        self._last_dt = curtime

        # call the callback
        if self.callback.is_dead():
            return False
        ret = self.callback()(self._dt)

        # if it's a once event, don't care about the result
        # just remove the event
        if not self.loop:
            return False

        # if the user returns False explicitly,
        # remove the event
        if ret is False:
            return False

        return True
Пример #4
0
class _Event(object):
    def __init__(self, loop, callback, timeout, starttime):
        self.loop = loop
        self.callback = WeakMethod(callback)
        self.timeout = timeout
        self._last_dt = starttime
        self._dt = 0.

    def do(self, dt):
        if self.callback.is_dead():
            return False
        self.callback()(dt)

    def tick(self, curtime):
        # timeout happen ?
        if curtime - self._last_dt < self.timeout:
            return True

        # calculate current timediff for this event
        self._dt = curtime - self._last_dt
        self._last_dt = curtime

        # call the callback
        if self.callback.is_dead():
            return False
        ret = self.callback()(self._dt)

        # if it's a once event, don't care about the result
        # just remove the event
        if not self.loop:
            return False

        # if the user returns False explicitly,
        # remove the event
        if ret is False:
            return False

        return True