我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用_weakref.ref()。
def __init__(self, data=None): self.data = set() def _remove(item, selfref=ref(self)): self = selfref() if self is not None: if self._iterating: self._pending_removals.append(item) else: self.data.discard(item) self._remove = _remove # A list of keys to be removed self._pending_removals = [] self._iterating = set() if data is not None: self.update(data)
def test_new_handle(): import _weakref BVoidP = new_pointer_type(new_void_type()) BCharP = new_pointer_type(new_primitive_type("char")) class mylist(list): pass o = mylist([2, 3, 4]) x = newp_handle(BVoidP, o) assert repr(x) == "<cdata 'void *' handle to [2, 3, 4]>" assert x assert from_handle(x) is o assert from_handle(cast(BCharP, x)) is o wr = _weakref.ref(o) del o import gc; gc.collect() assert wr() is not None assert from_handle(x) == list((2, 3, 4)) assert from_handle(cast(BCharP, x)) == list((2, 3, 4)) del x for i in range(3): if wr() is not None: import gc; gc.collect() assert wr() is None py.test.raises(RuntimeError, from_handle, cast(BCharP, 0))
def __init__(self, weakcontainer): # Don't create cycles self.weakcontainer = ref(weakcontainer)
def __contains__(self, item): try: wr = ref(item) except TypeError: return False return wr in self.data
def add(self, item): if self._pending_removals: self._commit_removals() self.data.add(ref(item, self._remove))
def remove(self, item): if self._pending_removals: self._commit_removals() self.data.remove(ref(item))
def __isub__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.difference_update(ref(item) for item in other) return self
def __iand__(self, other): if self._pending_removals: self._commit_removals() self.data.intersection_update(ref(item) for item in other) return self
def issubset(self, other): return self.data.issubset(ref(item) for item in other)
def __lt__(self, other): return self.data < set(ref(item) for item in other)
def issuperset(self, other): return self.data.issuperset(ref(item) for item in other)
def __eq__(self, other): if not isinstance(other, self.__class__): return NotImplemented return self.data == set(ref(item) for item in other)
def __ixor__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.symmetric_difference_update(ref(item, self._remove) for item in other) return self
def difference_update(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.difference_update(ref(item) for item in other)
def intersection_update(self, other): if self._pending_removals: self._commit_removals() self.data.intersection_update(ref(item) for item in other)
def __ge__(self, other): return self.data >= set(ref(item) for item in other)
def symmetric_difference_update(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.symmetric_difference_update(ref(item) for item in other)
def __ixor__(self, other): if self._pending_removals: self._commit_removals() if self is other: self.data.clear() else: self.data.symmetric_difference_update(ref(item) for item in other) return self
def __contains__(self, item): return ref(item) in self.data