forked from numpy/numpy
/
nditer_pywrap.c
2486 lines (2208 loc) · 69 KB
/
nditer_pywrap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file implements the CPython wrapper of NpyIter
*
* Copyright (c) 2010 by Mark Wiebe (mwwiebe@gmail.com)
* The University of British Columbia
*
* See LICENSE.txt for the license.
*/
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "numpy/arrayobject.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "alloc.h"
#include "common.h"
#include "conversion_utils.h"
#include "ctors.h"
/* Functions not part of the public NumPy C API */
npy_bool npyiter_has_writeback(NpyIter *iter);
typedef struct NewNpyArrayIterObject_tag NewNpyArrayIterObject;
struct NewNpyArrayIterObject_tag {
PyObject_HEAD
/* The iterator */
NpyIter *iter;
/* Flag indicating iteration started/stopped */
char started, finished;
/* Child to update for nested iteration */
NewNpyArrayIterObject *nested_child;
/* Cached values from the iterator */
NpyIter_IterNextFunc *iternext;
NpyIter_GetMultiIndexFunc *get_multi_index;
char **dataptrs;
PyArray_Descr **dtypes;
PyArrayObject **operands;
npy_intp *innerstrides, *innerloopsizeptr;
char readflags[NPY_MAXARGS];
char writeflags[NPY_MAXARGS];
};
static int npyiter_cache_values(NewNpyArrayIterObject *self)
{
NpyIter *iter = self->iter;
/* iternext and get_multi_index functions */
self->iternext = NpyIter_GetIterNext(iter, NULL);
if (self->iternext == NULL) {
return -1;
}
if (NpyIter_HasMultiIndex(iter) && !NpyIter_HasDelayedBufAlloc(iter)) {
self->get_multi_index = NpyIter_GetGetMultiIndex(iter, NULL);
}
else {
self->get_multi_index = NULL;
}
/* Internal data pointers */
self->dataptrs = NpyIter_GetDataPtrArray(iter);
self->dtypes = NpyIter_GetDescrArray(iter);
self->operands = NpyIter_GetOperandArray(iter);
if (NpyIter_HasExternalLoop(iter)) {
self->innerstrides = NpyIter_GetInnerStrideArray(iter);
self->innerloopsizeptr = NpyIter_GetInnerLoopSizePtr(iter);
}
else {
self->innerstrides = NULL;
self->innerloopsizeptr = NULL;
}
/* The read/write settings */
NpyIter_GetReadFlags(iter, self->readflags);
NpyIter_GetWriteFlags(iter, self->writeflags);
return 0;
}
static PyObject *
npyiter_new(PyTypeObject *subtype, PyObject *NPY_UNUSED(args),
PyObject *NPY_UNUSED(kwds))
{
NewNpyArrayIterObject *self;
self = (NewNpyArrayIterObject *)subtype->tp_alloc(subtype, 0);
if (self != NULL) {
self->iter = NULL;
self->nested_child = NULL;
}
return (PyObject *)self;
}
static int
NpyIter_GlobalFlagsConverter(PyObject *flags_in, npy_uint32 *flags)
{
npy_uint32 tmpflags = 0;
int iflags, nflags;
PyObject *f;
char *str = NULL;
Py_ssize_t length = 0;
npy_uint32 flag;
if (flags_in == NULL || flags_in == Py_None) {
return 1;
}
if (!PyTuple_Check(flags_in) && !PyList_Check(flags_in)) {
PyErr_SetString(PyExc_ValueError,
"Iterator global flags must be a list or tuple of strings");
return 0;
}
nflags = PySequence_Size(flags_in);
for (iflags = 0; iflags < nflags; ++iflags) {
f = PySequence_GetItem(flags_in, iflags);
if (f == NULL) {
return 0;
}
if (PyUnicode_Check(f)) {
/* accept unicode input */
PyObject *f_str;
f_str = PyUnicode_AsASCIIString(f);
if (f_str == NULL) {
Py_DECREF(f);
return 0;
}
Py_DECREF(f);
f = f_str;
}
if (PyBytes_AsStringAndSize(f, &str, &length) < 0) {
Py_DECREF(f);
return 0;
}
/* Use switch statements to quickly isolate the right flag */
flag = 0;
switch (str[0]) {
case 'b':
if (strcmp(str, "buffered") == 0) {
flag = NPY_ITER_BUFFERED;
}
break;
case 'c':
if (length >= 6) switch (str[5]) {
case 'e':
if (strcmp(str, "c_index") == 0) {
flag = NPY_ITER_C_INDEX;
}
break;
case 'i':
if (strcmp(str, "copy_if_overlap") == 0) {
flag = NPY_ITER_COPY_IF_OVERLAP;
}
break;
case 'n':
if (strcmp(str, "common_dtype") == 0) {
flag = NPY_ITER_COMMON_DTYPE;
}
break;
}
break;
case 'd':
if (strcmp(str, "delay_bufalloc") == 0) {
flag = NPY_ITER_DELAY_BUFALLOC;
}
break;
case 'e':
if (strcmp(str, "external_loop") == 0) {
flag = NPY_ITER_EXTERNAL_LOOP;
}
break;
case 'f':
if (strcmp(str, "f_index") == 0) {
flag = NPY_ITER_F_INDEX;
}
break;
case 'g':
/*
* Documentation is grow_inner, but initial implementation
* was growinner, so allowing for either.
*/
if (strcmp(str, "grow_inner") == 0 ||
strcmp(str, "growinner") == 0) {
flag = NPY_ITER_GROWINNER;
}
break;
case 'm':
if (strcmp(str, "multi_index") == 0) {
flag = NPY_ITER_MULTI_INDEX;
}
break;
case 'r':
if (strcmp(str, "ranged") == 0) {
flag = NPY_ITER_RANGED;
}
else if (strcmp(str, "refs_ok") == 0) {
flag = NPY_ITER_REFS_OK;
}
else if (strcmp(str, "reduce_ok") == 0) {
flag = NPY_ITER_REDUCE_OK;
}
break;
case 'z':
if (strcmp(str, "zerosize_ok") == 0) {
flag = NPY_ITER_ZEROSIZE_OK;
}
break;
}
if (flag == 0) {
PyErr_Format(PyExc_ValueError,
"Unexpected iterator global flag \"%s\"", str);
Py_DECREF(f);
return 0;
}
else {
tmpflags |= flag;
}
Py_DECREF(f);
}
*flags |= tmpflags;
return 1;
}
static int
NpyIter_OpFlagsConverter(PyObject *op_flags_in,
npy_uint32 *op_flags)
{
int iflags, nflags;
npy_uint32 flag;
if (!PyTuple_Check(op_flags_in) && !PyList_Check(op_flags_in)) {
PyErr_SetString(PyExc_ValueError,
"op_flags must be a tuple or array of per-op flag-tuples");
return 0;
}
nflags = PySequence_Size(op_flags_in);
*op_flags = 0;
for (iflags = 0; iflags < nflags; ++iflags) {
PyObject *f;
char *str = NULL;
Py_ssize_t length = 0;
f = PySequence_GetItem(op_flags_in, iflags);
if (f == NULL) {
return 0;
}
if (PyUnicode_Check(f)) {
/* accept unicode input */
PyObject *f_str;
f_str = PyUnicode_AsASCIIString(f);
if (f_str == NULL) {
Py_DECREF(f);
return 0;
}
Py_DECREF(f);
f = f_str;
}
if (PyBytes_AsStringAndSize(f, &str, &length) < 0) {
PyErr_Clear();
Py_DECREF(f);
PyErr_SetString(PyExc_ValueError,
"op_flags must be a tuple or array of per-op flag-tuples");
return 0;
}
/* Use switch statements to quickly isolate the right flag */
flag = 0;
switch (str[0]) {
case 'a':
if (length > 2) switch(str[2]) {
case 'i':
if (strcmp(str, "aligned") == 0) {
flag = NPY_ITER_ALIGNED;
}
break;
case 'l':
if (strcmp(str, "allocate") == 0) {
flag = NPY_ITER_ALLOCATE;
}
break;
case 'r':
if (strcmp(str, "arraymask") == 0) {
flag = NPY_ITER_ARRAYMASK;
}
break;
}
break;
case 'c':
if (strcmp(str, "copy") == 0) {
flag = NPY_ITER_COPY;
}
if (strcmp(str, "contig") == 0) {
flag = NPY_ITER_CONTIG;
}
break;
case 'n':
switch (str[1]) {
case 'b':
if (strcmp(str, "nbo") == 0) {
flag = NPY_ITER_NBO;
}
break;
case 'o':
if (strcmp(str, "no_subtype") == 0) {
flag = NPY_ITER_NO_SUBTYPE;
}
else if (strcmp(str, "no_broadcast") == 0) {
flag = NPY_ITER_NO_BROADCAST;
}
break;
}
break;
case 'o':
if (strcmp(str, "overlap_assume_elementwise") == 0) {
flag = NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE;
}
break;
case 'r':
if (length > 4) switch (str[4]) {
case 'o':
if (strcmp(str, "readonly") == 0) {
flag = NPY_ITER_READONLY;
}
break;
case 'w':
if (strcmp(str, "readwrite") == 0) {
flag = NPY_ITER_READWRITE;
}
break;
}
break;
case 'u':
switch (str[1]) {
case 'p':
if (strcmp(str, "updateifcopy") == 0) {
flag = NPY_ITER_UPDATEIFCOPY;
}
break;
}
break;
case 'v':
if (strcmp(str, "virtual") == 0) {
flag = NPY_ITER_VIRTUAL;
}
break;
case 'w':
if (length > 5) switch (str[5]) {
case 'o':
if (strcmp(str, "writeonly") == 0) {
flag = NPY_ITER_WRITEONLY;
}
break;
case 'm':
if (strcmp(str, "writemasked") == 0) {
flag = NPY_ITER_WRITEMASKED;
}
break;
}
break;
}
if (flag == 0) {
PyErr_Format(PyExc_ValueError,
"Unexpected per-op iterator flag \"%s\"", str);
Py_DECREF(f);
return 0;
}
else {
*op_flags |= flag;
}
Py_DECREF(f);
}
return 1;
}
static int
npyiter_convert_op_flags_array(PyObject *op_flags_in,
npy_uint32 *op_flags_array, npy_intp nop)
{
npy_intp iop;
if (!PyTuple_Check(op_flags_in) && !PyList_Check(op_flags_in)) {
PyErr_SetString(PyExc_ValueError,
"op_flags must be a tuple or array of per-op flag-tuples");
return 0;
}
if (PySequence_Size(op_flags_in) != nop) {
goto try_single_flags;
}
for (iop = 0; iop < nop; ++iop) {
PyObject *f = PySequence_GetItem(op_flags_in, iop);
if (f == NULL) {
return 0;
}
/* If the first item is a string, try as one set of flags */
if (iop == 0 && (PyBytes_Check(f) || PyUnicode_Check(f))) {
Py_DECREF(f);
goto try_single_flags;
}
if (NpyIter_OpFlagsConverter(f,
&op_flags_array[iop]) != 1) {
Py_DECREF(f);
return 0;
}
Py_DECREF(f);
}
return 1;
try_single_flags:
if (NpyIter_OpFlagsConverter(op_flags_in,
&op_flags_array[0]) != 1) {
return 0;
}
for (iop = 1; iop < nop; ++iop) {
op_flags_array[iop] = op_flags_array[0];
}
return 1;
}
static int
npyiter_convert_dtypes(PyObject *op_dtypes_in,
PyArray_Descr **op_dtypes,
npy_intp nop)
{
npy_intp iop;
/*
* If the input isn't a tuple of dtypes, try converting it as-is
* to a dtype, and replicating to all operands.
*/
if ((!PyTuple_Check(op_dtypes_in) && !PyList_Check(op_dtypes_in)) ||
PySequence_Size(op_dtypes_in) != nop) {
goto try_single_dtype;
}
for (iop = 0; iop < nop; ++iop) {
PyObject *dtype = PySequence_GetItem(op_dtypes_in, iop);
if (dtype == NULL) {
npy_intp i;
for (i = 0; i < iop; ++i ) {
Py_XDECREF(op_dtypes[i]);
}
return 0;
}
/* Try converting the object to a descr */
if (PyArray_DescrConverter2(dtype, &op_dtypes[iop]) != 1) {
npy_intp i;
for (i = 0; i < iop; ++i ) {
Py_XDECREF(op_dtypes[i]);
}
Py_DECREF(dtype);
PyErr_Clear();
goto try_single_dtype;
}
Py_DECREF(dtype);
}
return 1;
try_single_dtype:
if (PyArray_DescrConverter2(op_dtypes_in, &op_dtypes[0]) == 1) {
for (iop = 1; iop < nop; ++iop) {
op_dtypes[iop] = op_dtypes[0];
Py_XINCREF(op_dtypes[iop]);
}
return 1;
}
return 0;
}
static int
npyiter_convert_op_axes(PyObject *op_axes_in, int nop,
int **op_axes, int *oa_ndim)
{
PyObject *a;
int iop;
if ((!PyTuple_Check(op_axes_in) && !PyList_Check(op_axes_in)) ||
PySequence_Size(op_axes_in) != nop) {
PyErr_SetString(PyExc_ValueError,
"op_axes must be a tuple/list matching the number of ops");
return 0;
}
*oa_ndim = -1;
/* Copy the tuples into op_axes */
for (iop = 0; iop < nop; ++iop) {
int idim;
a = PySequence_GetItem(op_axes_in, iop);
if (a == NULL) {
return 0;
}
if (a == Py_None) {
op_axes[iop] = NULL;
} else {
if (!PyTuple_Check(a) && !PyList_Check(a)) {
PyErr_SetString(PyExc_ValueError,
"Each entry of op_axes must be None "
"or a tuple/list");
Py_DECREF(a);
return 0;
}
if (*oa_ndim == -1) {
*oa_ndim = PySequence_Size(a);
if (*oa_ndim > NPY_MAXDIMS) {
PyErr_SetString(PyExc_ValueError,
"Too many dimensions in op_axes");
Py_DECREF(a);
return 0;
}
}
if (PySequence_Size(a) != *oa_ndim) {
PyErr_SetString(PyExc_ValueError,
"Each entry of op_axes must have the same size");
Py_DECREF(a);
return 0;
}
for (idim = 0; idim < *oa_ndim; ++idim) {
PyObject *v = PySequence_GetItem(a, idim);
if (v == NULL) {
Py_DECREF(a);
return 0;
}
/* numpy.newaxis is None */
if (v == Py_None) {
op_axes[iop][idim] = -1;
}
else {
op_axes[iop][idim] = PyArray_PyIntAsInt(v);
if (op_axes[iop][idim]==-1 &&
PyErr_Occurred()) {
Py_DECREF(a);
Py_DECREF(v);
return 0;
}
}
Py_DECREF(v);
}
}
Py_DECREF(a);
}
if (*oa_ndim == -1) {
PyErr_SetString(PyExc_ValueError,
"If op_axes is provided, at least one list of axes "
"must be contained within it");
return 0;
}
return 1;
}
/*
* Converts the operand array and op_flags array into the form
* NpyIter_AdvancedNew needs. Sets nop, and on success, each
* op[i] owns a reference to an array object.
*/
static int
npyiter_convert_ops(PyObject *op_in, PyObject *op_flags_in,
PyArrayObject **op, npy_uint32 *op_flags,
int *nop_out)
{
int iop, nop;
/* nop and op */
if (PyTuple_Check(op_in) || PyList_Check(op_in)) {
nop = PySequence_Size(op_in);
if (nop == 0) {
PyErr_SetString(PyExc_ValueError,
"Must provide at least one operand");
return 0;
}
if (nop > NPY_MAXARGS) {
PyErr_SetString(PyExc_ValueError, "Too many operands");
return 0;
}
for (iop = 0; iop < nop; ++iop) {
PyObject *item = PySequence_GetItem(op_in, iop);
if (item == NULL) {
npy_intp i;
for (i = 0; i < iop; ++i) {
Py_XDECREF(op[i]);
}
return 0;
}
else if (item == Py_None) {
Py_DECREF(item);
item = NULL;
}
/* This is converted to an array after op flags are retrieved */
op[iop] = (PyArrayObject *)item;
}
}
else {
nop = 1;
/* Is converted to an array after op flags are retrieved */
Py_INCREF(op_in);
op[0] = (PyArrayObject *)op_in;
}
*nop_out = nop;
/* op_flags */
if (op_flags_in == NULL || op_flags_in == Py_None) {
for (iop = 0; iop < nop; ++iop) {
/*
* By default, make NULL operands writeonly and flagged for
* allocation, and everything else readonly. To write
* to a provided operand, you must specify the write flag manually.
*/
if (op[iop] == NULL) {
op_flags[iop] = NPY_ITER_WRITEONLY | NPY_ITER_ALLOCATE;
}
else {
op_flags[iop] = NPY_ITER_READONLY;
}
}
}
else if (npyiter_convert_op_flags_array(op_flags_in,
op_flags, nop) != 1) {
for (iop = 0; iop < nop; ++iop) {
Py_XDECREF(op[iop]);
}
*nop_out = 0;
return 0;
}
/* Now that we have the flags - convert all the ops to arrays */
for (iop = 0; iop < nop; ++iop) {
if (op[iop] != NULL) {
PyArrayObject *ao;
int fromanyflags = 0;
if (op_flags[iop]&(NPY_ITER_READWRITE|NPY_ITER_WRITEONLY)) {
fromanyflags |= NPY_ARRAY_WRITEBACKIFCOPY;
}
ao = (PyArrayObject *)PyArray_FROM_OF((PyObject *)op[iop],
fromanyflags);
if (ao == NULL) {
if (PyErr_Occurred() &&
PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_SetString(PyExc_TypeError,
"Iterator operand is flagged as writeable, "
"but is an object which cannot be written "
"back to via WRITEBACKIFCOPY");
}
for (iop = 0; iop < nop; ++iop) {
Py_DECREF(op[iop]);
}
*nop_out = 0;
return 0;
}
Py_DECREF(op[iop]);
op[iop] = ao;
}
}
return 1;
}
static int
npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"op", "flags", "op_flags", "op_dtypes",
"order", "casting", "op_axes", "itershape",
"buffersize",
NULL};
PyObject *op_in = NULL, *op_flags_in = NULL,
*op_dtypes_in = NULL, *op_axes_in = NULL;
int iop, nop = 0;
PyArrayObject *op[NPY_MAXARGS];
npy_uint32 flags = 0;
NPY_ORDER order = NPY_KEEPORDER;
NPY_CASTING casting = NPY_SAFE_CASTING;
npy_uint32 op_flags[NPY_MAXARGS];
PyArray_Descr *op_request_dtypes[NPY_MAXARGS];
int oa_ndim = -1;
int op_axes_arrays[NPY_MAXARGS][NPY_MAXDIMS];
int *op_axes[NPY_MAXARGS];
PyArray_Dims itershape = {NULL, -1};
int buffersize = 0;
if (self->iter != NULL) {
PyErr_SetString(PyExc_ValueError,
"Iterator was already initialized");
return -1;
}
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&OOO&O&OO&i:nditer", kwlist,
&op_in,
NpyIter_GlobalFlagsConverter, &flags,
&op_flags_in,
&op_dtypes_in,
PyArray_OrderConverter, &order,
PyArray_CastingConverter, &casting,
&op_axes_in,
PyArray_OptionalIntpConverter, &itershape,
&buffersize)) {
npy_free_cache_dim_obj(itershape);
return -1;
}
/* Set the dtypes and ops to all NULL to start */
memset(op_request_dtypes, 0, sizeof(op_request_dtypes));
/* op and op_flags */
if (npyiter_convert_ops(op_in, op_flags_in, op, op_flags, &nop)
!= 1) {
goto fail;
}
/* op_request_dtypes */
if (op_dtypes_in != NULL && op_dtypes_in != Py_None &&
npyiter_convert_dtypes(op_dtypes_in,
op_request_dtypes, nop) != 1) {
goto fail;
}
/* op_axes */
if (op_axes_in != NULL && op_axes_in != Py_None) {
/* Initialize to point to the op_axes arrays */
for (iop = 0; iop < nop; ++iop) {
op_axes[iop] = op_axes_arrays[iop];
}
if (npyiter_convert_op_axes(op_axes_in, nop,
op_axes, &oa_ndim) != 1) {
goto fail;
}
}
if (itershape.len != -1) {
if (oa_ndim == -1) {
oa_ndim = itershape.len;
memset(op_axes, 0, sizeof(op_axes[0]) * nop);
}
else if (oa_ndim != itershape.len) {
PyErr_SetString(PyExc_ValueError,
"'op_axes' and 'itershape' must have the same number "
"of entries equal to the iterator ndim");
goto fail;
}
}
self->iter = NpyIter_AdvancedNew(nop, op, flags, order, casting, op_flags,
op_request_dtypes,
oa_ndim, oa_ndim >= 0 ? op_axes : NULL,
itershape.ptr,
buffersize);
if (self->iter == NULL) {
goto fail;
}
/* Cache some values for the member functions to use */
if (npyiter_cache_values(self) < 0) {
goto fail;
}
if (NpyIter_GetIterSize(self->iter) == 0) {
self->started = 1;
self->finished = 1;
}
else {
self->started = 0;
self->finished = 0;
}
npy_free_cache_dim_obj(itershape);
/* Release the references we got to the ops and dtypes */
for (iop = 0; iop < nop; ++iop) {
Py_XDECREF(op[iop]);
Py_XDECREF(op_request_dtypes[iop]);
}
return 0;
fail:
npy_free_cache_dim_obj(itershape);
for (iop = 0; iop < nop; ++iop) {
Py_XDECREF(op[iop]);
Py_XDECREF(op_request_dtypes[iop]);
}
return -1;
}
NPY_NO_EXPORT PyObject *
NpyIter_NestedIters(PyObject *NPY_UNUSED(self),
PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"op", "axes", "flags", "op_flags",
"op_dtypes", "order",
"casting", "buffersize",
NULL};
PyObject *op_in = NULL, *axes_in = NULL,
*op_flags_in = NULL, *op_dtypes_in = NULL;
int iop, nop = 0, inest, nnest = 0;
PyArrayObject *op[NPY_MAXARGS];
npy_uint32 flags = 0, flags_inner;
NPY_ORDER order = NPY_KEEPORDER;
NPY_CASTING casting = NPY_SAFE_CASTING;
npy_uint32 op_flags[NPY_MAXARGS], op_flags_inner[NPY_MAXARGS];
PyArray_Descr *op_request_dtypes[NPY_MAXARGS],
*op_request_dtypes_inner[NPY_MAXARGS];
int op_axes_data[NPY_MAXDIMS];
int *nested_op_axes[NPY_MAXDIMS];
int nested_naxes[NPY_MAXDIMS], iaxes, naxes;
int negones[NPY_MAXDIMS];
char used_axes[NPY_MAXDIMS];
int buffersize = 0;
PyObject *ret = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O&OOO&O&i", kwlist,
&op_in,
&axes_in,
NpyIter_GlobalFlagsConverter, &flags,
&op_flags_in,
&op_dtypes_in,
PyArray_OrderConverter, &order,
PyArray_CastingConverter, &casting,
&buffersize)) {
return NULL;
}
/* axes */
if (!PyTuple_Check(axes_in) && !PyList_Check(axes_in)) {
PyErr_SetString(PyExc_ValueError,
"axes must be a tuple of axis arrays");
return NULL;
}
nnest = PySequence_Size(axes_in);
if (nnest < 2) {
PyErr_SetString(PyExc_ValueError,
"axes must have at least 2 entries for nested iteration");
return NULL;
}
naxes = 0;
memset(used_axes, 0, NPY_MAXDIMS);
for (inest = 0; inest < nnest; ++inest) {
PyObject *item = PySequence_GetItem(axes_in, inest);
npy_intp i;
if (item == NULL) {
return NULL;
}
if (!PyTuple_Check(item) && !PyList_Check(item)) {
PyErr_SetString(PyExc_ValueError,
"Each item in axes must be a an integer tuple");
Py_DECREF(item);
return NULL;
}
nested_naxes[inest] = PySequence_Size(item);
if (naxes + nested_naxes[inest] > NPY_MAXDIMS) {
PyErr_SetString(PyExc_ValueError,
"Too many axes given");
Py_DECREF(item);
return NULL;
}
for (i = 0; i < nested_naxes[inest]; ++i) {
PyObject *v = PySequence_GetItem(item, i);
npy_intp axis;
if (v == NULL) {
Py_DECREF(item);
return NULL;
}
axis = PyLong_AsLong(v);
Py_DECREF(v);
if (axis < 0 || axis >= NPY_MAXDIMS) {
PyErr_SetString(PyExc_ValueError,
"An axis is out of bounds");
Py_DECREF(item);
return NULL;
}
/*
* This check is very important, without it out of bounds
* data accesses are possible.
*/
if (used_axes[axis] != 0) {
PyErr_SetString(PyExc_ValueError,
"An axis is used more than once");
Py_DECREF(item);
return NULL;
}
used_axes[axis] = 1;
op_axes_data[naxes+i] = axis;
}
nested_op_axes[inest] = &op_axes_data[naxes];
naxes += nested_naxes[inest];
Py_DECREF(item);
}
/* op and op_flags */
if (npyiter_convert_ops(op_in, op_flags_in, op, op_flags, &nop)
!= 1) {
return NULL;
}
/* Set the dtypes to all NULL to start as well */
memset(op_request_dtypes, 0, sizeof(op_request_dtypes[0])*nop);
memset(op_request_dtypes_inner, 0,
sizeof(op_request_dtypes_inner[0])*nop);
/* op_request_dtypes */
if (op_dtypes_in != NULL && op_dtypes_in != Py_None &&
npyiter_convert_dtypes(op_dtypes_in,
op_request_dtypes, nop) != 1) {
goto fail;
}
ret = PyTuple_New(nnest);
if (ret == NULL) {
goto fail;
}
/* For broadcasting allocated arrays */
for (iaxes = 0; iaxes < naxes; ++iaxes) {
negones[iaxes] = -1;
}
/*
* Clear any unnecessary ALLOCATE flags, so we can use them
* to indicate exactly the allocated outputs. Also, separate
* the inner loop flags.
*/
for (iop = 0; iop < nop; ++iop) {
if ((op_flags[iop]&NPY_ITER_ALLOCATE) && op[iop] != NULL) {
op_flags[iop] &= ~NPY_ITER_ALLOCATE;
}
/*
* Clear any flags allowing copies or output allocation for
* the inner loop.
*/
op_flags_inner[iop] = op_flags[iop] & ~(NPY_ITER_COPY|
NPY_ITER_UPDATEIFCOPY|
NPY_ITER_ALLOCATE);
/*
* If buffering is enabled and copying is not,
* clear the nbo_aligned flag and strip the data type
* for the outer loops.
*/
if ((flags&(NPY_ITER_BUFFERED)) &&
!(op_flags[iop]&(NPY_ITER_COPY|
NPY_ITER_UPDATEIFCOPY|
NPY_ITER_ALLOCATE))) {
op_flags[iop] &= ~(NPY_ITER_NBO|NPY_ITER_ALIGNED|NPY_ITER_CONTIG);
op_request_dtypes_inner[iop] = op_request_dtypes[iop];
op_request_dtypes[iop] = NULL;
}
}
/* Only the inner loop gets the buffering and no inner flags */
flags_inner = flags&~NPY_ITER_COMMON_DTYPE;
flags &= ~(NPY_ITER_EXTERNAL_LOOP|
NPY_ITER_BUFFERED);
for (inest = 0; inest < nnest; ++inest) {
NewNpyArrayIterObject *iter;
int *op_axes_nop[NPY_MAXARGS];
/*
* All the operands' op_axes are the same, except for
* allocated outputs.
*/
for (iop = 0; iop < nop; ++iop) {
if (op_flags[iop]&NPY_ITER_ALLOCATE) {
if (inest == 0) {
op_axes_nop[iop] = NULL;