| 1 |
import unittest |
| 2 |
import unittest.mock |
| 3 |
from test.support import (verbose, refcount_test, |
| 4 |
cpython_only, requires_subprocess) |
| 5 |
from test.support.import_helper import import_module |
| 6 |
from test.support.os_helper import temp_dir, TESTFN, unlink |
| 7 |
from test.support.script_helper import assert_python_ok, make_script |
| 8 |
from test.support import threading_helper |
| 9 |
|
| 10 |
import gc |
| 11 |
import sys |
| 12 |
import sysconfig |
| 13 |
import textwrap |
| 14 |
import threading |
| 15 |
import time |
| 16 |
import weakref |
| 17 |
|
| 18 |
try: |
| 19 |
from _testcapi import with_tp_del |
| 20 |
except ImportError: |
| 21 |
def with_tp_del(cls): |
| 22 |
class C(object): |
| 23 |
def __new__(cls, *args, **kwargs): |
| 24 |
raise TypeError('requires _testcapi.with_tp_del') |
| 25 |
return C |
| 26 |
|
| 27 |
try: |
| 28 |
from _testcapi import ContainerNoGC |
| 29 |
except ImportError: |
| 30 |
ContainerNoGC = None |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class C1055820(object): |
| 41 |
def __init__(self, i): |
| 42 |
self.i = i |
| 43 |
self.loop = self |
| 44 |
|
| 45 |
class GC_Detector(object): |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
def __init__(self): |
| 50 |
self.gc_happened = False |
| 51 |
|
| 52 |
def it_happened(ignored): |
| 53 |
self.gc_happened = True |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
self.wr = weakref.ref(C1055820(666), it_happened) |
| 58 |
|
| 59 |
@with_tp_del |
| 60 |
class Uncollectable(object): |
| 61 |
"""Create a reference cycle with multiple __del__ methods. |
| 62 |
|
| 63 |
An object in a reference cycle will never have zero references, |
| 64 |
and so must be garbage collected. If one or more objects in the |
| 65 |
cycle have __del__ methods, the gc refuses to guess an order, |
| 66 |
and leaves the cycle uncollected.""" |
| 67 |
def __init__(self, partner=None): |
| 68 |
if partner is None: |
| 69 |
self.partner = Uncollectable(partner=self) |
| 70 |
else: |
| 71 |
self.partner = partner |
| 72 |
def __tp_del__(self): |
| 73 |
pass |
| 74 |
|
| 75 |
if sysconfig.get_config_vars().get('PY_CFLAGS', ''): |
| 76 |
BUILD_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS']) |
| 77 |
else: |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
BUILD_WITH_NDEBUG = (not hasattr(sys, 'gettotalrefcount')) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@cpython_only |
| 87 |
class GCTests(unittest.TestCase): |
| 88 |
def test_list(self): |
| 89 |
l = [] |
| 90 |
l.append(l) |
| 91 |
gc.collect() |
| 92 |
del l |
| 93 |
self.assertEqual(gc.collect(), 1) |
| 94 |
|
| 95 |
def test_dict(self): |
| 96 |
d = {} |
| 97 |
d[1] = d |
| 98 |
gc.collect() |
| 99 |
del d |
| 100 |
self.assertEqual(gc.collect(), 1) |
| 101 |
|
| 102 |
def test_tuple(self): |
| 103 |
|
| 104 |
l = [] |
| 105 |
t = (l,) |
| 106 |
l.append(t) |
| 107 |
gc.collect() |
| 108 |
del t |
| 109 |
del l |
| 110 |
self.assertEqual(gc.collect(), 2) |
| 111 |
|
| 112 |
def test_class(self): |
| 113 |
class A: |
| 114 |
pass |
| 115 |
A.a = A |
| 116 |
gc.collect() |
| 117 |
del A |
| 118 |
self.assertNotEqual(gc.collect(), 0) |
| 119 |
|
| 120 |
def test_newstyleclass(self): |
| 121 |
class A(object): |
| 122 |
pass |
| 123 |
gc.collect() |
| 124 |
del A |
| 125 |
self.assertNotEqual(gc.collect(), 0) |
| 126 |
|
| 127 |
def test_instance(self): |
| 128 |
class A: |
| 129 |
pass |
| 130 |
a = A() |
| 131 |
a.a = a |
| 132 |
gc.collect() |
| 133 |
del a |
| 134 |
self.assertNotEqual(gc.collect(), 0) |
| 135 |
|
| 136 |
def test_newinstance(self): |
| 137 |
class A(object): |
| 138 |
pass |
| 139 |
a = A() |
| 140 |
a.a = a |
| 141 |
gc.collect() |
| 142 |
del a |
| 143 |
self.assertNotEqual(gc.collect(), 0) |
| 144 |
class B(list): |
| 145 |
pass |
| 146 |
class C(B, A): |
| 147 |
pass |
| 148 |
a = C() |
| 149 |
a.a = a |
| 150 |
gc.collect() |
| 151 |
del a |
| 152 |
self.assertNotEqual(gc.collect(), 0) |
| 153 |
del B, C |
| 154 |
self.assertNotEqual(gc.collect(), 0) |
| 155 |
A.a = A() |
| 156 |
del A |
| 157 |
self.assertNotEqual(gc.collect(), 0) |
| 158 |
self.assertEqual(gc.collect(), 0) |
| 159 |
|
| 160 |
def test_method(self): |
| 161 |
|
| 162 |
class A: |
| 163 |
def __init__(self): |
| 164 |
self.init = self.__init__ |
| 165 |
a = A() |
| 166 |
gc.collect() |
| 167 |
del a |
| 168 |
self.assertNotEqual(gc.collect(), 0) |
| 169 |
|
| 170 |
@cpython_only |
| 171 |
def test_legacy_finalizer(self): |
| 172 |
|
| 173 |
|
| 174 |
@with_tp_del |
| 175 |
class A: |
| 176 |
def __tp_del__(self): pass |
| 177 |
class B: |
| 178 |
pass |
| 179 |
a = A() |
| 180 |
a.a = a |
| 181 |
id_a = id(a) |
| 182 |
b = B() |
| 183 |
b.b = b |
| 184 |
gc.collect() |
| 185 |
del a |
| 186 |
del b |
| 187 |
self.assertNotEqual(gc.collect(), 0) |
| 188 |
for obj in gc.garbage: |
| 189 |
if id(obj) == id_a: |
| 190 |
del obj.a |
| 191 |
break |
| 192 |
else: |
| 193 |
self.fail("didn't find obj in garbage (finalizer)") |
| 194 |
gc.garbage.remove(obj) |
| 195 |
|
| 196 |
@cpython_only |
| 197 |
def test_legacy_finalizer_newclass(self): |
| 198 |
|
| 199 |
|
| 200 |
@with_tp_del |
| 201 |
class A(object): |
| 202 |
def __tp_del__(self): pass |
| 203 |
class B(object): |
| 204 |
pass |
| 205 |
a = A() |
| 206 |
a.a = a |
| 207 |
id_a = id(a) |
| 208 |
b = B() |
| 209 |
b.b = b |
| 210 |
gc.collect() |
| 211 |
del a |
| 212 |
del b |
| 213 |
self.assertNotEqual(gc.collect(), 0) |
| 214 |
for obj in gc.garbage: |
| 215 |
if id(obj) == id_a: |
| 216 |
del obj.a |
| 217 |
break |
| 218 |
else: |
| 219 |
self.fail("didn't find obj in garbage (finalizer)") |
| 220 |
gc.garbage.remove(obj) |
| 221 |
|
| 222 |
def test_function(self): |
| 223 |
|
| 224 |
|
| 225 |
d = {} |
| 226 |
exec("def f(): pass\n", d) |
| 227 |
gc.collect() |
| 228 |
del d |
| 229 |
self.assertEqual(gc.collect(), 2) |
| 230 |
|
| 231 |
def test_function_tp_clear_leaves_consistent_state(self): |
| 232 |
|
| 233 |
code = """if 1: |
| 234 |
|
| 235 |
import gc |
| 236 |
import weakref |
| 237 |
|
| 238 |
class LateFin: |
| 239 |
__slots__ = ('ref',) |
| 240 |
|
| 241 |
def __del__(self): |
| 242 |
|
| 243 |
# 8. Now `latefin`'s finalizer is called. Here we |
| 244 |
# obtain a reference to `func`, which is currently |
| 245 |
# undergoing `tp_clear`. |
| 246 |
global func |
| 247 |
func = self.ref() |
| 248 |
|
| 249 |
class Cyclic(tuple): |
| 250 |
__slots__ = () |
| 251 |
|
| 252 |
# 4. The finalizers of all garbage objects are called. In |
| 253 |
# this case this is only us as `func` doesn't have a |
| 254 |
# finalizer. |
| 255 |
def __del__(self): |
| 256 |
|
| 257 |
# 5. Create a weakref to `func` now. If we had created |
| 258 |
# it earlier, it would have been cleared by the |
| 259 |
# garbage collector before calling the finalizers. |
| 260 |
self[1].ref = weakref.ref(self[0]) |
| 261 |
|
| 262 |
# 6. Drop the global reference to `latefin`. The only |
| 263 |
# remaining reference is the one we have. |
| 264 |
global latefin |
| 265 |
del latefin |
| 266 |
|
| 267 |
# 7. Now `func` is `tp_clear`-ed. This drops the last |
| 268 |
# reference to `Cyclic`, which gets `tp_dealloc`-ed. |
| 269 |
# This drops the last reference to `latefin`. |
| 270 |
|
| 271 |
latefin = LateFin() |
| 272 |
def func(): |
| 273 |
pass |
| 274 |
cyc = tuple.__new__(Cyclic, (func, latefin)) |
| 275 |
|
| 276 |
# 1. Create a reference cycle of `cyc` and `func`. |
| 277 |
func.__module__ = cyc |
| 278 |
|
| 279 |
# 2. Make the cycle unreachable, but keep the global reference |
| 280 |
# to `latefin` so that it isn't detected as garbage. This |
| 281 |
# way its finalizer will not be called immediately. |
| 282 |
del func, cyc |
| 283 |
|
| 284 |
# 3. Invoke garbage collection, |
| 285 |
# which will find `cyc` and `func` as garbage. |
| 286 |
gc.collect() |
| 287 |
|
| 288 |
# 9. Previously, this would crash because `func_qualname` |
| 289 |
# had been NULL-ed out by func_clear(). |
| 290 |
print(f"{func=}") |
| 291 |
""" |
| 292 |
|
| 293 |
rc, stdout, stderr = assert_python_ok("-c", code) |
| 294 |
self.assertEqual(rc, 0) |
| 295 |
self.assertRegex(stdout, rb"""\A\s*func=<function at \S+>\s*\Z""") |
| 296 |
self.assertFalse(stderr) |
| 297 |
|
| 298 |
@refcount_test |
| 299 |
def test_frame(self): |
| 300 |
def f(): |
| 301 |
frame = sys._getframe() |
| 302 |
gc.collect() |
| 303 |
f() |
| 304 |
self.assertEqual(gc.collect(), 1) |
| 305 |
|
| 306 |
def test_saveall(self): |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
gc.collect() |
| 313 |
|
| 314 |
self.assertEqual(gc.garbage, []) |
| 315 |
|
| 316 |
L = [] |
| 317 |
L.append(L) |
| 318 |
id_L = id(L) |
| 319 |
|
| 320 |
debug = gc.get_debug() |
| 321 |
gc.set_debug(debug | gc.DEBUG_SAVEALL) |
| 322 |
del L |
| 323 |
gc.collect() |
| 324 |
gc.set_debug(debug) |
| 325 |
|
| 326 |
self.assertEqual(len(gc.garbage), 1) |
| 327 |
obj = gc.garbage.pop() |
| 328 |
self.assertEqual(id(obj), id_L) |
| 329 |
|
| 330 |
def test_del(self): |
| 331 |
|
| 332 |
thresholds = gc.get_threshold() |
| 333 |
gc.enable() |
| 334 |
gc.set_threshold(1) |
| 335 |
|
| 336 |
class A: |
| 337 |
def __del__(self): |
| 338 |
dir(self) |
| 339 |
a = A() |
| 340 |
del a |
| 341 |
|
| 342 |
gc.disable() |
| 343 |
gc.set_threshold(*thresholds) |
| 344 |
|
| 345 |
def test_del_newclass(self): |
| 346 |
|
| 347 |
thresholds = gc.get_threshold() |
| 348 |
gc.enable() |
| 349 |
gc.set_threshold(1) |
| 350 |
|
| 351 |
class A(object): |
| 352 |
def __del__(self): |
| 353 |
dir(self) |
| 354 |
a = A() |
| 355 |
del a |
| 356 |
|
| 357 |
gc.disable() |
| 358 |
gc.set_threshold(*thresholds) |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
@refcount_test |
| 367 |
def test_get_count(self): |
| 368 |
gc.collect() |
| 369 |
a, b, c = gc.get_count() |
| 370 |
x = [] |
| 371 |
d, e, f = gc.get_count() |
| 372 |
self.assertEqual((b, c), (0, 0)) |
| 373 |
self.assertEqual((e, f), (0, 0)) |
| 374 |
|
| 375 |
self.assertLess(a, 5) |
| 376 |
|
| 377 |
|
| 378 |
self.assertGreater(d, a) |
| 379 |
|
| 380 |
@refcount_test |
| 381 |
def test_collect_generations(self): |
| 382 |
gc.collect() |
| 383 |
|
| 384 |
|
| 385 |
x = [] |
| 386 |
gc.collect(0) |
| 387 |
|
| 388 |
a, b, c = gc.get_count() |
| 389 |
gc.collect(1) |
| 390 |
|
| 391 |
d, e, f = gc.get_count() |
| 392 |
gc.collect(2) |
| 393 |
|
| 394 |
g, h, i = gc.get_count() |
| 395 |
|
| 396 |
|
| 397 |
self.assertEqual((b, c), (1, 0)) |
| 398 |
self.assertEqual((e, f), (0, 1)) |
| 399 |
self.assertEqual((h, i), (0, 0)) |
| 400 |
|
| 401 |
def test_trashcan(self): |
| 402 |
class Ouch: |
| 403 |
n = 0 |
| 404 |
def __del__(self): |
| 405 |
Ouch.n = Ouch.n + 1 |
| 406 |
if Ouch.n % 17 == 0: |
| 407 |
gc.collect() |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
gc.enable() |
| 423 |
N = 150 |
| 424 |
for count in range(2): |
| 425 |
t = [] |
| 426 |
for i in range(N): |
| 427 |
t = [t, Ouch()] |
| 428 |
u = [] |
| 429 |
for i in range(N): |
| 430 |
u = [u, Ouch()] |
| 431 |
v = {} |
| 432 |
for i in range(N): |
| 433 |
v = {1: v, 2: Ouch()} |
| 434 |
gc.disable() |
| 435 |
|
| 436 |
@threading_helper.requires_working_threading() |
| 437 |
def test_trashcan_threads(self): |
| 438 |
|
| 439 |
NESTING = 60 |
| 440 |
N_THREADS = 2 |
| 441 |
|
| 442 |
def sleeper_gen(): |
| 443 |
"""A generator that releases the GIL when closed or dealloc'ed.""" |
| 444 |
try: |
| 445 |
yield |
| 446 |
finally: |
| 447 |
time.sleep(0.000001) |
| 448 |
|
| 449 |
class C(list): |
| 450 |
|
| 451 |
inits = [] |
| 452 |
dels = [] |
| 453 |
def __init__(self, alist): |
| 454 |
self[:] = alist |
| 455 |
C.inits.append(None) |
| 456 |
def __del__(self): |
| 457 |
|
| 458 |
C.dels.append(None) |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
g = sleeper_gen() |
| 463 |
next(g) |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
def make_nested(): |
| 468 |
"""Create a sufficiently nested container object so that the |
| 469 |
trashcan mechanism is invoked when deallocating it.""" |
| 470 |
x = C([]) |
| 471 |
for i in range(NESTING): |
| 472 |
x = [C([x])] |
| 473 |
del x |
| 474 |
|
| 475 |
def run_thread(): |
| 476 |
"""Exercise make_nested() in a loop.""" |
| 477 |
while not exit: |
| 478 |
make_nested() |
| 479 |
|
| 480 |
old_switchinterval = sys.getswitchinterval() |
| 481 |
sys.setswitchinterval(1e-5) |
| 482 |
try: |
| 483 |
exit = [] |
| 484 |
threads = [] |
| 485 |
for i in range(N_THREADS): |
| 486 |
t = threading.Thread(target=run_thread) |
| 487 |
threads.append(t) |
| 488 |
with threading_helper.start_threads(threads, lambda: exit.append(1)): |
| 489 |
time.sleep(1.0) |
| 490 |
finally: |
| 491 |
sys.setswitchinterval(old_switchinterval) |
| 492 |
gc.collect() |
| 493 |
self.assertEqual(len(C.inits), len(C.dels)) |
| 494 |
|
| 495 |
def test_boom(self): |
| 496 |
class Boom: |
| 497 |
def __getattr__(self, someattribute): |
| 498 |
del self.attr |
| 499 |
raise AttributeError |
| 500 |
|
| 501 |
a = Boom() |
| 502 |
b = Boom() |
| 503 |
a.attr = b |
| 504 |
b.attr = a |
| 505 |
|
| 506 |
gc.collect() |
| 507 |
garbagelen = len(gc.garbage) |
| 508 |
del a, b |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
self.assertEqual(gc.collect(), 2) |
| 517 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 518 |
|
| 519 |
def test_boom2(self): |
| 520 |
class Boom2: |
| 521 |
def __init__(self): |
| 522 |
self.x = 0 |
| 523 |
|
| 524 |
def __getattr__(self, someattribute): |
| 525 |
self.x += 1 |
| 526 |
if self.x > 1: |
| 527 |
del self.attr |
| 528 |
raise AttributeError |
| 529 |
|
| 530 |
a = Boom2() |
| 531 |
b = Boom2() |
| 532 |
a.attr = b |
| 533 |
b.attr = a |
| 534 |
|
| 535 |
gc.collect() |
| 536 |
garbagelen = len(gc.garbage) |
| 537 |
del a, b |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
self.assertEqual(gc.collect(), 2) |
| 544 |
self.assertEqual(len(gc.garbage), garbagelen) |
| 545 |
|
| 546 |
def test_get_referents(self): |
| 547 |
alist = [1, 3, 5] |
| 548 |
got = gc.get_referents(alist) |
| 549 |
got.sort() |
| 550 |
self.assertEqual(got, alist) |
| 551 |
|
| 552 |
atuple = tuple(alist) |
| 553 |
got = gc.get_referents(atuple) |
| 554 |
got.sort() |
| 555 |
self.assertEqual(got, alist) |
| 556 |
|
| 557 |
adict = {1: 3, 5: 7} |
| 558 |
expected = [1, 3, 5, 7] |
| 559 |
got = gc.get_referents(adict) |
| 560 |
got.sort() |
| 561 |
self.assertEqual(got, expected) |
| 562 |
|
| 563 |
got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0)) |
| 564 |
got.sort() |
| 565 |
self.assertEqual(got, [0, 0] + list(range(5))) |
| 566 |
|
| 567 |
self.assertEqual(gc.get_referents(1, 'a', 4j), []) |
| 568 |
|
| 569 |
def test_is_tracked(self): |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
self.assertFalse(gc.is_tracked(None)) |
| 575 |
self.assertFalse(gc.is_tracked(1)) |
| 576 |
self.assertFalse(gc.is_tracked(1.0)) |
| 577 |
self.assertFalse(gc.is_tracked(1.0 + 5.0j)) |
| 578 |
self.assertFalse(gc.is_tracked(True)) |
| 579 |
self.assertFalse(gc.is_tracked(False)) |
| 580 |
self.assertFalse(gc.is_tracked(b"a")) |
| 581 |
self.assertFalse(gc.is_tracked("a")) |
| 582 |
self.assertFalse(gc.is_tracked(bytearray(b"a"))) |
| 583 |
self.assertFalse(gc.is_tracked(type)) |
| 584 |
self.assertFalse(gc.is_tracked(int)) |
| 585 |
self.assertFalse(gc.is_tracked(object)) |
| 586 |
self.assertFalse(gc.is_tracked(object())) |
| 587 |
|
| 588 |
class UserClass: |
| 589 |
pass |
| 590 |
|
| 591 |
class UserInt(int): |
| 592 |
pass |
| 593 |
|
| 594 |
|
| 595 |
class UserClassSlots: |
| 596 |
__slots__ = () |
| 597 |
|
| 598 |
|
| 599 |
class UserFloatSlots(float): |
| 600 |
__slots__ = () |
| 601 |
|
| 602 |
|
| 603 |
class UserIntSlots(int): |
| 604 |
__slots__ = () |
| 605 |
|
| 606 |
self.assertTrue(gc.is_tracked(gc)) |
| 607 |
self.assertTrue(gc.is_tracked(UserClass)) |
| 608 |
self.assertTrue(gc.is_tracked(UserClass())) |
| 609 |
self.assertTrue(gc.is_tracked(UserInt())) |
| 610 |
self.assertTrue(gc.is_tracked([])) |
| 611 |
self.assertTrue(gc.is_tracked(set())) |
| 612 |
self.assertTrue(gc.is_tracked(UserClassSlots())) |
| 613 |
self.assertTrue(gc.is_tracked(UserFloatSlots())) |
| 614 |
self.assertTrue(gc.is_tracked(UserIntSlots())) |
| 615 |
|
| 616 |
def test_is_finalized(self): |
| 617 |
|
| 618 |
self.assertFalse(gc.is_finalized(3)) |
| 619 |
|
| 620 |
storage = [] |
| 621 |
class Lazarus: |
| 622 |
def __del__(self): |
| 623 |
storage.append(self) |
| 624 |
|
| 625 |
lazarus = Lazarus() |
| 626 |
self.assertFalse(gc.is_finalized(lazarus)) |
| 627 |
|
| 628 |
del lazarus |
| 629 |
gc.collect() |
| 630 |
|
| 631 |
lazarus = storage.pop() |
| 632 |
self.assertTrue(gc.is_finalized(lazarus)) |
| 633 |
|
| 634 |
def test_bug1055820b(self): |
| 635 |
|
| 636 |
|
| 637 |
ouch = [] |
| 638 |
def callback(ignored): |
| 639 |
ouch[:] = [wr() for wr in WRs] |
| 640 |
|
| 641 |
Cs = [C1055820(i) for i in range(2)] |
| 642 |
WRs = [weakref.ref(c, callback) for c in Cs] |
| 643 |
c = None |
| 644 |
|
| 645 |
gc.collect() |
| 646 |
self.assertEqual(len(ouch), 0) |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
Cs = None |
| 651 |
gc.collect() |
| 652 |
self.assertEqual(len(ouch), 2) |
| 653 |
for x in ouch: |
| 654 |
|
| 655 |
|
| 656 |
self.assertEqual(x, None) |
| 657 |
|
| 658 |
def test_bug21435(self): |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
gc.collect() |
| 669 |
|
| 670 |
class A: |
| 671 |
pass |
| 672 |
|
| 673 |
class B: |
| 674 |
def __init__(self, x): |
| 675 |
self.x = x |
| 676 |
|
| 677 |
def __del__(self): |
| 678 |
self.attr = None |
| 679 |
|
| 680 |
def do_work(): |
| 681 |
a = A() |
| 682 |
b = B(A()) |
| 683 |
|
| 684 |
a.attr = b |
| 685 |
b.attr = a |
| 686 |
|
| 687 |
do_work() |
| 688 |
gc.collect() |
| 689 |
|
| 690 |
@cpython_only |
| 691 |
@requires_subprocess() |
| 692 |
def test_garbage_at_shutdown(self): |
| 693 |
import subprocess |
| 694 |
code = """if 1: |
| 695 |
import gc |
| 696 |
import _testcapi |
| 697 |
@_testcapi.with_tp_del |
| 698 |
class X: |
| 699 |
def __init__(self, name): |
| 700 |
self.name = name |
| 701 |
def __repr__(self): |
| 702 |
return "<X %%r>" %% self.name |
| 703 |
def __tp_del__(self): |
| 704 |
pass |
| 705 |
|
| 706 |
x = X('first') |
| 707 |
x.x = x |
| 708 |
x.y = X('second') |
| 709 |
del x |
| 710 |
gc.set_debug(%s) |
| 711 |
""" |
| 712 |
def run_command(code): |
| 713 |
p = subprocess.Popen([sys.executable, "-Wd", "-c", code], |
| 714 |
stdout=subprocess.PIPE, |
| 715 |
stderr=subprocess.PIPE) |
| 716 |
stdout, stderr = p.communicate() |
| 717 |
p.stdout.close() |
| 718 |
p.stderr.close() |
| 719 |
self.assertEqual(p.returncode, 0) |
| 720 |
self.assertEqual(stdout, b"") |
| 721 |
return stderr |
| 722 |
|
| 723 |
stderr = run_command(code % "0") |
| 724 |
self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " |
| 725 |
b"shutdown; use", stderr) |
| 726 |
self.assertNotIn(b"<X 'first'>", stderr) |
| 727 |
|
| 728 |
stderr = run_command(code % "gc.DEBUG_UNCOLLECTABLE") |
| 729 |
self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " |
| 730 |
b"shutdown", stderr) |
| 731 |
self.assertTrue( |
| 732 |
(b"[<X 'first'>, <X 'second'>]" in stderr) or |
| 733 |
(b"[<X 'second'>, <X 'first'>]" in stderr), stderr) |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
stderr = run_command(code % "gc.DEBUG_SAVEALL") |
| 738 |
self.assertNotIn(b"uncollectable objects at shutdown", stderr) |
| 739 |
|
| 740 |
def test_gc_main_module_at_shutdown(self): |
| 741 |
|
| 742 |
|
| 743 |
code = """if 1: |
| 744 |
class C: |
| 745 |
def __del__(self): |
| 746 |
print('__del__ called') |
| 747 |
l = [C()] |
| 748 |
l.append(l) |
| 749 |
""" |
| 750 |
rc, out, err = assert_python_ok('-c', code) |
| 751 |
self.assertEqual(out.strip(), b'__del__ called') |
| 752 |
|
| 753 |
def test_gc_ordinary_module_at_shutdown(self): |
| 754 |
|
| 755 |
with temp_dir() as script_dir: |
| 756 |
module = """if 1: |
| 757 |
class C: |
| 758 |
def __del__(self): |
| 759 |
print('__del__ called') |
| 760 |
l = [C()] |
| 761 |
l.append(l) |
| 762 |
""" |
| 763 |
code = """if 1: |
| 764 |
import sys |
| 765 |
sys.path.insert(0, %r) |
| 766 |
import gctest |
| 767 |
""" % (script_dir,) |
| 768 |
make_script(script_dir, 'gctest', module) |
| 769 |
rc, out, err = assert_python_ok('-c', code) |
| 770 |
self.assertEqual(out.strip(), b'__del__ called') |
| 771 |
|
| 772 |
def test_global_del_SystemExit(self): |
| 773 |
code = """if 1: |
| 774 |
class ClassWithDel: |
| 775 |
def __del__(self): |
| 776 |
print('__del__ called') |
| 777 |
a = ClassWithDel() |
| 778 |
a.link = a |
| 779 |
raise SystemExit(0)""" |
| 780 |
self.addCleanup(unlink, TESTFN) |
| 781 |
with open(TESTFN, 'w', encoding="utf-8") as script: |
| 782 |
script.write(code) |
| 783 |
rc, out, err = assert_python_ok(TESTFN) |
| 784 |
self.assertEqual(out.strip(), b'__del__ called') |
| 785 |
|
| 786 |
def test_get_stats(self): |
| 787 |
stats = gc.get_stats() |
| 788 |
self.assertEqual(len(stats), 3) |
| 789 |
for st in stats: |
| 790 |
self.assertIsInstance(st, dict) |
| 791 |
self.assertEqual(set(st), |
| 792 |
{"collected", "collections", "uncollectable"}) |
| 793 |
self.assertGreaterEqual(st["collected"], 0) |
| 794 |
self.assertGreaterEqual(st["collections"], 0) |
| 795 |
self.assertGreaterEqual(st["uncollectable"], 0) |
| 796 |
|
| 797 |
if gc.isenabled(): |
| 798 |
self.addCleanup(gc.enable) |
| 799 |
gc.disable() |
| 800 |
old = gc.get_stats() |
| 801 |
gc.collect(0) |
| 802 |
new = gc.get_stats() |
| 803 |
self.assertEqual(new[0]["collections"], old[0]["collections"] + 1) |
| 804 |
self.assertEqual(new[1]["collections"], old[1]["collections"]) |
| 805 |
self.assertEqual(new[2]["collections"], old[2]["collections"]) |
| 806 |
gc.collect(2) |
| 807 |
new = gc.get_stats() |
| 808 |
self.assertEqual(new[0]["collections"], old[0]["collections"] + 1) |
| 809 |
self.assertEqual(new[1]["collections"], old[1]["collections"]) |
| 810 |
self.assertEqual(new[2]["collections"], old[2]["collections"] + 1) |
| 811 |
|
| 812 |
def test_freeze(self): |
| 813 |
gc.freeze() |
| 814 |
self.assertGreater(gc.get_freeze_count(), 0) |
| 815 |
gc.unfreeze() |
| 816 |
self.assertEqual(gc.get_freeze_count(), 0) |
| 817 |
|
| 818 |
def test_get_objects(self): |
| 819 |
gc.collect() |
| 820 |
l = [] |
| 821 |
l.append(l) |
| 822 |
self.assertTrue( |
| 823 |
any(l is element for element in gc.get_objects(generation=0)) |
| 824 |
) |
| 825 |
self.assertFalse( |
| 826 |
any(l is element for element in gc.get_objects(generation=1)) |
| 827 |
) |
| 828 |
self.assertFalse( |
| 829 |
any(l is element for element in gc.get_objects(generation=2)) |
| 830 |
) |
| 831 |
gc.collect(generation=0) |
| 832 |
self.assertFalse( |
| 833 |
any(l is element for element in gc.get_objects(generation=0)) |
| 834 |
) |
| 835 |
self.assertTrue( |
| 836 |
any(l is element for element in gc.get_objects(generation=1)) |
| 837 |
) |
| 838 |
self.assertFalse( |
| 839 |
any(l is element for element in gc.get_objects(generation=2)) |
| 840 |
) |
| 841 |
gc.collect(generation=1) |
| 842 |
self.assertFalse( |
| 843 |
any(l is element for element in gc.get_objects(generation=0)) |
| 844 |
) |
| 845 |
self.assertFalse( |
| 846 |
any(l is element for element in gc.get_objects(generation=1)) |
| 847 |
) |
| 848 |
self.assertTrue( |
| 849 |
any(l is element for element in gc.get_objects(generation=2)) |
| 850 |
) |
| 851 |
gc.collect(generation=2) |
| 852 |
self.assertFalse( |
| 853 |
any(l is element for element in gc.get_objects(generation=0)) |
| 854 |
) |
| 855 |
self.assertFalse( |
| 856 |
any(l is element for element in gc.get_objects(generation=1)) |
| 857 |
) |
| 858 |
self.assertTrue( |
| 859 |
any(l is element for element in gc.get_objects(generation=2)) |
| 860 |
) |
| 861 |
del l |
| 862 |
gc.collect() |
| 863 |
|
| 864 |
def test_get_objects_arguments(self): |
| 865 |
gc.collect() |
| 866 |
self.assertEqual(len(gc.get_objects()), |
| 867 |
len(gc.get_objects(generation=None))) |
| 868 |
|
| 869 |
self.assertRaises(ValueError, gc.get_objects, 1000) |
| 870 |
self.assertRaises(ValueError, gc.get_objects, -1000) |
| 871 |
self.assertRaises(TypeError, gc.get_objects, "1") |
| 872 |
self.assertRaises(TypeError, gc.get_objects, 1.234) |
| 873 |
|
| 874 |
def test_resurrection_only_happens_once_per_object(self): |
| 875 |
class A: |
| 876 |
def __init__(self): |
| 877 |
self.me = self |
| 878 |
|
| 879 |
class Lazarus(A): |
| 880 |
resurrected = 0 |
| 881 |
resurrected_instances = [] |
| 882 |
|
| 883 |
def __del__(self): |
| 884 |
Lazarus.resurrected += 1 |
| 885 |
Lazarus.resurrected_instances.append(self) |
| 886 |
|
| 887 |
gc.collect() |
| 888 |
gc.disable() |
| 889 |
|
| 890 |
|
| 891 |
laz = Lazarus() |
| 892 |
self.assertEqual(Lazarus.resurrected, 0) |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
del laz |
| 897 |
gc.collect() |
| 898 |
self.assertEqual(Lazarus.resurrected, 1) |
| 899 |
self.assertEqual(len(Lazarus.resurrected_instances), 1) |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
Lazarus.resurrected_instances.clear() |
| 904 |
self.assertEqual(Lazarus.resurrected, 1) |
| 905 |
gc.collect() |
| 906 |
self.assertEqual(Lazarus.resurrected, 1) |
| 907 |
|
| 908 |
gc.enable() |
| 909 |
|
| 910 |
def test_resurrection_is_transitive(self): |
| 911 |
class Cargo: |
| 912 |
def __init__(self): |
| 913 |
self.me = self |
| 914 |
|
| 915 |
class Lazarus: |
| 916 |
resurrected_instances = [] |
| 917 |
|
| 918 |
def __del__(self): |
| 919 |
Lazarus.resurrected_instances.append(self) |
| 920 |
|
| 921 |
gc.collect() |
| 922 |
gc.disable() |
| 923 |
|
| 924 |
laz = Lazarus() |
| 925 |
cargo = Cargo() |
| 926 |
cargo_id = id(cargo) |
| 927 |
|
| 928 |
|
| 929 |
laz.cargo = cargo |
| 930 |
cargo.laz = laz |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
del laz, cargo |
| 935 |
gc.collect() |
| 936 |
self.assertEqual(len(Lazarus.resurrected_instances), 1) |
| 937 |
instance = Lazarus.resurrected_instances.pop() |
| 938 |
self.assertTrue(hasattr(instance, "cargo")) |
| 939 |
self.assertEqual(id(instance.cargo), cargo_id) |
| 940 |
|
| 941 |
gc.collect() |
| 942 |
gc.enable() |
| 943 |
|
| 944 |
def test_resurrection_does_not_block_cleanup_of_other_objects(self): |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
N = 100 |
| 950 |
|
| 951 |
class A: |
| 952 |
def __init__(self): |
| 953 |
self.me = self |
| 954 |
|
| 955 |
class Z(A): |
| 956 |
def __del__(self): |
| 957 |
zs.append(self) |
| 958 |
|
| 959 |
zs = [] |
| 960 |
|
| 961 |
def getstats(): |
| 962 |
d = gc.get_stats()[-1] |
| 963 |
return d['collected'], d['uncollectable'] |
| 964 |
|
| 965 |
gc.collect() |
| 966 |
gc.disable() |
| 967 |
|
| 968 |
|
| 969 |
oldc, oldnc = getstats() |
| 970 |
for i in range(N): |
| 971 |
A() |
| 972 |
t = gc.collect() |
| 973 |
c, nc = getstats() |
| 974 |
self.assertEqual(t, N) |
| 975 |
self.assertEqual(c - oldc, N) |
| 976 |
self.assertEqual(nc - oldnc, 0) |
| 977 |
|
| 978 |
|
| 979 |
oldc, oldnc = c, nc |
| 980 |
Z() |
| 981 |
|
| 982 |
t = gc.collect() |
| 983 |
c, nc = getstats() |
| 984 |
self.assertEqual(t, 0) |
| 985 |
self.assertEqual(c - oldc, 0) |
| 986 |
self.assertEqual(nc - oldnc, 0) |
| 987 |
|
| 988 |
|
| 989 |
oldc, oldnc = c, nc |
| 990 |
for i in range(N): |
| 991 |
A() |
| 992 |
Z() |
| 993 |
t = gc.collect() |
| 994 |
c, nc = getstats() |
| 995 |
self.assertEqual(t, N) |
| 996 |
self.assertEqual(c - oldc, N) |
| 997 |
self.assertEqual(nc - oldnc, 0) |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
oldc, oldnc = c, nc |
| 1002 |
zs.clear() |
| 1003 |
t = gc.collect() |
| 1004 |
c, nc = getstats() |
| 1005 |
self.assertEqual(t, 2) |
| 1006 |
self.assertEqual(c - oldc, 2) |
| 1007 |
self.assertEqual(nc - oldnc, 0) |
| 1008 |
|
| 1009 |
gc.enable() |
| 1010 |
|
| 1011 |
@unittest.skipIf(ContainerNoGC is None, |
| 1012 |
'requires ContainerNoGC extension type') |
| 1013 |
def test_trash_weakref_clear(self): |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
callback = unittest.mock.Mock() |
| 1044 |
|
| 1045 |
class A: |
| 1046 |
__slots__ = ['a', 'y', 'wz'] |
| 1047 |
|
| 1048 |
class Z: |
| 1049 |
pass |
| 1050 |
|
| 1051 |
|
| 1052 |
a = A() |
| 1053 |
a.a = a |
| 1054 |
a.y = ContainerNoGC(Z()) |
| 1055 |
a.wz = weakref.ref(a.y.value, callback) |
| 1056 |
|
| 1057 |
wr_cycle = [a.wz] |
| 1058 |
wr_cycle.append(wr_cycle) |
| 1059 |
|
| 1060 |
gc.collect() |
| 1061 |
gc.disable() |
| 1062 |
|
| 1063 |
del a, wr_cycle |
| 1064 |
gc.collect() |
| 1065 |
|
| 1066 |
|
| 1067 |
callback.assert_not_called() |
| 1068 |
gc.enable() |
| 1069 |
|
| 1070 |
|
| 1071 |
class GCCallbackTests(unittest.TestCase): |
| 1072 |
def setUp(self): |
| 1073 |
|
| 1074 |
self.enabled = gc.isenabled() |
| 1075 |
gc.disable() |
| 1076 |
self.debug = gc.get_debug() |
| 1077 |
gc.set_debug(0) |
| 1078 |
gc.callbacks.append(self.cb1) |
| 1079 |
gc.callbacks.append(self.cb2) |
| 1080 |
self.othergarbage = [] |
| 1081 |
|
| 1082 |
def tearDown(self): |
| 1083 |
|
| 1084 |
del self.visit |
| 1085 |
gc.callbacks.remove(self.cb1) |
| 1086 |
gc.callbacks.remove(self.cb2) |
| 1087 |
gc.set_debug(self.debug) |
| 1088 |
if self.enabled: |
| 1089 |
gc.enable() |
| 1090 |
|
| 1091 |
gc.collect() |
| 1092 |
for obj in gc.garbage: |
| 1093 |
if isinstance(obj, Uncollectable): |
| 1094 |
obj.partner = None |
| 1095 |
del gc.garbage[:] |
| 1096 |
del self.othergarbage |
| 1097 |
gc.collect() |
| 1098 |
|
| 1099 |
def preclean(self): |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
self.visit = [] |
| 1104 |
gc.collect() |
| 1105 |
garbage, gc.garbage[:] = gc.garbage[:], [] |
| 1106 |
self.othergarbage.append(garbage) |
| 1107 |
self.visit = [] |
| 1108 |
|
| 1109 |
def cb1(self, phase, info): |
| 1110 |
self.visit.append((1, phase, dict(info))) |
| 1111 |
|
| 1112 |
def cb2(self, phase, info): |
| 1113 |
self.visit.append((2, phase, dict(info))) |
| 1114 |
if phase == "stop" and hasattr(self, "cleanup"): |
| 1115 |
|
| 1116 |
uc = [e for e in gc.garbage if isinstance(e, Uncollectable)] |
| 1117 |
gc.garbage[:] = [e for e in gc.garbage |
| 1118 |
if not isinstance(e, Uncollectable)] |
| 1119 |
for e in uc: |
| 1120 |
e.partner = None |
| 1121 |
|
| 1122 |
def test_collect(self): |
| 1123 |
self.preclean() |
| 1124 |
gc.collect() |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
n = [v[0] for v in self.visit] |
| 1130 |
n1 = [i for i in n if i == 1] |
| 1131 |
n2 = [i for i in n if i == 2] |
| 1132 |
self.assertEqual(n1, [1]*2) |
| 1133 |
self.assertEqual(n2, [2]*2) |
| 1134 |
|
| 1135 |
|
| 1136 |
n = [v[1] for v in self.visit] |
| 1137 |
n1 = [i for i in n if i == "start"] |
| 1138 |
n2 = [i for i in n if i == "stop"] |
| 1139 |
self.assertEqual(n1, ["start"]*2) |
| 1140 |
self.assertEqual(n2, ["stop"]*2) |
| 1141 |
|
| 1142 |
|
| 1143 |
for v in self.visit: |
| 1144 |
info = v[2] |
| 1145 |
self.assertTrue("generation" in info) |
| 1146 |
self.assertTrue("collected" in info) |
| 1147 |
self.assertTrue("uncollectable" in info) |
| 1148 |
|
| 1149 |
def test_collect_generation(self): |
| 1150 |
self.preclean() |
| 1151 |
gc.collect(2) |
| 1152 |
for v in self.visit: |
| 1153 |
info = v[2] |
| 1154 |
self.assertEqual(info["generation"], 2) |
| 1155 |
|
| 1156 |
@cpython_only |
| 1157 |
def test_collect_garbage(self): |
| 1158 |
self.preclean() |
| 1159 |
|
| 1160 |
Uncollectable() |
| 1161 |
Uncollectable() |
| 1162 |
C1055820(666) |
| 1163 |
gc.collect() |
| 1164 |
for v in self.visit: |
| 1165 |
if v[1] != "stop": |
| 1166 |
continue |
| 1167 |
info = v[2] |
| 1168 |
self.assertEqual(info["collected"], 1) |
| 1169 |
self.assertEqual(info["uncollectable"], 4) |
| 1170 |
|
| 1171 |
|
| 1172 |
self.assertEqual(len(gc.garbage), 4) |
| 1173 |
for e in gc.garbage: |
| 1174 |
self.assertIsInstance(e, Uncollectable) |
| 1175 |
|
| 1176 |
|
| 1177 |
self.cleanup=True |
| 1178 |
self.visit = [] |
| 1179 |
gc.garbage[:] = [] |
| 1180 |
gc.collect() |
| 1181 |
for v in self.visit: |
| 1182 |
if v[1] != "stop": |
| 1183 |
continue |
| 1184 |
info = v[2] |
| 1185 |
self.assertEqual(info["collected"], 0) |
| 1186 |
self.assertEqual(info["uncollectable"], 2) |
| 1187 |
|
| 1188 |
|
| 1189 |
self.assertEqual(len(gc.garbage), 0) |
| 1190 |
|
| 1191 |
|
| 1192 |
@unittest.skipIf(BUILD_WITH_NDEBUG, |
| 1193 |
'built with -NDEBUG') |
| 1194 |
def test_refcount_errors(self): |
| 1195 |
self.preclean() |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
import_module("ctypes") |
| 1200 |
|
| 1201 |
import subprocess |
| 1202 |
code = textwrap.dedent(''' |
| 1203 |
from test.support import gc_collect, SuppressCrashReport |
| 1204 |
|
| 1205 |
a = [1, 2, 3] |
| 1206 |
b = [a] |
| 1207 |
|
| 1208 |
# Avoid coredump when Py_FatalError() calls abort() |
| 1209 |
SuppressCrashReport().__enter__() |
| 1210 |
|
| 1211 |
# Simulate the refcount of "a" being too low (compared to the |
| 1212 |
# references held on it by live data), but keeping it above zero |
| 1213 |
# (to avoid deallocating it): |
| 1214 |
import ctypes |
| 1215 |
ctypes.pythonapi.Py_DecRef(ctypes.py_object(a)) |
| 1216 |
|
| 1217 |
# The garbage collector should now have a fatal error |
| 1218 |
# when it reaches the broken object |
| 1219 |
gc_collect() |
| 1220 |
''') |
| 1221 |
p = subprocess.Popen([sys.executable, "-c", code], |
| 1222 |
stdout=subprocess.PIPE, |
| 1223 |
stderr=subprocess.PIPE) |
| 1224 |
stdout, stderr = p.communicate() |
| 1225 |
p.stdout.close() |
| 1226 |
p.stderr.close() |
| 1227 |
|
| 1228 |
self.assertRegex(stderr, |
| 1229 |
br'gcmodule\.c:[0-9]+: gc_decref: Assertion "gc_get_refs\(g\) > 0" failed.') |
| 1230 |
self.assertRegex(stderr, |
| 1231 |
br'refcount is too small') |
| 1232 |
|
| 1233 |
|
| 1234 |
address_regex = br'[0-9a-fA-Fx]+' |
| 1235 |
self.assertRegex(stderr, |
| 1236 |
br'object address : ' + address_regex) |
| 1237 |
self.assertRegex(stderr, |
| 1238 |
br'object refcount : 1') |
| 1239 |
self.assertRegex(stderr, |
| 1240 |
br'object type : ' + address_regex) |
| 1241 |
self.assertRegex(stderr, |
| 1242 |
br'object type name: list') |
| 1243 |
self.assertRegex(stderr, |
| 1244 |
br'object repr : \[1, 2, 3\]') |
| 1245 |
|
| 1246 |
|
| 1247 |
class GCTogglingTests(unittest.TestCase): |
| 1248 |
def setUp(self): |
| 1249 |
gc.enable() |
| 1250 |
|
| 1251 |
def tearDown(self): |
| 1252 |
gc.disable() |
| 1253 |
|
| 1254 |
def test_bug1055820c(self): |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
c0 = C1055820(0) |
| 1259 |
|
| 1260 |
gc.collect() |
| 1261 |
|
| 1262 |
c1 = C1055820(1) |
| 1263 |
c1.keep_c0_alive = c0 |
| 1264 |
del c0.loop |
| 1265 |
|
| 1266 |
c2 = C1055820(2) |
| 1267 |
c2wr = weakref.ref(c2) |
| 1268 |
|
| 1269 |
ouch = [] |
| 1270 |
def callback(ignored): |
| 1271 |
ouch[:] = [c2wr()] |
| 1272 |
|
| 1273 |
|
| 1274 |
c0wr = weakref.ref(c0, callback) |
| 1275 |
|
| 1276 |
c0 = c1 = c2 = None |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
junk = [] |
| 1309 |
i = 0 |
| 1310 |
detector = GC_Detector() |
| 1311 |
while not detector.gc_happened: |
| 1312 |
i += 1 |
| 1313 |
if i > 10000: |
| 1314 |
self.fail("gc didn't happen after 10000 iterations") |
| 1315 |
self.assertEqual(len(ouch), 0) |
| 1316 |
junk.append([]) |
| 1317 |
|
| 1318 |
self.assertEqual(len(ouch), 1) |
| 1319 |
for x in ouch: |
| 1320 |
|
| 1321 |
|
| 1322 |
self.assertEqual(x, None) |
| 1323 |
|
| 1324 |
def test_bug1055820d(self): |
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
ouch = [] |
| 1330 |
class D(C1055820): |
| 1331 |
def __del__(self): |
| 1332 |
ouch[:] = [c2wr()] |
| 1333 |
|
| 1334 |
d0 = D(0) |
| 1335 |
|
| 1336 |
gc.collect() |
| 1337 |
|
| 1338 |
c1 = C1055820(1) |
| 1339 |
c1.keep_d0_alive = d0 |
| 1340 |
del d0.loop |
| 1341 |
|
| 1342 |
c2 = C1055820(2) |
| 1343 |
c2wr = weakref.ref(c2) |
| 1344 |
|
| 1345 |
d0 = c1 = c2 = None |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
detector = GC_Detector() |
| 1376 |
junk = [] |
| 1377 |
i = 0 |
| 1378 |
while not detector.gc_happened: |
| 1379 |
i += 1 |
| 1380 |
if i > 10000: |
| 1381 |
self.fail("gc didn't happen after 10000 iterations") |
| 1382 |
self.assertEqual(len(ouch), 0) |
| 1383 |
junk.append([]) |
| 1384 |
|
| 1385 |
self.assertEqual(len(ouch), 1) |
| 1386 |
for x in ouch: |
| 1387 |
|
| 1388 |
|
| 1389 |
self.assertEqual(x, None) |
| 1390 |
|
| 1391 |
|
| 1392 |
class PythonFinalizationTests(unittest.TestCase): |
| 1393 |
def test_ast_fini(self): |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
code = textwrap.dedent(""" |
| 1400 |
import ast |
| 1401 |
import codecs |
| 1402 |
|
| 1403 |
# Small AST tree to keep their AST types alive |
| 1404 |
tree = ast.parse("def f(x, y): return 2*x-y") |
| 1405 |
x = [tree] |
| 1406 |
x.append(x) |
| 1407 |
|
| 1408 |
# Put the cycle somewhere to survive until the last GC collection. |
| 1409 |
# Codec search functions are only cleared at the end of |
| 1410 |
# interpreter_clear(). |
| 1411 |
def search_func(encoding): |
| 1412 |
return None |
| 1413 |
search_func.a = x |
| 1414 |
codecs.register(search_func) |
| 1415 |
""") |
| 1416 |
assert_python_ok("-c", code) |
| 1417 |
|
| 1418 |
|
| 1419 |
def setUpModule(): |
| 1420 |
global enabled, debug |
| 1421 |
enabled = gc.isenabled() |
| 1422 |
gc.disable() |
| 1423 |
assert not gc.isenabled() |
| 1424 |
debug = gc.get_debug() |
| 1425 |
gc.set_debug(debug & ~gc.DEBUG_LEAK) |
| 1426 |
gc.collect() |
| 1427 |
|
| 1428 |
|
| 1429 |
def tearDownModule(): |
| 1430 |
gc.set_debug(debug) |
| 1431 |
|
| 1432 |
if verbose: |
| 1433 |
print("restoring automatic collection") |
| 1434 |
|
| 1435 |
gc.enable() |
| 1436 |
assert gc.isenabled() |
| 1437 |
if not enabled: |
| 1438 |
gc.disable() |
| 1439 |
|
| 1440 |
|
| 1441 |
if __name__ == "__main__": |
| 1442 |
unittest.main() |