Skip to content

Instantly share code, notes, and snippets.

@jaraco
Created October 21, 2020 16:13
Show Gist options
  • Save jaraco/8fa0db176af7db2dd35ab6244c3f62b9 to your computer and use it in GitHub Desktop.
Save jaraco/8fa0db176af7db2dd35ab6244c3f62b9 to your computer and use it in GitHub Desktop.
import gc
import random
import functools
import contextlib
@functools.singledispatch
@contextlib.contextmanager
def make_context(obj):
yield random.randint(1, 100)
def test_referrers():
obj = object()
assert len(gc.get_referrers(obj)) == 0
with make_context(obj) as val:
assert len(gc.get_referrers(obj)) == 0
del obj
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