| Index: Lib/test/test_sys.py |
| =================================================================== |
| --- Lib/test/test_sys.py (revision 64518) |
| +++ Lib/test/test_sys.py (working copy) |
| @@ -408,6 +408,8 @@ |
| class SizeofTest(unittest.TestCase): |
| + TPFLAGS_HAVE_GC = 1<<14 |
| + |
| def setUp(self): |
| self.c = len(struct.pack('c', ' ')) |
| self.H = len(struct.pack('H', 0)) |
| @@ -419,6 +421,8 @@ |
| self.header = 'lP' |
| if hasattr(sys, "gettotalrefcount"): |
| self.header += '2P' |
| + import _testcapi |
| + self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD |
| self.file = open(test.test_support.TESTFN, 'wb') |
| def tearDown(self): |
| @@ -428,6 +432,11 @@ |
| def check_sizeof(self, o, size, size2=None): |
| """Check size of o. Possible are size and optionally size2).""" |
| result = sys.getsizeof(o) |
| + # add GC header size |
| + if type(o).__flags__ & self.TPFLAGS_HAVE_GC: |
| + size += self.gc_headsize |
| + if size2 != None: |
| + size2 += self.gc_headsize |
| msg = 'wrong size for %s: got %d, expected ' % (type(o), result) |
| if (size2 != None) and (result != size): |
| self.assertEqual(result, size2, msg + str(size2)) |
| @@ -443,6 +452,25 @@ |
| """ |
| 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')) |
| + self.assertEqual(sys.getsizeof(True, gc_head=True), size(h + 'l')) |
| + self.assertEqual(sys.getsizeof(True, gc_head=False), size(h + 'l')) |
| + # but lists are |
| + self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size) |
| + self.assertEqual(sys.getsizeof([], gc_head=False), size(h + 'P PP')) |
| + |
| + def test_default(self): |
| + h = self.header |
| + size = self.calcsize |
| + self.assertEqual(sys.getsizeof(True, -1), size(h + 'l')) |
| + self.assertEqual(sys.getsizeof(True, -1, gc_head=True), size(h + 'l')) |
| + |
| def test_standardtypes(self): |
| h = self.header |
| size = self.calcsize |