| 1 |
""" |
| 2 |
https://github.com/pjdelport/backports.weakref/blob/master/src/backports/weakref.py |
| 3 |
Partial backport of Python 3.6's weakref module: |
| 4 |
finalize (new in Python 3.4) |
| 5 |
Backport modifications are marked with "XXX backport". |
| 6 |
""" |
| 7 |
from __future__ import absolute_import |
| 8 |
|
| 9 |
import itertools |
| 10 |
import sys |
| 11 |
from weakref import ref |
| 12 |
|
| 13 |
__all__ = ['finalize'] |
| 14 |
|
| 15 |
|
| 16 |
class finalize(object): |
| 17 |
"""Class for finalization of weakrefable objects |
| 18 |
finalize(obj, func, *args, **kwargs) returns a callable finalizer |
| 19 |
object which will be called when obj is garbage collected. The |
| 20 |
first time the finalizer is called it evaluates func(*arg, **kwargs) |
| 21 |
and returns the result. After this the finalizer is dead, and |
| 22 |
calling it just returns None. |
| 23 |
When the program exits any remaining finalizers for which the |
| 24 |
atexit attribute is true will be run in reverse order of creation. |
| 25 |
By default atexit is true. |
| 26 |
""" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
__slots__ = () |
| 33 |
_registry = {} |
| 34 |
_shutdown = False |
| 35 |
_index_iter = itertools.count() |
| 36 |
_dirty = False |
| 37 |
_registered_with_atexit = False |
| 38 |
|
| 39 |
class _Info(object): |
| 40 |
__slots__ = ("weakref", "func", "args", "kwargs", "atexit", "index") |
| 41 |
|
| 42 |
def __init__(self, obj, func, *args, **kwargs): |
| 43 |
if not self._registered_with_atexit: |
| 44 |
|
| 45 |
|
| 46 |
import atexit |
| 47 |
atexit.register(self._exitfunc) |
| 48 |
finalize._registered_with_atexit = True |
| 49 |
info = self._Info() |
| 50 |
info.weakref = ref(obj, self) |
| 51 |
info.func = func |
| 52 |
info.args = args |
| 53 |
info.kwargs = kwargs or None |
| 54 |
info.atexit = True |
| 55 |
info.index = next(self._index_iter) |
| 56 |
self._registry[self] = info |
| 57 |
finalize._dirty = True |
| 58 |
|
| 59 |
def __call__(self, _=None): |
| 60 |
"""If alive then mark as dead and return func(*args, **kwargs); |
| 61 |
otherwise return None""" |
| 62 |
info = self._registry.pop(self, None) |
| 63 |
if info and not self._shutdown: |
| 64 |
return info.func(*info.args, **(info.kwargs or {})) |
| 65 |
|
| 66 |
def detach(self): |
| 67 |
"""If alive then mark as dead and return (obj, func, args, kwargs); |
| 68 |
otherwise return None""" |
| 69 |
info = self._registry.get(self) |
| 70 |
obj = info and info.weakref() |
| 71 |
if obj is not None and self._registry.pop(self, None): |
| 72 |
return (obj, info.func, info.args, info.kwargs or {}) |
| 73 |
|
| 74 |
def peek(self): |
| 75 |
"""If alive then return (obj, func, args, kwargs); |
| 76 |
otherwise return None""" |
| 77 |
info = self._registry.get(self) |
| 78 |
obj = info and info.weakref() |
| 79 |
if obj is not None: |
| 80 |
return (obj, info.func, info.args, info.kwargs or {}) |
| 81 |
|
| 82 |
@property |
| 83 |
def alive(self): |
| 84 |
"""Whether finalizer is alive""" |
| 85 |
return self in self._registry |
| 86 |
|
| 87 |
@property |
| 88 |
def atexit(self): |
| 89 |
"""Whether finalizer should be called at exit""" |
| 90 |
info = self._registry.get(self) |
| 91 |
return bool(info) and info.atexit |
| 92 |
|
| 93 |
@atexit.setter |
| 94 |
def atexit(self, value): |
| 95 |
info = self._registry.get(self) |
| 96 |
if info: |
| 97 |
info.atexit = bool(value) |
| 98 |
|
| 99 |
def __repr__(self): |
| 100 |
info = self._registry.get(self) |
| 101 |
obj = info and info.weakref() |
| 102 |
if obj is None: |
| 103 |
return '<%s object at %#x; dead>' % (type(self).__name__, id(self)) |
| 104 |
else: |
| 105 |
return '<%s object at %#x; for %r at %#x>' % \ |
| 106 |
(type(self).__name__, id(self), type(obj).__name__, id(obj)) |
| 107 |
|
| 108 |
@classmethod |
| 109 |
def _select_for_exit(cls): |
| 110 |
|
| 111 |
L = [(f,i) for (f,i) in cls._registry.items() if i.atexit] |
| 112 |
L.sort(key=lambda item:item[1].index) |
| 113 |
return [f for (f,i) in L] |
| 114 |
|
| 115 |
@classmethod |
| 116 |
def _exitfunc(cls): |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
reenable_gc = False |
| 121 |
try: |
| 122 |
if cls._registry: |
| 123 |
import gc |
| 124 |
if gc.isenabled(): |
| 125 |
reenable_gc = True |
| 126 |
gc.disable() |
| 127 |
pending = None |
| 128 |
while True: |
| 129 |
if pending is None or finalize._dirty: |
| 130 |
pending = cls._select_for_exit() |
| 131 |
finalize._dirty = False |
| 132 |
if not pending: |
| 133 |
break |
| 134 |
f = pending.pop() |
| 135 |
try: |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
f() |
| 141 |
except Exception: |
| 142 |
sys.excepthook(*sys.exc_info()) |
| 143 |
assert f not in cls._registry |
| 144 |
finally: |
| 145 |
|
| 146 |
finalize._shutdown = True |
| 147 |
if reenable_gc: |
| 148 |
gc.enable() |