~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/python/finalize.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
A module for externalized finalizers.
 
4
"""
 
5
 
 
6
import weakref
 
7
 
 
8
garbageKey = 0
 
9
 
 
10
def callbackFactory(num, fins):
 
11
    def _cb(w):
 
12
        del refs[num]
 
13
        for fx in fins:
 
14
            fx()
 
15
    return _cb
 
16
 
 
17
refs = {}
 
18
 
 
19
def register(inst):
 
20
    global garbageKey
 
21
    garbageKey += 1
 
22
    r = weakref.ref(inst, callbackFactory(garbageKey, inst.__finalizers__()))
 
23
    refs[garbageKey] = r
 
24
 
 
25
if __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'