@@ -105,15 +105,34 @@ def ctype_to_lists(ctype_arr, dim, shape, offset=0):
105105 offset += shape [0 ]
106106 return res
107107
108+ def get_info (dims , buf_len ):
109+ elements = 1
110+ numdims = len (dims )
111+ idims = [1 ]* 4
112+
113+ for i in range (numdims ):
114+ elements *= dims [i ]
115+ idims [i ] = dims [i ]
116+
117+ if (elements == 0 ):
118+ if (buf_len != 0 ):
119+ idims = [buf_len , 1 , 1 , 1 ]
120+ numdims = 1
121+ else :
122+ raise RuntimeError ("Invalid size" )
123+
124+ return numdims , idims
125+
126+
108127class array (base_array ):
109128
110- def __init__ (self , src = None , dims = (0 ,)):
129+ def __init__ (self , src = None , dims = (0 ,), type_char = None ):
111130
112131 super (array , self ).__init__ ()
113132
114133 buf = None
115134 buf_len = 0
116- type_char = 'f'
135+ _type_char = 'f'
117136 dtype = f32
118137
119138 if src is not None :
@@ -126,29 +145,37 @@ def __init__(self, src=None, dims=(0,)):
126145
127146 if isinstance (src , host .array ):
128147 buf ,buf_len = src .buffer_info ()
129- type_char = src .typecode
148+ _type_char = src .typecode
149+ numdims , idims = get_info (dims , buf_len )
130150 elif isinstance (src , list ):
131151 tmp = host .array ('f' , src )
132152 buf ,buf_len = tmp .buffer_info ()
133- type_char = tmp .typecode
134- else :
135- raise TypeError ("src is an object of unsupported class" )
153+ _type_char = tmp .typecode
154+ numdims , idims = get_info (dims , buf_len )
155+ elif isinstance (src , int ) or isinstance (src , ct .c_ulonglong ):
156+ buf = src
157+ numdims , idims = get_info (dims , buf_len )
158+
159+ elements = 1
160+ for dim in idims :
161+ elements *= dim
162+
163+ if (elements == 0 ):
164+ raise RuntimeError ("Expected dims when src is data pointer" )
136165
137- elements = 1
138- numdims = len (dims )
139- idims = [1 ]* 4
166+ if (type_char is None ):
167+ raise TypeError ("Expected type_char when src is data pointer" )
140168
141- for i in range (numdims ):
142- elements *= dims [i ]
143- idims [i ] = dims [i ]
169+ _type_char = type_char
144170
145- if (elements == 0 ):
146- idims = [buf_len , 1 , 1 , 1 ]
147- numdims = 1
171+ else :
172+ raise TypeError ("src is an object of unsupported class" )
148173
149- dtype = to_dtype [type_char ]
174+ if (type_char is not None and
175+ type_char != _type_char ):
176+ raise TypeError ("Can not create array of requested type from input data type" )
150177
151- self .arr = create_array (buf , numdims , idims , dtype )
178+ self .arr = create_array (buf , numdims , idims , to_dtype [ _type_char ] )
152179
153180 def copy (self ):
154181 out = array ()
0 commit comments