Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 1 | from test_support import verify, verbose, TestFailed |
Neil Schemenauer | 88c761a | 2001-07-12 13:25:53 +0000 | [diff] [blame] | 2 | import sys |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 3 | import gc |
| 4 | |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 5 | def expect(actual, expected, name): |
| 6 | if actual != expected: |
| 7 | raise TestFailed, "test_%s: actual %d, expected %d" % ( |
| 8 | name, actual, expected) |
| 9 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 10 | def expect_nonzero(actual, name): |
| 11 | if actual == 0: |
| 12 | raise TestFailed, "test_%s: unexpected zero" % name |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 13 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 14 | def run_test(name, thunk): |
| 15 | if verbose: |
| 16 | print "testing %s..." % name, |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 17 | thunk() |
| 18 | if verbose: |
| 19 | print "ok" |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 20 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 21 | def test_list(): |
| 22 | l = [] |
| 23 | l.append(l) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 24 | gc.collect() |
| 25 | del l |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 26 | expect(gc.collect(), 1, "list") |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 27 | |
| 28 | def test_dict(): |
| 29 | d = {} |
| 30 | d[1] = d |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 31 | gc.collect() |
| 32 | del d |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 33 | expect(gc.collect(), 1, "dict") |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 34 | |
| 35 | def test_tuple(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 36 | # since tuples are immutable we close the loop with a list |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 37 | l = [] |
| 38 | t = (l,) |
| 39 | l.append(t) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 40 | gc.collect() |
| 41 | del t |
| 42 | del l |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 43 | expect(gc.collect(), 2, "tuple") |
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 |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 51 | expect_nonzero(gc.collect(), "class") |
| 52 | |
Tim Peters | 1ce150c | 2001-10-15 22:49:27 +0000 | [diff] [blame^] | 53 | def test_newstyleclass(): |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 54 | class A(object): |
Tim Peters | 1ce150c | 2001-10-15 22:49:27 +0000 | [diff] [blame^] | 55 | pass |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 56 | gc.collect() |
| 57 | del A |
| 58 | expect_nonzero(gc.collect(), "staticclass") |
| 59 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 60 | def test_instance(): |
| 61 | class A: |
| 62 | pass |
| 63 | a = A() |
| 64 | a.a = a |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 65 | gc.collect() |
| 66 | del a |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 67 | expect_nonzero(gc.collect(), "instance") |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 69 | def test_newinstance(): |
| 70 | class A(object): |
| 71 | pass |
| 72 | a = A() |
| 73 | a.a = a |
| 74 | gc.collect() |
| 75 | del a |
| 76 | expect_nonzero(gc.collect(), "newinstance") |
| 77 | class B(list): |
| 78 | pass |
| 79 | class C(B, A): |
| 80 | pass |
| 81 | a = C() |
| 82 | a.a = a |
| 83 | gc.collect() |
| 84 | del a |
| 85 | expect_nonzero(gc.collect(), "newinstance(2)") |
| 86 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 87 | def test_method(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 88 | # Tricky: self.__init__ is a bound method, it references the instance. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 89 | class A: |
| 90 | def __init__(self): |
| 91 | self.init = self.__init__ |
| 92 | a = A() |
| 93 | gc.collect() |
| 94 | del a |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 95 | expect_nonzero(gc.collect(), "method") |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 96 | |
| 97 | def test_finalizer(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 98 | # A() is uncollectable if it is part of a cycle, make sure it shows up |
| 99 | # in gc.garbage. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 100 | class A: |
| 101 | def __del__(self): pass |
| 102 | class B: |
| 103 | pass |
| 104 | a = A() |
| 105 | a.a = a |
| 106 | id_a = id(a) |
| 107 | b = B() |
| 108 | b.b = b |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 109 | gc.collect() |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 110 | del a |
| 111 | del b |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 112 | expect_nonzero(gc.collect(), "finalizer") |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 113 | for obj in gc.garbage: |
| 114 | if id(obj) == id_a: |
| 115 | del obj.a |
| 116 | break |
| 117 | else: |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 118 | raise TestFailed, "didn't find obj in garbage (finalizer)" |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 119 | gc.garbage.remove(obj) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 120 | |
| 121 | def test_function(): |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 122 | # Tricky: f -> d -> f, code should call d.clear() after the exec to |
| 123 | # break the cycle. |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 124 | d = {} |
| 125 | exec("def f(): pass\n") in d |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 126 | gc.collect() |
| 127 | del d |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 128 | expect(gc.collect(), 2, "function") |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 129 | |
Neil Schemenauer | 88c761a | 2001-07-12 13:25:53 +0000 | [diff] [blame] | 130 | def test_frame(): |
| 131 | def f(): |
| 132 | frame = sys._getframe() |
| 133 | gc.collect() |
| 134 | f() |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 135 | expect(gc.collect(), 1, "frame") |
Neil Schemenauer | 88c761a | 2001-07-12 13:25:53 +0000 | [diff] [blame] | 136 | |
| 137 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 138 | def test_saveall(): |
| 139 | # Verify that cyclic garbage like lists show up in gc.garbage if the |
| 140 | # SAVEALL option is enabled. |
| 141 | debug = gc.get_debug() |
| 142 | gc.set_debug(debug | gc.DEBUG_SAVEALL) |
| 143 | l = [] |
| 144 | l.append(l) |
| 145 | id_l = id(l) |
| 146 | del l |
| 147 | gc.collect() |
| 148 | try: |
| 149 | for obj in gc.garbage: |
| 150 | if id(obj) == id_l: |
| 151 | del obj[:] |
| 152 | break |
| 153 | else: |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 154 | raise TestFailed, "didn't find obj in garbage (saveall)" |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 155 | gc.garbage.remove(obj) |
| 156 | finally: |
| 157 | gc.set_debug(debug) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 158 | |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 159 | def test_del(): |
| 160 | # __del__ methods can trigger collection, make this to happen |
| 161 | thresholds = gc.get_threshold() |
| 162 | gc.enable() |
| 163 | gc.set_threshold(1) |
| 164 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 165 | class A: |
| 166 | def __del__(self): |
| 167 | dir(self) |
Neil Schemenauer | a53cf79 | 2000-09-15 22:32:29 +0000 | [diff] [blame] | 168 | a = A() |
| 169 | del a |
| 170 | |
| 171 | gc.disable() |
| 172 | apply(gc.set_threshold, thresholds) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 173 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 174 | |
| 175 | def test_all(): |
Guido van Rossum | c907bd8 | 2001-10-02 19:49:47 +0000 | [diff] [blame] | 176 | gc.collect() # Delete 2nd generation garbage |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 177 | run_test("lists", test_list) |
| 178 | run_test("dicts", test_dict) |
| 179 | run_test("tuples", test_tuple) |
| 180 | run_test("classes", test_class) |
Tim Peters | 1ce150c | 2001-10-15 22:49:27 +0000 | [diff] [blame^] | 181 | run_test("new style classes", test_newstyleclass) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 182 | run_test("instances", test_instance) |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 183 | run_test("new instances", test_newinstance) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 184 | run_test("methods", test_method) |
| 185 | run_test("functions", test_function) |
Neil Schemenauer | 88c761a | 2001-07-12 13:25:53 +0000 | [diff] [blame] | 186 | run_test("frames", test_frame) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 187 | run_test("finalizers", test_finalizer) |
| 188 | run_test("__del__", test_del) |
| 189 | run_test("saveall", test_saveall) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 190 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 191 | def test(): |
| 192 | if verbose: |
| 193 | print "disabling automatic collection" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 194 | enabled = gc.isenabled() |
| 195 | gc.disable() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 196 | verify(not gc.isenabled() ) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 197 | debug = gc.get_debug() |
| 198 | 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] | 199 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 200 | try: |
| 201 | test_all() |
| 202 | finally: |
| 203 | gc.set_debug(debug) |
| 204 | # test gc.enable() even if GC is disabled by default |
| 205 | if verbose: |
| 206 | print "restoring automatic collection" |
| 207 | # make sure to always test gc.enable() |
| 208 | gc.enable() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 209 | verify(gc.isenabled()) |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 210 | if not enabled: |
| 211 | gc.disable() |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 212 | |
| 213 | |
Neil Schemenauer | faae266 | 2000-09-22 15:26:20 +0000 | [diff] [blame] | 214 | test() |