<s> import gc <EOL> import os <EOL> import sys <EOL> import unittest <EOL> import weakref <EOL> import pympler . asizeof as asizeof <EOL> from collections import deque <EOL> from inspect import stack <EOL> def test_flatsize ( failf = None , stdf = None ) : <EOL> '''<STR_LIT>''' <EOL> t , g , e = [ ] , asizeof . _getsizeof , <NUM_LIT:0> <EOL> if g : <EOL> for v in asizeof . _typedefs . copy ( ) . values ( ) : <EOL> t . append ( v . type ) <EOL> try : <EOL> if v . type . __module__ not in ( '<STR_LIT>' , ) : <EOL> t . append ( v . type ( ) ) <EOL> except Exception : <EOL> pass <EOL> t . extend ( ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> , <NUM_LIT:4> : <NUM_LIT:4> , <NUM_LIT:5> : <NUM_LIT:5> , <NUM_LIT:6> : <NUM_LIT:6> , <NUM_LIT:7> : <NUM_LIT:7> , <NUM_LIT:8> : <NUM_LIT:8> } , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , [ <NUM_LIT:0> ] * <NUM_LIT:100> , <EOL> '<STR_LIT>' , '<STR_LIT:x>' * <NUM_LIT> , <EOL> ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) , ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ) , ( <NUM_LIT:0> , ) * <NUM_LIT:100> , <EOL> asizeof . _Slots ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) ) , <EOL> asizeof . _Slots ( ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ) ) , <EOL> asizeof . _Slots ( ( <NUM_LIT:0> , ) * <NUM_LIT:100> ) , <EOL> <NUM_LIT:0> , <NUM_LIT:1> << <NUM_LIT:8> , <NUM_LIT:1> << <NUM_LIT:16> , <NUM_LIT:1> << <NUM_LIT:32> , <NUM_LIT:1> << <NUM_LIT:64> , <NUM_LIT:1> << <NUM_LIT> , <EOL> complex ( <NUM_LIT:0> , <NUM_LIT:1> ) , True , False ) ) <EOL> asizeof . _getsizeof = None <EOL> for o in t : <EOL> a = asizeof . flatsize ( o ) <EOL> s = sys . getsizeof ( o , <NUM_LIT:0> ) <EOL> if a != s : <EOL> if isinstance ( o , ( dict , list , set , frozenset , tuple , bytes ) ) : <EOL> x = '<STR_LIT>' <EOL> elif ( isinstance ( o , ( type , bool , asizeof . ABCMeta ) ) and <EOL> sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) ) : <EOL> x = '<STR_LIT>' <EOL> elif isinstance ( o , str ) and sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:3> ) : <EOL> x = '<STR_LIT>' <EOL> elif isinstance ( o , deque ) and sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> x = '<STR_LIT>' <EOL> else : <EOL> x = '<STR_LIT>' % asizeof . _typedefof ( o ) <EOL> e += <NUM_LIT:1> <EOL> if failf : <EOL> failf ( '<STR_LIT>' , <EOL> a , s , asizeof . _nameof ( type ( o ) ) , _repr ( o ) ) <EOL> if stdf : <EOL> asizeof . _printf ( '<STR_LIT>' , <EOL> a , s , asizeof . _nameof ( type ( o ) ) , _repr ( o ) , x , file = stdf ) <EOL> asizeof . _getsizeof = g <EOL> return len ( t ) , e <EOL> if hasattr ( sys , '<STR_LIT>' ) : <EOL> class AsizeofTest ( unittest . TestCase ) : <EOL> def _failf ( self , fmt , * args ) : <EOL> self . fail ( fmt % args ) <EOL> def test_flatsize_vs_getsizeof ( self ) : <EOL> '''<STR_LIT>''' <EOL> n , e = test_flatsize ( failf = self . _failf ) <EOL> self . assert_ ( n , msg = '<STR_LIT>' % test_flatsize ) <EOL> self . assert_ ( not e , msg = '<STR_LIT>' % ( e , n ) ) <EOL> else : <EOL> class AsizeofTest ( unittest . TestCase ) : <EOL> def test_flatsize ( self ) : <EOL> '''<STR_LIT>''' <EOL> l = [ "<STR_LIT>" , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , "<STR_LIT>" , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] <EOL> for _type in ( list , tuple , set , frozenset ) : <EOL> data = _type ( l ) <EOL> bsz = asizeof . basicsize ( data ) <EOL> isz = asizeof . itemsize ( data ) <EOL> lng = asizeof . leng ( data ) <EOL> fsz = asizeof . flatsize ( data ) <EOL> self . assertEqual ( fsz , bsz + ( lng * isz ) , ( fsz , bsz , lng , isz ) ) <EOL> self . assertRaises ( ValueError , asizeof . flatsize , l , ** { '<STR_LIT>' : <NUM_LIT:3> } ) <EOL> class Foo ( object ) : <EOL> def __init__ ( self , content ) : <EOL> self . data = content <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class ThinFoo ( object ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , content ) : <EOL> self . tdata = content <EOL> class OldFoo : <EOL> def __init__ ( self , content ) : <EOL> self . odata = content <EOL> class PseudoDict ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:0> <EOL> def get ( self , * args , ** kwargs ) : <EOL> return None <EOL> def has_key ( self , * args , ** kwargs ) : <EOL> return False <EOL> def items ( self ) : <EOL> return [ ] <EOL> def keys ( self ) : <EOL> return [ ] <EOL> def values ( self ) : <EOL> return [ ] <EOL> class TypesTest ( unittest . TestCase ) : <EOL> def test_generator ( self ) : <EOL> '''<STR_LIT>''' <EOL> def infinite_gen ( ) : <EOL> i = <NUM_LIT:1> <EOL> while True : <EOL> yield i <EOL> i += <NUM_LIT:1> <EOL> gen = infinite_gen ( ) <EOL> s1 = asizeof . asizeof ( gen , code = True ) <EOL> for i in gen : <EOL> self . assertEqual ( i , <NUM_LIT:1> ) <EOL> break <EOL> for i in gen : <EOL> self . assertEqual ( i , <NUM_LIT:2> ) <EOL> break <EOL> s2 = asizeof . asizeof ( gen , code = True ) <EOL> s3 = asizeof . asizeof ( gen , code = False ) <EOL> for i in gen : <EOL> self . assertEqual ( i , <NUM_LIT:3> ) <EOL> break <EOL> self . assertEqual ( s1 , s2 ) <EOL> self . assertEqual ( s3 , <NUM_LIT:0> ) <EOL> def test_methods ( self ) : <EOL> '''<STR_LIT>''' <EOL> def foo ( ) : <EOL> pass <EOL> s1 = asizeof . asizeof ( self . test_methods , code = True ) <EOL> s2 = asizeof . asizeof ( TypesTest . test_methods , code = True ) <EOL> s3 = asizeof . asizeof ( foo , code = True ) <EOL> def test_classes ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . assert_ ( asizeof . asizeof ( Foo , code = True ) > <NUM_LIT:0> ) <EOL> self . assert_ ( asizeof . asizeof ( ThinFoo , code = True ) > <NUM_LIT:0> ) <EOL> self . assert_ ( asizeof . asizeof ( OldFoo , code = True ) > <NUM_LIT:0> ) <EOL> self . assert_ ( asizeof . asizeof ( Foo ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) > <NUM_LIT:0> ) <EOL> self . assert_ ( asizeof . asizeof ( ThinFoo ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) > <NUM_LIT:0> ) <EOL> self . assert_ ( asizeof . asizeof ( OldFoo ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) > <NUM_LIT:0> ) <EOL> s1 = asizeof . asizeof ( Foo ( "<STR_LIT>" ) ) <EOL> s2 = asizeof . asizeof ( Foo ( "<STR_LIT>" ) ) <EOL> self . assert_ ( s2 >= s1 ) <EOL> s3 = asizeof . asizeof ( ThinFoo ( "<STR_LIT>" ) ) <EOL> self . assert_ ( s3 <= s1 ) <EOL> def test_special_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> module_size = asizeof . asizeof ( unittest ) <EOL> self . assertTrue ( module_size > <NUM_LIT:0> , module_size ) <EOL> def test_enumerate ( self ) : <EOL> '''<STR_LIT>''' <EOL> enum = enumerate ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> enum_size = asizeof . asizeof ( enum ) <EOL> self . assertTrue ( enum_size > <NUM_LIT:0> , enum_size ) <EOL> refs = asizeof . named_refs ( enum ) <EOL> ref_names = set ( [ name for name , _ in refs ] ) <EOL> self . assertTrue ( set ( [ '<STR_LIT>' ] ) <= ref_names , ref_names ) <EOL> def test_array ( self ) : <EOL> from array import array <EOL> arr = array ( '<STR_LIT:i>' , [ <NUM_LIT:0> ] * <NUM_LIT:100> ) <EOL> arr_size = asizeof . asizeof ( arr ) <EOL> buf_size = arr . buffer_info ( ) [ <NUM_LIT:1> ] * arr . itemsize <EOL> self . assertTrue ( arr_size >= buf_size , ( arr_size , buf_size ) ) <EOL> def test_weakref ( self ) : <EOL> '''<STR_LIT>''' <EOL> alive = Foo ( '<STR_LIT>' ) <EOL> aref = weakref . ref ( alive ) <EOL> dead = Foo ( '<STR_LIT>' ) <EOL> dref = weakref . ref ( dead ) <EOL> del dead <EOL> aref_size = asizeof . asizeof ( aref ) <EOL> self . assertTrue ( aref_size > asizeof . asizeof ( alive ) , aref_size ) <EOL> refs = asizeof . named_refs ( aref ) <EOL> dref_size = asizeof . asizeof ( dref ) <EOL> self . assertTrue ( dref_size > <NUM_LIT:0> , dref_size ) <EOL> self . assertNotEqual ( dref_size , aref_size ) <EOL> refs = asizeof . named_refs ( dref ) <EOL> def test_os_stat ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> stat = os . stat ( __file__ ) <EOL> except Exception : <EOL> pass <EOL> else : <EOL> stat_size = asizeof . asizeof ( stat ) <EOL> self . assertTrue ( stat_size > <NUM_LIT:0> , stat_size ) <EOL> refs = asizeof . named_refs ( stat ) <EOL> ref_names = set ( [ name for name , _ in refs ] ) <EOL> self . assertTrue ( set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <= ref_names , ref_names ) <EOL> try : <EOL> stat = os . statvfs ( __file__ ) <EOL> except Exception : <EOL> pass <EOL> else : <EOL> stat_size = asizeof . asizeof ( stat ) <EOL> self . assertTrue ( stat_size > <NUM_LIT:0> , stat_size ) <EOL> refs = asizeof . named_refs ( stat ) <EOL> ref_names = set ( [ name for name , _ in refs ] ) <EOL> self . assertTrue ( set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <= ref_names , ref_names ) <EOL> def test_exception ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except Exception : <EOL> etype , exc , etb = sys . exc_info ( ) <EOL> try : <EOL> tb_size = asizeof . asizeof ( etb ) <EOL> self . assertTrue ( tb_size > <NUM_LIT:0> , tb_size ) <EOL> refs = asizeof . named_refs ( etb ) <EOL> ref_names = set ( [ name for name , _ in refs ] ) <EOL> self . assertTrue ( set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <= ref_names , ref_names ) <EOL> ex_size = asizeof . asizeof ( etype , exc ) <EOL> self . assertTrue ( ex_size > <NUM_LIT:0> , ex_size ) <EOL> finally : <EOL> del etb <EOL> def test_ignore_frame ( self ) : <EOL> '''<STR_LIT>''' <EOL> gc . collect ( ) <EOL> gc . disable ( ) <EOL> s = asizeof . asizeof ( all = True , code = True ) <EOL> self . assertEqual ( gc . collect ( ) , <NUM_LIT:0> ) <EOL> gc . enable ( ) <EOL> def test_closure ( self ) : <EOL> '''<STR_LIT>''' <EOL> def outer ( x ) : <EOL> def inner ( ) : <EOL> return x <EOL> return inner <EOL> data = [ <NUM_LIT:1> ] * <NUM_LIT:1000> <EOL> closure = outer ( data ) <EOL> size_closure = asizeof . asizeof ( closure , code = True ) <EOL> size_data = asizeof . asizeof ( data ) <EOL> self . assertTrue ( size_closure >= size_data , ( size_closure , size_data ) ) <EOL> def test_namedtuple ( self ) : <EOL> '''<STR_LIT>''' <EOL> from collections import namedtuple <EOL> Point = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ) <EOL> point = Point ( x = <NUM_LIT:11> , y = <NUM_LIT> ) <EOL> size = asizeof . asized ( point , detail = <NUM_LIT:1> ) <EOL> refs = [ ref . name for ref in size . refs ] <EOL> self . assertTrue ( '<STR_LIT>' not in refs , refs ) <EOL> class FunctionTest ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> def test_asized ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . assertEqual ( list ( asizeof . asized ( detail = <NUM_LIT:2> ) ) , [ ] ) <EOL> self . assertRaises ( KeyError , asizeof . asized , ** { '<STR_LIT:all>' : True } ) <EOL> sized = asizeof . asized ( Foo ( <NUM_LIT> ) , detail = <NUM_LIT:2> ) <EOL> self . assertEqual ( sized . name , '<STR_LIT>' ) <EOL> refs = [ ref for ref in sized . refs if ref . name == '<STR_LIT>' ] <EOL> self . assertEqual ( len ( refs ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( refs [ <NUM_LIT:0> ] , sized . get ( '<STR_LIT>' ) ) <EOL> refs = [ ref for ref in refs [ <NUM_LIT:0> ] . refs if ref . name == '<STR_LIT>' ] <EOL> self . assertEqual ( len ( refs ) , <NUM_LIT:1> , refs ) <EOL> i = <NUM_LIT> <EOL> self . assertEqual ( refs [ <NUM_LIT:0> ] . size , asizeof . asizeof ( i ) , refs [ <NUM_LIT:0> ] . size ) <EOL> sizer = asizeof . Asizer ( ) <EOL> sized_objs = sizer . asized ( Foo ( <NUM_LIT:3> ) , Foo ( <NUM_LIT:4> ) , detail = <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( sized_objs ) , <NUM_LIT:2> ) <EOL> def test_asized_detail ( self ) : <EOL> foo = Foo ( <NUM_LIT> ) <EOL> size1 = asizeof . asized ( foo , detail = <NUM_LIT:1> ) <EOL> size2 = asizeof . asized ( foo , detail = <NUM_LIT:2> ) <EOL> self . assertEqual ( size1 . size , size2 . size ) <EOL> def test_asized_format ( self ) : <EOL> '''<STR_LIT>''' <EOL> foo = Foo ( <NUM_LIT> ) <EOL> sized1 = asizeof . asized ( foo , detail = <NUM_LIT:1> ) <EOL> sized2 = asizeof . asized ( foo , detail = <NUM_LIT:2> ) <EOL> sized1_no = sized1 . format ( '<STR_LIT>' , order_by = '<STR_LIT:name>' ) <EOL> sized1_d1 = sized1 . format ( '<STR_LIT>' , depth = <NUM_LIT:1> , order_by = '<STR_LIT:name>' ) <EOL> sized1_d2 = sized1 . format ( '<STR_LIT>' , depth = <NUM_LIT:2> , order_by = '<STR_LIT:name>' ) <EOL> sized2_d1 = sized2 . format ( '<STR_LIT>' , depth = <NUM_LIT:1> , order_by = '<STR_LIT:name>' ) <EOL> sized2_d2 = sized2 . format ( '<STR_LIT>' , depth = <NUM_LIT:2> , order_by = '<STR_LIT:name>' ) <EOL> self . assertEqual ( sized1_no , "<STR_LIT>" ) <EOL> self . assertEqual ( sized1_no , sized1_d1 ) <EOL> self . assertEqual ( sized1_no , sized1_d2 ) <EOL> self . assertEqual ( sized1_d1 , sized2_d1 ) <EOL> self . assertNotEqual ( sized2_d1 , sized2_d2 ) <EOL> def test_asizesof ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . assertEqual ( list ( asizeof . asizesof ( ) ) , [ ] ) <EOL> self . assertRaises ( KeyError , asizeof . asizesof , ** { '<STR_LIT:all>' : True } ) <EOL> objs = [ Foo ( <NUM_LIT> ) , ThinFoo ( "<STR_LIT>" ) , OldFoo ( <NUM_LIT> ) ] <EOL> sizes = list ( asizeof . asizesof ( * objs ) ) <EOL> objs . reverse ( ) <EOL> rsizes = list ( asizeof . asizesof ( * objs ) ) <EOL> self . assertEqual ( len ( sizes ) , <NUM_LIT:3> ) <EOL> rsizes . reverse ( ) <EOL> self . assertEqual ( sizes , rsizes , ( sizes , rsizes ) ) <EOL> objs . reverse ( ) <EOL> isizes = [ asizeof . asizeof ( obj ) for obj in objs ] <EOL> self . assertEqual ( sizes , isizes ) <EOL> sizer = asizeof . Asizer ( ) <EOL> asizer_sizes = sizer . asizesof ( * objs ) <EOL> self . assertEqual ( list ( asizer_sizes ) , sizes ) <EOL> code_sizes = sizer . asizesof ( * objs , ** dict ( code = True ) ) <EOL> self . failIfEqual ( list ( code_sizes ) , sizes ) <EOL> def test_asizeof ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . assertEqual ( asizeof . asizeof ( ) , <NUM_LIT:0> ) <EOL> objs = [ Foo ( <NUM_LIT> ) , ThinFoo ( "<STR_LIT>" ) , OldFoo ( <NUM_LIT> ) ] <EOL> total = asizeof . asizeof ( * objs ) <EOL> sizes = list ( asizeof . asizesof ( * objs ) ) <EOL> sum = <NUM_LIT:0> <EOL> for sz in sizes : <EOL> sum += sz <EOL> self . assertEqual ( total , sum , ( total , sum ) ) <EOL> def test_asizer_limit ( self ) : <EOL> '''<STR_LIT>''' <EOL> objs = [ Foo ( <NUM_LIT> ) , ThinFoo ( "<STR_LIT>" ) , OldFoo ( <NUM_LIT> ) ] <EOL> sizer = [ asizeof . Asizer ( ) for _ in range ( <NUM_LIT:4> ) ] <EOL> for limit , asizer in enumerate ( sizer ) : <EOL> asizer . asizeof ( objs , limit = limit ) <EOL> limit_sizes = [ asizer . total for asizer in sizer ] <EOL> self . assertTrue ( limit_sizes [ <NUM_LIT:0> ] < limit_sizes [ <NUM_LIT:1> ] , limit_sizes ) <EOL> self . assertTrue ( limit_sizes [ <NUM_LIT:1> ] < limit_sizes [ <NUM_LIT:2> ] , limit_sizes ) <EOL> self . assertTrue ( limit_sizes [ <NUM_LIT:2> ] < limit_sizes [ <NUM_LIT:3> ] , limit_sizes ) <EOL> def test_basicsize ( self ) : <EOL> '''<STR_LIT>''' <EOL> objects = [ <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT:a>' , True , None ] <EOL> for o in objects : <EOL> self . assertEqual ( asizeof . basicsize ( o ) , type ( o ) . __basicsize__ ) <EOL> objects = [ [ ] , ( ) , { } ] <EOL> for o in objects : <EOL> self . assertEqual ( asizeof . basicsize ( o ) - asizeof . _sizeof_CPyGC_Head , <EOL> type ( o ) . __basicsize__ ) <EOL> l1 = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> l2 = [ "<STR_LIT>" , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , "<STR_LIT>" , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] <EOL> self . assertEqual ( asizeof . basicsize ( l1 ) , asizeof . basicsize ( l2 ) ) <EOL> def test_itemsize ( self ) : <EOL> '''<STR_LIT>''' <EOL> objects = [ <NUM_LIT:1> , True , None , ( ) ] <EOL> for o in objects : <EOL> self . assertEqual ( asizeof . itemsize ( o ) , type ( o ) . __itemsize__ ) <EOL> itemsizes = [ ( { } , asizeof . _sizeof_CPyDictEntry ) , <EOL> ( set ( ) , asizeof . _sizeof_Csetentry ) , <EOL> ] <EOL> for o , itemsize in itemsizes : <EOL> self . assertEqual ( asizeof . itemsize ( o ) , itemsize ) <EOL> def test_leng ( self ) : <EOL> '''<STR_LIT>''' <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> s = "<STR_LIT>" <EOL> self . assert_ ( asizeof . leng ( l ) >= len ( l ) , asizeof . leng ( l ) ) <EOL> self . assertEqual ( asizeof . leng ( tuple ( l ) ) , len ( l ) ) <EOL> self . assert_ ( asizeof . leng ( set ( l ) ) >= len ( set ( l ) ) ) <EOL> self . assert_ ( asizeof . leng ( s ) >= len ( s ) ) <EOL> self . assert_ ( asizeof . leng ( <NUM_LIT> ) in [ None , <NUM_LIT:1> ] , asizeof . leng ( <NUM_LIT> ) ) <EOL> base = <NUM_LIT:2> <EOL> try : <EOL> base = long ( base ) <EOL> except NameError : <EOL> pass <EOL> self . assertEqual ( asizeof . leng ( base ** <NUM_LIT:8> - <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( asizeof . leng ( base ** <NUM_LIT:16> - <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> self . assert_ ( asizeof . leng ( base ** <NUM_LIT:32> - <NUM_LIT:1> ) >= <NUM_LIT:1> ) <EOL> self . assert_ ( asizeof . leng ( base ** <NUM_LIT:64> - <NUM_LIT:1> ) >= <NUM_LIT:2> ) <EOL> def test_refs ( self ) : <EOL> '''<STR_LIT>''' <EOL> f = Foo ( <NUM_LIT> ) <EOL> refs = list ( asizeof . refs ( f ) ) <EOL> self . assert_ ( len ( refs ) >= <NUM_LIT:1> , len ( refs ) ) <EOL> self . assert_ ( { '<STR_LIT:data>' : <NUM_LIT> } in refs , refs ) <EOL> f = OldFoo ( <NUM_LIT> ) <EOL> refs = list ( asizeof . refs ( f ) ) <EOL> self . assert_ ( len ( refs ) >= <NUM_LIT:1> , len ( refs ) ) <EOL> self . assert_ ( { '<STR_LIT>' : <NUM_LIT> } in refs , refs ) <EOL> f = ThinFoo ( <NUM_LIT> ) <EOL> refs = list ( asizeof . refs ( f ) ) <EOL> self . assert_ ( len ( refs ) >= <NUM_LIT:2> , len ( refs ) ) <EOL> self . assert_ ( <NUM_LIT> in refs , refs ) <EOL> self . assert_ ( ( '<STR_LIT>' , ) in refs , refs ) <EOL> def test_exclude_types ( self ) : <EOL> '''<STR_LIT>''' <EOL> sizer = asizeof . Asizer ( ) <EOL> sizer . exclude_types ( Foo ) <EOL> self . assertEqual ( sizer . asizeof ( Foo ( '<STR_LIT>' ) ) , <NUM_LIT:0> ) <EOL> def test_asizer ( self ) : <EOL> '''<STR_LIT>''' <EOL> sizer = asizeof . Asizer ( ) <EOL> obj = '<STR_LIT>' <EOL> mutable = [ obj ] <EOL> sizer . asizeof ( obj ) <EOL> self . assertEqual ( sizer . total , asizeof . asizeof ( obj ) ) <EOL> sizer . asizeof ( mutable , mutable ) <EOL> self . assertEqual ( sizer . duplicate , <NUM_LIT:1> ) <EOL> self . assertEqual ( sizer . total , asizeof . asizeof ( obj , mutable ) ) <EOL> def test_adict ( self ) : <EOL> '''<STR_LIT>''' <EOL> pdict = PseudoDict ( ) <EOL> size1 = asizeof . asizeof ( pdict ) <EOL> asizeof . adict ( PseudoDict ) <EOL> size2 = asizeof . asizeof ( pdict ) <EOL> self . assertEqual ( size1 , size2 ) <EOL> def test_private_slots ( self ) : <EOL> class PrivateSlot ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , data ) : <EOL> self . __data = data <EOL> data = [ <NUM_LIT> ] * <NUM_LIT:100> <EOL> container = PrivateSlot ( data ) <EOL> size1 = asizeof . asizeof ( container ) <EOL> size2 = asizeof . asizeof ( data ) <EOL> self . assertTrue ( size1 > size2 , ( size1 , size2 ) ) <EOL> def _repr ( o ) : <EOL> return repr ( o ) <EOL> class AsizeofDemos ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> MAX = sys . getrecursionlimit ( ) <EOL> class DevNull ( object ) : <EOL> def write ( self , text ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> self . _stdout = sys . stdout <EOL> sys . stdout = self . DevNull ( ) <EOL> def tearDown ( self ) : <EOL> sys . stdout = self . _stdout <EOL> def _printf ( self , * args ) : <EOL> pass <EOL> def _print_asizeof ( self , obj , infer = False , stats = <NUM_LIT:0> ) : <EOL> a = [ _repr ( obj ) , ] <EOL> for d , c in ( ( <NUM_LIT:0> , False ) , ( self . MAX , False ) , ( self . MAX , True ) ) : <EOL> a . append ( asizeof . asizeof ( obj , limit = d , code = c , infer = infer , stats = stats ) ) <EOL> self . _printf ( "<STR_LIT>" , * a ) <EOL> def _print_functions ( self , obj , name = None , align = <NUM_LIT:8> , detail = MAX , code = False , limit = MAX , <EOL> opt = '<STR_LIT>' , ** unused ) : <EOL> if name : <EOL> self . _printf ( '<STR_LIT>' , os . linesep , name , opt ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , asizeof . basicsize ( obj ) ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , asizeof . itemsize ( obj ) ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , asizeof . leng ( obj ) ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , _repr ( asizeof . refs ( obj ) ) ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , asizeof . flatsize ( obj , align = align ) ) <EOL> self . _printf ( '<STR_LIT>' , '<STR_LIT>' , asizeof . asized ( obj , align = align , detail = detail , code = code , limit = limit ) ) <EOL> class C : pass <EOL> class D ( dict ) : <EOL> _attr1 = None <EOL> _attr2 = None <EOL> class E ( D ) : <EOL> def __init__ ( self , a1 = <NUM_LIT:1> , a2 = <NUM_LIT:2> ) : <EOL> self . _attr1 = a1 <EOL> self . _attr2 = a2 <EOL> class P ( object ) : <EOL> _p = None <EOL> def _get_p ( self ) : <EOL> return self . _p <EOL> p = property ( _get_p ) <EOL> class O : <EOL> a = None <EOL> b = None <EOL> class S ( object ) : <EOL> __slots__ = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> class T ( object ) : <EOL> __slots__ = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> def __init__ ( self ) : <EOL> self . a = self . b = <NUM_LIT:0> <EOL> def test_all ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , True , '<STR_LIT>' , '<STR_LIT>' ) <EOL> asizeof . asizeof ( limit = self . MAX , code = True , stats = self . MAX , all = True ) <EOL> def test_basic ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for o in ( None , True , False , <EOL> <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' , <EOL> { } , ( ) , [ ] ) : <EOL> self . _print_asizeof ( o , infer = True ) <EOL> def test_class ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for o in ( self . C ( ) , self . C . __dict__ , <EOL> self . D ( ) , self . D . __dict__ , <EOL> self . E ( ) , self . E . __dict__ , <EOL> self . P ( ) , self . P . __dict__ , self . P . p , <EOL> self . O ( ) , self . O . __dict__ , <EOL> self . S ( ) , self . S . __dict__ , <EOL> self . S ( ) , self . S . __dict__ , <EOL> self . T ( ) , self . T . __dict__ ) : <EOL> self . _print_asizeof ( o , infer = True ) <EOL> def test_code ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for o in ( self . C , self . D , self . E , self . P , self . S , self . T , <EOL> type , <EOL> asizeof . _co_refs , asizeof . _dict_refs , asizeof . _inst_refs , asizeof . _len_int , asizeof . _seq_refs , lambda x : x , <EOL> ( asizeof . _co_refs , asizeof . _dict_refs , asizeof . _inst_refs , asizeof . _len_int , asizeof . _seq_refs ) , <EOL> asizeof . _typedefs ) : <EOL> self . _print_asizeof ( o ) <EOL> def test_dict ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> import UserDict <EOL> for o in ( UserDict . IterableUserDict ( ) , UserDict . UserDict ( ) ) : <EOL> self . _print_asizeof ( o ) <EOL> except ImportError : <EOL> pass <EOL> class _Dict ( dict ) : <EOL> pass <EOL> for o in ( dict ( ) , _Dict ( ) , <EOL> self . P . __dict__ , <EOL> weakref . WeakKeyDictionary ( ) , weakref . WeakValueDictionary ( ) , <EOL> asizeof . _typedefs ) : <EOL> self . _print_asizeof ( o , infer = True ) <EOL> def test_generator ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , True , '<STR_LIT>' ) <EOL> def gen ( x ) : <EOL> i = <NUM_LIT:0> <EOL> while i < x : <EOL> yield i <EOL> i += <NUM_LIT:1> <EOL> a = gen ( <NUM_LIT:5> ) <EOL> b = gen ( <NUM_LIT:50> ) <EOL> asizeof . asizeof ( a , code = True , stats = <NUM_LIT:1> ) <EOL> asizeof . asizeof ( b , code = True , stats = <NUM_LIT:1> ) <EOL> asizeof . asizeof ( a , code = True , stats = <NUM_LIT:1> ) <EOL> def test_globals ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> asizeof . asizeof ( globals ( ) , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> self . _print_functions ( globals ( ) , '<STR_LIT>' , opt = '<STR_LIT>' ) <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> asizeof . asizesof ( globals ( ) , locals ( ) , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> asizeof . asized ( globals ( ) , align = <NUM_LIT:0> , detail = self . MAX , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> def test_long ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> _L5d = long ( <NUM_LIT:1> ) << <NUM_LIT:64> <EOL> _L17d = long ( <NUM_LIT:1> ) << <NUM_LIT> <EOL> t = '<STR_LIT>' <EOL> except NameError : <EOL> _L5d = <NUM_LIT:1> << <NUM_LIT:64> <EOL> _L17d = <NUM_LIT:1> << <NUM_LIT> <EOL> t = '<STR_LIT>' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , t , <NUM_LIT:0> , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> for o in ( <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> self . MAX , <NUM_LIT:1> << <NUM_LIT:32> , _L5d , - _L5d , _L17d , - _L17d ) : <EOL> self . _printf ( "<STR_LIT>" , _repr ( o ) , asizeof . asizeof ( o , align = <NUM_LIT:0> , limit = <NUM_LIT:0> ) , <EOL> asizeof . basicsize ( o ) , asizeof . leng ( o ) , asizeof . itemsize ( o ) ) <EOL> def test_iterator ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> o = iter ( '<STR_LIT>' ) <EOL> e = iter ( '<STR_LIT>' ) <EOL> d = iter ( { } ) <EOL> i = iter ( asizeof . _items ( { <NUM_LIT:1> : <NUM_LIT:1> } ) ) <EOL> k = iter ( asizeof . _keys ( { <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) ) <EOL> v = iter ( asizeof . _values ( { <NUM_LIT:4> : <NUM_LIT:4> , <NUM_LIT:5> : <NUM_LIT:5> , <NUM_LIT:6> : <NUM_LIT:6> } ) ) <EOL> l = iter ( [ ] ) <EOL> t = iter ( ( ) ) <EOL> asizeof . asizesof ( o , e , d , i , k , v , l , t , limit = <NUM_LIT:0> , code = False , stats = <NUM_LIT:1> ) <EOL> asizeof . asizesof ( o , e , d , i , k , v , l , t , limit = <NUM_LIT:9> , code = False , stats = <NUM_LIT:1> ) <EOL> def test_locals ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> asizeof . asizeof ( locals ( ) , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> self . _print_functions ( locals ( ) , '<STR_LIT>' , opt = '<STR_LIT>' ) <EOL> def test_pairs ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> n = m = <NUM_LIT:200> <EOL> p = { } <EOL> for i in range ( n ) : <EOL> q = { } <EOL> for j in range ( m ) : <EOL> q [ j ] = None <EOL> p [ i ] = q <EOL> p = asizeof . asizeof ( p , stats = <NUM_LIT:1> ) <EOL> t = { } <EOL> for i in range ( n ) : <EOL> for j in range ( m ) : <EOL> t [ ( i , j ) ] = None <EOL> t = asizeof . asizeof ( t , stats = <NUM_LIT:1> ) <EOL> self . _printf ( '<STR_LIT>' , os . linesep , asizeof . _p100 ( p , t ) ) <EOL> def test_slots ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> class Old : <EOL> pass <EOL> class New ( object ) : <EOL> __slots__ = ( '<STR_LIT:n>' , ) <EOL> class Sub ( New ) : <EOL> __slots__ = { '<STR_LIT:s>' : '<STR_LIT>' } <EOL> def __init__ ( self ) : <EOL> New . __init__ ( self ) <EOL> o , n , s = Old ( ) , New ( ) , Sub ( ) <EOL> asizeof . asizesof ( o , n , s , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> o . o = '<STR_LIT:o>' <EOL> n . n = '<STR_LIT:n>' <EOL> s . n = '<STR_LIT:S>' <EOL> s . s = '<STR_LIT:s>' <EOL> asizeof . asizesof ( o , n , s , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> o . o = '<STR_LIT:x>' <EOL> n . n = '<STR_LIT:x>' <EOL> s . n = '<STR_LIT:x>' <EOL> s . s = '<STR_LIT:x>' <EOL> asizeof . asizesof ( o , n , s , '<STR_LIT:x>' , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> o . o = '<STR_LIT:o>' * <NUM_LIT:1000> <EOL> n . n = '<STR_LIT:n>' * <NUM_LIT:1000> <EOL> s . n = '<STR_LIT:n>' * <NUM_LIT:1000> <EOL> s . s = '<STR_LIT:s>' * <NUM_LIT:1000> <EOL> asizeof . asizesof ( o , n , s , '<STR_LIT:x>' * <NUM_LIT:1000> , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> def test_stack ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> asizeof . asizeof ( stack ( self . MAX ) [ <NUM_LIT:1> : ] , limit = self . MAX , code = False , stats = <NUM_LIT:1> ) <EOL> self . _print_functions ( stack ( self . MAX ) [ <NUM_LIT:1> : ] , '<STR_LIT>' , opt = '<STR_LIT>' ) <EOL> def test_sys_mods ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , '<STR_LIT>' , False , '<STR_LIT>' , '<STR_LIT>' ) <EOL> asizeof . asizeof ( limit = self . MAX , code = False , stats = <NUM_LIT:1> , * sys . modules . values ( ) ) <EOL> self . _print_functions ( sys . modules , '<STR_LIT>' , opt = '<STR_LIT>' ) <EOL> def test_typedefs ( self ) : <EOL> '''<STR_LIT>''' <EOL> t = len ( asizeof . _typedefs ) <EOL> w = len ( str ( t ) ) * '<STR_LIT:U+0020>' <EOL> self . _printf ( '<STR_LIT>' , os . linesep , t , '<STR_LIT>' ) <EOL> for k , v in sorted ( ( asizeof . _prepr ( k ) , v ) for k , v in asizeof . _items ( asizeof . _typedefs ) ) : <EOL> s = '<STR_LIT>' % v . format ( ) <EOL> self . _printf ( '<STR_LIT>' , w , k , s ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> suite = unittest . makeSuite ( [ AsizeofTest , TypesTest , FunctionTest , AsizeofDemos ] , '<STR_LIT:test>' ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:1> ) . run ( suite ) </s>
