test_weakref.py 884 B

12345678910111213141516171819202122232425262728293031323334
  1. import gc
  2. import greenlet
  3. import weakref
  4. import unittest
  5. class WeakRefTests(unittest.TestCase):
  6. def test_dead_weakref(self):
  7. def _dead_greenlet():
  8. g = greenlet.greenlet(lambda: None)
  9. g.switch()
  10. return g
  11. o = weakref.ref(_dead_greenlet())
  12. gc.collect()
  13. self.assertEqual(o(), None)
  14. def test_inactive_weakref(self):
  15. o = weakref.ref(greenlet.greenlet())
  16. gc.collect()
  17. self.assertEqual(o(), None)
  18. def test_dealloc_weakref(self):
  19. seen = []
  20. def worker():
  21. try:
  22. greenlet.getcurrent().parent.switch()
  23. finally:
  24. seen.append(g())
  25. g = greenlet.greenlet(worker)
  26. g.switch()
  27. g2 = greenlet.greenlet(lambda: None, g)
  28. g = weakref.ref(g2)
  29. g2 = None
  30. self.assertEqual(seen, [None])