我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用gc.is_tracked()。
def _not_tracked(self, t): # Nested containers can take several collections to untrack gc.collect() gc.collect() self.assertFalse(gc.is_tracked(t), t)
def _tracked(self, t): self.assertTrue(gc.is_tracked(t), t) gc.collect() gc.collect() self.assertTrue(gc.is_tracked(t), t)
def _not_tracked(self, t): # Nested tuples can take several collections to untrack gc.collect() gc.collect() self.assertFalse(gc.is_tracked(t), t)
def test_is_tracked(self): # Atomic built-in types are not tracked, user-defined objects and # mutable containers are. # NOTE: types with special optimizations (e.g. tuple) have tests # in their own test files instead. self.assertFalse(gc.is_tracked(None)) self.assertFalse(gc.is_tracked(1)) self.assertFalse(gc.is_tracked(1.0)) self.assertFalse(gc.is_tracked(1.0 + 5.0j)) self.assertFalse(gc.is_tracked(True)) self.assertFalse(gc.is_tracked(False)) self.assertFalse(gc.is_tracked(b"a")) self.assertFalse(gc.is_tracked("a")) self.assertFalse(gc.is_tracked(bytearray(b"a"))) self.assertFalse(gc.is_tracked(type)) self.assertFalse(gc.is_tracked(int)) self.assertFalse(gc.is_tracked(object)) self.assertFalse(gc.is_tracked(object())) class UserClass: pass self.assertTrue(gc.is_tracked(gc)) self.assertTrue(gc.is_tracked(UserClass)) self.assertTrue(gc.is_tracked(UserClass())) self.assertTrue(gc.is_tracked([])) self.assertTrue(gc.is_tracked(set()))
def check_slots(self, obj, base, extra): expected = sys.getsizeof(base) + struct.calcsize(extra) if gc.is_tracked(obj) and not gc.is_tracked(base): expected += self.gc_headsize self.assertEqual(sys.getsizeof(obj), expected)
def test_is_tracked(self): # Atomic built-in types are not tracked, user-defined objects and # mutable containers are. # NOTE: types with special optimizations (e.g. tuple) have tests # in their own test files instead. self.assertFalse(gc.is_tracked(None)) self.assertFalse(gc.is_tracked(1)) self.assertFalse(gc.is_tracked(1.0)) self.assertFalse(gc.is_tracked(1.0 + 5.0j)) self.assertFalse(gc.is_tracked(True)) self.assertFalse(gc.is_tracked(False)) self.assertFalse(gc.is_tracked("a")) self.assertFalse(gc.is_tracked(u"a")) self.assertFalse(gc.is_tracked(bytearray("a"))) self.assertFalse(gc.is_tracked(type)) self.assertFalse(gc.is_tracked(int)) self.assertFalse(gc.is_tracked(object)) self.assertFalse(gc.is_tracked(object())) class OldStyle: pass class NewStyle(object): pass self.assertTrue(gc.is_tracked(gc)) self.assertTrue(gc.is_tracked(OldStyle)) self.assertTrue(gc.is_tracked(OldStyle())) self.assertTrue(gc.is_tracked(NewStyle)) self.assertTrue(gc.is_tracked(NewStyle())) self.assertTrue(gc.is_tracked([])) self.assertTrue(gc.is_tracked(set()))
def test_non_gc(self): with SimpleBase.test(): s = NonGC() self.assertFalse(gc.is_tracked(s)) ids = [id(s)] del s gc.collect() self.assert_del_calls(ids) self.assert_survivors([]) gc.collect() self.assert_del_calls(ids) self.assert_survivors([])
def test_non_gc_resurrect(self): with SimpleBase.test(): s = NonGCResurrector() self.assertFalse(gc.is_tracked(s)) ids = [id(s)] del s gc.collect() self.assert_del_calls(ids) self.assert_survivors(ids) self.clear_survivors() gc.collect() self.assert_del_calls(ids * 2) self.assert_survivors(ids)
def _not_tracked(self, t): # Nested memoryslotss can take several collections to untrack gc.collect() gc.collect() self.assertFalse(gc.is_tracked(t), t)