summaryrefslogtreecommitdiff
path: root/reference_test.py
blob: 9998737aa752a6824d11a085df8f6bf2f59824d9 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python

import urwid
from weakref import ref

b = urwid.Button("test one")

def hold_b_ref(x):
    print "hello", b

urwid.connect_signal(b, 'click', hold_b_ref)

r = ref(b)

assert r()
del b
# circular reference is detected and removed:
assert not r()

class Foo(object):
    def __init__(self):
        self.btn = urwid.Button("test two")
        urwid.connect_signal(self.btn, 'click', self.say_hi)

    def say_hi(self, btn):
        print "hi"

f = Foo()
r = ref(f.btn)
assert r()
f.btn = None
# circular reference is detected and removed:
assert not r()



b = urwid.Button("test one")
def hold_b_ref(x):
    print "hello", b
urwid.connect_signal(b, 'click', hold_b_ref)
r = ref(b)
assert r()
del hold_b_ref
del b
# circular reference detected and removed:
assert not r()


f = Foo()
r = ref(f)
assert r()
import gc
gc.collect()
f = None
# circular reference only removed after gc.collect()
assert r()
assert gc.collect()
assert not r()