Test that in cases where the garbage cannot be collected, we raise an error, instead of hanging forever trying to clear it.
(self)
| 1584 | |
| 1585 | @pytest.mark.slow |
| 1586 | def test_fails(self): |
| 1587 | """ |
| 1588 | Test that in cases where the garbage cannot be collected, we raise an |
| 1589 | error, instead of hanging forever trying to clear it. |
| 1590 | """ |
| 1591 | |
| 1592 | class ReferenceCycleInDel: |
| 1593 | """ |
| 1594 | An object that not only contains a reference cycle, but creates new |
| 1595 | cycles whenever it's garbage-collected and its __del__ runs |
| 1596 | """ |
| 1597 | make_cycle = True |
| 1598 | |
| 1599 | def __init__(self): |
| 1600 | self.cycle = self |
| 1601 | |
| 1602 | def __del__(self): |
| 1603 | # break the current cycle so that `self` can be freed |
| 1604 | self.cycle = None |
| 1605 | |
| 1606 | if ReferenceCycleInDel.make_cycle: |
| 1607 | # but create a new one so that the garbage collector has more |
| 1608 | # work to do. |
| 1609 | ReferenceCycleInDel() |
| 1610 | |
| 1611 | try: |
| 1612 | w = weakref.ref(ReferenceCycleInDel()) |
| 1613 | try: |
| 1614 | with assert_raises(RuntimeError): |
| 1615 | # this will be unable to get a baseline empty garbage |
| 1616 | assert_no_gc_cycles(lambda: None) |
| 1617 | except AssertionError: |
| 1618 | # the above test is only necessary if the GC actually tried to free |
| 1619 | # our object anyway, which python 2.7 does not. |
| 1620 | if w() is not None: |
| 1621 | pytest.skip("GC does not call __del__ on cyclic objects") |
| 1622 | raise |
| 1623 | |
| 1624 | finally: |
| 1625 | # make sure that we stop creating reference cycles |
| 1626 | ReferenceCycleInDel.make_cycle = False |
nothing calls this directly
no test coverage detected