blob: 1fb7e0bfddb8ef3fe61e54e6eefa6db358d2a991 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import os
from functools import partial
from weakref import ref
from .obj import BaseDelayedObject
def finalize_instance(obj, weakref_inst):
try:
obj.__finalizer__()
finally:
obj.__disable_finalization__()
class WeakRefProxy(BaseDelayedObject):
def __instantiate_proxy_instance__(self):
obj = BaseDelayedObject.__instantiate_proxy_instance__(self)
weakref = ref(self, partial(finalize_instance, obj))
obj.__enable_finalization__(weakref)
return obj
def __enable_finalization__(self, weakref):
# note we directly access the class, to ensure the instance hasn't overshadowed.
self.__class__.__finalizer_weakrefs__[os.getpid()][id(self)] = weakref
def __disable_finalization__(self):
# note we directly access the class, to ensure the instance hasn't overshadowed.
# use pop to allow for repeat invocations of __disable_finalization__
d = self.__class__.__finalizer_weakrefs__.get(os.getpid)
if d is not None:
d.pop(id(self), None)
|