| 1 |
import unittest |
| 2 |
from test.support import (verbose, run_unittest, start_threads, |
| 3 |
requires_type_collecting) |
| 4 |
import sys |
| 5 |
import time |
| 6 |
import gc |
| 7 |
import weakref |
| 8 |
|
| 9 |
try: |
| 10 |
import threading |
| 11 |
except ImportError: |
| 12 |
threading = None |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class C1055820(object): |
| 23 |
def __init__(self, i): |
| 24 |
self.i = i |
| 25 |
self.loop = self |
| 26 |
|
| 27 |
class GC_Detector(object): |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def __init__(self): |
| 32 |
self.gc_happened = False |
| 33 |
|
| 34 |
def it_happened(ignored): |
| 35 |
self.gc_happened = True |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
self.wr = weakref.ref(C1055820(666), it_happened) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class GCTests(unittest.TestCase): |
| 46 |
def test_list(self): |
| 47 |
l = [] |
| 48 |
l.append(l) |
| 49 |
gc.collect() |
| 50 |
del l |
| 51 |
self.assertEqual(gc.collect(), 1) |
| 52 |
|
| 53 |
def test_dict(self): |
| 54 |
d = {} |
| 55 |
d[1] = d |
| 56 |
gc.collect() |
| 57 |
del d |
| 58 |
self.assertEqual(gc.collect(), 1) |
| 59 |
|
| 60 |
def test_tuple(self): |
| 61 |
|
| 62 |
l = [] |
| 63 |
t = (l,) |
| 64 |
l.append(t) |
| 65 |
gc.collect() |
| 66 |
del t |
| 67 |
del l |
| 68 |
self.assertEqual(gc.collect(), 2) |
| 69 |
|
| 70 |
def test_class(self): |
| 71 |
class A: |
| 72 |
pass |
| 73 |
A.a = A |
| 74 |
gc.collect() |
| 75 |
del A |
| 76 |
self.assertNotEqual(gc.collect(), 0) |
| 77 |
|
| 78 |
def test_newstyleclass(self): |
| 79 |
class A(object): |
| 80 |
pass |
| 81 |
gc.collect() |
| 82 |
del A |
| 83 |
self.assertNotEqual(gc.collect(), 0) |
| 84 |
|
| 85 |
def test_instance(self): |
| 86 |
class A: |
| 87 |
pass |
| 88 |
a = A() |
| 89 |
a.a = a |
| 90 |
gc.collect() |
| 91 |
del a |
| 92 |
self.assertNotEqual(gc.collect(), 0) |
| 93 |
|
| 94 |
@requires_type_collecting |
| 95 |
def test_newinstance(self): |
| 96 |
class A(object): |
| 97 |
pass |
| 98 |
a = A() |
| 99 |
a.a = a |
| 100 |
gc.collect() |
| 101 |
del a |
| 102 |
self.assertNotEqual(gc.collect(), 0) |
| 103 |
class B(list): |
| 104 |
pass |
| 105 |
class C(B, A): |
| 106 |
pass |
| 107 |
a = C() |
| 108 |
a.a = a |
| 109 |
gc.collect() |
| 110 |
del a |
| 111 |
self.assertNotEqual(gc.collect(), 0) |
| 112 |
del B, C |
| 113 |
self.assertNotEqual(gc.collect(), 0) |
| 114 |
A.a = A() |
| 115 |
del A |
| 116 |
self.assertNotEqual(gc.collect(), 0) |
| 117 |
self.assertEqual(gc.collect(), 0) |
| 118 |
|
| 119 |
def test_method(self): |
| 120 |
|
| 121 |
class A: |
| 122 |
def __init__(self): |
| 123 |
self.init = self.__init__ |
| 124 |
a = A() |
| 125 |
gc.collect() |
| 126 |
del a |
| 127 |
self.assertNotEqual(gc.collect(), 0) |
| 128 |
|
| 129 |
def test_finalizer(self): |
| 130 |
|
| 131 |
|
| 132 |
class A: |
| 133 |
def __del__(self): pass |
| 134 |
class B: |
| 135 |
pass |
| 136 |
a = A() |
| 137 |
a.a = a |
| 138 |
id_a = id(a) |
| 139 |
b = B() |
| 140 |
b.b = b |
| 141 |
gc.collect() |
| 142 |
del a |
| 143 |
del b |
| 144 |
self.assertNotEqual(gc.collect(), 0) |
| 145 |
for obj in gc.garbage: |
| 146 |
if id(obj) == id_a: |
| 147 |
del obj.a |
| 148 |
break |
| 149 |
else: |
| 150 |
self.fail("didn't find obj in garbage (finalizer)") |
| 151 |
gc.garbage.remove(obj) |
| 152 |
|
| 153 |
def test_finalizer_newclass(self): |
| 154 |
|
| 155 |
|
| 156 |
class A(object): |
| 157 |
def __del__(self): pass |
| 158 |
class B(object): |
| 159 |
pass |
| 160 |
a = A() |
| 161 |
a.a = a |
| 162 |
id_a = id(a) |
| 163 |
b = B() |
| 164 |
b.b = b |
| 165 |
gc.collect() |
| 166 |
del a |
| 167 |
del b |
| 168 |
self.assertNotEqual(gc.collect(), 0) |
| 169 |
for obj in gc.garbage: |
| 170 |
if id(obj) == id_a: |
| 171 |
del obj.a |
| 172 |
break |
| 173 |
else: |
| 174 |
self.fail("didn't find obj in garbage (finalizer)") |
| 175 |
gc.garbage.remove(obj) |
| 176 |
|
| 177 |
def test_function(self): |
| 178 |
|
| 179 |
|
| 180 |
d = {} |
| 181 |
exec("def f(): pass\n") in d |
| 182 |
gc.collect() |
| 183 |
del d |
| 184 |
self.assertEqual(gc.collect(), 2) |
| 185 |
|
| 186 |
def test_frame(self): |
| 187 |
def f(): |
| 188 |
frame = sys._getframe() |
| 189 |
gc.collect() |
| 190 |
f() |
| 191 |
self.assertEqual(gc.collect(), 1) |
| 192 |
|
| 193 |
def test_saveall(self): |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
gc.collect() |
| 200 |
|
| 201 |
self.assertEqual(gc.garbage, []) |
| 202 |
|
| 203 |
L = [] |
| 204 |
L.append(L) |
| 205 |
id_L = id(L) |
| 206 |
|
| 207 |
debug = gc.get_debug() |
| 208 |
gc.set_debug(debug | gc.DEBUG_SAVEALL) |
| 209 |
del L |
| 210 |
gc.collect() |
| 211 |
gc.set_debug(debug) |
| 212 |
|
| 213 |
self.assertEqual(len(gc.garbage), 1) |
| 214 |
obj = gc.garbage.pop() |
| 215 |
self.assertEqual(id(obj), id_L) |
| 216 |
|
| 217 |
def test_del(self): |
| 218 |
|
| 219 |
thresholds = gc.get_threshold() |
| 220 |
gc.enable() |
| 221 |
gc.set_threshold(1) |
| 222 |
|
| 223 |
class A: |
| 224 |
def __del__(self): |
| 225 |
dir(self) |
| 226 |
a = A() |
| 227 |
del a |
| 228 |
|
| 229 |
gc.disable() |
| 230 |
gc.set_threshold(*thresholds) |
| 231 |
|
| 232 |
def test_del_newclass(self): |
| 233 |
|
| 234 |
thresholds = gc.get_threshold() |
| 235 |
gc.enable() |
| 236 |
gc.set_threshold(1) |
| 237 |
|
| 238 |
class A(object): |
| 239 |
def __del__(self): |
| 240 |
dir(self) |
| 241 |
a = A() |
| 242 |
del a |
| 243 |
|
| 244 |
gc.disable() |
| 245 |
gc.set_threshold(*thresholds) |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
def test_get_count(self): |
| 254 |
|
| 255 |
assertEqual = self._baseAssertEqual |
| 256 |
gc.collect() |
| 257 |
assertEqual(gc.get_count(), (0, 0, 0)) |
| 258 |
a = dict() |
| 259 |
|
| 260 |
|
| 261 |
assertEqual(gc.get_count(), (2, 0, 0)) |
| 262 |
|
| 263 |
def test_collect_generations(self): |
| 264 |
|
| 265 |
assertEqual = self.assertEqual |
| 266 |
gc.collect() |
| 267 |
a = dict() |
| 268 |
gc.collect(0) |
| 269 |
assertEqual(gc.get_count(), (0, 1, 0)) |
| 270 |
gc.collect(1) |
| 271 |
assertEqual(gc.get_count(), (0, 0, 1)) |
| 272 |
gc.collect(2) |
| 273 |
assertEqual(gc.get_count(), (0, 0, 0)) |
| 274 |
|
| 275 |
def test_trashcan(self): |
| 276 |
class Ouch: |
| 277 |
n = 0 |
| 278 |
def __del__(self): |
| 279 |
Ouch.n = Ouch.n + 1 |
| 280 |
if Ouch.n % 17 == 0: |
| 281 |
gc.collect() |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
gc.enable() |
| 297 |
N = 150 |
| 298 |
for count in range(2): |
| 299 |
t = [] |
| 300 |
for i in range(N): |
| 301 |
t = [t, Ouch()] |
| 302 |
u = [] |
| 303 |
for i in range(N): |
| 304 |
u = [u, Ouch()] |
| 305 |
v = {} |
| 306 |
for i in range(N): |
| 307 |
v = {1: v, 2: Ouch()} |
| 308 |
gc.disable() |
| 309 |
|
| 310 |
@unittest.skipUnless(threading, "test meaningless on builds without threads") |
| 311 |
def test_trashcan_threads(self): |
| 312 |
|
| 313 |
NESTING = 60 |
| 314 |
N_THREADS = 2 |
| 315 |
|
| 316 |
def sleeper_gen(): |
| 317 |
"""A generator that releases the GIL when closed or dealloc'ed.""" |
| 318 |
try: |
| 319 |
yield |
| 320 |
finally: |
| 321 |
time.sleep(0.000001) |
| 322 |
|
| 323 |
class C(list): |
| 324 |
|
| 325 |
inits = [] |
| 326 |
dels = [] |
| 327 |
def __init__(self, alist): |
| 328 |
self[:] = alist |
| 329 |
C.inits.append(None) |
| 330 |
def __del__(self): |
| 331 |
|
| 332 |
C.dels.append(None) |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
g = sleeper_gen() |
| 337 |
next(g) |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
def make_nested(): |
| 342 |
"""Create a sufficiently nested container object so that the |
| 343 |
trashcan mechanism is invoked when deallocating it.""" |
| 344 |
x = C([]) |
| 345 |
for i in range(NESTING): |
| 346 |
x = [C([x])] |
| 347 |
del x |
| 348 |
|
| 349 |
def run_thread(): |
| 350 |
"""Exercise make_nested() in a loop.""" |
| 351 |
while not exit: |
| 352 |
make_nested() |
| 353 |
|
| 354 |
old_checkinterval = sys.getcheckinterval() |
| 355 |
sys.setcheckinterval(3) |
| 356 |
try: |
| 357 |
exit = [] |
| 358 |
threads = [] |
| 359 |
for i in range(N_THREADS): |
| 360 |
t = threading.Thread(target=run_thread) |
| 361 |
threads.append(t) |
| 362 |
with start_threads(threads, lambda: exit.append(1)): |
| 363 |
time.sleep(1.0) |
| 364 |
finally: |
| 365 |
sys.setcheckinterval(old_checkinterval) |
| 366 |
gc.collect() |
| 367 |
self.assertEqual(len(C.inits), len(C.dels)) |
| 368 |
|
| 369 |
def test_boom(self): |
| 370 |
class Boom: |
| 371 |
def __getattr__(self, someattribute): |
| 372 |
del self.attr |
| 373 |
raise AttributeError |
| 374 |
|
| 375 |
a = Boom() |
| 376 |
b = Boom() |
| 377 |
a.attr = b |
| 378 |
b.attr = a |
| 379 |
|
| 380 |
gc.collect() |
| 381 |
garbagelen = len(gc.garbage) |
| 382 |
del a, b |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
self.assertEqual(gc.collect(), 4) |
| 391 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 392 |
|
| 393 |
def test_boom2(self): |
| 394 |
class Boom2: |
| 395 |
def __init__(self): |
| 396 |
self.x = 0 |
| 397 |
|
| 398 |
def __getattr__(self, someattribute): |
| 399 |
self.x += 1 |
| 400 |
if self.x > 1: |
| 401 |
del self.attr |
| 402 |
raise AttributeError |
| 403 |
|
| 404 |
a = Boom2() |
| 405 |
b = Boom2() |
| 406 |
a.attr = b |
| 407 |
b.attr = a |
| 408 |
|
| 409 |
gc.collect() |
| 410 |
garbagelen = len(gc.garbage) |
| 411 |
del a, b |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
self.assertEqual(gc.collect(), 4) |
| 418 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 419 |
|
| 420 |
def test_boom_new(self): |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
class Boom_New(object): |
| 425 |
def __getattr__(self, someattribute): |
| 426 |
del self.attr |
| 427 |
raise AttributeError |
| 428 |
|
| 429 |
a = Boom_New() |
| 430 |
b = Boom_New() |
| 431 |
a.attr = b |
| 432 |
b.attr = a |
| 433 |
|
| 434 |
gc.collect() |
| 435 |
garbagelen = len(gc.garbage) |
| 436 |
del a, b |
| 437 |
self.assertEqual(gc.collect(), 4) |
| 438 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 439 |
|
| 440 |
def test_boom2_new(self): |
| 441 |
class Boom2_New(object): |
| 442 |
def __init__(self): |
| 443 |
self.x = 0 |
| 444 |
|
| 445 |
def __getattr__(self, someattribute): |
| 446 |
self.x += 1 |
| 447 |
if self.x > 1: |
| 448 |
del self.attr |
| 449 |
raise AttributeError |
| 450 |
|
| 451 |
a = Boom2_New() |
| 452 |
b = Boom2_New() |
| 453 |
a.attr = b |
| 454 |
b.attr = a |
| 455 |
|
| 456 |
gc.collect() |
| 457 |
garbagelen = len(gc.garbage) |
| 458 |
del a, b |
| 459 |
self.assertEqual(gc.collect(), 4) |
| 460 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 461 |
|
| 462 |
def test_get_referents(self): |
| 463 |
alist = [1, 3, 5] |
| 464 |
got = gc.get_referents(alist) |
| 465 |
got.sort() |
| 466 |
self.assertEqual(got, alist) |
| 467 |
|
| 468 |
atuple = tuple(alist) |
| 469 |
got = gc.get_referents(atuple) |
| 470 |
got.sort() |
| 471 |
self.assertEqual(got, alist) |
| 472 |
|
| 473 |
adict = {1: 3, 5: 7} |
| 474 |
expected = [1, 3, 5, 7] |
| 475 |
got = gc.get_referents(adict) |
| 476 |
got.sort() |
| 477 |
self.assertEqual(got, expected) |
| 478 |
|
| 479 |
got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0)) |
| 480 |
got.sort() |
| 481 |
self.assertEqual(got, [0, 0] + range(5)) |
| 482 |
|
| 483 |
self.assertEqual(gc.get_referents(1, 'a', 4j), []) |
| 484 |
|
| 485 |
def test_is_tracked(self): |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
self.assertFalse(gc.is_tracked(None)) |
| 491 |
self.assertFalse(gc.is_tracked(1)) |
| 492 |
self.assertFalse(gc.is_tracked(1.0)) |
| 493 |
self.assertFalse(gc.is_tracked(1.0 + 5.0j)) |
| 494 |
self.assertFalse(gc.is_tracked(True)) |
| 495 |
self.assertFalse(gc.is_tracked(False)) |
| 496 |
self.assertFalse(gc.is_tracked("a")) |
| 497 |
self.assertFalse(gc.is_tracked(u"a")) |
| 498 |
self.assertFalse(gc.is_tracked(bytearray("a"))) |
| 499 |
self.assertFalse(gc.is_tracked(type)) |
| 500 |
self.assertFalse(gc.is_tracked(int)) |
| 501 |
self.assertFalse(gc.is_tracked(object)) |
| 502 |
self.assertFalse(gc.is_tracked(object())) |
| 503 |
|
| 504 |
class OldStyle: |
| 505 |
pass |
| 506 |
class NewStyle(object): |
| 507 |
pass |
| 508 |
self.assertTrue(gc.is_tracked(gc)) |
| 509 |
self.assertTrue(gc.is_tracked(OldStyle)) |
| 510 |
self.assertTrue(gc.is_tracked(OldStyle())) |
| 511 |
self.assertTrue(gc.is_tracked(NewStyle)) |
| 512 |
self.assertTrue(gc.is_tracked(NewStyle())) |
| 513 |
self.assertTrue(gc.is_tracked([])) |
| 514 |
self.assertTrue(gc.is_tracked(set())) |
| 515 |
|
| 516 |
def test_bug1055820b(self): |
| 517 |
|
| 518 |
|
| 519 |
ouch = [] |
| 520 |
def callback(ignored): |
| 521 |
ouch[:] = [wr() for wr in WRs] |
| 522 |
|
| 523 |
Cs = [C1055820(i) for i in range(2)] |
| 524 |
WRs = [weakref.ref(c, callback) for c in Cs] |
| 525 |
c = None |
| 526 |
|
| 527 |
gc.collect() |
| 528 |
self.assertEqual(len(ouch), 0) |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
Cs = None |
| 533 |
gc.collect() |
| 534 |
self.assertEqual(len(ouch), 2) |
| 535 |
for x in ouch: |
| 536 |
|
| 537 |
|
| 538 |
self.assertEqual(x, None) |
| 539 |
|
| 540 |
class GCTogglingTests(unittest.TestCase): |
| 541 |
def setUp(self): |
| 542 |
gc.enable() |
| 543 |
|
| 544 |
def tearDown(self): |
| 545 |
gc.disable() |
| 546 |
|
| 547 |
def test_bug1055820c(self): |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
c0 = C1055820(0) |
| 552 |
|
| 553 |
gc.collect() |
| 554 |
|
| 555 |
c1 = C1055820(1) |
| 556 |
c1.keep_c0_alive = c0 |
| 557 |
del c0.loop |
| 558 |
|
| 559 |
c2 = C1055820(2) |
| 560 |
c2wr = weakref.ref(c2) |
| 561 |
|
| 562 |
ouch = [] |
| 563 |
def callback(ignored): |
| 564 |
ouch[:] = [c2wr()] |
| 565 |
|
| 566 |
|
| 567 |
c0wr = weakref.ref(c0, callback) |
| 568 |
|
| 569 |
c0 = c1 = c2 = None |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
junk = [] |
| 602 |
i = 0 |
| 603 |
detector = GC_Detector() |
| 604 |
while not detector.gc_happened: |
| 605 |
i += 1 |
| 606 |
if i > 10000: |
| 607 |
self.fail("gc didn't happen after 10000 iterations") |
| 608 |
self.assertEqual(len(ouch), 0) |
| 609 |
junk.append([]) |
| 610 |
|
| 611 |
self.assertEqual(len(ouch), 1) |
| 612 |
for x in ouch: |
| 613 |
|
| 614 |
|
| 615 |
self.assertEqual(x, None) |
| 616 |
|
| 617 |
def test_bug1055820d(self): |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
ouch = [] |
| 623 |
class D(C1055820): |
| 624 |
def __del__(self): |
| 625 |
ouch[:] = [c2wr()] |
| 626 |
|
| 627 |
d0 = D(0) |
| 628 |
|
| 629 |
gc.collect() |
| 630 |
|
| 631 |
c1 = C1055820(1) |
| 632 |
c1.keep_d0_alive = d0 |
| 633 |
del d0.loop |
| 634 |
|
| 635 |
c2 = C1055820(2) |
| 636 |
c2wr = weakref.ref(c2) |
| 637 |
|
| 638 |
d0 = c1 = c2 = None |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
detector = GC_Detector() |
| 669 |
junk = [] |
| 670 |
i = 0 |
| 671 |
while not detector.gc_happened: |
| 672 |
i += 1 |
| 673 |
if i > 10000: |
| 674 |
self.fail("gc didn't happen after 10000 iterations") |
| 675 |
self.assertEqual(len(ouch), 0) |
| 676 |
junk.append([]) |
| 677 |
|
| 678 |
self.assertEqual(len(ouch), 1) |
| 679 |
for x in ouch: |
| 680 |
|
| 681 |
|
| 682 |
self.assertEqual(x, None) |
| 683 |
|
| 684 |
def test_main(): |
| 685 |
enabled = gc.isenabled() |
| 686 |
gc.disable() |
| 687 |
assert not gc.isenabled() |
| 688 |
debug = gc.get_debug() |
| 689 |
gc.set_debug(debug & ~gc.DEBUG_LEAK) |
| 690 |
|
| 691 |
try: |
| 692 |
gc.collect() |
| 693 |
run_unittest(GCTests, GCTogglingTests) |
| 694 |
finally: |
| 695 |
gc.set_debug(debug) |
| 696 |
|
| 697 |
if verbose: |
| 698 |
print "restoring automatic collection" |
| 699 |
|
| 700 |
gc.enable() |
| 701 |
assert gc.isenabled() |
| 702 |
if not enabled: |
| 703 |
gc.disable() |
| 704 |
|
| 705 |
if __name__ == "__main__": |
| 706 |
test_main() |