Showing content from http://mail.python.org/pipermail/python-dev/attachments/20141026/f60b2cc0/attachment.py below:
import unittest import gc import time import weakref class ReferentDummy: def __init__(self, name): self.name = name def __str__(self): return self.name class ResurrectionDummy: def __del__(self): ResurrectionDummy.resurrected = self.toResurrect class GCDetector(): gcIndex = 0 def __del__(self): GCDetector.gcIndex += 1 maxGCRun = 10 def runGC(): """ This is needed for Jython, since theoretically Java gc is not guaranteed to run if gc.collect is called; the run is only attempted. This method assures that actually a gc run happens. """ currentIndex = GCDetector.gcIndex gcCount = 0 detector = GCDetector() detector = None while currentIndex == GCDetector.gcIndex and gcCount < maxGCRun: gc.collect() gcCount += 1 time.sleep(0.1) class GCTests(unittest.TestCase): def test_id_after_resurrection(self): l = ["ab"] rd = ResurrectionDummy() rd.toResurrect = l savedId = id(l) l = None rd = None runGC() #needed for Jython etc, even though no cyclic trash appears self.assertEqual(id(ResurrectionDummy.resurrected), savedId) def test_id_after_resurrection_cyclic(self): #CPython 2.7.5 fails this test rd = ResurrectionDummy() l = ["ab", rd] rd.toResurrect = l savedId = id(l) l = None rd = None runGC() self.assertEqual(id(ResurrectionDummy.resurrected), savedId) def test_weakref_after_resurrection(self): l = ReferentDummy("ab") rd = ResurrectionDummy() rd.toResurrect = l wref = weakref.ref(l) self.assertIn(wref, weakref.getweakrefs(l)) l = None rd = None runGC() #needed for Jython etc, even though no cyclic trash appears self.assertIn(wref, weakref.getweakrefs(ResurrectionDummy.resurrected)) def test_weakref_after_resurrection_cyclic(self): #CPython 2.7.5 fails this test l = ReferentDummy("ab") rd = ResurrectionDummy() rd.toResurrect = l l.cycleLink = rd wref = weakref.ref(l) self.assertIn(wref, weakref.getweakrefs(l)) l = None rd = None runGC() self.assertIn(wref, weakref.getweakrefs(ResurrectionDummy.resurrected)) def test_main(): unittest.main() if __name__ == "__main__": test_main()
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4