-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
conversion_utils.c
1140 lines (1055 loc) · 29.6 KB
/
conversion_utils.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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "numpy/arrayobject.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "common.h"
#include "arraytypes.h"
#include "conversion_utils.h"
/****************************************************************
* Useful function for conversion when used with PyArg_ParseTuple
****************************************************************/
/*NUMPY_API
*
* Useful to pass as converter function for O& processing in PyArgs_ParseTuple.
*
* This conversion function can be used with the "O&" argument for
* PyArg_ParseTuple. It will immediately return an object of array type
* or will convert to a NPY_ARRAY_CARRAY any other object.
*
* If you use PyArray_Converter, you must DECREF the array when finished
* as you get a new reference to it.
*/
NPY_NO_EXPORT int
PyArray_Converter(PyObject *object, PyObject **address)
{
if (PyArray_Check(object)) {
*address = object;
Py_INCREF(object);
return NPY_SUCCEED;
}
else {
*address = PyArray_FromAny(object, NULL, 0, 0,
NPY_ARRAY_CARRAY, NULL);
if (*address == NULL) {
return NPY_FAIL;
}
return NPY_SUCCEED;
}
}
/*NUMPY_API
* Useful to pass as converter function for O& processing in
* PyArgs_ParseTuple for output arrays
*/
NPY_NO_EXPORT int
PyArray_OutputConverter(PyObject *object, PyArrayObject **address)
{
if (object == NULL || object == Py_None) {
*address = NULL;
return NPY_SUCCEED;
}
if (PyArray_Check(object)) {
*address = (PyArrayObject *)object;
return NPY_SUCCEED;
}
else {
PyErr_SetString(PyExc_TypeError,
"output must be an array");
*address = NULL;
return NPY_FAIL;
}
}
/*NUMPY_API
* Get intp chunk from sequence
*
* This function takes a Python sequence object and allocates and
* fills in an intp array with the converted values.
*
* Remember to free the pointer seq.ptr when done using
* PyDimMem_FREE(seq.ptr)**
*/
NPY_NO_EXPORT int
PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq)
{
Py_ssize_t len;
int nd;
seq->ptr = NULL;
seq->len = 0;
if (obj == Py_None) {
return NPY_SUCCEED;
}
len = PySequence_Size(obj);
if (len == -1) {
/* Check to see if it is an integer number */
if (PyNumber_Check(obj)) {
/*
* After the deprecation the PyNumber_Check could be replaced
* by PyIndex_Check.
*/
len = 1;
}
}
if (len < 0) {
PyErr_SetString(PyExc_TypeError,
"expected sequence object with len >= 0 or a single integer");
return NPY_FAIL;
}
if (len > NPY_MAXDIMS) {
PyErr_Format(PyExc_ValueError, "sequence too large; "
"must be smaller than %d", NPY_MAXDIMS);
return NPY_FAIL;
}
if (len > 0) {
seq->ptr = PyDimMem_NEW(len);
if (seq->ptr == NULL) {
PyErr_NoMemory();
return NPY_FAIL;
}
}
seq->len = len;
nd = PyArray_IntpFromIndexSequence(obj, (npy_intp *)seq->ptr, len);
if (nd == -1 || nd != len) {
PyDimMem_FREE(seq->ptr);
seq->ptr = NULL;
return NPY_FAIL;
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Get buffer chunk from object
*
* this function takes a Python object which exposes the (single-segment)
* buffer interface and returns a pointer to the data segment
*
* You should increment the reference count by one of buf->base
* if you will hang on to a reference
*
* You only get a borrowed reference to the object. Do not free the
* memory...
*/
NPY_NO_EXPORT int
PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf)
{
Py_ssize_t buflen;
buf->ptr = NULL;
buf->flags = NPY_ARRAY_BEHAVED;
buf->base = NULL;
if (obj == Py_None) {
return NPY_SUCCEED;
}
if (PyObject_AsWriteBuffer(obj, &(buf->ptr), &buflen) < 0) {
PyErr_Clear();
buf->flags &= ~NPY_ARRAY_WRITEABLE;
if (PyObject_AsReadBuffer(obj, (const void **)&(buf->ptr),
&buflen) < 0) {
return NPY_FAIL;
}
}
buf->len = (npy_intp) buflen;
/* Point to the base of the buffer object if present */
#if defined(NPY_PY3K)
if (PyMemoryView_Check(obj)) {
buf->base = PyMemoryView_GET_BASE(obj);
}
#else
if (PyBuffer_Check(obj)) {
buf->base = ((PyArray_Chunk *)obj)->base;
}
#endif
if (buf->base == NULL) {
buf->base = obj;
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Get axis from an object (possibly None) -- a converter function,
*
* See also PyArray_ConvertMultiAxis, which also handles a tuple of axes.
*/
NPY_NO_EXPORT int
PyArray_AxisConverter(PyObject *obj, int *axis)
{
if (obj == Py_None) {
*axis = NPY_MAXDIMS;
}
else {
*axis = PyArray_PyIntAsInt(obj);
if (PyErr_Occurred()) {
return NPY_FAIL;
}
}
return NPY_SUCCEED;
}
/*
* Converts an axis parameter into an ndim-length C-array of
* boolean flags, True for each axis specified.
*
* If obj is None or NULL, everything is set to True. If obj is a tuple,
* each axis within the tuple is set to True. If obj is an integer,
* just that axis is set to True.
*/
NPY_NO_EXPORT int
PyArray_ConvertMultiAxis(PyObject *axis_in, int ndim, npy_bool *out_axis_flags)
{
/* None means all of the axes */
if (axis_in == Py_None || axis_in == NULL) {
memset(out_axis_flags, 1, ndim);
return NPY_SUCCEED;
}
/* A tuple of which axes */
else if (PyTuple_Check(axis_in)) {
int i, naxes;
memset(out_axis_flags, 0, ndim);
naxes = PyTuple_Size(axis_in);
if (naxes < 0) {
return NPY_FAIL;
}
for (i = 0; i < naxes; ++i) {
PyObject *tmp = PyTuple_GET_ITEM(axis_in, i);
int axis = PyArray_PyIntAsInt(tmp);
int axis_orig = axis;
if (error_converting(axis)) {
return NPY_FAIL;
}
if (axis < 0) {
axis += ndim;
}
if (axis < 0 || axis >= ndim) {
PyErr_Format(PyExc_ValueError,
"'axis' entry %d is out of bounds [-%d, %d)",
axis_orig, ndim, ndim);
return NPY_FAIL;
}
if (out_axis_flags[axis]) {
PyErr_SetString(PyExc_ValueError,
"duplicate value in 'axis'");
return NPY_FAIL;
}
out_axis_flags[axis] = 1;
}
return NPY_SUCCEED;
}
/* Try to interpret axis as an integer */
else {
int axis, axis_orig;
memset(out_axis_flags, 0, ndim);
axis = PyArray_PyIntAsInt(axis_in);
axis_orig = axis;
if (error_converting(axis)) {
return NPY_FAIL;
}
if (axis < 0) {
axis += ndim;
}
/*
* Special case letting axis={-1,0} slip through for scalars,
* for backwards compatibility reasons.
*/
if (ndim == 0 && (axis == 0 || axis == -1)) {
return NPY_SUCCEED;
}
if (axis < 0 || axis >= ndim) {
PyErr_Format(PyExc_ValueError,
"'axis' entry %d is out of bounds [-%d, %d)",
axis_orig, ndim, ndim);
return NPY_FAIL;
}
out_axis_flags[axis] = 1;
return NPY_SUCCEED;
}
}
/*NUMPY_API
* Convert an object to true / false
*/
NPY_NO_EXPORT int
PyArray_BoolConverter(PyObject *object, npy_bool *val)
{
if (PyObject_IsTrue(object)) {
*val = NPY_TRUE;
}
else {
*val = NPY_FALSE;
}
if (PyErr_Occurred()) {
return NPY_FAIL;
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert object to endian
*/
NPY_NO_EXPORT int
PyArray_ByteorderConverter(PyObject *obj, char *endian)
{
char *str;
PyObject *tmp = NULL;
if (PyUnicode_Check(obj)) {
obj = tmp = PyUnicode_AsASCIIString(obj);
}
*endian = NPY_SWAP;
str = PyBytes_AsString(obj);
if (!str) {
Py_XDECREF(tmp);
return NPY_FAIL;
}
if (strlen(str) < 1) {
PyErr_SetString(PyExc_ValueError,
"Byteorder string must be at least length 1");
Py_XDECREF(tmp);
return NPY_FAIL;
}
*endian = str[0];
if (str[0] != NPY_BIG && str[0] != NPY_LITTLE
&& str[0] != NPY_NATIVE && str[0] != NPY_IGNORE) {
if (str[0] == 'b' || str[0] == 'B') {
*endian = NPY_BIG;
}
else if (str[0] == 'l' || str[0] == 'L') {
*endian = NPY_LITTLE;
}
else if (str[0] == 'n' || str[0] == 'N') {
*endian = NPY_NATIVE;
}
else if (str[0] == 'i' || str[0] == 'I') {
*endian = NPY_IGNORE;
}
else if (str[0] == 's' || str[0] == 'S') {
*endian = NPY_SWAP;
}
else {
PyErr_Format(PyExc_ValueError,
"%s is an unrecognized byteorder",
str);
Py_XDECREF(tmp);
return NPY_FAIL;
}
}
Py_XDECREF(tmp);
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert object to sort kind
*/
NPY_NO_EXPORT int
PyArray_SortkindConverter(PyObject *obj, NPY_SORTKIND *sortkind)
{
char *str;
PyObject *tmp = NULL;
if (PyUnicode_Check(obj)) {
obj = tmp = PyUnicode_AsASCIIString(obj);
}
*sortkind = NPY_QUICKSORT;
str = PyBytes_AsString(obj);
if (!str) {
Py_XDECREF(tmp);
return NPY_FAIL;
}
if (strlen(str) < 1) {
PyErr_SetString(PyExc_ValueError,
"Sort kind string must be at least length 1");
Py_XDECREF(tmp);
return NPY_FAIL;
}
if (str[0] == 'q' || str[0] == 'Q') {
*sortkind = NPY_QUICKSORT;
}
else if (str[0] == 'h' || str[0] == 'H') {
*sortkind = NPY_HEAPSORT;
}
else if (str[0] == 'm' || str[0] == 'M') {
*sortkind = NPY_MERGESORT;
}
else {
PyErr_Format(PyExc_ValueError,
"%s is an unrecognized kind of sort",
str);
Py_XDECREF(tmp);
return NPY_FAIL;
}
Py_XDECREF(tmp);
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert object to searchsorted side
*/
NPY_NO_EXPORT int
PyArray_SearchsideConverter(PyObject *obj, void *addr)
{
NPY_SEARCHSIDE *side = (NPY_SEARCHSIDE *)addr;
char *str;
PyObject *tmp = NULL;
if (PyUnicode_Check(obj)) {
obj = tmp = PyUnicode_AsASCIIString(obj);
}
str = PyBytes_AsString(obj);
if (!str || strlen(str) < 1) {
PyErr_SetString(PyExc_ValueError,
"expected nonempty string for keyword 'side'");
Py_XDECREF(tmp);
return NPY_FAIL;
}
if (str[0] == 'l' || str[0] == 'L') {
*side = NPY_SEARCHLEFT;
}
else if (str[0] == 'r' || str[0] == 'R') {
*side = NPY_SEARCHRIGHT;
}
else {
PyErr_Format(PyExc_ValueError,
"'%s' is an invalid value for keyword 'side'", str);
Py_XDECREF(tmp);
return NPY_FAIL;
}
Py_XDECREF(tmp);
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert an object to FORTRAN / C / ANY / KEEP
*/
NPY_NO_EXPORT int
PyArray_OrderConverter(PyObject *object, NPY_ORDER *val)
{
char *str;
/* Leave the desired default from the caller for NULL/Py_None */
if (object == NULL || object == Py_None) {
return NPY_SUCCEED;
}
else if (PyUnicode_Check(object)) {
PyObject *tmp;
int ret;
tmp = PyUnicode_AsASCIIString(object);
ret = PyArray_OrderConverter(tmp, val);
Py_DECREF(tmp);
return ret;
}
else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) {
if (PyObject_IsTrue(object)) {
*val = NPY_FORTRANORDER;
}
else {
*val = NPY_CORDER;
}
if (PyErr_Occurred()) {
return NPY_FAIL;
}
return NPY_SUCCEED;
}
else {
str = PyBytes_AS_STRING(object);
if (str[0] == 'C' || str[0] == 'c') {
*val = NPY_CORDER;
}
else if (str[0] == 'F' || str[0] == 'f') {
*val = NPY_FORTRANORDER;
}
else if (str[0] == 'A' || str[0] == 'a') {
*val = NPY_ANYORDER;
}
else if (str[0] == 'K' || str[0] == 'k') {
*val = NPY_KEEPORDER;
}
else {
PyErr_SetString(PyExc_TypeError,
"order not understood");
return NPY_FAIL;
}
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert an object to NPY_RAISE / NPY_CLIP / NPY_WRAP
*/
NPY_NO_EXPORT int
PyArray_ClipmodeConverter(PyObject *object, NPY_CLIPMODE *val)
{
if (object == NULL || object == Py_None) {
*val = NPY_RAISE;
}
else if (PyBytes_Check(object)) {
char *str;
str = PyBytes_AS_STRING(object);
if (str[0] == 'C' || str[0] == 'c') {
*val = NPY_CLIP;
}
else if (str[0] == 'W' || str[0] == 'w') {
*val = NPY_WRAP;
}
else if (str[0] == 'R' || str[0] == 'r') {
*val = NPY_RAISE;
}
else {
PyErr_SetString(PyExc_TypeError,
"clipmode not understood");
return NPY_FAIL;
}
}
else if (PyUnicode_Check(object)) {
PyObject *tmp;
int ret;
tmp = PyUnicode_AsASCIIString(object);
ret = PyArray_ClipmodeConverter(tmp, val);
Py_DECREF(tmp);
return ret;
}
else {
int number = PyArray_PyIntAsInt(object);
if (error_converting(number)) {
goto fail;
}
if (number <= (int) NPY_RAISE
&& number >= (int) NPY_CLIP) {
*val = (NPY_CLIPMODE) number;
}
else {
goto fail;
}
}
return NPY_SUCCEED;
fail:
PyErr_SetString(PyExc_TypeError,
"clipmode not understood");
return NPY_FAIL;
}
/*NUMPY_API
* Convert an object to an array of n NPY_CLIPMODE values.
* This is intended to be used in functions where a different mode
* could be applied to each axis, like in ravel_multi_index.
*/
NPY_NO_EXPORT int
PyArray_ConvertClipmodeSequence(PyObject *object, NPY_CLIPMODE *modes, int n)
{
int i;
/* Get the clip mode(s) */
if (object && (PyTuple_Check(object) || PyList_Check(object))) {
if (PySequence_Size(object) != n) {
PyErr_Format(PyExc_ValueError,
"list of clipmodes has wrong length (%d instead of %d)",
(int)PySequence_Size(object), n);
return NPY_FAIL;
}
for (i = 0; i < n; ++i) {
PyObject *item = PySequence_GetItem(object, i);
if(item == NULL) {
return NPY_FAIL;
}
if(PyArray_ClipmodeConverter(item, &modes[i]) != NPY_SUCCEED) {
Py_DECREF(item);
return NPY_FAIL;
}
Py_DECREF(item);
}
}
else if (PyArray_ClipmodeConverter(object, &modes[0]) == NPY_SUCCEED) {
for (i = 1; i < n; ++i) {
modes[i] = modes[0];
}
}
else {
return NPY_FAIL;
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Convert any Python object, *obj*, to an NPY_CASTING enum.
*/
NPY_NO_EXPORT int
PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting)
{
char *str = NULL;
Py_ssize_t length = 0;
if (PyUnicode_Check(obj)) {
PyObject *str_obj;
int ret;
str_obj = PyUnicode_AsASCIIString(obj);
if (str_obj == NULL) {
return 0;
}
ret = PyArray_CastingConverter(str_obj, casting);
Py_DECREF(str_obj);
return ret;
}
if (PyBytes_AsStringAndSize(obj, &str, &length) == -1) {
return 0;
}
if (length >= 2) switch (str[2]) {
case 0:
if (strcmp(str, "no") == 0) {
*casting = NPY_NO_CASTING;
return 1;
}
break;
case 'u':
if (strcmp(str, "equiv") == 0) {
*casting = NPY_EQUIV_CASTING;
return 1;
}
break;
case 'f':
if (strcmp(str, "safe") == 0) {
*casting = NPY_SAFE_CASTING;
return 1;
}
break;
case 'm':
if (strcmp(str, "same_kind") == 0) {
*casting = NPY_SAME_KIND_CASTING;
return 1;
}
break;
case 's':
if (strcmp(str, "unsafe") == 0) {
*casting = NPY_UNSAFE_CASTING;
return 1;
}
break;
}
PyErr_SetString(PyExc_ValueError,
"casting must be one of 'no', 'equiv', 'safe', "
"'same_kind', or 'unsafe'");
return 0;
}
/*****************************
* Other conversion functions
*****************************/
/*NUMPY_API*/
NPY_NO_EXPORT int
PyArray_PyIntAsInt(PyObject *o)
{
npy_intp long_value;
/* This assumes that NPY_SIZEOF_INTP >= NPY_SIZEOF_INT */
long_value = PyArray_PyIntAsIntp(o);
#if (NPY_SIZEOF_INTP > NPY_SIZEOF_INT)
if ((long_value < INT_MIN) || (long_value > INT_MAX)) {
PyErr_SetString(PyExc_ValueError, "integer won't fit into a C int");
return -1;
}
#endif
return (int) long_value;
}
/*NUMPY_API*/
NPY_NO_EXPORT npy_intp
PyArray_PyIntAsIntp(PyObject *o)
{
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
npy_long long_value = -1;
#else
npy_longlong long_value = -1;
#endif
PyObject *obj, *err;
static char *msg = "an integer is required";
if (!o) {
PyErr_SetString(PyExc_TypeError, msg);
return -1;
}
/* Be a bit stricter and not allow bools */
if (PyBool_Check(o)) {
if (DEPRECATE("using a boolean instead of an integer"
" will result in an error in the future") < 0) {
return -1;
}
}
/*
* Since it is the usual case, first check if o is an integer. This is
* an exact check, since otherwise __index__ is used.
*/
#if !defined(NPY_PY3K)
if PyInt_CheckExact(o) {
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
long_value = (npy_longlong) PyInt_AsLong(o);
#else
long_value = PyInt_AsLong(o);
#endif
goto finish;
}
else
#endif
if PyLong_CheckExact(o) {
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
long_value = PyLong_AsLongLong(o);
#else
long_value = PyLong_AsLong(o);
#endif
goto finish;
}
/*
* The most general case. PyNumber_Index(o) covers everything
* including arrays. In principle it may be possible to replace
* the whole function by PyIndex_AsSSize_t after deprecation.
*/
obj = PyNumber_Index(o);
if (obj) {
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
long_value = PyLong_AsLongLong(obj);
#else
long_value = PyLong_AsLong(obj);
#endif
Py_DECREF(obj);
goto finish;
}
else {
/*
* Set the TypeError like PyNumber_Index(o) would after trying
* the general case.
*/
PyErr_Clear();
}
/*
* For backward compatibility check the number C-Api number protcol
* This should be removed up the finish label after deprecation.
*/
if (Py_TYPE(o)->tp_as_number != NULL &&
Py_TYPE(o)->tp_as_number->nb_int != NULL) {
obj = Py_TYPE(o)->tp_as_number->nb_int(o);
if (obj == NULL) {
return -1;
}
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
long_value = PyLong_AsLongLong(obj);
#else
long_value = PyLong_AsLong(obj);
#endif
Py_DECREF(obj);
}
#if !defined(NPY_PY3K)
else if (Py_TYPE(o)->tp_as_number != NULL &&
Py_TYPE(o)->tp_as_number->nb_long != NULL) {
obj = Py_TYPE(o)->tp_as_number->nb_long(o);
if (obj == NULL) {
return -1;
}
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
long_value = PyLong_AsLongLong(obj);
#else
long_value = PyLong_AsLong(obj);
#endif
Py_DECREF(obj);
}
#endif
else {
PyErr_SetString(PyExc_TypeError, msg);
return -1;
}
/* Give a deprecation warning, unless there was already an error */
if (!error_converting(long_value)) {
if (DEPRECATE("using a non-integer number instead of an integer"
" will result in an error in the future") < 0) {
return -1;
}
}
finish:
if (long_value == -1) {
err = PyErr_Occurred();
/* Only replace TypeError's here, which are the normal errors. */
if (err) {
if (PyErr_GivenExceptionMatches(err, PyExc_TypeError)) {
PyErr_SetString(PyExc_TypeError, msg);
}
return -1;
}
}
#if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP)
#if (NPY_SIZEOF_LONGLONG > NPY_SIZEOF_INTP)
if ((long_value < NPY_MIN_INTP) || (long_value > NPY_MAX_INTP)) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C numpy.intp");
return -1;
}
#endif
#else
#if (NPY_SIZEOF_LONG > NPY_SIZEOF_INTP)
if ((long_value < NPY_MIN_INTP) || (long_value > NPY_MAX_INTP)) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C numpy.intp");
return -1;
}
#endif
#endif
return (npy_intp) long_value;
}
/*
* PyArray_IntpFromIndexSequence
* Returns the number of dimensions or -1 if an error occurred.
* vals must be large enough to hold maxvals.
* Opposed to PyArray_IntpFromSequence it uses and returns npy_intp
* for the number of values.
*/
NPY_NO_EXPORT npy_intp
PyArray_IntpFromIndexSequence(PyObject *seq, npy_intp *vals, npy_intp maxvals)
{
Py_ssize_t nd;
npy_intp i;
PyObject *op, *err;
/*
* Check to see if sequence is a single integer first.
* or, can be made into one
*/
nd = PySequence_Length(seq);
if (nd == -1) {
if (PyErr_Occurred()) {
PyErr_Clear();
}
vals[0] = PyArray_PyIntAsIntp(seq);
if(vals[0] == -1) {
err = PyErr_Occurred();
if (err &&
PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
PyErr_SetString(PyExc_ValueError,
"Maximum allowed dimension exceeded");
}
if(err != NULL) {
return -1;
}
}
nd = 1;
}
else {
for (i = 0; i < PyArray_MIN(nd,maxvals); i++) {
op = PySequence_GetItem(seq, i);
if (op == NULL) {
return -1;
}
vals[i] = PyArray_PyIntAsIntp(op);
if(vals[i] == -1) {
err = PyErr_Occurred();
if (err &&
PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
PyErr_SetString(PyExc_ValueError,
"Maximum allowed dimension exceeded");
}
if(err != NULL) {
return -1;
}
}
}
}
return nd;
}
/*NUMPY_API
* PyArray_IntpFromSequence
* Returns the number of integers converted or -1 if an error occurred.
* vals must be large enough to hold maxvals
*/
NPY_NO_EXPORT int
PyArray_IntpFromSequence(PyObject *seq, npy_intp *vals, int maxvals)
{
return PyArray_IntpFromIndexSequence(seq, vals, (npy_intp)maxvals);
}
/**
* WARNING: This flag is a bad idea, but was the only way to both
* 1) Support unpickling legacy pickles with object types.
* 2) Deprecate (and later disable) usage of O4 and O8
*
* The key problem is that the pickled representation unpickles by
* directly calling the dtype constructor, which has no way of knowing
* that it is in an unpickle context instead of a normal context without
* evil global state like we create here.
*/
NPY_NO_EXPORT int evil_global_disable_warn_O4O8_flag = 0;
/*NUMPY_API
* Typestr converter
*/
NPY_NO_EXPORT int
PyArray_TypestrConvert(int itemsize, int gentype)
{
int newtype = NPY_NOTYPE;
PyArray_Descr *temp;
const char *msg = "Specified size is invalid for this data type.\n"
"Size will be ignored in NumPy 1.7 but may throw an exception in future versions.";
switch (gentype) {
case NPY_GENBOOLLTR:
if (itemsize == 1) {
newtype = NPY_BOOL;
}
break;
case NPY_SIGNEDLTR:
switch(itemsize) {
case 1:
newtype = NPY_INT8;
break;
case 2:
newtype = NPY_INT16;
break;
case 4:
newtype = NPY_INT32;
break;
case 8:
newtype = NPY_INT64;
break;
#ifdef NPY_INT128
case 16:
newtype = NPY_INT128;
break;
#endif
}
break;
case NPY_UNSIGNEDLTR:
switch(itemsize) {
case 1:
newtype = NPY_UINT8;
break;
case 2:
newtype = NPY_UINT16;
break;
case 4:
newtype = NPY_UINT32;
break;
case 8:
newtype = NPY_UINT64;
break;
#ifdef NPY_INT128
case 16:
newtype = NPY_UINT128;
break;
#endif
}
break;
case NPY_FLOATINGLTR:
switch(itemsize) {
case 2:
newtype = NPY_FLOAT16;
break;
case 4:
newtype = NPY_FLOAT32;
break;
case 8:
newtype = NPY_FLOAT64;
break;
#ifdef NPY_FLOAT80
case 10:
newtype = NPY_FLOAT80;
break;
#endif
#ifdef NPY_FLOAT96
case 12:
newtype = NPY_FLOAT96;
break;