Guest User

Itertools Tee Dereference Bug?

a guest
Mar 23rd, 2020
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import weakref
  2. import itertools
  3. import collections
  4.  
  5. def tee_from_documentation(iterable, n=2):
  6.     it = iter(iterable)
  7.     deques = [collections.deque() for i in range(n)]
  8.     def gen(mydeque):
  9.         while True:
  10.             if not mydeque:             # when the local deque is empty
  11.                 try:
  12.                     newval = next(it)   # fetch a new value and
  13.                 except StopIteration:
  14.                     return
  15.                 for d in deques:        # load it to all the deques
  16.                     d.append(newval)
  17.             yield mydeque.popleft()
  18.     return tuple(gen(d) for d in deques)
  19.  
  20. class Object:
  21.     pass
  22.  
  23. def test(tee_impl):
  24.     print(f"Using {tee_impl}")
  25.     generator = (Object() for i in range(1))
  26.     iterator = tee_impl(generator, 1)[0]
  27.     ref = [weakref.ref(item) for item in iterator][0]
  28.     print(f"  De-referenced after iteration: {ref() is None}")
  29.     del iterator
  30.     print(f"  De-referenced after iterator delete: {ref() is None}")
  31.    
  32. test(tee_from_documentation)
  33. test(itertools.tee)
  34.  
  35. # Result:
  36. #
  37. # Using <function tee_from_documentation at 0x7f98902e80e0>
  38. #  De-referenced after iteration: True
  39. #  De-referenced after iterator delete: True
  40. # Using <built-in function tee>
  41. #  De-referenced after iteration: False
  42. #  De-referenced after iterator delete: True
RAW Paste Data