MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_getallocatedblocks

Method test_getallocatedblocks

Lib/test/test_sys.py:1119–1158  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1117 @unittest.skipUnless(hasattr(sys, "getallocatedblocks"),
1118 "sys.getallocatedblocks unavailable on this build")
1119 def test_getallocatedblocks(self):
1120 try:
1121 import _testinternalcapi
1122 except ImportError:
1123 with_pymalloc = support.with_pymalloc()
1124 else:
1125 try:
1126 alloc_name = _testinternalcapi.pymem_getallocatorsname()
1127 except RuntimeError as exc:
1128 # "cannot get allocators name" (ex: tracemalloc is used)
1129 with_pymalloc = True
1130 else:
1131 with_pymalloc = (alloc_name in ('pymalloc', 'pymalloc_debug'))
1132
1133 # Some sanity checks
1134 a = sys.getallocatedblocks()
1135 self.assertIs(type(a), int)
1136 if with_pymalloc:
1137 self.assertGreater(a, 0)
1138 else:
1139 # When WITH_PYMALLOC isn't available, we don't know anything
1140 # about the underlying implementation: the function might
1141 # return 0 or something greater.
1142 self.assertGreaterEqual(a, 0)
1143 gc.collect()
1144 b = sys.getallocatedblocks()
1145 self.assertLessEqual(b, a)
1146 try:
1147 # The reported blocks will include immortalized strings, but the
1148 # total ref count will not. This will sanity check that among all
1149 # other objects (those eligible for garbage collection) there
1150 # are more references being tracked than allocated blocks.
1151 interned_immortal = sys.getunicodeinternedsize(_only_immortal=True)
1152 self.assertLess(a - interned_immortal, sys.gettotalrefcount())
1153 except AttributeError:
1154 # gettotalrefcount() not available
1155 pass
1156 gc.collect()
1157 c = sys.getallocatedblocks()
1158 self.assertIn(c, range(b - 50, b + 50))
1159
1160 def test_is_gil_enabled(self):
1161 if support.Py_GIL_DISABLED:

Callers

nothing calls this directly

Calls 7

assertGreaterMethod · 0.80
assertGreaterEqualMethod · 0.80
collectMethod · 0.80
assertLessEqualMethod · 0.80
assertInMethod · 0.80
assertIsMethod · 0.45
assertLessMethod · 0.45

Tested by

no test coverage detected