r"""A weak reference to a Storage. The cdata member is a Python number containing the integer representation of the Storage pointer.
| 25 | |
| 26 | |
| 27 | class StorageWeakRef: |
| 28 | r"""A weak reference to a Storage. |
| 29 | |
| 30 | The cdata member is a Python number containing the integer representation of |
| 31 | the Storage pointer. |
| 32 | """ |
| 33 | |
| 34 | __slots__ = ["cdata", "_free_weak_ref"] |
| 35 | |
| 36 | def __init__(self, storage): |
| 37 | self.cdata = storage._weak_ref() |
| 38 | # Save a direct reference to _free_weak_ref because the `torch` module |
| 39 | # might be cleared during Python shutdown before this module is cleared. |
| 40 | self._free_weak_ref = torch.Storage._free_weak_ref # type: ignore[attr-defined] |
| 41 | |
| 42 | @classmethod |
| 43 | def from_weakref(cls, cdata): |
| 44 | instance = cls.__new__(cls) |
| 45 | instance.cdata = cdata |
| 46 | instance._free_weak_ref = torch.Storage._free_weak_ref # type: ignore[attr-defined] |
| 47 | return instance |
| 48 | |
| 49 | def expired(self): |
| 50 | return torch.Storage._expired(self.cdata) # type: ignore[attr-defined] |
| 51 | |
| 52 | def __del__(self): |
| 53 | self._free_weak_ref(self.cdata) |
| 54 | |
| 55 | def __hash__(self): |
| 56 | return self.cdata |
| 57 | |
| 58 | def __eq__(self, other): |
| 59 | if id(self) == id(other): |
| 60 | return True |
| 61 | return self.cdata == other.cdata |
| 62 | |
| 63 | |
| 64 | class SharedCache(dict): |
no outgoing calls
no test coverage detected