@@ -105,15 +105,34 @@ def ctype_to_lists(ctype_arr, dim, shape, offset=0):
105
105
offset += shape [0 ]
106
106
return res
107
107
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
+
108
127
class array (base_array ):
109
128
110
- def __init__ (self , src = None , dims = (0 ,)):
129
+ def __init__ (self , src = None , dims = (0 ,), type_char = None ):
111
130
112
131
super (array , self ).__init__ ()
113
132
114
133
buf = None
115
134
buf_len = 0
116
- type_char = 'f'
135
+ _type_char = 'f'
117
136
dtype = f32
118
137
119
138
if src is not None :
@@ -126,29 +145,37 @@ def __init__(self, src=None, dims=(0,)):
126
145
127
146
if isinstance (src , host .array ):
128
147
buf ,buf_len = src .buffer_info ()
129
- type_char = src .typecode
148
+ _type_char = src .typecode
149
+ numdims , idims = get_info (dims , buf_len )
130
150
elif isinstance (src , list ):
131
151
tmp = host .array ('f' , src )
132
152
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" )
136
165
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" )
140
168
141
- for i in range (numdims ):
142
- elements *= dims [i ]
143
- idims [i ] = dims [i ]
169
+ _type_char = type_char
144
170
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" )
148
173
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" )
150
177
151
- self .arr = create_array (buf , numdims , idims , dtype )
178
+ self .arr = create_array (buf , numdims , idims , to_dtype [ _type_char ] )
152
179
153
180
def copy (self ):
154
181
out = array ()
0 commit comments