<s> import _rawffi <EOL> from _ctypes . basics import _CData , _CDataMeta , store_reference , keepalive_key <EOL> from _ctypes . basics import ensure_objects <EOL> from _ctypes . structure import round_up , names_and_fields , struct_getattr , struct_setattr <EOL> def _set_shape ( tp ) : <EOL> size = tp . _sizeofinstances ( ) <EOL> alignment = tp . _alignmentofinstances ( ) <EOL> tp . _ffiopaque = _rawffi . Structure ( ( size , alignment ) ) <EOL> tp . _ffiargshape = tp . _ffishape = ( tp . _ffiopaque , <NUM_LIT:1> ) <EOL> tp . _fficompositesize = tp . _ffiopaque . size <EOL> tp . _ffiarrays = { } <EOL> for name , field in tp . _fieldtypes . iteritems ( ) : <EOL> tp . _ffiarrays [ name ] = _rawffi . Array ( field . ctype . _ffishape ) <EOL> class UnionMeta ( _CDataMeta ) : <EOL> def __new__ ( self , name , cls , typedict ) : <EOL> res = type . __new__ ( self , name , cls , typedict ) <EOL> if '<STR_LIT>' in typedict : <EOL> res . _names , rawfields , res . _fieldtypes = names_and_fields ( <EOL> typedict [ '<STR_LIT>' ] , cls [ <NUM_LIT:0> ] , True , <EOL> typedict . get ( '<STR_LIT>' , None ) ) <EOL> _set_shape ( res ) <EOL> def __init__ ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> size = self . __class__ . _sizeofinstances ( ) <EOL> self . __dict__ [ '<STR_LIT>' ] = { } <EOL> self . __dict__ [ '<STR_LIT>' ] = self . _ffiopaque ( autofree = True ) <EOL> res . __init__ = __init__ <EOL> return res <EOL> def _sizeofinstances ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _size_ = max ( [ field . size for field in <EOL> self . _fieldtypes . values ( ) ] + [ <NUM_LIT:0> ] ) <EOL> return self . _size_ <EOL> def _alignmentofinstances ( self ) : <EOL> from ctypes import alignment <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _alignment_ = max ( [ alignment ( field . ctype ) for field in <EOL> self . _fieldtypes . values ( ) ] + [ <NUM_LIT:1> ] ) <EOL> return self . _alignment_ <EOL> __getattr__ = struct_getattr <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == '<STR_LIT>' : <EOL> if self . __dict__ . get ( '<STR_LIT>' , None ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if self in [ v for k , v in value ] : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> self . _names , rawfields , self . _fieldtypes = names_and_fields ( <EOL> value , self . __bases__ [ <NUM_LIT:0> ] , True , <EOL> self . __dict__ . get ( '<STR_LIT>' , None ) ) <EOL> _CDataMeta . __setattr__ ( self , '<STR_LIT>' , value ) <EOL> _set_shape ( self ) <EOL> _CDataMeta . __setattr__ ( self , name , value ) <EOL> def _CData_output ( self , resarray , base = None , index = - <NUM_LIT:1> ) : <EOL> res = self . __new__ ( self ) <EOL> ffiopaque = self . _ffiopaque . fromaddress ( resarray . buffer ) <EOL> res . __dict__ [ '<STR_LIT>' ] = ffiopaque <EOL> res . __dict__ [ '<STR_LIT>' ] = base <EOL> res . __dict__ [ '<STR_LIT>' ] = index <EOL> return res <EOL> def _CData_retval ( self , resbuffer ) : <EOL> res = self . __new__ ( self ) <EOL> res . __dict__ [ '<STR_LIT>' ] = resbuffer <EOL> res . __dict__ [ '<STR_LIT>' ] = None <EOL> res . __dict__ [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> return res <EOL> class Union ( _CData ) : <EOL> __metaclass__ = UnionMeta <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> fieldtype = self . _fieldtypes [ name ] . ctype <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> val = self . _ffiarrays [ name ] . fromaddress ( self . _buffer . buffer , <NUM_LIT:1> ) <EOL> offset = self . __class__ . _fieldtypes [ name ] . num <EOL> return fieldtype . _CData_output ( val , self , offset ) <EOL> def __setattr__ ( self , name , value ) : <EOL> try : <EOL> fieldtype = self . _fieldtypes [ name ] . ctype <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> if ensure_objects ( value ) is not None : <EOL> key = keepalive_key ( getattr ( self . __class__ , name ) . num ) <EOL> store_reference ( self , key , value . _objects ) <EOL> arg = fieldtype . _CData_value ( value ) <EOL> if fieldtype . _fficompositesize is not None : <EOL> from ctypes import memmove <EOL> dest = self . _buffer . buffer <EOL> memmove ( dest , arg , fieldtype . _fficompositesize ) <EOL> else : <EOL> buf = self . _ffiarrays [ name ] . fromaddress ( self . _buffer . buffer , <NUM_LIT:1> ) <EOL> buf [ <NUM_LIT:0> ] = arg <EOL> def _get_buffer_value ( self ) : <EOL> return self . _buffer . buffer </s>
