-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweakref_finalize_toy_example.py
46 lines (34 loc) · 1.33 KB
/
weakref_finalize_toy_example.py
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
35
36
37
38
39
40
41
42
43
44
45
46
import weakref
class ShopKeeper:
class _MembersNeededForFinalize:
__slots__ = ("serno", "exception_harness")
def __init__(self, shop_keeper_obj, serno, exception_harness):
self.serno = serno
self.exception_harness = exception_harness
weakref.finalize(shop_keeper_obj, self.close)
def close(self):
if self.exception_harness:
try:
print(" Close", self.serno, 1 / (self.serno - 1))
except Exception:
print(" PROBLEM", self.serno)
else:
print(" Close", self.serno, 1 / (self.serno - 1))
__slots__ = ("__weakref__", "_mnff")
def __init__(self, serno, exception_harness):
self._mnff = ShopKeeper._MembersNeededForFinalize(
self, serno, exception_harness
)
def short_term_shop_keeper(serno, exception_harness):
print(" Start short term", serno, exception_harness)
ShopKeeper(serno, exception_harness)
print(" End short term")
def experiment():
print("Start experiment")
for exception_harness in [False, True]:
for serno in range(3):
short_term_shop_keeper(serno, exception_harness)
print("End experiment")
if __name__ == "__main__":
experiment()
print("All done.")