| LEFT | RIGHT |
|---|---|
| 1 # -*- coding: iso-8859-1 -*- | 1 # -*- coding: iso-8859-1 -*- |
| 2 import unittest, test.test_support | 2 import unittest, test.test_support |
| 3 import sys, cStringIO, os | 3 import sys, cStringIO, os |
| 4 import struct | 4 import struct |
| 5 | 5 |
| 6 class SysModuleTest(unittest.TestCase): | 6 class SysModuleTest(unittest.TestCase): |
| 7 | 7 |
| 8 def test_original_displayhook(self): | 8 def test_original_displayhook(self): |
| 9 import __builtin__ | 9 import __builtin__ |
| 10 savestdout = sys.stdout | 10 savestdout = sys.stdout |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 self.H = len(struct.pack('H', 0)) | 415 self.H = len(struct.pack('H', 0)) |
| 416 self.i = len(struct.pack('i', 0)) | 416 self.i = len(struct.pack('i', 0)) |
| 417 self.l = len(struct.pack('l', 0)) | 417 self.l = len(struct.pack('l', 0)) |
| 418 self.P = len(struct.pack('P', 0)) | 418 self.P = len(struct.pack('P', 0)) |
| 419 # due to missing size_t information from struct, it is assumed that | 419 # due to missing size_t information from struct, it is assumed that |
| 420 # sizeof(Py_ssize_t) = sizeof(void*) | 420 # sizeof(Py_ssize_t) = sizeof(void*) |
| 421 self.header = 'lP' | 421 self.header = 'lP' |
| 422 if hasattr(sys, "gettotalrefcount"): | 422 if hasattr(sys, "gettotalrefcount"): |
| 423 self.header += '2P' | 423 self.header += '2P' |
| 424 import _testcapi | 424 import _testcapi |
| 425 self.gc_headsize = _testcapi.GC_HEADSIZE | 425 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD |
| 426 self.file = open(test.test_support.TESTFN, 'wb') | 426 self.file = open(test.test_support.TESTFN, 'wb') |
| 427 | 427 |
| 428 def tearDown(self): | 428 def tearDown(self): |
| 429 self.file.close() | 429 self.file.close() |
| 430 test.test_support.unlink(test.test_support.TESTFN) | 430 test.test_support.unlink(test.test_support.TESTFN) |
| 431 | 431 |
| 432 def check_sizeof(self, o, size, size2=None): | 432 def check_sizeof(self, o, size, size2=None): |
| 433 """Check size of o. Possible are size and optionally size2).""" | 433 """Check size of o. Possible are size and optionally size2).""" |
| 434 result = sys.getsizeof(o) | 434 result = sys.getsizeof(o) |
| 435 # add GC header size | 435 # add GC header size |
| 436 if type(o).__flags__ & SizeofTest.TPFLAGS_HAVE_GC: | 436 if type(o).__flags__ & self.TPFLAGS_HAVE_GC: |
|
Martin v. Löwis
2008/06/26 18:05:31
alternatively: self.TPFLAGS_HAVE_GC
| |
| 437 size += self.gc_headsize | 437 size += self.gc_headsize |
| 438 if size2 != None: | 438 if size2 != None: |
| 439 size2 += self.gc_headsize | 439 size2 += self.gc_headsize |
| 440 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) | 440 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) |
| 441 if (size2 != None) and (result != size): | 441 if (size2 != None) and (result != size): |
| 442 self.assertEqual(result, size2, msg + str(size2)) | 442 self.assertEqual(result, size2, msg + str(size2)) |
| 443 else: | 443 else: |
| 444 self.assertEqual(result, size, msg + str(size)) | 444 self.assertEqual(result, size, msg + str(size)) |
| 445 | 445 |
| 446 def calcsize(self, fmt): | 446 def calcsize(self, fmt): |
| 447 """Wrapper around struct.calcsize which enforces the alignment of the | 447 """Wrapper around struct.calcsize which enforces the alignment of the |
| 448 end of a structure to the alignment requirement of pointer. | 448 end of a structure to the alignment requirement of pointer. |
| 449 | 449 |
| 450 Note: This wrapper should only be used if a pointer member is included | 450 Note: This wrapper should only be used if a pointer member is included |
| 451 and no member with a size larger than a pointer exists. | 451 and no member with a size larger than a pointer exists. |
| 452 """ | 452 """ |
| 453 return struct.calcsize(fmt + '0P') | 453 return struct.calcsize(fmt + '0P') |
| 454 | 454 |
| 455 def test_gc_head_size(self): | 455 def test_gc_head_size(self): |
| 456 # Check that the gc header size is added to objects tracked by the gc. | 456 # Check that the gc header size is added to objects tracked by the gc. |
| 457 h = self.header | 457 h = self.header |
| 458 size = self.calcsize | 458 size = self.calcsize |
| 459 gc_header_size = self.gc_headsize | 459 gc_header_size = self.gc_headsize |
| 460 # bool objects are not gc tracked | 460 # bool objects are not gc tracked |
| 461 self.assertEqual(sys.getsizeof(True), size(h + 'l')) | 461 self.assertEqual(sys.getsizeof(True), size(h + 'l')) |
| 462 self.assertEqual(sys.getsizeof(True, gc_head=True), size(h + 'l')) | |
| 463 self.assertEqual(sys.getsizeof(True, gc_head=False), size(h + 'l')) | |
| 462 # but lists are | 464 # but lists are |
| 463 self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size) | 465 self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size) |
| 464 | 466 self.assertEqual(sys.getsizeof([], gc_head=False), size(h + 'P PP')) |
| 467 | |
| 468 def test_default(self): | |
| 469 h = self.header | |
| 470 size = self.calcsize | |
| 471 self.assertEqual(sys.getsizeof(True, -1), size(h + 'l')) | |
| 472 self.assertEqual(sys.getsizeof(True, -1, gc_head=True), size(h + 'l')) | |
| 473 | |
| 465 def test_standardtypes(self): | 474 def test_standardtypes(self): |
| 466 h = self.header | 475 h = self.header |
| 467 size = self.calcsize | 476 size = self.calcsize |
| 468 # bool | 477 # bool |
| 469 self.check_sizeof(True, size(h + 'l')) | 478 self.check_sizeof(True, size(h + 'l')) |
| 470 # buffer | 479 # buffer |
| 471 self.check_sizeof(buffer(''), size(h + '2P2Pil')) | 480 self.check_sizeof(buffer(''), size(h + '2P2Pil')) |
| 472 # cell | 481 # cell |
| 473 def get_cell(): | 482 def get_cell(): |
| 474 x = 42 | 483 x = 42 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 579 self.check_sizeof((1,2,3), size(h) + 3*self.P) | 588 self.check_sizeof((1,2,3), size(h) + 3*self.P) |
| 580 | 589 |
| 581 | 590 |
| 582 def test_main(): | 591 def test_main(): |
| 583 test_classes = (SysModuleTest, SizeofTest) | 592 test_classes = (SysModuleTest, SizeofTest) |
| 584 | 593 |
| 585 test.test_support.run_unittest(*test_classes) | 594 test.test_support.run_unittest(*test_classes) |
| 586 | 595 |
| 587 if __name__ == "__main__": | 596 if __name__ == "__main__": |
| 588 test_main() | 597 test_main() |
| LEFT | RIGHT |