Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
test.test_sys.SizeofTest Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def check_sizeof
def calcsize
def test_gc_head_size
def test_default
def test_objecttypes
def test_pythontypes

Public Attributes

 c
 H
 i
 l
 P
 header
 vheader
 longdigit
 gc_headsize
 file

Static Public Attributes

int TPFLAGS_HAVE_GC = 1
int TPFLAGS_HEAPTYPE = 1

Private Attributes

 __x

Detailed Description

Definition at line 568 of file test_sys.py.


Member Function Documentation

def test.test_sys.SizeofTest.calcsize (   self,
  fmt 
)
Wrapper around struct.calcsize which enforces the alignment of the
end of a structure to the alignment requirement of pointer.

Note: This wrapper should only be used if a pointer member is included
and no member with a size larger than a pointer exists.

Definition at line 605 of file test_sys.py.

00605 
00606     def calcsize(self, fmt):
00607         """Wrapper around struct.calcsize which enforces the alignment of the
00608         end of a structure to the alignment requirement of pointer.
00609 
00610         Note: This wrapper should only be used if a pointer member is included
00611         and no member with a size larger than a pointer exists.
00612         """
00613         return struct.calcsize(fmt + '0P')

Here is the caller graph for this function:

def test.test_sys.SizeofTest.check_sizeof (   self,
  o,
  size 
)

Definition at line 595 of file test_sys.py.

00595 
00596     def check_sizeof(self, o, size):
00597         result = sys.getsizeof(o)
00598         # add GC header size
00599         if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
00600            ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
00601             size += self.gc_headsize
00602         msg = 'wrong size for %s: got %d, expected %d' \
00603               % (type(o), result, size)
00604         self.assertEqual(result, size, msg)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 573 of file test_sys.py.

00573 
00574     def setUp(self):
00575         self.c = len(struct.pack('c', b' '))
00576         self.H = len(struct.pack('H', 0))
00577         self.i = len(struct.pack('i', 0))
00578         self.l = len(struct.pack('l', 0))
00579         self.P = len(struct.pack('P', 0))
00580         # due to missing size_t information from struct, it is assumed that
00581         # sizeof(Py_ssize_t) = sizeof(void*)
00582         self.header = 'PP'
00583         self.vheader = self.header + 'P'
00584         if hasattr(sys, "gettotalrefcount"):
00585             self.header += '2P'
00586             self.vheader += '2P'
00587         self.longdigit = sys.int_info.sizeof_digit
00588         import _testcapi
00589         self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
00590         self.file = open(test.support.TESTFN, 'wb')

Here is the caller graph for this function:

Definition at line 591 of file test_sys.py.

00591 
00592     def tearDown(self):
00593         self.file.close()
00594         test.support.unlink(test.support.TESTFN)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 625 of file test_sys.py.

00625 
00626     def test_default(self):
00627         h = self.header
00628         vh = self.vheader
00629         size = self.calcsize
00630         self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
00631         self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)

Here is the call graph for this function:

Definition at line 614 of file test_sys.py.

00614 
00615     def test_gc_head_size(self):
00616         # Check that the gc header size is added to objects tracked by the gc.
00617         h = self.header
00618         vh = self.vheader
00619         size = self.calcsize
00620         gc_header_size = self.gc_headsize
00621         # bool objects are not gc tracked
00622         self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
00623         # but lists are
00624         self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)

Here is the call graph for this function:

Definition at line 632 of file test_sys.py.

00632 
00633     def test_objecttypes(self):
00634         # check all types defined in Objects/
00635         h = self.header
00636         vh = self.vheader
00637         size = self.calcsize
00638         check = self.check_sizeof
00639         # bool
00640         check(True, size(vh) + self.longdigit)
00641         # buffer
00642         # XXX
00643         # builtin_function_or_method
00644         check(len, size(h + '3P'))
00645         # bytearray
00646         samples = [b'', b'u'*100000]
00647         for sample in samples:
00648             x = bytearray(sample)
00649             check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
00650         # bytearray_iterator
00651         check(iter(bytearray()), size(h + 'PP'))
00652         # cell
00653         def get_cell():
00654             x = 42
00655             def inner():
00656                 return x
00657             return inner
00658         check(get_cell().__closure__[0], size(h + 'P'))
00659         # code
00660         check(get_cell().__code__, size(h + '5i8Pi3P'))
00661         # complex
00662         check(complex(0,1), size(h + '2d'))
00663         # method_descriptor (descriptor object)
00664         check(str.lower, size(h + '2PP'))
00665         # classmethod_descriptor (descriptor object)
00666         # XXX
00667         # member_descriptor (descriptor object)
00668         import datetime
00669         check(datetime.timedelta.days, size(h + '2PP'))
00670         # getset_descriptor (descriptor object)
00671         import collections
00672         check(collections.defaultdict.default_factory, size(h + '2PP'))
00673         # wrapper_descriptor (descriptor object)
00674         check(int.__add__, size(h + '2P2P'))
00675         # method-wrapper (descriptor object)
00676         check({}.__iter__, size(h + '2P'))
00677         # dict
00678         check({}, size(h + '3P2P' + 8*'P2P'))
00679         longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
00680         check(longdict, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
00681         # dictionary-keyiterator
00682         check({}.keys(), size(h + 'P'))
00683         # dictionary-valueiterator
00684         check({}.values(), size(h + 'P'))
00685         # dictionary-itemiterator
00686         check({}.items(), size(h + 'P'))
00687         # dictproxy
00688         class C(object): pass
00689         check(C.__dict__, size(h + 'P'))
00690         # BaseException
00691         check(BaseException(), size(h + '5P'))
00692         # UnicodeEncodeError
00693         check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5P 2P2PP'))
00694         # UnicodeDecodeError
00695         # XXX
00696 #        check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
00697         # UnicodeTranslateError
00698         check(UnicodeTranslateError("", 0, 1, ""), size(h + '5P 2P2PP'))
00699         # ellipses
00700         check(Ellipsis, size(h + ''))
00701         # EncodingMap
00702         import codecs, encodings.iso8859_3
00703         x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
00704         check(x, size(h + '32B2iB'))
00705         # enumerate
00706         check(enumerate([]), size(h + 'l3P'))
00707         # reverse
00708         check(reversed(''), size(h + 'PP'))
00709         # float
00710         check(float(0), size(h + 'd'))
00711         # sys.floatinfo
00712         check(sys.float_info, size(vh) + self.P * len(sys.float_info))
00713         # frame
00714         import inspect
00715         CO_MAXBLOCKS = 20
00716         x = inspect.currentframe()
00717         ncells = len(x.f_code.co_cellvars)
00718         nfrees = len(x.f_code.co_freevars)
00719         extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
00720                   ncells + nfrees - 1
00721         check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
00722         # function
00723         def func(): pass
00724         check(func, size(h + '11P'))
00725         class c():
00726             @staticmethod
00727             def foo():
00728                 pass
00729             @classmethod
00730             def bar(cls):
00731                 pass
00732             # staticmethod
00733             check(foo, size(h + 'P'))
00734             # classmethod
00735             check(bar, size(h + 'P'))
00736         # generator
00737         def get_gen(): yield 1
00738         check(get_gen(), size(h + 'Pi2P'))
00739         # iterator
00740         check(iter('abc'), size(h + 'lP'))
00741         # callable-iterator
00742         import re
00743         check(re.finditer('',''), size(h + '2P'))
00744         # list
00745         samples = [[], [1,2,3], ['1', '2', '3']]
00746         for sample in samples:
00747             check(sample, size(vh + 'PP') + len(sample)*self.P)
00748         # sortwrapper (list)
00749         # XXX
00750         # cmpwrapper (list)
00751         # XXX
00752         # listiterator (list)
00753         check(iter([]), size(h + 'lP'))
00754         # listreverseiterator (list)
00755         check(reversed([]), size(h + 'lP'))
00756         # long
00757         check(0, size(vh))
00758         check(1, size(vh) + self.longdigit)
00759         check(-1, size(vh) + self.longdigit)
00760         PyLong_BASE = 2**sys.int_info.bits_per_digit
00761         check(int(PyLong_BASE), size(vh) + 2*self.longdigit)
00762         check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
00763         check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
00764         # memory
00765         check(memoryview(b''), size(h + 'PP2P2i7P'))
00766         # module
00767         check(unittest, size(h + '3P'))
00768         # None
00769         check(None, size(h + ''))
00770         # NotImplementedType
00771         check(NotImplemented, size(h))
00772         # object
00773         check(object(), size(h + ''))
00774         # property (descriptor object)
00775         class C(object):
00776             def getx(self): return self.__x
00777             def setx(self, value): self.__x = value
00778             def delx(self): del self.__x
00779             x = property(getx, setx, delx, "")
00780             check(x, size(h + '4Pi'))
00781         # PyCapsule
00782         # XXX
00783         # rangeiterator
00784         check(iter(range(1)), size(h + '4l'))
00785         # reverse
00786         check(reversed(''), size(h + 'PP'))
00787         # range
00788         check(range(1), size(h + '4P'))
00789         check(range(66000), size(h + '4P'))
00790         # set
00791         # frozenset
00792         PySet_MINSIZE = 8
00793         samples = [[], range(10), range(50)]
00794         s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
00795         for sample in samples:
00796             minused = len(sample)
00797             if minused == 0: tmp = 1
00798             # the computation of minused is actually a bit more complicated
00799             # but this suffices for the sizeof test
00800             minused = minused*2
00801             newsize = PySet_MINSIZE
00802             while newsize <= minused:
00803                 newsize = newsize << 1
00804             if newsize <= 8:
00805                 check(set(sample), s)
00806                 check(frozenset(sample), s)
00807             else:
00808                 check(set(sample), s + newsize*struct.calcsize('lP'))
00809                 check(frozenset(sample), s + newsize*struct.calcsize('lP'))
00810         # setiterator
00811         check(iter(set()), size(h + 'P3P'))
00812         # slice
00813         check(slice(0), size(h + '3P'))
00814         # super
00815         check(super(int), size(h + '3P'))
00816         # tuple
00817         check((), size(vh))
00818         check((1,2,3), size(vh) + 3*self.P)
00819         # type
00820         # (PyTypeObject + PyNumberMethods + PyMappingMethods +
00821         #  PySequenceMethods + PyBufferProcs)
00822         s = size(vh + 'P2P15Pl4PP9PP11PI') + size('16Pi17P 3P 10P 2P 2P')
00823         check(int, s)
00824         # class
00825         class newstyleclass(object): pass
00826         check(newstyleclass, s)
00827         # unicode
00828         usize = len('\0'.encode('unicode-internal'))
00829         samples = ['', '1'*100]
00830         # we need to test for both sizes, because we don't know if the string
00831         # has been cached
00832         for s in samples:
00833             basicsize =  size(h + 'PPPiP') + usize * (len(s) + 1)
00834             check(s, basicsize)
00835         # weakref
00836         import weakref
00837         check(weakref.ref(int), size(h + '2Pl2P'))
00838         # weakproxy
00839         # XXX
00840         # weakcallableproxy
00841         check(weakref.proxy(int), size(h + '2Pl2P'))

Here is the call graph for this function:

Definition at line 842 of file test_sys.py.

00842 
00843     def test_pythontypes(self):
00844         # check all types defined in Python/
00845         h = self.header
00846         vh = self.vheader
00847         size = self.calcsize
00848         check = self.check_sizeof
00849         # _ast.AST
00850         import _ast
00851         check(_ast.AST(), size(h + ''))
00852         # imp.NullImporter
00853         import imp
00854         check(imp.NullImporter(self.file.name), size(h + ''))
00855         try:
00856             raise TypeError
00857         except TypeError:
00858             tb = sys.exc_info()[2]
00859             # traceback
00860             if tb != None:
00861                 check(tb, size(h + '2P2i'))
00862         # symtable entry
00863         # XXX
00864         # sys.flags
00865         check(sys.flags, size(vh) + self.P * len(sys.flags))
00866 

Here is the call graph for this function:


Member Data Documentation

Definition at line 776 of file test_sys.py.

Definition at line 574 of file test_sys.py.

Definition at line 589 of file test_sys.py.

Definition at line 588 of file test_sys.py.

Definition at line 575 of file test_sys.py.

Definition at line 581 of file test_sys.py.

Definition at line 576 of file test_sys.py.

Definition at line 577 of file test_sys.py.

Definition at line 586 of file test_sys.py.

Definition at line 578 of file test_sys.py.

Definition at line 570 of file test_sys.py.

Definition at line 571 of file test_sys.py.

Definition at line 582 of file test_sys.py.


The documentation for this class was generated from the following file: