@@ -31,6 +31,11 @@ cdef inline _is_container(object obj):
31
31
return not _is_trivial_container(obj) and isinstance (obj, ContainerABC)
32
32
33
33
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
+
34
39
cdef _get_array_shape(object obj, int32_t * dims, int32_t * ndims):
35
40
cdef:
36
41
int32_t mylen = len (obj)
@@ -45,7 +50,7 @@ cdef _get_array_shape(object obj, int32_t *dims, int32_t *ndims):
45
50
dims[ndims[0 ] - 1 ] = mylen
46
51
47
52
for elem in obj:
48
- if _is_container (elem):
53
+ if _is_sub_array (elem):
49
54
if elemlen == - 2 :
50
55
elemlen = len (elem)
51
56
ndims[0 ] += 1
@@ -133,7 +138,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
133
138
int32_t ndims = hton.unpack_int32(buf.read(4 ))
134
139
int32_t flags = hton.unpack_int32(buf.read(4 ))
135
140
uint32_t elem_oid = hton.unpack_int32(buf.read(4 ))
136
- tuple result
141
+ list result
137
142
uint32_t i
138
143
int32_t elem_len
139
144
int64_t elem_count = 1
@@ -142,7 +147,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
142
147
Codec elem_codec
143
148
144
149
if ndims == 0 :
145
- result = ( )
150
+ result = cpython.PyList_New( 0 )
146
151
return result
147
152
148
153
if ndims > ARRAY_MAXDIM:
@@ -169,7 +174,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
169
174
170
175
if ndims == 1 :
171
176
# Fast path for flat arrays
172
- result = cpython.PyTuple_New (elem_count)
177
+ result = cpython.PyList_New (elem_count)
173
178
174
179
for i in range (elem_count):
175
180
elem_len = hton.unpack_int32(buf.read(4 ))
@@ -180,7 +185,7 @@ cdef inline array_decode(ConnectionSettings settings, FastReadBuffer buf,
180
185
elem = decoder(settings, elem_buf, decoder_arg)
181
186
182
187
cpython.Py_INCREF(elem)
183
- cpython.PyTuple_SET_ITEM (result, i, elem)
188
+ cpython.PyList_SET_ITEM (result, i, elem)
184
189
185
190
else :
186
191
result = _nested_array_decode(settings, buf,
@@ -200,20 +205,20 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
200
205
cdef:
201
206
int32_t elem_len
202
207
int32_t d1, d2, d3, d4, d5, d6
203
- tuple result
208
+ list result
204
209
object elem
205
- tuple stride1, stride2, stride3, stride4, stride5
210
+ list stride1, stride2, stride3, stride4, stride5
206
211
207
212
# Nested array. The approach here is dumb, but fast: rely
208
213
# on the dimension limit and shape data using nested loops.
209
214
# Alas, Cython doesn't have preprocessor macros.
210
215
#
211
- result = cpython.PyTuple_New (dims[0 ])
216
+ result = cpython.PyList_New (dims[0 ])
212
217
213
218
for d1 in range (dims[0 ]):
214
- stride1 = cpython.PyTuple_New (dims[1 ])
219
+ stride1 = cpython.PyList_New (dims[1 ])
215
220
cpython.Py_INCREF(stride1)
216
- cpython.PyTuple_SET_ITEM (result, d1, stride1)
221
+ cpython.PyList_SET_ITEM (result, d1, stride1)
217
222
218
223
for d2 in range (dims[1 ]):
219
224
if ndims == 2 :
@@ -226,12 +231,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
226
231
decoder_arg)
227
232
228
233
cpython.Py_INCREF(elem)
229
- cpython.PyTuple_SET_ITEM (stride1, d2, elem)
234
+ cpython.PyList_SET_ITEM (stride1, d2, elem)
230
235
231
236
else :
232
- stride2 = cpython.PyTuple_New (dims[2 ])
237
+ stride2 = cpython.PyList_New (dims[2 ])
233
238
cpython.Py_INCREF(stride2)
234
- cpython.PyTuple_SET_ITEM (stride1, d2, stride2)
239
+ cpython.PyList_SET_ITEM (stride1, d2, stride2)
235
240
236
241
for d3 in range (dims[2 ]):
237
242
if ndims == 3 :
@@ -244,12 +249,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
244
249
decoder_arg)
245
250
246
251
cpython.Py_INCREF(elem)
247
- cpython.PyTuple_SET_ITEM (stride2, d3, elem)
252
+ cpython.PyList_SET_ITEM (stride2, d3, elem)
248
253
249
254
else :
250
- stride3 = cpython.PyTuple_New (dims[3 ])
255
+ stride3 = cpython.PyList_New (dims[3 ])
251
256
cpython.Py_INCREF(stride3)
252
- cpython.PyTuple_SET_ITEM (stride2, d3, stride3)
257
+ cpython.PyList_SET_ITEM (stride2, d3, stride3)
253
258
254
259
for d4 in range (dims[3 ]):
255
260
if ndims == 4 :
@@ -262,12 +267,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
262
267
decoder_arg)
263
268
264
269
cpython.Py_INCREF(elem)
265
- cpython.PyTuple_SET_ITEM (stride3, d4, elem)
270
+ cpython.PyList_SET_ITEM (stride3, d4, elem)
266
271
267
272
else :
268
- stride4 = cpython.PyTuple_New (dims[4 ])
273
+ stride4 = cpython.PyList_New (dims[4 ])
269
274
cpython.Py_INCREF(stride4)
270
- cpython.PyTuple_SET_ITEM (stride3, d4, stride4)
275
+ cpython.PyList_SET_ITEM (stride3, d4, stride4)
271
276
272
277
for d5 in range (dims[4 ]):
273
278
if ndims == 5 :
@@ -280,12 +285,12 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
280
285
decoder_arg)
281
286
282
287
cpython.Py_INCREF(elem)
283
- cpython.PyTuple_SET_ITEM (stride4, d5, elem)
288
+ cpython.PyList_SET_ITEM (stride4, d5, elem)
284
289
285
290
else :
286
- stride5 = cpython.PyTuple_New (dims[5 ])
291
+ stride5 = cpython.PyList_New (dims[5 ])
287
292
cpython.Py_INCREF(stride5)
288
- cpython.PyTuple_SET_ITEM (stride4, d5, stride5)
293
+ cpython.PyList_SET_ITEM (stride4, d5, stride5)
289
294
290
295
for d6 in range (dims[5 ]):
291
296
elem_len = hton.unpack_int32(buf.read(4 ))
@@ -297,7 +302,7 @@ cdef inline _nested_array_decode(ConnectionSettings settings,
297
302
decoder_arg)
298
303
299
304
cpython.Py_INCREF(elem)
300
- cpython.PyTuple_SET_ITEM (stride5, d6, elem)
305
+ cpython.PyList_SET_ITEM (stride5, d6, elem)
301
306
302
307
return result
303
308
0 commit comments