Python weakref 模块,ReferenceError() 实例源码

我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用weakref.ReferenceError()

项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_WeakObject(self):
        """
        Tests basic extended weak object support.
        """
        obj = TestClass()
        ref = weakref.ref(obj)

        objref = weakobj.objectref(obj)
        objref2 = weakobj.objectref(obj)

        self.assertEquals(objref, objref2)
        self.assertEquals(objref.foo(), obj.foo())

        # Delete what should be the only reference to the original object.
        del obj
        self.assertEqual(ref(), None)

        try:
            objref.foo()
        except weakref.ReferenceError:
            pass
        else:
            self.fail('Weak object should be invalidated')
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_WeakBoundMethod(self):
        """
        Test that weakly-bound methods can be called like a regular bound
        method, and are invalidated on object destruction.
        """
        obj = TestClass()
        foo = weakobj.boundmethod(obj.foo)

        # Check that the weakly-bound method has the same behavior.
        self.assertEqual(obj.foo(), foo())

        # Check that weakly-bound notifications behave correctly.
        self._notificationTest(weakobj.boundmethod(obj.bar))

        # Delete the object and ensure that the method is invalidated.
        del obj
        try:
            foo()
        except weakref.ReferenceError:
            pass
        else:
            self.fail('Weak bound method should be invalidated')
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_proxy_ref(self):
        o = C()
        o.bar = 1
        ref1 = weakref.proxy(o, self.callback)
        ref2 = weakref.proxy(o, self.callback)
        del o
        gc_collect()

        def check(proxy):
            proxy.bar

        self.assertRaises(weakref.ReferenceError, check, ref1)
        self.assertRaises(weakref.ReferenceError, check, ref2)
        ref3 = weakref.proxy(C())
        gc_collect()
        self.assertRaises(weakref.ReferenceError, bool, ref3)
        self.assertEqual(self.cbcalled, 2)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __getattr__(self, name):

        # if we are proxy to a ComponentData
        # object, we need to make sure NOT to
        # invoke its deepcopy method
        if name == "__deepcopy__":
            raise AttributeError

        if name in Alias.__slots__:
            # should never happen
            assert False

        if name in Alias._component_slots:
            return super(Alias, self).__dict__[name].__get__(self)

        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return getattr(self.aliased_object, name)

    #
    # __setattr__ and __delattr__ will always be invoked
    #
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __delattr__(self, name):
        # avoid infinite recursion
        if name in Alias.__slots__:
            raise TypeError("'dictproxy' object does not support item deletion")

        if name in Alias._component_slots:
            del super(Alias, self).__dict__[name]

        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is not longer valid")
        return delattr(self.aliased_object, name)

    #
    # In addition to overloading __getattr__, the following magic
    # methods need to be redirected as Python does not access them
    # through __getitem__. If the aliased object does not implement
    # them an exception will be thrown
    #

    # E.g., IndexedComponent
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_WeakObjectInstanceMethodLifetime(self):
        """
        Checks that bound functions returned from a weak object do not prevent
        the referenced object from being deleted, as compared to weakref.proxy.
        """
        obj = TestClass()

        # Get a local reference to the bound method through a proxy; it will
        # increase obj's reference count.
        reffoo = weakref.proxy(obj).foo

        # Get a local reference to the bound method through a weak object; it
        # should not increase obj's reference count.
        objfoo = weakobj.objectref(obj).foo

        # Remove the original reference, and call the functions; this should
        # succeed because of reffoo's im_self.
        del obj
        reffoo()
        objfoo()

        # Remove the local reference-via-proxy, which should now allow the
        # original object to be deleted.
        del reffoo

        try:
            objfoo()
        except weakref.ReferenceError:
            pass
        else:
            self.fail('Weak object should be invalidated')
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def __getref__(self):
        ref = self.__ref__()
        if ref is None:
            raise weakref.ReferenceError('weakly-referenced object no longer exists')
        return ref
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def __getref__(self):
        ref = self.im_self()
        if ref is None:
            raise weakref.ReferenceError('weakly-referenced object no longer exists')
        return self.__functype__(self.im_func, ref, self.im_class)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_proxy_ref(self):
        o = C()
        o.bar = 1
        ref1 = weakref.proxy(o, self.callback)
        ref2 = weakref.proxy(o, self.callback)
        del o

        def check(proxy):
            proxy.bar

        self.assertRaises(weakref.ReferenceError, check, ref1)
        self.assertRaises(weakref.ReferenceError, check, ref2)
        self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
        self.assertEqual(self.cbcalled, 2)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_proxy_ref(self):
        o = C()
        o.bar = 1
        ref1 = weakref.proxy(o, self.callback)
        ref2 = weakref.proxy(o, self.callback)
        del o

        def check(proxy):
            proxy.bar

        self.assertRaises(weakref.ReferenceError, check, ref1)
        self.assertRaises(weakref.ReferenceError, check, ref2)
        self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
        self.assertEqual(self.cbcalled, 2)
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_proxy_ref(self):
        o = C()
        o.bar = 1
        ref1 = weakref.proxy(o, self.callback)
        ref2 = weakref.proxy(o, self.callback)
        del o

        def check(proxy):
            proxy.bar

        self.assertRaises(weakref.ReferenceError, check, ref1)
        self.assertRaises(weakref.ReferenceError, check, ref2)
        self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
        self.assertTrue(self.cbcalled == 2)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __setattr__(self, name, attr):
        # avoid infinite recursion
        if name in Alias.__slots__:
            # this is how you access a slot while bypassing __setattr__
            return Alias.__dict__[name].__set__(self, attr)

        if name in Alias._component_slots:
            super(Alias, self).__dict__[name] = attr
            return

        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is not longer valid")
        return setattr(self.aliased_object, name, attr)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __len__(self):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__len__()
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __contains__(self, key):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__contains__(key)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __getitem__(self, key):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__getitem__(key)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __setitem__(self, key, value):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__setitem__(key, value)

    # E.g., NumValue
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __float__(self):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__float__()
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __int__(self):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__int__()
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __lt__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__lt__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __le__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__le__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __ge__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__ge__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __eq__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__eq__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __add__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__add__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __sub__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__sub__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __div__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__div__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __truediv__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__truediv__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __pow__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__pow__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __radd__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__radd__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __rsub__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__rsub__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __rdiv__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__rdiv__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __rtruediv__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__rtruediv__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __rpow__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__rpow__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __iadd__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__iadd__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __isub__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__isub__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __idiv__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__idiv__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __itruediv__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__itruediv__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __ipow__(self, other):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__ipow__(other)
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __neg__(self):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__neg__()
项目:pyomo    作者:Pyomo    | 项目源码 | 文件源码
def __pos__(self):
        if self.aliased_object is None:
            raise weakref.ReferenceError("Proxy is no longer valid")
        return self.aliased_object.__pos__()