diff options
author | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-07-10 17:13:55 (GMT) |
---|---|---|
committer | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-07-10 17:13:55 (GMT) |
commit | 476290299885a87b8b90b55bbfef067945e63913 (patch) | |
tree | 0256a50d68580213b98bb1baa63cdd65fbda4a96 /Lib | |
parent | 5930d8f05e501ac0327c8b752a21412b0fb97cae (diff) | |
download | cpython-476290299885a87b8b90b55bbfef067945e63913.zip cpython-476290299885a87b8b90b55bbfef067945e63913.tar.gz cpython-476290299885a87b8b90b55bbfef067945e63913.tar.bz2 |
Added garbage collector overhead and optional default return value to
sys.getsizeof.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_sys.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index ff0d7a5..12ba113 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -389,6 +389,9 @@ class SysModuleTest(unittest.TestCase): class SizeofTest(unittest.TestCase): + TPFLAGS_HAVE_GC = 1<<14 + TPFLAGS_HEAPTYPE = 1L<<9 + def setUp(self): self.c = len(struct.pack('c', ' ')) self.H = len(struct.pack('H', 0)) @@ -402,6 +405,8 @@ class SizeofTest(unittest.TestCase): if hasattr(sys, "gettotalrefcount"): self.header += '2P' self.vheader += '2P' + import _testcapi + self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD self.file = open(test.test_support.TESTFN, 'wb') def tearDown(self): @@ -410,6 +415,9 @@ class SizeofTest(unittest.TestCase): def check_sizeof(self, o, size): result = sys.getsizeof(o) + if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\ + ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))): + size += self.gc_headsize msg = 'wrong size for %s: got %d, expected %d' \ % (type(o), result, size) self.assertEqual(result, size, msg) @@ -423,6 +431,21 @@ class SizeofTest(unittest.TestCase): """ return struct.calcsize(fmt + '0P') + def test_gc_head_size(self): + # Check that the gc header size is added to objects tracked by the gc. + h = self.header + size = self.calcsize + gc_header_size = self.gc_headsize + # bool objects are not gc tracked + self.assertEqual(sys.getsizeof(True), size(h + 'l')) + # but lists are + self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size) + + def test_default(self): + h = self.header + size = self.calcsize + self.assertEqual(sys.getsizeof(True, -1), size(h + 'l')) + def test_objecttypes(self): # check all types defined in Objects/ h = self.header |