root/trunk/twisted/python/finalize.py

Revision 6637, 0.7 KB (checked in by glyph, 7 years ago)

Object database for Twisted. This Checkin Sponsored by Divmod, LLC

Line 
1
2"""
3A module for externalized finalizers.
4"""
5
6import weakref
7
8garbageKey = 0
9
10def callbackFactory(num, fins):
11    def _cb(w):
12        del refs[num]
13        for fx in fins:
14            fx()
15    return _cb
16
17refs = {}
18
19def register(inst):
20    global garbageKey
21    garbageKey += 1
22    r = weakref.ref(inst, callbackFactory(garbageKey, inst.__finalizers__()))
23    refs[garbageKey] = r
24
25if __name__ == '__main__':
26    def fin():
27        print 'I am _so_ dead.'
28
29    class Finalizeable:
30        """
31        An un-sucky __del__
32        """
33
34        def __finalizers__(self):
35            """
36            I'm going away.
37            """
38            return [fin]
39
40    f = Finalizeable()
41    f.f2 = f
42    register(f)
43    del f
44    import gc
45    gc.collect()
46    print 'deled'
Note: See TracBrowser for help on using the browser.