Ticket #7101: pbg_refs.py

File pbg_refs.py, 879 bytes (added by Eric Pettersen, 2 years ago)
Line 
1# replace PseuodbondGroup __init__ to allow tracking via weak refs
2
3_weak_refs = []
4def replacement__init__(self, *args, **kw):
5 self._orig__init__(*args, **kw)
6 from weakref import ref
7 _weak_refs.append(ref(self))
8
9from chimerax.atomic import PseudobondGroup
10PseudobondGroup._orig__init__ = PseudobondGroup.__init__
11PseudobondGroup.__init__ = replacement__init__
12
13from chimerax.core.commands import run
14run(session, "open 7azs")
15run(session, "close")
16import gc
17gc.collect()
18import sys
19alive = [x for x in _weak_refs if x() is not None]
20print("before wait", len(alive), "live refs:", [(x(), "%d refs" % sys.getrefcount(x())) for x in alive])
21if len(alive) > 0:
22 print("Referrers to first entry:")
23 print(gc.get_referrers(alive[0]()))
24run(session, "wait 1")
25alive = [x for x in _weak_refs if x() is not None]
26print("after wait", len(alive), "live refs:", [x() for x in alive])
27
28