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

Side by Side Diff: 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/
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:
View unified diff | Download patch
OLDNEW
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
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'
424 import _testcapi
425 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
422 self.file = open(test.test_support.TESTFN, 'wb') 426 self.file = open(test.test_support.TESTFN, 'wb')
423 427
424 def tearDown(self): 428 def tearDown(self):
425 self.file.close() 429 self.file.close()
426 test.test_support.unlink(test.test_support.TESTFN) 430 test.test_support.unlink(test.test_support.TESTFN)
427 431
428 def check_sizeof(self, o, size, size2=None): 432 def check_sizeof(self, o, size, size2=None):
429 """Check size of o. Possible are size and optionally size2).""" 433 """Check size of o. Possible are size and optionally size2)."""
430 result = sys.getsizeof(o) 434 result = sys.getsizeof(o)
435 # add GC header size
436 if type(o).__flags__ & self.TPFLAGS_HAVE_GC:
437 size += self.gc_headsize
438 if size2 != None:
439 size2 += self.gc_headsize
431 msg = 'wrong size for %s: got %d, expected ' % (type(o), result) 440 msg = 'wrong size for %s: got %d, expected ' % (type(o), result)
432 if (size2 != None) and (result != size): 441 if (size2 != None) and (result != size):
433 self.assertEqual(result, size2, msg + str(size2)) 442 self.assertEqual(result, size2, msg + str(size2))
434 else: 443 else:
435 self.assertEqual(result, size, msg + str(size)) 444 self.assertEqual(result, size, msg + str(size))
436 445
437 def calcsize(self, fmt): 446 def calcsize(self, fmt):
438 """Wrapper around struct.calcsize which enforces the alignment of the 447 """Wrapper around struct.calcsize which enforces the alignment of the
439 end of a structure to the alignment requirement of pointer. 448 end of a structure to the alignment requirement of pointer.
440 449
441 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
442 and no member with a size larger than a pointer exists. 451 and no member with a size larger than a pointer exists.
443 """ 452 """
444 return struct.calcsize(fmt + '0P') 453 return struct.calcsize(fmt + '0P')
445 454
455 def test_gc_head_size(self):
456 # Check that the gc header size is added to objects tracked by the gc.
457 h = self.header
458 size = self.calcsize
459 gc_header_size = self.gc_headsize
460 # bool objects are not gc tracked
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'))
464 # but lists are
465 self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)
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
446 def test_standardtypes(self): 474 def test_standardtypes(self):
447 h = self.header 475 h = self.header
448 size = self.calcsize 476 size = self.calcsize
449 # bool 477 # bool
450 self.check_sizeof(True, size(h + 'l')) 478 self.check_sizeof(True, size(h + 'l'))
451 # buffer 479 # buffer
452 self.check_sizeof(buffer(''), size(h + '2P2Pil')) 480 self.check_sizeof(buffer(''), size(h + '2P2Pil'))
453 # cell 481 # cell
454 def get_cell(): 482 def get_cell():
455 x = 42 483 x = 42
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 self.check_sizeof((1,2,3), size(h) + 3*self.P) 588 self.check_sizeof((1,2,3), size(h) + 3*self.P)
561 589
562 590
563 def test_main(): 591 def test_main():
564 test_classes = (SysModuleTest, SizeofTest) 592 test_classes = (SysModuleTest, SizeofTest)
565 593
566 test.test_support.run_unittest(*test_classes) 594 test.test_support.run_unittest(*test_classes)
567 595
568 if __name__ == "__main__": 596 if __name__ == "__main__":
569 test_main() 597 test_main()
OLDNEW

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