<s> from pypy . interpreter . baseobjspace import Wrappable <EOL> from pypy . interpreter . typedef import GetSetProperty , TypeDef <EOL> from pypy . interpreter . typedef import interp_attrproperty , interp_attrproperty_w <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . interpreter . gateway import interp2app , ObjSpace , W_Root <EOL> class Index ( object ) : <EOL> """<STR_LIT>""" <EOL> class IntIndex ( Index ) : <EOL> def __init__ ( self , intval ) : <EOL> self . index = intval <EOL> class SliceIndex ( Index ) : <EOL> def __init__ ( self , sliceval ) : <EOL> self . slice = sliceval <EOL> def indices ( self , dim ) : <EOL> """<STR_LIT>""" <EOL> return self . slice . indices ( dim ) <EOL> class EllipsisIndex ( Index ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def get_storage_size ( dims ) : <EOL> n = <NUM_LIT:1> <EOL> for d in dims : <EOL> n *= d <EOL> assert d >= <NUM_LIT:0> <EOL> if n == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> return n <EOL> def is_integer ( space , w_obj ) : <EOL> return space . is_true ( space . isinstance ( w_obj , space . w_int ) ) <EOL> def convert_index_type ( space , w_obj ) : <EOL> if space . is_true ( space . isinstance ( w_obj , space . w_int ) ) : <EOL> return [ IntIndex ( space . int_w ( w_obj ) ) ] , False , False <EOL> if not space . is_true ( space . isinstance ( w_obj , space . w_tuple ) ) : <EOL> raise OperationError ( space . w_IndexError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> multi_index = [ ] <EOL> has_slice = False <EOL> has_ellipsis = False <EOL> for w_idx in space . unpackiterable ( w_obj ) : <EOL> if space . is_true ( space . isinstance ( w_idx , space . w_int ) ) : <EOL> multi_index . append ( IntIndex ( space . int_w ( w_idx ) ) ) <EOL> elif space . is_true ( space . isinstance ( w_idx , space . w_slice ) ) : <EOL> multi_index . append ( SliceIndex ( space . unwrap ( w_idx ) ) ) <EOL> has_slice = True <EOL> elif space . is_w ( w_idx , space . w_Ellipsis ) : <EOL> multi_index . append ( EllipsisIndex ( ) ) <EOL> has_ellipsis = True <EOL> else : <EOL> raise OperationError ( space . w_IndexError , <EOL> space . wrap ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return multi_index , has_slice , has_ellipsis <EOL> class W_Array ( Wrappable ) : <EOL> def __init__ ( self , space , dims , strides = None ) : <EOL> self . space = space <EOL> assert isinstance ( dims , list ) <EOL> self . dims = dims <EOL> self . strides = [ <NUM_LIT:1> ] <EOL> self . base_offset = <NUM_LIT:0> <EOL> if strides is None : <EOL> stride = <NUM_LIT:1> <EOL> for n in self . dims [ : - <NUM_LIT:1> ] : <EOL> stride *= n <EOL> self . strides . append ( stride ) <EOL> self . strides . reverse ( ) <EOL> else : <EOL> self . strides = strides <EOL> def check_scalar_index ( self , space , w_index ) : <EOL> if not space . is_true ( space . isinstance ( w_index , space . w_int ) ) : <EOL> raise NotImplementedError <EOL> idx = space . unwrap ( w_index ) <EOL> assert isinstance ( idx , int ) <EOL> return idx <EOL> def descr___getitem__ ( self , space , w_index ) : <EOL> multi_idx , has_slice , has_ellipsis = convert_index_type ( space , w_index ) <EOL> if not has_slice and not has_ellipsis and len ( multi_idx ) == len ( self . dims ) : <EOL> idx_tuple = [ ] <EOL> for idx in multi_idx : <EOL> assert isinstance ( idx , IntIndex ) <EOL> idx_tuple . append ( idx . index ) <EOL> return self . get_single_item ( space , idx_tuple ) <EOL> if has_ellipsis : <EOL> pass <EOL> return self . get_slice ( space , multi_idx ) <EOL> def descr___setitem__ ( self , space , w_index , w_value ) : <EOL> multi_idx , has_slice , has_ellipsis = convert_index_type ( space , w_index ) <EOL> if not has_slice and not has_ellipsis and len ( multi_idx ) == len ( self . dims ) : <EOL> idx_tuple = [ ] <EOL> for idx in multi_idx : <EOL> assert isinstance ( idx , IntIndex ) <EOL> idx_tuple . append ( idx . index ) <EOL> return self . set_single_item ( space , idx_tuple , w_value ) <EOL> if len ( multi_idx ) < len ( self . dims ) : <EOL> pass <EOL> if has_ellipsis : <EOL> pass <EOL> return self . set_slice ( space , multi_idx , w_value ) <EOL> def descr_typecode ( self , space ) : <EOL> return self . typecode ( space ) <EOL> def fget_shape ( space , self ) : <EOL> return space . newtuple ( [ self . space . wrap ( i ) for i in self . dims ] ) <EOL> def get_array_offset ( self , idx_tuple ) : <EOL> if len ( idx_tuple ) > len ( self . dims ) : <EOL> raise RuntimeError <EOL> idx = self . base_offset <EOL> for i in range ( len ( idx_tuple ) ) : <EOL> idx += self . strides [ i ] * idx_tuple [ i ] <EOL> return idx <EOL> def typecode ( self , space ) : <EOL> raise OperationError ( space . w_NotImplemented , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> class W_Array_Float ( W_Array ) : <EOL> def __init__ ( self , space , dims , strides = None , storage = None ) : <EOL> W_Array . __init__ ( self , space , dims , strides ) <EOL> self . storage = [ ] <EOL> if storage is not None : <EOL> assert isinstance ( storage , list ) <EOL> assert isinstance ( storage [ <NUM_LIT:0> ] , float ) <EOL> self . storage = storage <EOL> else : <EOL> storage_size = get_storage_size ( dims ) <EOL> self . storage = [ <NUM_LIT:0.0> ] * storage_size <EOL> def get_single_item ( self , space , idx_tuple ) : <EOL> if len ( idx_tuple ) != len ( self . dims ) : <EOL> raise RuntimeError <EOL> idx = self . get_array_offset ( idx_tuple ) <EOL> return space . wrap ( self . storage [ idx ] ) <EOL> def set_single_item ( self , space , idx_tuple , w_value ) : <EOL> idx = self . get_array_offset ( idx_tuple ) <EOL> value = space . float_w ( w_value ) <EOL> self . storage [ idx ] = value <EOL> def get_slice ( self , space , multi_idx ) : <EOL> dims = [ ] <EOL> strides = [ ] <EOL> last_stride = <NUM_LIT:1> <EOL> offset = self . base_offset <EOL> for i in range ( len ( multi_idx ) ) : <EOL> idx = multi_idx [ i ] <EOL> if isinstance ( idx , SliceIndex ) : <EOL> start , stop , step = idx . indices ( self . dims [ i ] ) <EOL> dim = ( stop - start ) // step <EOL> stride = self . strides [ i ] * step <EOL> dims . append ( dim ) <EOL> strides . append ( step ) <EOL> elif isinstance ( idx , IntIndex ) : <EOL> offset += idx . index * self . strides [ i ] <EOL> array = W_Array_Float ( space , dims , strides , self . storage ) <EOL> return space . wrap ( array ) <EOL> def typecode ( self , space ) : <EOL> return space . wrap ( '<STR_LIT:d>' ) <EOL> class W_Array_Integer ( W_Array ) : <EOL> def __init__ ( self , space , dims , strides = None , storage = None ) : <EOL> W_Array . __init__ ( self , space , dims , strides ) <EOL> self . storage = [ ] <EOL> if storage is not None : <EOL> assert isinstance ( storage , list ) <EOL> assert isinstance ( storage [ <NUM_LIT:0> ] , int ) <EOL> self . storage = storage <EOL> else : <EOL> storage_size = get_storage_size ( dims ) <EOL> self . storage = [ <NUM_LIT:0> ] * storage_size <EOL> def get_single_item ( self , space , idx_tuple ) : <EOL> if len ( idx_tuple ) != len ( self . dims ) : <EOL> raise RuntimeError <EOL> idx = self . get_array_offset ( idx_tuple ) <EOL> return space . wrap ( self . storage [ idx ] ) <EOL> def set_single_item ( self , space , idx_tuple , w_value ) : <EOL> idx = self . get_array_offset ( idx_tuple ) <EOL> value = space . int_w ( w_value ) <EOL> self . storage [ idx ] = value <EOL> def get_slice ( self , space , multi_idx ) : <EOL> dims = [ ] <EOL> strides = [ ] <EOL> last_stride = <NUM_LIT:1> <EOL> offset = self . base_offset <EOL> for i in range ( len ( multi_idx ) ) : <EOL> idx = multi_idx [ i ] <EOL> if isinstance ( idx , SliceIndex ) : <EOL> start , stop , step = idx . indices ( self . dims [ i ] ) <EOL> dim = ( stop - start ) // step <EOL> stride = self . strides [ i ] * step <EOL> dims . append ( dim ) <EOL> strides . append ( step ) <EOL> elif isinstance ( idx , IntIndex ) : <EOL> offset += idx . index * self . strides [ i ] <EOL> array = W_Array_Float ( space , dims , strides , self . storage ) <EOL> return space . wrap ( array ) <EOL> def typecode ( self , space ) : <EOL> return space . wrap ( '<STR_LIT:l>' ) <EOL> descr___getitem__ = interp2app ( W_Array . descr___getitem__ , unwrap_spec = [ '<STR_LIT>' , ObjSpace , W_Root ] ) <EOL> descr___setitem__ = interp2app ( W_Array . descr___setitem__ , unwrap_spec = [ '<STR_LIT>' , ObjSpace , W_Root , W_Root ] ) <EOL> descr_typecode = interp2app ( W_Array . descr_typecode , unwrap_spec = [ '<STR_LIT>' , ObjSpace ] ) <EOL> W_Array . typedef = TypeDef ( "<STR_LIT>" , <EOL> shape = GetSetProperty ( W_Array . fget_shape , cls = W_Array ) , <EOL> __getitem__ = descr___getitem__ , <EOL> __setitem__ = descr___setitem__ , <EOL> typecode = descr_typecode , <EOL> ) <EOL> def w_zeros ( space , w_dim_tuple , type_str ) : <EOL> dims = [ ] <EOL> for w_int in space . unpackiterable ( w_dim_tuple ) : <EOL> dims . append ( space . unwrap ( w_int ) ) <EOL> if type_str == '<STR_LIT:d>' : <EOL> return space . wrap ( W_Array_Float ( space , dims ) ) <EOL> raise OperationError ( space . w_ValueError , space . wrap ( '<STR_LIT>' ) ) <EOL> w_zeros . unwrap_spec = [ ObjSpace , W_Root , str ] <EOL> TOWER_TYPES_VALUES = [ ( int , <NUM_LIT:1> ) , ( float , <NUM_LIT:1.0> ) , ( complex , <NUM_LIT> + <NUM_LIT> ) ] <EOL> TOWER_TYPES_VALUES = TOWER_TYPES_VALUES [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> TOWER_TYPES = [ typ for typ , val in TOWER_TYPES_VALUES ] <EOL> def OpError ( space , w_arg ) : <EOL> raise OperationError ( space . w_TypeError , space . wrap ( '<STR_LIT>' % str ( w_arg ) ) ) <EOL> def int_array_from_iterable ( space , w_it ) : <EOL> N = space . int_w ( space . len ( w_it ) ) <EOL> storage = N * [ <NUM_LIT:0> ] <EOL> i = <NUM_LIT:0> <EOL> for w_obj in space . unpackiterable ( w_it ) : <EOL> storage [ i ] = space . int_w ( w_it ) <EOL> i += <NUM_LIT:1> <EOL> def float_array_from_iterable ( space , w_it ) : <EOL> N = space . int_w ( space . len ( w_it ) ) <EOL> storage = N * [ <NUM_LIT:0.> ] <EOL> i = <NUM_LIT:0> <EOL> for w_obj in space . unpackiterable ( w_it ) : <EOL> storage [ i ] = space . float_w ( w_it ) <EOL> i += <NUM_LIT:1> <EOL> def array ( space , w_arg ) : <EOL> TYPES = "<STR_LIT>" <EOL> if ( space . is_true ( space . isinstance ( w_arg , space . w_list ) ) or <EOL> space . is_true ( space . isinstance ( w_arg , space . w_tuple ) ) ) : <EOL> if not space . is_true ( w_arg ) : <EOL> return W_Array_Integer ( space , [ <NUM_LIT:0> , ] ) <EOL> ty = - <NUM_LIT:1> <EOL> for w_obj in space . unpackiterable ( w_arg ) : <EOL> if ty < <NUM_LIT:0> and space . is_true ( space . isinstance ( w_arg , space . w_int ) ) : <EOL> ty = <NUM_LIT:0> <EOL> if ty < <NUM_LIT:1> and space . is_true ( space . isinstance ( w_arg , space . w_float ) ) : <EOL> ty = <NUM_LIT:1> <EOL> else : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> if ty == <NUM_LIT:2> : <EOL> break <EOL> if ty == <NUM_LIT:0> : <EOL> return int_array_from_iterable ( space , w_arg ) <EOL> elif ty == <NUM_LIT:1> : <EOL> return float_array_from_iterable ( space , w_arg ) <EOL> elif ty == <NUM_LIT:2> : <EOL> raise OperationError ( space . w_NotImplementedError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> else : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> elif space . is_true ( space . isinstance ( w_arg , space . w_int ) ) : <EOL> ivalue = space . int_w ( w_arg ) <EOL> return W_Array_Integer ( space , [ ] , storage = [ ivalue ] ) <EOL> elif space . is_true ( space . isinstance ( w_arg , space . w_float ) ) : <EOL> fvalue = space . float_w ( w_arg ) <EOL> return W_Array_Float ( space , [ ] , storage = [ fvalue ] ) <EOL> else : <EOL> OpError ( space , w_arg ) <EOL> array . unwrap_spec = [ ObjSpace , W_Root ] </s>
