Mercurial > cpython-lru_cache-weakref
diff Lib/test/test_sys.py @ 80777:c40f4c19d20b
Issue #13390: New function :func:`sys.getallocatedblocks()` returns the number of memory blocks currently allocated.
Also, the ``-R`` option to regrtest uses this function to guard against memory allocation leaks.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 Dec 2012 14:28:26 +0100 |
| parents | 49e57a0e4a2d |
| children | a85673b55177 |
line wrap: on
line diff
--- a/Lib/test/test_sys.py Sun Dec 09 16:22:17 2012 +1000 +++ b/Lib/test/test_sys.py Sun Dec 09 14:28:26 2012 +0100 @@ -6,6 +6,7 @@ import warnings import operator import codecs +import gc # count the number of test runs, used to create unique # strings to intern in test_intern() @@ -611,6 +612,29 @@ ret, out, err = assert_python_ok(*args) self.assertIn(b"free PyDictObjects", err) + @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), + "sys.getallocatedblocks unavailable on this build") + def test_getallocatedblocks(self): + # Some sanity checks + a = sys.getallocatedblocks() + self.assertIs(type(a), int) + self.assertGreater(a, 0) + try: + # While we could imagine a Python session where the number of + # multiple buffer objects would exceed the sharing of references, + # it is unlikely to happen in a normal test run. + self.assertLess(a, sys.gettotalrefcount()) + except AttributeError: + # gettotalrefcount() not available + pass + gc.collect() + b = sys.getallocatedblocks() + self.assertLessEqual(b, a) + gc.collect() + c = sys.getallocatedblocks() + self.assertIn(c, range(b - 50, b + 50)) + + class SizeofTest(unittest.TestCase): def setUp(self):
