<s> """<STR_LIT:U+0020>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> from ctypes import * <EOL> class SDL_array : <EOL> def __init__ ( self , ptr , count , ctype ) : <EOL> """<STR_LIT>""" <EOL> count = int ( count ) <EOL> assert count >= <NUM_LIT:0> <EOL> if not ptr : <EOL> ptr = ( ctype * count ) ( ) <EOL> self . ptr = ptr <EOL> self . count = count <EOL> self . ctype = ctype <EOL> self . _ctypes_array = None <EOL> def as_bytes ( self ) : <EOL> """<STR_LIT>""" <EOL> return SDL_array ( self . ptr , ( self . count * sizeof ( self . ctype ) ) , c_ubyte ) <EOL> def as_int16 ( self ) : <EOL> """<STR_LIT>""" <EOL> return SDL_array ( self . ptr , <EOL> self . count * sizeof ( self . ctype ) / <NUM_LIT:2> , <EOL> c_ushort ) <EOL> def as_int32 ( self ) : <EOL> """<STR_LIT>""" <EOL> return SDL_array ( self . ptr , <EOL> self . count * sizeof ( self . ctype ) / <NUM_LIT:4> , <EOL> c_uint ) <EOL> def as_ctypes ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _ctypes_array : <EOL> self . _ctypes_array = cast ( self . ptr , POINTER ( self . ctype * self . count ) ) . contents <EOL> return self . _ctypes_array <EOL> def have_numpy ( cls ) : <EOL> """<STR_LIT>""" <EOL> return _have_numpy <EOL> def as_numpy ( self , shape = None ) : <EOL> """<STR_LIT>""" <EOL> if not _have_numpy : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> if self . ctype not in _numpy_typemap : <EOL> raise TypeError ( '<STR_LIT>' % self . ctype ) <EOL> if shape is None : <EOL> shape = ( self . count , ) <EOL> ar = numpy . frombuffer ( self . as_ctypes ( ) , _numpy_typemap [ self . ctype ] ) <EOL> ar = ar . reshape ( shape ) <EOL> return ar <EOL> def to_string ( self ) : <EOL> """<STR_LIT>""" <EOL> count = sizeof ( self . ctype ) * self . count <EOL> s = create_string_buffer ( count ) <EOL> memmove ( s , self . ptr , count ) <EOL> return s . raw <EOL> def from_string ( self , data ) : <EOL> """<STR_LIT>""" <EOL> memmove ( self . ptr , data , len ( data ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . ctype , self . count ) <EOL> def __len__ ( self ) : <EOL> return self . count <EOL> def __getitem__ ( self , key ) : <EOL> if type ( key ) is slice : <EOL> if key . step : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self . as_ctypes ( ) [ key . start : key . stop ] <EOL> else : <EOL> return self . as_ctypes ( ) [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> if type ( key ) is slice : <EOL> if key . step : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . as_ctypes ( ) [ key . start : key . stop ] = value <EOL> else : <EOL> self . as_ctypes ( ) [ key ] = value <EOL> def to_ctypes ( values , count , ctype ) : <EOL> """<STR_LIT>""" <EOL> ref = values <EOL> if isinstance ( values , SDL_array ) : <EOL> values = values . as_ctypes ( ) <EOL> if isinstance ( values , Array ) : <EOL> if values . _type_ is ctype : <EOL> return ref , values <EOL> else : <EOL> return ref , cast ( values , POINTER ( ctype * count ) ) . contents <EOL> if type ( values ) == str : <EOL> ref = create_string_buffer ( values ) <EOL> return ref , cast ( ref , POINTER ( ctype * count ) ) . contents <EOL> ar = ( ctype * count ) ( * values ) <EOL> return ar , ar </s>
