fin_wrap¶
Finalizer wrapper for automatic cleanup of objects.
The fin_wrap class wraps an object stored on a parent object and registers a weakref finalizer that calls a cleanup function when the wrapper is garbage collected. This ensures resources are released when the wrapped object is no longer referenced.
Classes:
| Name | Description |
|---|---|
fin_wrap |
Wraps an object on a parent with a weakref finalizer for cleanup. |
FinWrapper |
Wraps an object with attribute delegation via fin_wrap. |
Functions:
| Name | Description |
|---|---|
fin_wrap_np |
Wraps an object in a simple namespace with a finalizer. |
Example
import gc
from py_misc_utils import fin_wrap as fw
class Res:
def close(self):
print('closed')
owner = type('Owner', (), {})()
res = Res()
fw.fin_wrap(owner, 'res', res, finfn=res.close)
del owner.res # Runs the finalizer without waiting for GC.
gc.collect()
fin_wrap
¶
Wrapper registering a weakref finalizer on a parent attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
The object the wrapped attribute is stored on. |
required | |
name
|
The attribute name the wrapped object is stored under. |
required | |
obj
|
The object to wrap. |
required | |
finfn
|
A callable to invoke when the wrapper is garbage collected. |
None
|
|
cleanup
|
If True, the wrapped object is cleaned up when the wrapper attribute is removed before garbage collection. |
False
|
Source code in py_misc_utils/fin_wrap.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
FinWrapper
¶
Object wrapper delegating attribute access to the wrapped object.
The wrapped object is stored under a reserved attribute name and the dunders below forward every other attribute access to it, so that the wrapper transparently behaves like the wrapped object while carrying a finalizer for cleanup. Note that attribute delegation - including dunder lookups such as len - only works for objects exposing a dict (instances of plain classes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The object to wrap and delegate to. |
required | |
finfn
|
A callable to invoke when the wrapper is garbage collected. |
required |
Example
import gc
from py_misc_utils import fin_wrap as fw
class Thing:
pass
done = []
w = fw.FinWrapper(Thing(), lambda: done.append('closed'))
w.size = 3
assert w.size == 3
del w
gc.collect()
Source code in py_misc_utils/fin_wrap.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
fin_wrap_np(obj, finfn, name='v')
¶
Wrap an object into a fresh namespace object with a finalizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The object to wrap. |
required | |
finfn
|
A callable to invoke when the wrapper is garbage collected. |
required | |
name
|
Attribute name under which the object is stored. |
'v'
|
Returns:
| Type | Description |
|---|---|
|
The wrapper object holding the object and the finalizer. |
Source code in py_misc_utils/fin_wrap.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |