| OLD | NEW |
| 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 self.assertEqual(out, unichr(0xa2).encode("cp424")) | 400 self.assertEqual(out, unichr(0xa2).encode("cp424")) |
| 401 | 401 |
| 402 env["PYTHONIOENCODING"] = "ascii:replace" | 402 env["PYTHONIOENCODING"] = "ascii:replace" |
| 403 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], | 403 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], |
| 404 stdout = subprocess.PIPE, env=env) | 404 stdout = subprocess.PIPE, env=env) |
| 405 out = p.stdout.read().strip() | 405 out = p.stdout.read().strip() |
| 406 self.assertEqual(out, '?') | 406 self.assertEqual(out, '?') |
| 407 | 407 |
| 408 | 408 |
| 409 class SizeofTest(unittest.TestCase): | 409 class SizeofTest(unittest.TestCase): |
| 410 |
| 411 TPFLAGS_HAVE_GC = 1<<14 |
| 410 | 412 |
| 411 def setUp(self): | 413 def setUp(self): |
| 412 self.c = len(struct.pack('c', ' ')) | 414 self.c = len(struct.pack('c', ' ')) |
| 413 self.H = len(struct.pack('H', 0)) | 415 self.H = len(struct.pack('H', 0)) |
| 414 self.i = len(struct.pack('i', 0)) | 416 self.i = len(struct.pack('i', 0)) |
| 415 self.l = len(struct.pack('l', 0)) | 417 self.l = len(struct.pack('l', 0)) |
| 416 self.P = len(struct.pack('P', 0)) | 418 self.P = len(struct.pack('P', 0)) |
| 417 # 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 |
| 418 # sizeof(Py_ssize_t) = sizeof(void*) | 420 # sizeof(Py_ssize_t) = sizeof(void*) |
| 419 self.header = 'lP' | 421 self.header = 'lP' |
| 420 if hasattr(sys, "gettotalrefcount"): | 422 if hasattr(sys, "gettotalrefcount"): |
| 421 self.header += '2P' | 423 self.header += '2P' |
| 422 self.file = open(test.test_support.TESTFN, 'wb') | 424 self.file = open(test.test_support.TESTFN, 'wb') |
| 423 | 425 |
| 424 def tearDown(self): | 426 def tearDown(self): |
| 425 self.file.close() | 427 self.file.close() |
| 426 test.test_support.unlink(test.test_support.TESTFN) | 428 test.test_support.unlink(test.test_support.TESTFN) |
| 427 | 429 |
| 428 def check_sizeof(self, o, size, size2=None): | 430 def check_sizeof(self, o, size, size2=None): |
| 429 """Check size of o. Possible are size and optionally size2).""" | 431 """Check size of o. Possible are size and optionally size2).""" |
| 430 result = sys.getsizeof(o) | 432 result = sys.getsizeof(o) |
| 433 # add GC header size |
| 434 if type(o).__flags__ & SizeofTest.TPFLAGS_HAVE_GC: |
| 435 size += 32 |
| 436 if size2 != None: |
| 437 size2 += 32 |
| 431 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) | 438 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) |
| 432 if (size2 != None) and (result != size): | 439 if (size2 != None) and (result != size): |
| 433 self.assertEqual(result, size2, msg + str(size2)) | 440 self.assertEqual(result, size2, msg + str(size2)) |
| 434 else: | 441 else: |
| 435 self.assertEqual(result, size, msg + str(size)) | 442 self.assertEqual(result, size, msg + str(size)) |
| 436 | 443 |
| 437 def calcsize(self, fmt): | 444 def calcsize(self, fmt): |
| 438 """Wrapper around struct.calcsize which enforces the alignment of the | 445 """Wrapper around struct.calcsize which enforces the alignment of the |
| 439 end of a structure to the alignment requirement of pointer. | 446 end of a structure to the alignment requirement of pointer. |
| 440 | 447 |
| 441 Note: This wrapper should only be used if a pointer member is included | 448 Note: This wrapper should only be used if a pointer member is included |
| 442 and no member with a size larger than a pointer exists. | 449 and no member with a size larger than a pointer exists. |
| 443 """ | 450 """ |
| 444 return struct.calcsize(fmt + '0P') | 451 return struct.calcsize(fmt + '0P') |
| 452 |
| 453 def test_gc_head_size(self): |
| 454 # Check that the gc header size is added to objects tracked by the gc. |
| 455 h = self.header |
| 456 size = self.calcsize |
| 457 gc_header_size = 32#struct.calcsize('PPP' + '0P0P') |
| 458 # bool objects are not gc tracked |
| 459 self.assertEqual(sys.getsizeof(True), size(h + 'l')) |
| 460 # but lists are |
| 461 self.assertEqual(sys.getsizeof([]), size(h + 'lPP') + gc_header_size) |
| 445 | 462 |
| 446 def test_standardtypes(self): | 463 def test_standardtypes(self): |
| 447 h = self.header | 464 h = self.header |
| 448 size = self.calcsize | 465 size = self.calcsize |
| 449 # bool | 466 # bool |
| 450 self.check_sizeof(True, size(h + 'l')) | 467 self.check_sizeof(True, size(h + 'l')) |
| 451 # buffer | 468 # buffer |
| 452 self.check_sizeof(buffer(''), size(h + '2P2Pil')) | 469 self.check_sizeof(buffer(''), size(h + '2P2Pil')) |
| 453 # cell | 470 # cell |
| 454 def get_cell(): | 471 def get_cell(): |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 self.check_sizeof((1,2,3), size(h) + 3*self.P) | 577 self.check_sizeof((1,2,3), size(h) + 3*self.P) |
| 561 | 578 |
| 562 | 579 |
| 563 def test_main(): | 580 def test_main(): |
| 564 test_classes = (SysModuleTest, SizeofTest) | 581 test_classes = (SysModuleTest, SizeofTest) |
| 565 | 582 |
| 566 test.test_support.run_unittest(*test_classes) | 583 test.test_support.run_unittest(*test_classes) |
| 567 | 584 |
| 568 if __name__ == "__main__": | 585 if __name__ == "__main__": |
| 569 test_main() | 586 test_main() |
| OLD | NEW |