Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 1 | from test_support import verify, verbose, TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 2 | import gc |
| 3 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 4 | def run_test(name, thunk): |
| 5 | if verbose: |
| 6 | print "testing %s..." % name, |
| 7 | try: |
| 8 | thunk() |
| 9 | except TestFailed: |
| 10 | if verbose: |
| 11 | print "failed (expected %s but got %s)" % (result, |
| 12 | test_result) |
| 13 | raise TestFailed, name |
| 14 | else: |
| 15 | if verbose: |
| 16 | print "ok" |
| 17 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 18 | def test_list(): |
| 19 | l = [] |
| 20 | l.append(l) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 21 | gc.collect() |
| 22 | del l |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 23 | if gc.collect() != 1: |
| 24 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 25 | |
| 26 | def test_dict(): |
| 27 | d = {} |
| 28 | d[1] = d |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 29 | gc.collect() |
| 30 | del d |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 31 | if gc.collect() != 1: |
| 32 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 33 | |
| 34 | def test_tuple(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 35 | # since tuples are immutable we close the loop with a list |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 36 | l = [] |
| 37 | t = (l,) |
| 38 | l.append(t) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 39 | gc.collect() |
| 40 | del t |
| 41 | del l |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 42 | if gc.collect() != 2: |
| 43 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 44 | |
| 45 | def test_class(): |
| 46 | class A: |
| 47 | pass |
| 48 | A.a = A |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 49 | gc.collect() |
| 50 | del A |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 51 | if gc.collect() == 0: |
| 52 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 53 | |
| 54 | def test_instance(): |
| 55 | class A: |
| 56 | pass |
| 57 | a = A() |
| 58 | a.a = a |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 59 | gc.collect() |
| 60 | del a |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 61 | if gc.collect() == 0: |
| 62 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 63 | |
| 64 | def test_method(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 65 | # Tricky: self.__init__ is a bound method, it references the instance. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 66 | class A: |
| 67 | def __init__(self): |
| 68 | self.init = self.__init__ |
| 69 | a = A() |
| 70 | gc.collect() |
| 71 | del a |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 72 | if gc.collect() == 0: |
| 73 | raise TestFailed |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 74 | |
| 75 | def test_finalizer(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 76 | # A() is uncollectable if it is part of a cycle, make sure it shows up |
| 77 | # in gc.garbage. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 78 | class A: |
| 79 | def __del__(self): pass |
| 80 | class B: |
| 81 | pass |
| 82 | a = A() |
| 83 | a.a = a |
| 84 | id_a = id(a) |
| 85 | b = B() |
| 86 | b.b = b |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 87 | gc.collect() |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 88 | del a |
| 89 | del b |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 90 | if gc.collect() == 0: |
| 91 | raise TestFailed |
| 92 | for obj in gc.garbage: |
| 93 | if id(obj) == id_a: |
| 94 | del obj.a |
| 95 | break |
| 96 | else: |
| 97 | raise TestFailed |
| 98 | gc.garbage.remove(obj) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 99 | |
| 100 | def test_function(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 101 | # Tricky: f -> d -> f, code should call d.clear() after the exec to |
| 102 | # break the cycle. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 103 | d = {} |
| 104 | exec("def f(): pass\n") in d |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 105 | gc.collect() |
| 106 | del d |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 107 | if gc.collect() != 2: |
| 108 | raise TestFailed |
| 109 | |
| 110 | def test_saveall(): |
| 111 | # Verify that cyclic garbage like lists show up in gc.garbage if the |
| 112 | # SAVEALL option is enabled. |
| 113 | debug = gc.get_debug() |
| 114 | gc.set_debug(debug | gc.DEBUG_SAVEALL) |
| 115 | l = [] |
| 116 | l.append(l) |
| 117 | id_l = id(l) |
| 118 | del l |
| 119 | gc.collect() |
| 120 | try: |
| 121 | for obj in gc.garbage: |
| 122 | if id(obj) == id_l: |
| 123 | del obj[:] |
| 124 | break |
| 125 | else: |
| 126 | raise TestFailed |
| 127 | gc.garbage.remove(obj) |
| 128 | finally: |
| 129 | gc.set_debug(debug) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 130 | |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 131 | def test_del(): |
| 132 | # __del__ methods can trigger collection, make this to happen |
| 133 | thresholds = gc.get_threshold() |
| 134 | gc.enable() |
| 135 | gc.set_threshold(1) |
| 136 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 137 | class A: |
| 138 | def __del__(self): |
| 139 | dir(self) |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 140 | a = A() |
| 141 | del a |
| 142 | |
| 143 | gc.disable() |
| 144 | apply(gc.set_threshold, thresholds) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 145 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 146 | |
| 147 | def test_all(): |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 148 | run_test("lists", test_list) |
| 149 | run_test("dicts", test_dict) |
| 150 | run_test("tuples", test_tuple) |
| 151 | run_test("classes", test_class) |
| 152 | run_test("instances", test_instance) |
| 153 | run_test("methods", test_method) |
| 154 | run_test("functions", test_function) |
| 155 | run_test("finalizers", test_finalizer) |
| 156 | run_test("__del__", test_del) |
| 157 | run_test("saveall", test_saveall) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 158 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 159 | def test(): |
| 160 | if verbose: |
| 161 | print "disabling automatic collection" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 162 | enabled = gc.isenabled() |
| 163 | gc.disable() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 164 | verify(not gc.isenabled() ) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 165 | debug = gc.get_debug() |
| 166 | gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 167 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 168 | try: |
| 169 | test_all() |
| 170 | finally: |
| 171 | gc.set_debug(debug) |
| 172 | # test gc.enable() even if GC is disabled by default |
| 173 | if verbose: |
| 174 | print "restoring automatic collection" |
| 175 | # make sure to always test gc.enable() |
| 176 | gc.enable() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 177 | verify(gc.isenabled()) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 178 | if not enabled: |
| 179 | gc.disable() |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 180 | |
| 181 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 182 | test() |