11# -*- coding: iso-8859-1 -*-
22import unittest , test .support
33import sys , io , os
4+ import struct
45
56class SysModuleTest (unittest .TestCase ):
67
@@ -373,13 +374,16 @@ def test_ioencoding(self):
373374class SizeofTest (unittest .TestCase ):
374375
375376 def setUp (self ):
376- import struct
377+ self .c = len (struct .pack ('c' , ' ' ))
378+ self .H = len (struct .pack ('H' , 0 ))
377379 self .i = len (struct .pack ('i' , 0 ))
378380 self .l = len (struct .pack ('l' , 0 ))
379- self .p = len (struct .pack ('P' , 0 ))
380- self .headersize = self .l + self .p
381+ self .P = len (struct .pack ('P' , 0 ))
382+ # due to missing size_t information from struct, it is assumed that
383+ # sizeof(Py_ssize_t) = sizeof(void*)
384+ self .header = 'PP'
381385 if hasattr (sys , "gettotalrefcount" ):
382- self .headersize += 2 * self . p
386+ self .header += '2P'
383387 self .file = open (test .support .TESTFN , 'wb' )
384388
385389 def tearDown (self ):
@@ -395,52 +399,38 @@ def check_sizeof(self, o, size, size2=None):
395399 else :
396400 self .assertEqual (result , size , msg + str (size ))
397401
398- def align (self , value ):
399- mod = value % self .p
400- if mod != 0 :
401- return value - mod + self .p
402- else :
403- return value
402+ def calcsize (self , fmt ):
403+ """Wrapper around struct.calcsize which enforces the alignment of the
404+ end of a structure to the alignment requirement of pointer.
404405
405- def test_align (self ):
406- self .assertEqual (self .align (0 ) % self .p , 0 )
407- self .assertEqual (self .align (1 ) % self .p , 0 )
408- self .assertEqual (self .align (3 ) % self .p , 0 )
409- self .assertEqual (self .align (4 ) % self .p , 0 )
410- self .assertEqual (self .align (7 ) % self .p , 0 )
411- self .assertEqual (self .align (8 ) % self .p , 0 )
412- self .assertEqual (self .align (9 ) % self .p , 0 )
406+ Note: This wrapper should only be used if a pointer member is included
407+ and no member with a size larger than a pointer exists.
408+ """
409+ return struct .calcsize (fmt + '0P' )
413410
414411 def test_standardtypes (self ):
415- i = self .i
416- l = self .l
417- p = self .p
418- h = self .headersize
419- # bool
420- self .check_sizeof (True , h + 2 * l )
421- # bytearray
422- self .check_sizeof (bytes (), h + self .align (i ) + l + p )
412+ h = self .header
413+ size = self .calcsize
423414 # cell
424415 def get_cell ():
425416 x = 42
426417 def inner ():
427418 return x
428419 return inner
429- self .check_sizeof (get_cell ().__closure__ [0 ], h + p )
420+ self .check_sizeof (get_cell ().__closure__ [0 ], size ( h + 'P' ) )
430421 # code
431- self .check_sizeof (get_cell ().__code__ , h + self .align (5 * i ) + 8 * p + \
432- self .align (i ) + 2 * p )
422+ self .check_sizeof (get_cell ().__code__ , size (h + '5i8Pi2P' ))
433423 # complex
434- self .check_sizeof (complex (0 ,1 ), h + 2 * 8 )
424+ self .check_sizeof (complex (0 ,1 ), size ( h + '2d' ) )
435425 # enumerate
436- self .check_sizeof (enumerate ([]), h + l + 3 * p )
426+ self .check_sizeof (enumerate ([]), size ( h + 'l3P' ) )
437427 # reverse
438- self .check_sizeof (reversed ('' ), h + l + p )
428+ self .check_sizeof (reversed ('' ), size ( h + 'PP' ) )
439429 # float
440- self .check_sizeof (float (0 ), h + 8 )
430+ self .check_sizeof (float (0 ), size ( h + 'd' ) )
441431 # function
442432 def func (): pass
443- self .check_sizeof (func , h + 11 * p )
433+ self .check_sizeof (func , size ( h + '11P' ) )
444434 class c ():
445435 @staticmethod
446436 def foo ():
@@ -449,58 +439,47 @@ def foo():
449439 def bar (cls ):
450440 pass
451441 # staticmethod
452- self .check_sizeof (foo , h + l )
442+ self .check_sizeof (foo , size ( h + 'P' ) )
453443 # classmethod
454- self .check_sizeof (bar , h + l )
444+ self .check_sizeof (bar , size ( h + 'P' ) )
455445 # generator
456446 def get_gen (): yield 1
457- self .check_sizeof (get_gen (), h + p + self . align ( i ) + 2 * p )
447+ self .check_sizeof (get_gen (), size ( h + 'Pi2P' ) )
458448 # builtin_function_or_method
459- self .check_sizeof (abs , h + 3 * p )
449+ self .check_sizeof (abs , size ( h + '3P' ) )
460450 # module
461- self .check_sizeof (unittest , h + 3 * p )
451+ self .check_sizeof (unittest , size ( h + '3P' ) )
462452 # range
463- self .check_sizeof (range (1 ), h + 3 * p )
453+ self .check_sizeof (range (1 ), size ( h + '3P' ) )
464454 # slice
465- self .check_sizeof (slice (0 ), h + 3 * p )
455+ self .check_sizeof (slice (0 ), size ( h + '3P' ) )
466456
467- h += l
457+ h += 'P'
458+ # bool
459+ self .check_sizeof (True , size (h + 'H' ))
468460 # new-style class
469461 class class_newstyle (object ):
470462 def method ():
471463 pass
472464 # type (PyTypeObject + PyNumberMethods + PyMappingMethods +
473465 # PySequenceMethods + PyBufferProcs)
474- self .check_sizeof (class_newstyle , h +
475- # PyTypeObject
476- p + 2 * l + 15 * p + l + 4 * p + l + 9 * p + l + 11 * p +
477- self .align (4 ) +
478- # PyNumberMethods
479- 16 * p + self .align (i ) + 17 * p +
480- 3 * p + # PyMappingMethods
481- 10 * p + # PySequenceMethods
482- 2 * p + # PyBufferProcs
483- 2 * p ) # *ht_name and *ht_slots
466+ self .check_sizeof (class_newstyle , size (h + 'P2P15Pl4PP9PP11PI' ) + \
467+ size ('16Pi17P 3P 10P 2P 2P' ))
484468
485469 def test_specialtypes (self ):
486- i = self .i
487- l = self .l
488- p = self .p
489- h = self .headersize
470+ h = self .header
471+ size = self .calcsize
490472 # dict
491- self .check_sizeof ({}, h + 3 * l + 3 * p + 8 * ( l + 2 * p ))
473+ self .check_sizeof ({}, size ( h + '3P2P' ) + 8 * size ( 'P2P' ))
492474 longdict = {1 :1 , 2 :2 , 3 :3 , 4 :4 , 5 :5 , 6 :6 , 7 :7 , 8 :8 }
493- self .check_sizeof (longdict , h + 3 * l + 3 * p + 8 * (l + 2 * p ) + 16 * (l + 2 * p ))
494- # list
495- self .check_sizeof ([], h + l + p + l )
496- self .check_sizeof ([1 , 2 , 3 ], h + l + p + l + 3 * l )
475+ self .check_sizeof (longdict , size (h + '3P2P' ) + (8 + 16 )* size ('P2P' ))
497476 # unicode
498477 usize = len ('\0 ' .encode ('unicode-internal' ))
499478 samples = ['' , '1' * 100 ]
500479 # we need to test for both sizes, because we don't know if the string
501480 # has been cached
502481 for s in samples :
503- basicsize = h + l + p + l + l + p + usize * (len (s ) + 1 )
482+ basicsize = size ( h + 'PPliP' ) + usize * (len (s ) + 1 )
504483 defenc = bytes (s , 'ascii' )
505484 self .check_sizeof (s , basicsize ,
506485 size2 = basicsize + sys .getsizeof (defenc ))
@@ -512,17 +491,20 @@ def test_specialtypes(self):
512491 finally :
513492 self .check_sizeof (s , basicsize + sys .getsizeof (defenc ))
514493
515- h += l
494+ h += 'P'
495+ # list
496+ self .check_sizeof ([], size (h + 'PP' ))
497+ self .check_sizeof ([1 , 2 , 3 ], size (h + 'PP' ) + 3 * self .P )
516498 # long
517- self .check_sizeof (0 , h + self . align ( 2 ))
518- self .check_sizeof (1 , h + self . align ( 2 ))
519- self .check_sizeof (- 1 , h + self . align ( 2 ))
520- self .check_sizeof (32768 , h + self . align ( 2 ) + 2 )
521- self .check_sizeof (32768 * 32768 - 1 , h + self . align ( 2 ) + 2 )
522- self .check_sizeof (32768 * 32768 , h + self . align ( 2 ) + 4 )
499+ self .check_sizeof (0 , size ( h + 'H' ))
500+ self .check_sizeof (1 , size ( h + 'H' ))
501+ self .check_sizeof (- 1 , size ( h + 'H' ))
502+ self .check_sizeof (32768 , size ( h + 'H' ) + self . H )
503+ self .check_sizeof (32768 * 32768 - 1 , size ( h + 'H' ) + self . H )
504+ self .check_sizeof (32768 * 32768 , size ( h + 'H' ) + 2 * self . H )
523505 # tuple
524- self .check_sizeof ((), h )
525- self .check_sizeof ((1 ,2 ,3 ), h + 3 * p )
506+ self .check_sizeof ((), size ( h ) )
507+ self .check_sizeof ((1 ,2 ,3 ), size ( h ) + 3 * self . P )
526508
527509
528510def test_main ():
0 commit comments