@@ -31,6 +31,11 @@ cdef inline _is_container(object obj):
3131 return not _is_trivial_container(obj) and isinstance (obj, ContainerABC)
3232
3333
34+ cdef inline _is_sub_array(object obj):
35+ return not _is_trivial_container(obj) and isinstance (obj, ContainerABC) \
36+ and not cpython.PyTuple_Check(obj)
37+
38+
3439cdef _get_array_shape(object obj, int32_t * dims, int32_t * ndims):
3540 cdef:
3641 int32_t mylen = len (obj)
@@ -45,7 +50,7 @@ cdef _get_array_shape(object obj, int32_t *dims, int32_t *ndims):
4550 dims[ndims[0 ] - 1 ] = mylen
4651
4752 for elem in obj:
48- if _is_container (elem):
53+ if _is_sub_array (elem):
4954 if elemlen == - 2 :
5055 elemlen = len (elem)
5156 ndims[0 ] += 1
@@ -133,7 +138,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
133138 int32_t ndims = hton.unpack_int32(buf.read(4 ))
134139 int32_t flags = hton.unpack_int32(buf.read(4 ))
135140 uint32_t elem_oid = hton.unpack_int32(buf.read(4 ))
136- tuple result
141+ list result
137142 uint32_t i
138143 int32_t elem_len
139144 int64_t elem_count = 1
@@ -142,7 +147,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
142147 Codec elem_codec
143148
144149 if ndims == 0 :
145- result = ( )
150+ result = cpython.PyList_New( 0 )
146151 return result
147152
148153 if ndims > ARRAY_MAXDIM:
@@ -169,7 +174,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
169174
170175 if ndims == 1 :
171176 # Fast path for flat arrays
172- result = cpython.PyTuple_New (elem_count)
177+ result = cpython.PyList_New (elem_count)
173178
174179 for i in range (elem_count):
175180 elem_len = hton.unpack_int32(buf.read(4 ))
@@ -180,7 +185,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
180185 elem = decoder(settings, elem_buf, decoder_arg)
181186
182187 cpython.Py_INCREF(elem)
183- cpython.PyTuple_SET_ITEM (result, i, elem)
188+ cpython.PyList_SET_ITEM (result, i, elem)
184189
185190 else :
186191 result = _nested_array_decode(settings, buf,
@@ -200,20 +205,20 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
200205 cdef:
201206 int32_t elem_len
202207 int32_t d1, d2, d3, d4, d5, d6
203- tuple result
208+ list result
204209 object elem
205- tuple stride1, stride2, stride3, stride4, stride5
210+ list stride1, stride2, stride3, stride4, stride5
206211
207212 # Nested array. The approach here is dumb, but fast: rely
208213 # on the dimension limit and shape data using nested loops.
209214 # Alas, Cython doesn't have preprocessor macros.
210215 #
211- result = cpython.PyTuple_New (dims[0 ])
216+ result = cpython.PyList_New (dims[0 ])
212217
213218 for d1 in range (dims[0 ]):
214- stride1 = cpython.PyTuple_New (dims[1 ])
219+ stride1 = cpython.PyList_New (dims[1 ])
215220 cpython.Py_INCREF(stride1)
216- cpython.PyTuple_SET_ITEM (result, d1, stride1)
221+ cpython.PyList_SET_ITEM (result, d1, stride1)
217222
218223 for d2 in range (dims[1 ]):
219224 if ndims == 2 :
@@ -226,12 +231,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
226231 decoder_arg)
227232
228233 cpython.Py_INCREF(elem)
229- cpython.PyTuple_SET_ITEM (stride1, d2, elem)
234+ cpython.PyList_SET_ITEM (stride1, d2, elem)
230235
231236 else :
232- stride2 = cpython.PyTuple_New (dims[2 ])
237+ stride2 = cpython.PyList_New (dims[2 ])
233238 cpython.Py_INCREF(stride2)
234- cpython.PyTuple_SET_ITEM (stride1, d2, stride2)
239+ cpython.PyList_SET_ITEM (stride1, d2, stride2)
235240
236241 for d3 in range (dims[2 ]):
237242 if ndims == 3 :
@@ -244,12 +249,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
244249 decoder_arg)
245250
246251 cpython.Py_INCREF(elem)
247- cpython.PyTuple_SET_ITEM (stride2, d3, elem)
252+ cpython.PyList_SET_ITEM (stride2, d3, elem)
248253
249254 else :
250- stride3 = cpython.PyTuple_New (dims[3 ])
255+ stride3 = cpython.PyList_New (dims[3 ])
251256 cpython.Py_INCREF(stride3)
252- cpython.PyTuple_SET_ITEM (stride2, d3, stride3)
257+ cpython.PyList_SET_ITEM (stride2, d3, stride3)
253258
254259 for d4 in range (dims[3 ]):
255260 if ndims == 4 :
@@ -262,12 +267,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
262267 decoder_arg)
263268
264269 cpython.Py_INCREF(elem)
265- cpython.PyTuple_SET_ITEM (stride3, d4, elem)
270+ cpython.PyList_SET_ITEM (stride3, d4, elem)
266271
267272 else :
268- stride4 = cpython.PyTuple_New (dims[4 ])
273+ stride4 = cpython.PyList_New (dims[4 ])
269274 cpython.Py_INCREF(stride4)
270- cpython.PyTuple_SET_ITEM (stride3, d4, stride4)
275+ cpython.PyList_SET_ITEM (stride3, d4, stride4)
271276
272277 for d5 in range (dims[4 ]):
273278 if ndims == 5 :
@@ -280,12 +285,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
280285 decoder_arg)
281286
282287 cpython.Py_INCREF(elem)
283- cpython.PyTuple_SET_ITEM (stride4, d5, elem)
288+ cpython.PyList_SET_ITEM (stride4, d5, elem)
284289
285290 else :
286- stride5 = cpython.PyTuple_New (dims[5 ])
291+ stride5 = cpython.PyList_New (dims[5 ])
287292 cpython.Py_INCREF(stride5)
288- cpython.PyTuple_SET_ITEM (stride4, d5, stride5)
293+ cpython.PyList_SET_ITEM (stride4, d5, stride5)
289294
290295 for d6 in range (dims[5 ]):
291296 elem_len = hton.unpack_int32(buf.read(4 ))
@@ -297,7 +302,7 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
297302 decoder_arg)
298303
299304 cpython.Py_INCREF(elem)
300- cpython.PyTuple_SET_ITEM (stride5, d6, elem)
305+ cpython.PyList_SET_ITEM (stride5, d6, elem)
301306
302307 return result
303308
0 commit comments