Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(106)

Delta Between Two Patch Sets: Lib/test/test_sys.py

Issue 2414: Add gc header size to returned sizeof information SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Left Patch Set: Created 1 year, 5 months ago
Right Patch Set: added a default return value and made this and the gc_head inclusion optional Created 1 year, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 self.c = len(struct.pack('c', ' ')) 414 self.c = len(struct.pack('c', ' '))
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
425 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
424 self.file = open(test.test_support.TESTFN, 'wb') 426 self.file = open(test.test_support.TESTFN, 'wb')
425 427
426 def tearDown(self): 428 def tearDown(self):
427 self.file.close() 429 self.file.close()
428 test.test_support.unlink(test.test_support.TESTFN) 430 test.test_support.unlink(test.test_support.TESTFN)
429 431
430 def check_sizeof(self, o, size, size2=None): 432 def check_sizeof(self, o, size, size2=None):
431 """Check size of o. Possible are size and optionally size2).""" 433 """Check size of o. Possible are size and optionally size2)."""
432 result = sys.getsizeof(o) 434 result = sys.getsizeof(o)
433 # add GC header size 435 # add GC header size
434 if type(o).__flags__ & SizeofTest.TPFLAGS_HAVE_GC: 436 if type(o).__flags__ & self.TPFLAGS_HAVE_GC:
435 size += 32 437 size += self.gc_headsize
436 if size2 != None: 438 if size2 != None:
437 size2 += 32 439 size2 += self.gc_headsize
438 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) 440 msg = 'wrong size for %s: got %d, expected ' % (type(o), result)
439 if (size2 != None) and (result != size): 441 if (size2 != None) and (result != size):
440 self.assertEqual(result, size2, msg + str(size2)) 442 self.assertEqual(result, size2, msg + str(size2))
441 else: 443 else:
442 self.assertEqual(result, size, msg + str(size)) 444 self.assertEqual(result, size, msg + str(size))
443 445
444 def calcsize(self, fmt): 446 def calcsize(self, fmt):
445 """Wrapper around struct.calcsize which enforces the alignment of the 447 """Wrapper around struct.calcsize which enforces the alignment of the
446 end of a structure to the alignment requirement of pointer. 448 end of a structure to the alignment requirement of pointer.
447 449
448 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
449 and no member with a size larger than a pointer exists. 451 and no member with a size larger than a pointer exists.
450 """ 452 """
451 return struct.calcsize(fmt + '0P') 453 return struct.calcsize(fmt + '0P')
452 454
453 def test_gc_head_size(self): 455 def test_gc_head_size(self):
454 # 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.
455 h = self.header 457 h = self.header
456 size = self.calcsize 458 size = self.calcsize
457 gc_header_size = 32#struct.calcsize('PPP' + '0P0P') 459 gc_header_size = self.gc_headsize
458 # bool objects are not gc tracked 460 # bool objects are not gc tracked
459 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'))
460 # but lists are 464 # but lists are
461 self.assertEqual(sys.getsizeof([]), size(h + 'lPP') + gc_header_size) 465 self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)
462 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
463 def test_standardtypes(self): 474 def test_standardtypes(self):
464 h = self.header 475 h = self.header
465 size = self.calcsize 476 size = self.calcsize
466 # bool 477 # bool
467 self.check_sizeof(True, size(h + 'l')) 478 self.check_sizeof(True, size(h + 'l'))
468 # buffer 479 # buffer
469 self.check_sizeof(buffer(''), size(h + '2P2Pil')) 480 self.check_sizeof(buffer(''), size(h + '2P2Pil'))
470 # cell 481 # cell
471 def get_cell(): 482 def get_cell():
472 x = 42 483 x = 42
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 self.check_sizeof((1,2,3), size(h) + 3*self.P) 588 self.check_sizeof((1,2,3), size(h) + 3*self.P)
578 589
579 590
580 def test_main(): 591 def test_main():
581 test_classes = (SysModuleTest, SizeofTest) 592 test_classes = (SysModuleTest, SizeofTest)
582 593
583 test.test_support.run_unittest(*test_classes) 594 test.test_support.run_unittest(*test_classes)
584 595
585 if __name__ == "__main__": 596 if __name__ == "__main__":
586 test_main() 597 test_main()
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497