diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-12-09 13:28:26 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-12-09 13:28:26 (GMT) |
commit | f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2 (patch) | |
tree | fd5ad7ad717440f4586652180cb31a3cf2e52e97 /Lib/test/test_sys.py | |
parent | b4b8f234d4407492c4493e3a16bc8263139c7869 (diff) | |
download | cpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.zip cpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.tar.gz cpython-f9d0b1256fd5a9ae90fa8e8418bd450ec6b7f5f2.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_sys.py')
-rw-r--r-- | Lib/test/test_sys.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index a1074c3..055592b 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -6,6 +6,7 @@ import textwrap 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 @@ class SysModuleTest(unittest.TestCase): 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): |