-
-
Save jaraco/1829cc14c16ca65fb795774f30369f23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
import random | |
import contextlib | |
class Opener: | |
def __enter__(self): | |
return random.randint(1, 100) | |
def __exit__(self, *args): | |
pass | |
@contextlib.contextmanager | |
def make_context(obj): | |
print('a', len(gc.get_referrers(obj))) | |
with obj as val: | |
print('b', len(gc.get_referrers(obj))) | |
del obj | |
yield val | |
def test_referrers(): | |
opener = Opener() | |
assert len(gc.get_referrers(opener)) == 0 | |
with make_context(opener) as val: | |
assert len(gc.get_referrers(opener)) == 0 | |
del opener | |
assert isinstance(val, int) | |
__name__ == '__main__' and test_referrers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment