1
#define PY_SSIZE_T_CLEAN
2
#include <Python.h>
3
#include "structmember.h"
4

5
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
6
#define _MULTIARRAYMODULE
7
#include "numpy/arrayobject.h"
8
#include "numpy/arrayscalars.h"
9

10
#include "numpy/npy_math.h"
11

12
#include "npy_config.h"
13

14
#include "npy_ctypes.h"
15
#include "npy_pycompat.h"
16
#include "multiarraymodule.h"
17

18
#include "common.h"
19
#include "ctors.h"
20
#include "convert_datatype.h"
21
#include "shape.h"
22
#include "npy_buffer.h"
23
#include "lowlevel_strided_loops.h"
24
#include "_datetime.h"
25
#include "datetime_strings.h"
26
#include "array_assign.h"
27
#include "mapping.h" /* for array_item_asarray */
28
#include "templ_common.h" /* for npy_mul_with_overflow_intp */
29
#include "alloc.h"
30
#include <assert.h>
31

32
#include "get_attr_string.h"
33
#include "array_coercion.h"
34

35
/*
36
 * Reading from a file or a string.
37
 *
38
 * As much as possible, we try to use the same code for both files and strings,
39
 * so the semantics for fromstring and fromfile are the same, especially with
40
 * regards to the handling of text representations.
41
 */
42

43
/*
44
 * Scanning function for next element parsing and separator skipping.
45
 * These functions return:
46
 *   - 0 to indicate more data to read
47
 *   - -1 when reading stopped at the end of the string/file
48
 *   - -2 when reading stopped before the end was reached.
49
 *
50
 * The dtype specific parsing functions may set the python error state
51
 * (they have to get the GIL first) additionally.
52
 */
53
typedef int (*next_element)(void **, void *, PyArray_Descr *, void *);
54
typedef int (*skip_separator)(void **, const char *, void *);
55

56

57
static npy_bool
58
string_is_fully_read(char const* start, char const* end) {
59 1
    if (end == NULL) {
60 1
        return *start == '\0';  /* null terminated */
61
    }
62
    else {
63 1
        return start >= end;  /* fixed length */
64
    }
65
}
66

67

68
static int
69 1
fromstr_next_element(char **s, void *dptr, PyArray_Descr *dtype,
70
                     const char *end)
71
{
72 1
    char *e = *s;
73 1
    int r = dtype->f->fromstr(*s, dptr, &e, dtype);
74
    /*
75
     * fromstr always returns 0 for basic dtypes; s points to the end of the
76
     * parsed string. If s is not changed an error occurred or the end was
77
     * reached.
78
     */
79 1
    if (*s == e || r < 0) {
80
        /* Nothing read, could be end of string or an error (or both) */
81 1
        if (string_is_fully_read(*s, end)) {
82
            return -1;
83
        }
84 1
        return -2;
85
    }
86 1
    *s = e;
87 1
    if (end != NULL && *s > end) {
88
        /* Stop the iteration if we read far enough */
89
        return -1;
90
    }
91 1
    return 0;
92
}
93

94
static int
95 1
fromfile_next_element(FILE **fp, void *dptr, PyArray_Descr *dtype,
96
                      void *NPY_UNUSED(stream_data))
97
{
98
    /* the NULL argument is for backwards-compatibility */
99 1
    int r = dtype->f->scanfunc(*fp, dptr, NULL, dtype);
100
    /* r can be EOF or the number of items read (0 or 1) */
101 1
    if (r == 1) {
102
        return 0;
103
    }
104 1
    else if (r == EOF) {
105
        return -1;
106
    }
107
    else {
108
        /* unable to read more, but EOF not reached indicating an error. */
109 1
        return -2;
110
    }
111
}
112

113
/*
114
 * Remove multiple whitespace from the separator, and add a space to the
115
 * beginning and end. This simplifies the separator-skipping code below.
116
 */
117
static char *
118 1
swab_separator(const char *sep)
119
{
120 1
    int skip_space = 0;
121
    char *s, *start;
122

123 1
    s = start = malloc(strlen(sep)+3);
124 1
    if (s == NULL) {
125 0
        PyErr_NoMemory();
126 0
        return NULL;
127
    }
128
    /* add space to front if there isn't one */
129 1
    if (*sep != '\0' && !isspace(*sep)) {
130 1
        *s = ' '; s++;
131
    }
132 1
    while (*sep != '\0') {
133 1
        if (isspace(*sep)) {
134 1
            if (skip_space) {
135 0
                sep++;
136
            }
137
            else {
138 1
                *s = ' ';
139 1
                s++;
140 1
                sep++;
141 1
                skip_space = 1;
142
            }
143
        }
144
        else {
145 1
            *s = *sep;
146 1
            s++;
147 1
            sep++;
148 1
            skip_space = 0;
149
        }
150
    }
151
    /* add space to end if there isn't one */
152 1
    if (s != start && s[-1] == ' ') {
153 1
        *s = ' ';
154 1
        s++;
155
    }
156 1
    *s = '\0';
157 1
    return start;
158
}
159

160
/*
161
 * Assuming that the separator is the next bit in the string (file), skip it.
162
 *
163
 * Single spaces in the separator are matched to arbitrary-long sequences
164
 * of whitespace in the input. If the separator consists only of spaces,
165
 * it matches one or more whitespace characters.
166
 *
167
 * If we can't match the separator, return -2.
168
 * If we hit the end of the string (file), return -1.
169
 * Otherwise, return 0.
170
 */
171
static int
172 1
fromstr_skip_separator(char **s, const char *sep, const char *end)
173
{
174 1
    char *string = *s;
175 1
    int result = 0;
176

177
    while (1) {
178 1
        char c = *string;
179 1
        if (string_is_fully_read(string, end)) {
180
            result = -1;
181
            break;
182
        }
183 1
        else if (*sep == '\0') {
184 1
            if (string != *s) {
185
                /* matched separator */
186
                result = 0;
187
                break;
188
            }
189
            else {
190
                /* separator was whitespace wildcard that didn't match */
191 1
                result = -2;
192 1
                break;
193
            }
194
        }
195 1
        else if (*sep == ' ') {
196
            /* whitespace wildcard */
197 1
            if (!isspace(c)) {
198 1
                sep++;
199 1
                continue;
200
            }
201
        }
202 1
        else if (*sep != c) {
203
            result = -2;
204
            break;
205
        }
206
        else {
207 1
            sep++;
208
        }
209 1
        string++;
210
    }
211 1
    *s = string;
212 1
    return result;
213
}
214

215
static int
216 1
fromfile_skip_separator(FILE **fp, const char *sep, void *NPY_UNUSED(stream_data))
217
{
218 1
    int result = 0;
219 1
    const char *sep_start = sep;
220

221
    while (1) {
222 1
        int c = fgetc(*fp);
223

224 1
        if (c == EOF) {
225
            result = -1;
226
            break;
227
        }
228 1
        else if (*sep == '\0') {
229 1
            ungetc(c, *fp);
230 1
            if (sep != sep_start) {
231
                /* matched separator */
232
                result = 0;
233
                break;
234
            }
235
            else {
236
                /* separator was whitespace wildcard that didn't match */
237 1
                result = -2;
238 1
                break;
239
            }
240
        }
241 1
        else if (*sep == ' ') {
242
            /* whitespace wildcard */
243 1
            if (!isspace(c)) {
244 1
                sep++;
245 1
                sep_start++;
246 1
                ungetc(c, *fp);
247
            }
248 1
            else if (sep == sep_start) {
249 1
                sep_start--;
250
            }
251
        }
252 1
        else if (*sep != c) {
253 1
            ungetc(c, *fp);
254 1
            result = -2;
255 1
            break;
256
        }
257
        else {
258 1
            sep++;
259
        }
260
    }
261 1
    return result;
262
}
263

264
/*
265
 * Change a sub-array field to the base descriptor
266
 * and update the dimensions and strides
267
 * appropriately.  Dimensions and strides are added
268
 * to the end.
269
 *
270
 * Strides are only added if given (because data is given).
271
 */
272
static int
273 1
_update_descr_and_dimensions(PyArray_Descr **des, npy_intp *newdims,
274
                             npy_intp *newstrides, int oldnd)
275
{
276
    PyArray_Descr *old;
277
    int newnd;
278
    int numnew;
279
    npy_intp *mydim;
280
    int i;
281
    int tuple;
282

283 1
    old = *des;
284 1
    *des = old->subarray->base;
285

286

287 1
    mydim = newdims + oldnd;
288 1
    tuple = PyTuple_Check(old->subarray->shape);
289 1
    if (tuple) {
290 1
        numnew = PyTuple_GET_SIZE(old->subarray->shape);
291
    }
292
    else {
293
        numnew = 1;
294
    }
295

296

297 1
    newnd = oldnd + numnew;
298 1
    if (newnd > NPY_MAXDIMS) {
299
        goto finish;
300
    }
301 1
    if (tuple) {
302 1
        for (i = 0; i < numnew; i++) {
303 1
            mydim[i] = (npy_intp) PyLong_AsLong(
304 1
                    PyTuple_GET_ITEM(old->subarray->shape, i));
305
        }
306
    }
307
    else {
308 0
        mydim[0] = (npy_intp) PyLong_AsLong(old->subarray->shape);
309
    }
310

311 1
    if (newstrides) {
312
        npy_intp tempsize;
313
        npy_intp *mystrides;
314

315 1
        mystrides = newstrides + oldnd;
316
        /* Make new strides -- always C-contiguous */
317 1
        tempsize = (*des)->elsize;
318 1
        for (i = numnew - 1; i >= 0; i--) {
319 1
            mystrides[i] = tempsize;
320 1
            tempsize *= mydim[i] ? mydim[i] : 1;
321
        }
322
    }
323

324 1
 finish:
325 1
    Py_INCREF(*des);
326 1
    Py_DECREF(old);
327 1
    return newnd;
328
}
329

330
NPY_NO_EXPORT void
331 1
_unaligned_strided_byte_copy(char *dst, npy_intp outstrides, char *src,
332
                             npy_intp instrides, npy_intp N, int elsize)
333
{
334
    npy_intp i;
335 1
    char *tout = dst;
336 1
    char *tin = src;
337

338
#define _COPY_N_SIZE(size) \
339
    for(i=0; i<N; i++) { \
340
        memcpy(tout, tin, size); \
341
        tin += instrides; \
342
        tout += outstrides; \
343
    } \
344
    return
345

346 1
    switch(elsize) {
347
    case 8:
348 1
        _COPY_N_SIZE(8);
349
    case 4:
350 1
        _COPY_N_SIZE(4);
351
    case 1:
352 1
        _COPY_N_SIZE(1);
353
    case 2:
354 1
        _COPY_N_SIZE(2);
355
    case 16:
356 1
        _COPY_N_SIZE(16);
357
    default:
358 1
        _COPY_N_SIZE(elsize);
359
    }
360
#undef _COPY_N_SIZE
361

362
}
363

364
NPY_NO_EXPORT void
365 1
_strided_byte_swap(void *p, npy_intp stride, npy_intp n, int size)
366
{
367 1
    char *a, *b, c = 0;
368
    int j, m;
369

370 1
    switch(size) {
371
    case 1: /* no byteswap necessary */
372
        break;
373 1
    case 4:
374 1
        if (npy_is_aligned((void*)((npy_intp)p | stride), sizeof(npy_uint32))) {
375 1
            for (a = (char*)p; n > 0; n--, a += stride) {
376 1
                npy_uint32 * a_ = (npy_uint32 *)a;
377 1
                *a_ = npy_bswap4(*a_);
378
            }
379
        }
380
        else {
381 1
            for (a = (char*)p; n > 0; n--, a += stride) {
382 1
                npy_bswap4_unaligned(a);
383
            }
384
        }
385
        break;
386 1
    case 8:
387 1
        if (npy_is_aligned((void*)((npy_intp)p | stride), sizeof(npy_uint64))) {
388 1
            for (a = (char*)p; n > 0; n--, a += stride) {
389 1
                npy_uint64 * a_ = (npy_uint64 *)a;
390 1
                *a_ = npy_bswap8(*a_);
391
            }
392
        }
393
        else {
394 1
            for (a = (char*)p; n > 0; n--, a += stride) {
395 1
                npy_bswap8_unaligned(a);
396
            }
397
        }
398
        break;
399 1
    case 2:
400 1
        if (npy_is_aligned((void*)((npy_intp)p | stride), sizeof(npy_uint16))) {
401 1
            for (a = (char*)p; n > 0; n--, a += stride) {
402 1
                npy_uint16 * a_ = (npy_uint16 *)a;
403 1
                *a_ = npy_bswap2(*a_);
404
            }
405
        }
406
        else {
407 0
            for (a = (char*)p; n > 0; n--, a += stride) {
408 0
                npy_bswap2_unaligned(a);
409
            }
410
        }
411
        break;
412 1
    default:
413 1
        m = size/2;
414 1
        for (a = (char *)p; n > 0; n--, a += stride - m) {
415 1
            b = a + (size - 1);
416 1
            for (j = 0; j < m; j++) {
417 1
                c=*a; *a++ = *b; *b-- = c;
418
            }
419
        }
420
        break;
421
    }
422
}
423

424
NPY_NO_EXPORT void
425 1
byte_swap_vector(void *p, npy_intp n, int size)
426
{
427 1
    _strided_byte_swap(p, (npy_intp) size, n, size);
428 1
    return;
429
}
430

431
/* If numitems > 1, then dst must be contiguous */
432
NPY_NO_EXPORT void
433 1
copy_and_swap(void *dst, void *src, int itemsize, npy_intp numitems,
434
              npy_intp srcstrides, int swap)
435
{
436 1
    if ((numitems == 1) || (itemsize == srcstrides)) {
437 1
        memcpy(dst, src, itemsize*numitems);
438
    }
439
    else {
440
        npy_intp i;
441
        char *s1 = (char *)src;
442
        char *d1 = (char *)dst;
443

444 0
        for (i = 0; i < numitems; i++) {
445 0
            memcpy(d1, s1, itemsize);
446 0
            d1 += itemsize;
447 0
            s1 += srcstrides;
448
        }
449
    }
450

451 1
    if (swap) {
452
        byte_swap_vector(dst, numitems, itemsize);
453
    }
454
}
455

456

457
/*
458
 * Recursive helper to assign using a coercion cache. This function
459
 * must consume the cache depth first, just as the cache was originally
460
 * produced.
461
 */
462
NPY_NO_EXPORT int
463 1
PyArray_AssignFromCache_Recursive(
464
        PyArrayObject *self, const int ndim, coercion_cache_obj **cache)
465
{
466
    /* Consume first cache element by extracting information and freeing it */
467 1
    PyObject *original_obj = (*cache)->converted_obj;
468 1
    PyObject *obj = (*cache)->arr_or_sequence;
469 1
    Py_INCREF(obj);
470 1
    npy_bool sequence = (*cache)->sequence;
471 1
    int depth = (*cache)->depth;
472 1
    *cache = npy_unlink_coercion_cache(*cache);
473

474
    /*
475
     * The maximum depth is special (specifically for objects), but usually
476
     * unrolled in the sequence branch below.
477
     */
478 1
    if (NPY_UNLIKELY(depth == ndim)) {
479
        /*
480
         * We have reached the maximum depth. We should simply assign to the
481
         * element in principle. There is one exception. If this is a 0-D
482
         * array being stored into a 0-D array (but we do not reach here then).
483
         */
484 1
        if (PyArray_ISOBJECT(self)) {
485
            assert(ndim != 0);  /* guaranteed by PyArray_AssignFromCache */
486
            assert(PyArray_NDIM(self) == 0);
487 1
            Py_DECREF(obj);
488 1
            return PyArray_Pack(PyArray_DESCR(self), PyArray_BYTES(self),
489
                                original_obj);
490
        }
491 1
        if (sequence) {
492
            /*
493
             * Sanity check which may be removed, the error is raised already
494
             * in `PyArray_DiscoverDTypeAndShape`.
495
             */
496
            assert(0);
497 0
            PyErr_SetString(PyExc_RuntimeError,
498
                    "setting an array element with a sequence");
499 0
            goto fail;
500
        }
501 1
        else if (original_obj != obj || !PyArray_CheckExact(obj)) {
502
            /*
503
             * If the leave node is an array-like, but not a numpy array,
504
             * we pretend it is an arbitrary scalar.  This means that in
505
             * most cases (where the dtype is int or float), we will end
506
             * up using float(array-like), or int(array-like).  That does
507
             * not support general casting, but helps Quantity and masked
508
             * arrays, because it allows them to raise an error when
509
             * `__float__()` or `__int__()` is called.
510
             */
511 1
            Py_DECREF(obj);
512 1
            return PyArray_SETITEM(self, PyArray_BYTES(self), original_obj);
513
        }
514
    }
515

516
    /* The element is either a sequence, or an array */
517 1
    if (!sequence) {
518
        /* Straight forward array assignment */
519
        assert(PyArray_Check(obj));
520 1
        if (PyArray_CopyInto(self, (PyArrayObject *)obj) < 0) {
521
            goto fail;
522
        }
523
    }
524
    else {
525
        assert(depth != ndim);
526 1
        npy_intp length = PySequence_Length(obj);
527 1
        if (length != PyArray_DIMS(self)[0]) {
528 1
            PyErr_SetString(PyExc_RuntimeError,
529
                    "Inconsistent object during array creation? "
530
                    "Content of sequences changed (length inconsistent).");
531 1
            goto fail;
532
        }
533

534 1
        for (npy_intp i = 0; i < length; i++) {
535 1
            PyObject *value = PySequence_Fast_GET_ITEM(obj, i);
536

537 1
            if (*cache == NULL || (*cache)->converted_obj != value ||
538 1
                        (*cache)->depth != depth + 1) {
539 1
                if (ndim != depth + 1) {
540 1
                    PyErr_SetString(PyExc_RuntimeError,
541
                            "Inconsistent object during array creation? "
542
                            "Content of sequences changed (now too shallow).");
543 1
                    goto fail;
544
                }
545
                /* Straight forward assignment of elements */
546
                char *item;
547 1
                item = (PyArray_BYTES(self) + i * PyArray_STRIDES(self)[0]);
548 1
                if (PyArray_Pack(PyArray_DESCR(self), item, value) < 0) {
549
                    goto fail;
550
                }
551
            }
552
            else {
553
                PyArrayObject *view;
554 1
                view = (PyArrayObject *)array_item_asarray(self, i);
555
                if (view < 0) {
556
                    goto fail;
557
                }
558 1
                if (PyArray_AssignFromCache_Recursive(view, ndim, cache) < 0) {
559 1
                    Py_DECREF(view);
560
                    goto fail;
561
                }
562 1
                Py_DECREF(view);
563
            }
564
        }
565
    }
566 1
    Py_DECREF(obj);
567
    return 0;
568

569 1
  fail:
570 1
    Py_DECREF(obj);
571
    return -1;
572
}
573

574

575
/**
576
 * Fills an item based on a coercion cache object. It consumes the cache
577
 * object while doing so.
578
 *
579
 * @param self Array to fill.
580
 * @param cache coercion_cache_object, will be consumed. The cache must not
581
 *        contain a single array (must start with a sequence). The array case
582
 *        should be handled by `PyArray_FromArray()` before.
583
 * @return 0 on success -1 on failure.
584
 */
585
NPY_NO_EXPORT int
586 1
PyArray_AssignFromCache(PyArrayObject *self, coercion_cache_obj *cache) {
587 1
    int ndim = PyArray_NDIM(self);
588
    /*
589
     * Do not support ndim == 0 now with an array in the cache.
590
     * The ndim == 0 is special because np.array(np.array(0), dtype=object)
591
     * should unpack the inner array.
592
     * Since the single-array case is special, it is handled previously
593
     * in either case.
594
     */
595
    assert(cache->sequence);
596
    assert(ndim != 0);  /* guaranteed if cache contains a sequence */
597

598 1
    if (PyArray_AssignFromCache_Recursive(self, ndim, &cache) < 0) {
599
        /* free the remaining cache. */
600 1
        npy_free_coercion_cache(cache);
601 1
        return -1;
602
    }
603

604
    /*
605
     * Sanity check, this is the initial call, and when it returns, the
606
     * cache has to be fully consumed, otherwise something is wrong.
607
     * NOTE: May be nicer to put into a recursion helper.
608
     */
609 1
    if (cache != NULL) {
610 1
        PyErr_SetString(PyExc_RuntimeError,
611
                "Inconsistent object during array creation? "
612
                "Content of sequences changed (cache not consumed).");
613 1
        return -1;
614
    }
615
    return 0;
616
}
617

618

619
static void
620 1
raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr)
621
{
622
    static PyObject *exc_type = NULL;
623

624 1
    npy_cache_import(
625
        "numpy.core._exceptions", "_ArrayMemoryError",
626
        &exc_type);
627 1
    if (exc_type == NULL) {
628
        goto fail;
629
    }
630

631 1
    PyObject *shape = PyArray_IntTupleFromIntp(nd, dims);
632 1
    if (shape == NULL) {
633
        goto fail;
634
    }
635

636
    /* produce an error object */
637 1
    PyObject *exc_value = PyTuple_Pack(2, shape, (PyObject *)descr);
638 1
    Py_DECREF(shape);
639 1
    if (exc_value == NULL){
640
        goto fail;
641
    }
642 1
    PyErr_SetObject(exc_type, exc_value);
643 1
    Py_DECREF(exc_value);
644
    return;
645

646 0
fail:
647
    /* we couldn't raise the formatted exception for some reason */
648 0
    PyErr_WriteUnraisable(NULL);
649 0
    PyErr_NoMemory();
650
}
651

652
/*
653
 * Generic new array creation routine.
654
 * Internal variant with calloc argument for PyArray_Zeros.
655
 *
656
 * steals a reference to descr. On failure or descr->subarray, descr will
657
 * be decrefed.
658
 */
659
NPY_NO_EXPORT PyObject *
660 1
PyArray_NewFromDescr_int(
661
        PyTypeObject *subtype, PyArray_Descr *descr, int nd,
662
        npy_intp const *dims, npy_intp const *strides, void *data,
663
        int flags, PyObject *obj, PyObject *base, int zeroed,
664
        int allow_emptystring)
665
{
666
    PyArrayObject_fields *fa;
667
    int i;
668
    npy_intp nbytes;
669

670 1
    if (descr->subarray) {
671
        PyObject *ret;
672
        npy_intp newdims[2*NPY_MAXDIMS];
673 1
        npy_intp *newstrides = NULL;
674 1
        memcpy(newdims, dims, nd*sizeof(npy_intp));
675 1
        if (strides) {
676 1
            newstrides = newdims + NPY_MAXDIMS;
677 1
            memcpy(newstrides, strides, nd*sizeof(npy_intp));
678
        }
679 1
        nd =_update_descr_and_dimensions(&descr, newdims,
680
                                         newstrides, nd);
681 1
        ret = PyArray_NewFromDescr_int(
682
                subtype, descr,
683
                nd, newdims, newstrides, data,
684
                flags, obj, base,
685
                zeroed, allow_emptystring);
686
        return ret;
687
    }
688

689 1
    if ((unsigned int)nd > (unsigned int)NPY_MAXDIMS) {
690 0
        PyErr_Format(PyExc_ValueError,
691
                     "number of dimensions must be within [0, %d]",
692
                     NPY_MAXDIMS);
693 0
        Py_DECREF(descr);
694
        return NULL;
695
    }
696

697
    /* Check datatype element size */
698 1
    nbytes = descr->elsize;
699 1
    if (PyDataType_ISUNSIZED(descr)) {
700 1
        if (!PyDataType_ISFLEXIBLE(descr)) {
701 0
            PyErr_SetString(PyExc_TypeError, "Empty data-type");
702 0
            Py_DECREF(descr);
703
            return NULL;
704
        }
705 1
        else if (PyDataType_ISSTRING(descr) && !allow_emptystring &&
706 1
                 data == NULL) {
707 1
            PyArray_DESCR_REPLACE(descr);
708 1
            if (descr == NULL) {
709
                return NULL;
710
            }
711 1
            if (descr->type_num == NPY_STRING) {
712 1
                nbytes = descr->elsize = 1;
713
            }
714
            else {
715 1
                nbytes = descr->elsize = sizeof(npy_ucs4);
716
            }
717
        }
718
    }
719

720
    /* Check dimensions and multiply them to nbytes */
721 1
    for (i = 0; i < nd; i++) {
722 1
        npy_intp dim = dims[i];
723

724 1
        if (dim == 0) {
725
            /*
726
             * Compare to PyArray_OverflowMultiplyList that
727
             * returns 0 in this case.
728
             */
729 1
            continue;
730
        }
731

732 1
        if (dim < 0) {
733 0
            PyErr_SetString(PyExc_ValueError,
734
                "negative dimensions are not allowed");
735 0
            Py_DECREF(descr);
736
            return NULL;
737
        }
738

739
        /*
740
         * Care needs to be taken to avoid integer overflow when
741
         * multiplying the dimensions together to get the total size of the
742
         * array.
743
         */
744 1
        if (npy_mul_with_overflow_intp(&nbytes, nbytes, dim)) {
745 1
            PyErr_SetString(PyExc_ValueError,
746
                "array is too big; `arr.size * arr.dtype.itemsize` "
747
                "is larger than the maximum possible size.");
748 1
            Py_DECREF(descr);
749
            return NULL;
750
        }
751
    }
752

753 1
    fa = (PyArrayObject_fields *) subtype->tp_alloc(subtype, 0);
754 1
    if (fa == NULL) {
755 0
        Py_DECREF(descr);
756
        return NULL;
757
    }
758 1
    fa->nd = nd;
759 1
    fa->dimensions = NULL;
760 1
    fa->data = NULL;
761 1
    if (data == NULL) {
762 1
        fa->flags = NPY_ARRAY_DEFAULT;
763 1
        if (flags) {
764 1
            fa->flags |= NPY_ARRAY_F_CONTIGUOUS;
765 1
            if (nd > 1) {
766 1
                fa->flags &= ~NPY_ARRAY_C_CONTIGUOUS;
767
            }
768
            flags = NPY_ARRAY_F_CONTIGUOUS;
769
        }
770
    }
771
    else {
772 1
        fa->flags = (flags & ~NPY_ARRAY_WRITEBACKIFCOPY);
773 1
        fa->flags &= ~NPY_ARRAY_UPDATEIFCOPY;
774
    }
775 1
    fa->descr = descr;
776 1
    fa->base = (PyObject *)NULL;
777 1
    fa->weakreflist = (PyObject *)NULL;
778

779 1
    if (nd > 0) {
780 1
        fa->dimensions = npy_alloc_cache_dim(2 * nd);
781 1
        if (fa->dimensions == NULL) {
782 0
            PyErr_NoMemory();
783 0
            goto fail;
784
        }
785 1
        fa->strides = fa->dimensions + nd;
786
        if (nd) {
787 1
            memcpy(fa->dimensions, dims, sizeof(npy_intp)*nd);
788
        }
789 1
        if (strides == NULL) {  /* fill it in */
790 1
            _array_fill_strides(fa->strides, dims, nd, descr->elsize,
791
                                flags, &(fa->flags));
792
        }
793
        else {
794
            /*
795
             * we allow strides even when we create
796
             * the memory, but be careful with this...
797
             */
798
            if (nd) {
799 1
                memcpy(fa->strides, strides, sizeof(npy_intp)*nd);
800
            }
801
        }
802
    }
803
    else {
804 1
        fa->dimensions = fa->strides = NULL;
805 1
        fa->flags |= NPY_ARRAY_F_CONTIGUOUS;
806
    }
807

808 1
    if (data == NULL) {
809
        /*
810
         * Allocate something even for zero-space arrays
811
         * e.g. shape=(0,) -- otherwise buffer exposure
812
         * (a.data) doesn't work as it should.
813
         * Could probably just allocate a few bytes here. -- Chuck
814
         */
815 1
        if (nbytes == 0) {
816 1
            nbytes = descr->elsize ? descr->elsize : 1;
817
        }
818
        /*
819
         * It is bad to have uninitialized OBJECT pointers
820
         * which could also be sub-fields of a VOID array
821
         */
822 1
        if (zeroed || PyDataType_FLAGCHK(descr, NPY_NEEDS_INIT)) {
823 1
            data = npy_alloc_cache_zero(nbytes);
824
        }
825
        else {
826 1
            data = npy_alloc_cache(nbytes);
827
        }
828 1
        if (data == NULL) {
829 1
            raise_memory_error(fa->nd, fa->dimensions, descr);
830 1
            goto fail;
831
        }
832 1
        fa->flags |= NPY_ARRAY_OWNDATA;
833
    }
834
    else {
835
        /*
836
         * If data is passed in, this object won't own it by default.
837
         * Caller must arrange for this to be reset if truly desired
838
         */
839 1
        fa->flags &= ~NPY_ARRAY_OWNDATA;
840
    }
841 1
    fa->data = data;
842

843
    /*
844
     * always update the flags to get the right CONTIGUOUS, ALIGN properties
845
     * not owned data and input strides may not be aligned and on some
846
     * platforms (debian sparc) malloc does not provide enough alignment for
847
     * long double types
848
     */
849 1
    PyArray_UpdateFlags((PyArrayObject *)fa, NPY_ARRAY_UPDATE_ALL);
850

851
    /* Set the base object. It's important to do it here so that
852
     * __array_finalize__ below receives it
853
     */
854 1
    if (base != NULL) {
855 1
        Py_INCREF(base);
856 1
        if (PyArray_SetBaseObject((PyArrayObject *)fa, base) < 0) {
857
            goto fail;
858
        }
859
    }
860

861
    /*
862
     * call the __array_finalize__
863
     * method if a subtype.
864
     * If obj is NULL, then call method with Py_None
865
     */
866 1
    if ((subtype != &PyArray_Type)) {
867
        PyObject *res, *func, *args;
868

869 1
        func = PyObject_GetAttr((PyObject *)fa, npy_ma_str_array_finalize);
870 1
        if (func && func != Py_None) {
871 1
            if (PyCapsule_CheckExact(func)) {
872
                /* A C-function is stored here */
873
                PyArray_FinalizeFunc *cfunc;
874 0
                cfunc = PyCapsule_GetPointer(func, NULL);
875 0
                Py_DECREF(func);
876 0
                if (cfunc == NULL) {
877
                    goto fail;
878
                }
879 0
                if (cfunc((PyArrayObject *)fa, obj) < 0) {
880
                    goto fail;
881
                }
882
            }
883
            else {
884 1
                args = PyTuple_New(1);
885 1
                if (obj == NULL) {
886 1
                    obj=Py_None;
887
                }
888 1
                Py_INCREF(obj);
889 1
                PyTuple_SET_ITEM(args, 0, obj);
890 1
                res = PyObject_Call(func, args, NULL);
891 1
                Py_DECREF(args);
892 1
                Py_DECREF(func);
893 1
                if (res == NULL) {
894
                    goto fail;
895
                }
896
                else {
897 1
                    Py_DECREF(res);
898
                }
899
            }
900
        }
901 1
        else Py_XDECREF(func);
902
    }
903
    return (PyObject *)fa;
904

905 1
 fail:
906 1
    Py_DECREF(fa);
907
    return NULL;
908
}
909

910

911
/*NUMPY_API
912
 * Generic new array creation routine.
913
 *
914
 * steals a reference to descr. On failure or when dtype->subarray is
915
 * true, dtype will be decrefed.
916
 */
917
NPY_NO_EXPORT PyObject *
918 1
PyArray_NewFromDescr(
919
        PyTypeObject *subtype, PyArray_Descr *descr,
920
        int nd, npy_intp const *dims, npy_intp const *strides, void *data,
921
        int flags, PyObject *obj)
922
{
923 1
    return PyArray_NewFromDescrAndBase(
924
            subtype, descr,
925
            nd, dims, strides, data,
926
            flags, obj, NULL);
927
}
928

929
/*
930
 * Sets the base object using PyArray_SetBaseObject
931
 */
932
NPY_NO_EXPORT PyObject *
933 1
PyArray_NewFromDescrAndBase(
934
        PyTypeObject *subtype, PyArray_Descr *descr,
935
        int nd, npy_intp const *dims, npy_intp const *strides, void *data,
936
        int flags, PyObject *obj, PyObject *base)
937
{
938 1
    return PyArray_NewFromDescr_int(subtype, descr, nd,
939
                                    dims, strides, data,
940
                                    flags, obj, base, 0, 0);
941
}
942

943
/*
944
 * Creates a new array with the same shape as the provided one,
945
 * with possible memory layout order, data type and shape changes.
946
 *
947
 * prototype - The array the new one should be like.
948
 * order     - NPY_CORDER - C-contiguous result.
949
 *             NPY_FORTRANORDER - Fortran-contiguous result.
950
 *             NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise.
951
 *             NPY_KEEPORDER - Keeps the axis ordering of prototype.
952
 * dtype     - If not NULL, overrides the data type of the result.
953
 * ndim      - If not -1, overrides the shape of the result.
954
 * dims      - If ndim is not -1, overrides the shape of the result.
955
 * subok     - If 1, use the prototype's array subtype, otherwise
956
 *             always create a base-class array.
957
 *
958
 * NOTE: If dtype is not NULL, steals the dtype reference.  On failure or when
959
 * dtype->subarray is true, dtype will be decrefed.
960
 */
961
NPY_NO_EXPORT PyObject *
962 1
PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order,
963
                              PyArray_Descr *dtype, int ndim, npy_intp const *dims, int subok)
964
{
965 1
    PyObject *ret = NULL;
966

967 1
    if (ndim == -1) {
968 1
        ndim = PyArray_NDIM(prototype);
969 1
        dims = PyArray_DIMS(prototype);
970
    }
971 1
    else if (order == NPY_KEEPORDER && (ndim != PyArray_NDIM(prototype))) {
972 1
        order = NPY_CORDER;
973
    }
974

975
    /* If no override data type, use the one from the prototype */
976 1
    if (dtype == NULL) {
977 1
        dtype = PyArray_DESCR(prototype);
978 1
        Py_INCREF(dtype);
979
    }
980

981
    /* Handle ANYORDER and simple KEEPORDER cases */
982 1
    switch (order) {
983 1
        case NPY_ANYORDER:
984 1
            order = PyArray_ISFORTRAN(prototype) ?
985 1
                                    NPY_FORTRANORDER : NPY_CORDER;
986
            break;
987 1
        case NPY_KEEPORDER:
988 1
            if (PyArray_IS_C_CONTIGUOUS(prototype) || ndim <= 1) {
989
                order = NPY_CORDER;
990
                break;
991
            }
992 1
            else if (PyArray_IS_F_CONTIGUOUS(prototype)) {
993
                order = NPY_FORTRANORDER;
994
                break;
995
            }
996
            break;
997
        default:
998
            break;
999
    }
1000

1001
    /* If it's not KEEPORDER, this is simple */
1002 1
    if (order != NPY_KEEPORDER) {
1003 1
        ret = PyArray_NewFromDescr(subok ? Py_TYPE(prototype) : &PyArray_Type,
1004
                                        dtype,
1005
                                        ndim,
1006
                                        dims,
1007
                                        NULL,
1008
                                        NULL,
1009
                                        order,
1010
                                        subok ? (PyObject *)prototype : NULL);
1011
    }
1012
    /* KEEPORDER needs some analysis of the strides */
1013
    else {
1014
        npy_intp strides[NPY_MAXDIMS], stride;
1015
        npy_stride_sort_item strideperm[NPY_MAXDIMS];
1016
        int idim;
1017

1018 1
        PyArray_CreateSortedStridePerm(ndim,
1019 1
                                        PyArray_STRIDES(prototype),
1020
                                        strideperm);
1021

1022
        /* Build the new strides */
1023 1
        stride = dtype->elsize;
1024 1
        for (idim = ndim-1; idim >= 0; --idim) {
1025 1
            npy_intp i_perm = strideperm[idim].perm;
1026 1
            strides[i_perm] = stride;
1027 1
            stride *= dims[i_perm];
1028
        }
1029

1030
        /* Finally, allocate the array */
1031 1
        ret = PyArray_NewFromDescr(subok ? Py_TYPE(prototype) : &PyArray_Type,
1032
                                        dtype,
1033
                                        ndim,
1034
                                        dims,
1035
                                        strides,
1036
                                        NULL,
1037
                                        0,
1038
                                        subok ? (PyObject *)prototype : NULL);
1039
    }
1040

1041 1
    return ret;
1042
}
1043

1044
/*NUMPY_API
1045
 * Creates a new array with the same shape as the provided one,
1046
 * with possible memory layout order and data type changes.
1047
 *
1048
 * prototype - The array the new one should be like.
1049
 * order     - NPY_CORDER - C-contiguous result.
1050
 *             NPY_FORTRANORDER - Fortran-contiguous result.
1051
 *             NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise.
1052
 *             NPY_KEEPORDER - Keeps the axis ordering of prototype.
1053
 * dtype     - If not NULL, overrides the data type of the result.
1054
 * subok     - If 1, use the prototype's array subtype, otherwise
1055
 *             always create a base-class array.
1056
 *
1057
 * NOTE: If dtype is not NULL, steals the dtype reference.  On failure or when
1058
 * dtype->subarray is true, dtype will be decrefed.
1059
 */
1060
NPY_NO_EXPORT PyObject *
1061 1
PyArray_NewLikeArray(PyArrayObject *prototype, NPY_ORDER order,
1062
                     PyArray_Descr *dtype, int subok)
1063
{
1064 1
    return PyArray_NewLikeArrayWithShape(prototype, order, dtype, -1, NULL, subok);
1065
}
1066

1067
/*NUMPY_API
1068
 * Generic new array creation routine.
1069
 */
1070
NPY_NO_EXPORT PyObject *
1071 1
PyArray_New(
1072
        PyTypeObject *subtype, int nd, npy_intp const *dims, int type_num,
1073
        npy_intp const *strides, void *data, int itemsize, int flags,
1074
        PyObject *obj)
1075
{
1076
    PyArray_Descr *descr;
1077
    PyObject *new;
1078

1079 1
    descr = PyArray_DescrFromType(type_num);
1080 1
    if (descr == NULL) {
1081
        return NULL;
1082
    }
1083 1
    if (PyDataType_ISUNSIZED(descr)) {
1084 1
        if (itemsize < 1) {
1085 0
            PyErr_SetString(PyExc_ValueError,
1086
                            "data type must provide an itemsize");
1087 0
            Py_DECREF(descr);
1088
            return NULL;
1089
        }
1090 1
        PyArray_DESCR_REPLACE(descr);
1091 1
        descr->elsize = itemsize;
1092
    }
1093 1
    new = PyArray_NewFromDescr(subtype, descr, nd, dims, strides,
1094
                               data, flags, obj);
1095 1
    return new;
1096
}
1097

1098

1099
NPY_NO_EXPORT PyArray_Descr *
1100 1
_dtype_from_buffer_3118(PyObject *memoryview)
1101
{
1102
    PyArray_Descr *descr;
1103 1
    Py_buffer *view = PyMemoryView_GET_BUFFER(memoryview);
1104 1
    if (view->format != NULL) {
1105 1
        descr = _descriptor_from_pep3118_format(view->format);
1106 1
        if (descr == NULL) {
1107
            return NULL;
1108
        }
1109
    }
1110
    else {
1111
        /* If no format is specified, just assume a byte array
1112
         * TODO: void would make more sense here, as it wouldn't null
1113
         *       terminate.
1114
         */
1115 0
        descr = PyArray_DescrNewFromType(NPY_STRING);
1116 0
        descr->elsize = view->itemsize;
1117
    }
1118
    return descr;
1119
}
1120

1121

1122
NPY_NO_EXPORT PyObject *
1123 1
_array_from_buffer_3118(PyObject *memoryview)
1124
{
1125
    /* PEP 3118 */
1126
    Py_buffer *view;
1127 1
    PyArray_Descr *descr = NULL;
1128 1
    PyObject *r = NULL;
1129
    int nd, flags;
1130
    Py_ssize_t d;
1131
    npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS];
1132

1133 1
    view = PyMemoryView_GET_BUFFER(memoryview);
1134 1
    nd = view->ndim;
1135 1
    descr = _dtype_from_buffer_3118(memoryview);
1136

1137 1
    if (descr == NULL) {
1138
        return NULL;
1139
    }
1140

1141
    /* Sanity check */
1142 1
    if (descr->elsize != view->itemsize) {
1143
        /* Ctypes has bugs in its PEP3118 implementation, which we need to
1144
         * work around.
1145
         *
1146
         * bpo-10746
1147
         * bpo-32780
1148
         * bpo-32782
1149
         *
1150
         * Note that even if the above are fixed in master, we have to drop the
1151
         * early patch versions of python to actually make use of the fixes.
1152
         */
1153 1
        if (!npy_ctypes_check(Py_TYPE(view->obj))) {
1154
            /* This object has no excuse for a broken PEP3118 buffer */
1155 0
            PyErr_Format(
1156
                    PyExc_RuntimeError,
1157
                   "Item size %zd for PEP 3118 buffer format "
1158
                    "string %s does not match the dtype %c item size %d.",
1159 0
                    view->itemsize, view->format, descr->type,
1160
                    descr->elsize);
1161 0
            Py_DECREF(descr);
1162
            return NULL;
1163
        }
1164

1165 1
        if (PyErr_Warn(
1166
                    PyExc_RuntimeWarning,
1167
                    "A builtin ctypes object gave a PEP3118 format "
1168
                    "string that does not match its itemsize, so a "
1169
                    "best-guess will be made of the data type. "
1170
                    "Newer versions of python may behave correctly.") < 0) {
1171 0
            Py_DECREF(descr);
1172
            return NULL;
1173
        }
1174

1175
        /* Thankfully, np.dtype(ctypes_type) works in most cases.
1176
         * For an array input, this produces a dtype containing all the
1177
         * dimensions, so the array is now 0d.
1178
         */
1179 1
        nd = 0;
1180 1
        Py_DECREF(descr);
1181 1
        descr = (PyArray_Descr *)PyObject_CallFunctionObjArgs(
1182 1
                (PyObject *)&PyArrayDescr_Type, Py_TYPE(view->obj), NULL);
1183 1
        if (descr == NULL) {
1184
            return NULL;
1185
        }
1186 1
        if (descr->elsize != view->len) {
1187 0
            PyErr_SetString(
1188
                    PyExc_RuntimeError,
1189
                    "For the given ctypes object, neither the item size "
1190
                    "computed from the PEP 3118 buffer format nor from "
1191
                    "converting the type to a np.dtype matched the actual "
1192
                    "size. This is a bug both in python and numpy");
1193 0
            Py_DECREF(descr);
1194
            return NULL;
1195
        }
1196
    }
1197

1198 1
    if (view->shape != NULL) {
1199
        int k;
1200 1
        if (nd > NPY_MAXDIMS || nd < 0) {
1201 1
            PyErr_Format(PyExc_RuntimeError,
1202
                "PEP3118 dimensions do not satisfy 0 <= ndim <= NPY_MAXDIMS");
1203 1
            goto fail;
1204
        }
1205 1
        for (k = 0; k < nd; ++k) {
1206 1
            shape[k] = view->shape[k];
1207
        }
1208 1
        if (view->strides != NULL) {
1209 1
            for (k = 0; k < nd; ++k) {
1210 1
                strides[k] = view->strides[k];
1211
            }
1212
        }
1213
        else {
1214 0
            d = view->len;
1215 0
            for (k = 0; k < nd; ++k) {
1216 0
                if (view->shape[k] != 0) {
1217 0
                    d /= view->shape[k];
1218
                }
1219 0
                strides[k] = d;
1220
            }
1221
        }
1222
    }
1223
    else {
1224 1
        if (nd == 1) {
1225 0
            shape[0] = view->len / view->itemsize;
1226 0
            strides[0] = view->itemsize;
1227
        }
1228 1
        else if (nd > 1) {
1229 0
            PyErr_SetString(PyExc_RuntimeError,
1230
                           "ndim computed from the PEP 3118 buffer format "
1231
                           "is greater than 1, but shape is NULL.");
1232 0
            goto fail;
1233
        }
1234
    }
1235

1236 1
    flags = NPY_ARRAY_BEHAVED & (view->readonly ? ~NPY_ARRAY_WRITEABLE : ~0);
1237 1
    r = PyArray_NewFromDescrAndBase(
1238
            &PyArray_Type, descr,
1239
            nd, shape, strides, view->buf,
1240
            flags, NULL, memoryview);
1241 1
    return r;
1242

1243

1244 1
fail:
1245 1
    Py_XDECREF(r);
1246 1
    Py_XDECREF(descr);
1247
    return NULL;
1248

1249
}
1250

1251

1252
/**
1253
 * Attempts to extract an array from an array-like object.
1254
 *
1255
 * array-like is defined as either
1256
 *
1257
 * * an object implementing the PEP 3118 buffer interface;
1258
 * * an object with __array_struct__ or __array_interface__ attributes;
1259
 * * an object with an __array__ function.
1260
 *
1261
 * @param op The object to convert to an array
1262
 * @param requested_type a requested dtype instance, may be NULL; The result
1263
 *                       DType may be used, but is not enforced.
1264
 * @param writeable whether the result must be writeable.
1265
 * @param context Unused parameter, must be NULL (should be removed later).
1266
 *
1267
 * @returns The array object, Py_NotImplemented if op is not array-like,
1268
 *          or NULL with an error set. (A new reference to Py_NotImplemented
1269
 *          is returned.)
1270
 */
1271
NPY_NO_EXPORT PyObject *
1272 1
_array_from_array_like(PyObject *op,
1273
        PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context) {
1274
    PyObject* tmp;
1275

1276
    /*
1277
     * If op supports the PEP 3118 buffer interface.
1278
     * We skip bytes and unicode since they are considered scalars. Unicode
1279
     * would fail but bytes would be incorrectly converted to a uint8 array.
1280
     */
1281 1
    if (!PyBytes_Check(op) && !PyUnicode_Check(op)) {
1282 1
        PyObject *memoryview = PyMemoryView_FromObject(op);
1283 1
        if (memoryview == NULL) {
1284 1
            PyErr_Clear();
1285
        }
1286
        else {
1287 1
            tmp = _array_from_buffer_3118(memoryview);
1288 1
            Py_DECREF(memoryview);
1289 1
            if (tmp == NULL) {
1290
                return NULL;
1291
            }
1292

1293 1
            if (writeable
1294 0
                && PyArray_FailUnlessWriteable(
1295
                        (PyArrayObject *)tmp, "PEP 3118 buffer") < 0) {
1296 0
                Py_DECREF(tmp);
1297
                return NULL;
1298
            }
1299

1300
            return tmp;
1301
        }
1302
    }
1303

1304
    /*
1305
     * If op supports the __array_struct__ or __array_interface__ interface.
1306
     */
1307 1
    tmp = PyArray_FromStructInterface(op);
1308 1
    if (tmp == NULL) {
1309
        return NULL;
1310
    }
1311 1
    if (tmp == Py_NotImplemented) {
1312
        /* Until the return, NotImplemented is always a borrowed reference*/
1313 1
        tmp = PyArray_FromInterface(op);
1314 1
        if (tmp == NULL) {
1315
            return NULL;
1316
        }
1317
    }
1318

1319
    /*
1320
     * If op supplies the __array__ function.
1321
     * The documentation says this should produce a copy, so
1322
     * we skip this method if writeable is true, because the intent
1323
     * of writeable is to modify the operand.
1324
     * XXX: If the implementation is wrong, and/or if actual
1325
     *      usage requires this behave differently,
1326
     *      this should be changed!
1327
     */
1328 1
    if (!writeable && tmp == Py_NotImplemented) {
1329 1
        tmp = PyArray_FromArrayAttr(op, requested_dtype, context);
1330 1
        if (tmp == NULL) {
1331
            return NULL;
1332
        }
1333
    }
1334

1335 1
    if (tmp != Py_NotImplemented) {
1336 1
        if (writeable &&
1337 0
                PyArray_FailUnlessWriteable((PyArrayObject *)tmp,
1338
                        "array interface object") < 0) {
1339 0
            Py_DECREF(tmp);
1340
            return NULL;
1341
        }
1342
        return tmp;
1343
    }
1344

1345
    /* Until here Py_NotImplemented was borrowed */
1346 1
    Py_INCREF(Py_NotImplemented);
1347 1
    return Py_NotImplemented;
1348
}
1349

1350

1351
/*NUMPY_API*/
1352
NPY_NO_EXPORT int
1353 0
PyArray_GetArrayParamsFromObject(PyObject *NPY_UNUSED(op),
1354
        PyArray_Descr *NPY_UNUSED(requested_dtype),
1355
        npy_bool NPY_UNUSED(writeable),
1356
        PyArray_Descr **NPY_UNUSED(out_dtype),
1357
        int *NPY_UNUSED(out_ndim), npy_intp *NPY_UNUSED(out_dims),
1358
        PyArrayObject **NPY_UNUSED(out_arr), PyObject *NPY_UNUSED(context))
1359
{
1360
    /* Deprecated in NumPy 1.19, removed in NumPy 1.20. */
1361 0
    PyErr_SetString(PyExc_RuntimeError,
1362
            "PyArray_GetArrayParamsFromObject() C-API function is removed "
1363
            "`PyArray_FromAny()` should be used at this time.  New C-API "
1364
            "may be exposed in the future (please do request this if it "
1365
            "would help you).");
1366 0
    return -1;
1367
}
1368

1369

1370
/*NUMPY_API
1371
 * Does not check for NPY_ARRAY_ENSURECOPY and NPY_ARRAY_NOTSWAPPED in flags
1372
 * Steals a reference to newtype --- which can be NULL
1373
 */
1374
NPY_NO_EXPORT PyObject *
1375 1
PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
1376
                int max_depth, int flags, PyObject *context)
1377
{
1378
    /*
1379
     * This is the main code to make a NumPy array from a Python
1380
     * Object.  It is called from many different places.
1381
     */
1382 1
    PyArrayObject *arr = NULL, *ret;
1383 1
    PyArray_Descr *dtype = NULL;
1384 1
    coercion_cache_obj *cache = NULL;
1385 1
    int ndim = 0;
1386
    npy_intp dims[NPY_MAXDIMS];
1387

1388 1
    if (context != NULL) {
1389 0
        PyErr_SetString(PyExc_RuntimeError, "'context' must be NULL");
1390 0
        return NULL;
1391
    }
1392

1393
    PyArray_Descr *fixed_descriptor;
1394
    PyArray_DTypeMeta *fixed_DType;
1395 1
    if (PyArray_ExtractDTypeAndDescriptor((PyObject *)newtype,
1396
            &fixed_descriptor, &fixed_DType) < 0) {
1397 0
        Py_XDECREF(newtype);
1398
        return NULL;
1399
    }
1400 1
    Py_XDECREF(newtype);
1401

1402 1
    ndim = PyArray_DiscoverDTypeAndShape(op,
1403
            NPY_MAXDIMS, dims, &cache, fixed_DType, fixed_descriptor, &dtype);
1404

1405 1
    Py_XDECREF(fixed_descriptor);
1406 1
    Py_XDECREF(fixed_DType);
1407 1
    if (ndim < 0) {
1408
        return NULL;
1409
    }
1410 1
    if (dtype == NULL) {
1411 1
        dtype = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
1412
    }
1413

1414 1
    if (min_depth != 0 && ndim < min_depth) {
1415 1
        PyErr_SetString(PyExc_ValueError,
1416
                "object of too small depth for desired array");
1417 1
        Py_DECREF(dtype);
1418 1
        npy_free_coercion_cache(cache);
1419 1
        return NULL;
1420
    }
1421 1
    if (max_depth != 0 && ndim > max_depth) {
1422 1
        PyErr_SetString(PyExc_ValueError,
1423
                "object too deep for desired array");
1424 1
        Py_DECREF(dtype);
1425 1
        npy_free_coercion_cache(cache);
1426 1
        return NULL;
1427
    }
1428

1429
    /* Got the correct parameters, but the cache may already hold the result */
1430 1
    if (cache != NULL && !(cache->sequence)) {
1431
        /*
1432
         * There is only a single array-like and it was converted, it
1433
         * may still have the incorrect type, but that is handled below.
1434
         */
1435
        assert(cache->converted_obj == op);
1436 1
        arr = (PyArrayObject *)(cache->arr_or_sequence);
1437
        /* we may need to cast or assert flags (e.g. copy) */
1438 1
        PyObject *res = PyArray_FromArray(arr, dtype, flags);
1439 1
        npy_unlink_coercion_cache(cache);
1440 1
        return res;
1441
    }
1442 1
    else if (cache == NULL && PyArray_IsScalar(op, Void) &&
1443 1
            !(((PyVoidScalarObject *)op)->flags & NPY_ARRAY_OWNDATA) &&
1444
            newtype == NULL) {
1445
        /*
1446
         * Special case, we return a *view* into void scalars, mainly to
1447
         * allow things similar to the "reversed" assignment:
1448
         *    arr[indx]["field"] = val  # instead of arr["field"][indx] = val
1449
         *
1450
         * It is unclear that this is necessary in this particular code path.
1451
         * Note that this path is only activated when the user did _not_
1452
         * provide a dtype (newtype is NULL).
1453
         */
1454
        assert(ndim == 0);
1455

1456 1
        return PyArray_NewFromDescrAndBase(
1457
                &PyArray_Type, dtype,
1458
                0, NULL, NULL,
1459 1
                ((PyVoidScalarObject *)op)->obval,
1460
                ((PyVoidScalarObject *)op)->flags,
1461
                NULL, op);
1462
    }
1463

1464
    /* There was no array (or array-like) passed in directly. */
1465 1
    if ((flags & NPY_ARRAY_WRITEBACKIFCOPY) ||
1466
            (flags & NPY_ARRAY_UPDATEIFCOPY)) {
1467 1
        PyErr_SetString(PyExc_TypeError,
1468
                        "WRITEBACKIFCOPY used for non-array input.");
1469 1
        Py_DECREF(dtype);
1470
        return NULL;
1471
    }
1472

1473
    /* Create a new array and copy the data */
1474 1
    ret = (PyArrayObject *)PyArray_NewFromDescr(
1475
            &PyArray_Type, dtype, ndim, dims, NULL, NULL,
1476
            flags&NPY_ARRAY_F_CONTIGUOUS, NULL);
1477 1
    if (ret == NULL) {
1478
        return NULL;
1479
    }
1480 1
    if (cache == NULL) {
1481
        /* This is a single item. Set it directly. */
1482
        assert(ndim == 0);
1483 1
        if (PyArray_Pack(PyArray_DESCR(ret), PyArray_DATA(ret), op) < 0) {
1484 1
            Py_DECREF(ret);
1485
            return NULL;
1486
        }
1487
        return (PyObject *)ret;
1488
    }
1489
    assert(ndim != 0);
1490
    assert(op == cache->converted_obj);
1491 1
    if (PyArray_AssignFromCache(ret, cache) < 0) {
1492 1
        Py_DECREF(ret);
1493
        return NULL;
1494
    }
1495
    return (PyObject *)ret;
1496
}
1497

1498
/*
1499
 * flags is any of
1500
 * NPY_ARRAY_C_CONTIGUOUS (formerly CONTIGUOUS),
1501
 * NPY_ARRAY_F_CONTIGUOUS (formerly FORTRAN),
1502
 * NPY_ARRAY_ALIGNED,
1503
 * NPY_ARRAY_WRITEABLE,
1504
 * NPY_ARRAY_NOTSWAPPED,
1505
 * NPY_ARRAY_ENSURECOPY,
1506
 * NPY_ARRAY_UPDATEIFCOPY,
1507
 * NPY_ARRAY_WRITEBACKIFCOPY,
1508
 * NPY_ARRAY_FORCECAST,
1509
 * NPY_ARRAY_ENSUREARRAY,
1510
 * NPY_ARRAY_ELEMENTSTRIDES
1511
 *
1512
 * or'd (|) together
1513
 *
1514
 * Any of these flags present means that the returned array should
1515
 * guarantee that aspect of the array.  Otherwise the returned array
1516
 * won't guarantee it -- it will depend on the object as to whether or
1517
 * not it has such features.
1518
 *
1519
 * Note that NPY_ARRAY_ENSURECOPY is enough
1520
 * to guarantee NPY_ARRAY_C_CONTIGUOUS, NPY_ARRAY_ALIGNED and
1521
 * NPY_ARRAY_WRITEABLE and therefore it is redundant to include
1522
 * those as well.
1523
 *
1524
 * NPY_ARRAY_BEHAVED == NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE
1525
 * NPY_ARRAY_CARRAY = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_BEHAVED
1526
 * NPY_ARRAY_FARRAY = NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_BEHAVED
1527
 *
1528
 * NPY_ARRAY_F_CONTIGUOUS can be set in the FLAGS to request a FORTRAN array.
1529
 * Fortran arrays are always behaved (aligned,
1530
 * notswapped, and writeable) and not (C) CONTIGUOUS (if > 1d).
1531
 *
1532
 * NPY_ARRAY_UPDATEIFCOPY is deprecated in favor of
1533
 * NPY_ARRAY_WRITEBACKIFCOPY in 1.14
1534

1535
 * NPY_ARRAY_WRITEBACKIFCOPY flag sets this flag in the returned
1536
 * array if a copy is made and the base argument points to the (possibly)
1537
 * misbehaved array. Before returning to python, PyArray_ResolveWritebackIfCopy
1538
 * must be called to update the contents of the original array from the copy.
1539
 *
1540
 * NPY_ARRAY_FORCECAST will cause a cast to occur regardless of whether or not
1541
 * it is safe.
1542
 *
1543
 * context is passed through to PyArray_GetArrayParamsFromObject
1544
 */
1545

1546
/*NUMPY_API
1547
 * steals a reference to descr -- accepts NULL
1548
 */
1549
NPY_NO_EXPORT PyObject *
1550 1
PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth,
1551
                     int max_depth, int requires, PyObject *context)
1552
{
1553
    PyObject *obj;
1554 1
    if (requires & NPY_ARRAY_NOTSWAPPED) {
1555 1
        if (!descr && PyArray_Check(op) &&
1556 1
                PyArray_ISBYTESWAPPED((PyArrayObject* )op)) {
1557 0
            descr = PyArray_DescrNew(PyArray_DESCR((PyArrayObject *)op));
1558
        }
1559 1
        else if (descr && !PyArray_ISNBO(descr->byteorder)) {
1560 0
            PyArray_DESCR_REPLACE(descr);
1561
        }
1562 1
        if (descr && descr->byteorder != NPY_IGNORE) {
1563 1
            descr->byteorder = NPY_NATIVE;
1564
        }
1565
    }
1566

1567 1
    obj = PyArray_FromAny(op, descr, min_depth, max_depth, requires, context);
1568 1
    if (obj == NULL) {
1569
        return NULL;
1570
    }
1571 1
    if ((requires & NPY_ARRAY_ELEMENTSTRIDES) &&
1572 0
        !PyArray_ElementStrides(obj)) {
1573
        PyObject *ret;
1574 0
        ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER);
1575 0
        Py_DECREF(obj);
1576
        obj = ret;
1577
    }
1578
    return obj;
1579
}
1580

1581

1582
/*NUMPY_API
1583
 * steals reference to newtype --- acc. NULL
1584
 */
1585
NPY_NO_EXPORT PyObject *
1586 1
PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
1587
{
1588

1589 1
    PyArrayObject *ret = NULL;
1590 1
    int copy = 0;
1591
    int arrflags;
1592
    PyArray_Descr *oldtype;
1593 1
    NPY_CASTING casting = NPY_SAFE_CASTING;
1594

1595 1
    oldtype = PyArray_DESCR(arr);
1596 1
    if (newtype == NULL) {
1597
        /*
1598
         * Check if object is of array with Null newtype.
1599
         * If so return it directly instead of checking for casting.
1600
         */
1601 1
        if (flags == 0) {
1602 1
            Py_INCREF(arr);
1603 1
            return (PyObject *)arr;
1604
        }
1605 1
        newtype = oldtype;
1606 1
        Py_INCREF(oldtype);
1607
    }
1608 1
    else if (PyDataType_ISUNSIZED(newtype)) {
1609 1
        PyArray_DESCR_REPLACE(newtype);
1610 1
        if (newtype == NULL) {
1611
            return NULL;
1612
        }
1613 1
        newtype->elsize = oldtype->elsize;
1614
    }
1615

1616
    /* If the casting if forced, use the 'unsafe' casting rule */
1617 1
    if (flags & NPY_ARRAY_FORCECAST) {
1618 1
        casting = NPY_UNSAFE_CASTING;
1619
    }
1620

1621
    /* Raise an error if the casting rule isn't followed */
1622 1
    if (!PyArray_CanCastArrayTo(arr, newtype, casting)) {
1623 1
        PyErr_Clear();
1624 1
        npy_set_invalid_cast_error(
1625 1
                PyArray_DESCR(arr), newtype, casting, PyArray_NDIM(arr) == 0);
1626 1
        Py_DECREF(newtype);
1627
        return NULL;
1628
    }
1629

1630 1
    arrflags = PyArray_FLAGS(arr);
1631
           /* If a guaranteed copy was requested */
1632 1
    copy = (flags & NPY_ARRAY_ENSURECOPY) ||
1633
           /* If C contiguous was requested, and arr is not */
1634 1
           ((flags & NPY_ARRAY_C_CONTIGUOUS) &&
1635 1
                   (!(arrflags & NPY_ARRAY_C_CONTIGUOUS))) ||
1636
           /* If an aligned array was requested, and arr is not */
1637 1
           ((flags & NPY_ARRAY_ALIGNED) &&
1638 1
                   (!(arrflags & NPY_ARRAY_ALIGNED))) ||
1639
           /* If a Fortran contiguous array was requested, and arr is not */
1640 1
           ((flags & NPY_ARRAY_F_CONTIGUOUS) &&
1641 1
                   (!(arrflags & NPY_ARRAY_F_CONTIGUOUS))) ||
1642
           /* If a writeable array was requested, and arr is not */
1643 1
           ((flags & NPY_ARRAY_WRITEABLE) &&
1644 1
                   (!(arrflags & NPY_ARRAY_WRITEABLE))) ||
1645 1
           !PyArray_EquivTypes(oldtype, newtype);
1646

1647
    if (copy) {
1648 1
        NPY_ORDER order = NPY_KEEPORDER;
1649 1
        int subok = 1;
1650

1651
        /* Set the order for the copy being made based on the flags */
1652 1
        if (flags & NPY_ARRAY_F_CONTIGUOUS) {
1653
            order = NPY_FORTRANORDER;
1654
        }
1655 1
        else if (flags & NPY_ARRAY_C_CONTIGUOUS) {
1656 1
            order = NPY_CORDER;
1657
        }
1658

1659 1
        if ((flags & NPY_ARRAY_ENSUREARRAY)) {
1660 1
            subok = 0;
1661
        }
1662 1
        ret = (PyArrayObject *)PyArray_NewLikeArray(arr, order,
1663
                                                    newtype, subok);
1664 1
        if (ret == NULL) {
1665
            return NULL;
1666
        }
1667

1668 1
        if (PyArray_CopyInto(ret, arr) < 0) {
1669 1
            Py_DECREF(ret);
1670
            return NULL;
1671
        }
1672

1673 1
        if (flags & NPY_ARRAY_UPDATEIFCOPY) {
1674
            /* This is the ONLY place the NPY_ARRAY_UPDATEIFCOPY flag
1675
             * is still used.
1676
             * Can be deleted once the flag itself is removed
1677
             */
1678

1679
            /* 2017-Nov-10 1.14 */
1680 1
            if (DEPRECATE(
1681
                    "NPY_ARRAY_UPDATEIFCOPY, NPY_ARRAY_INOUT_ARRAY, and "
1682
                    "NPY_ARRAY_INOUT_FARRAY are deprecated, use NPY_WRITEBACKIFCOPY, "
1683
                    "NPY_ARRAY_INOUT_ARRAY2, or NPY_ARRAY_INOUT_FARRAY2 respectively "
1684
                    "instead, and call PyArray_ResolveWritebackIfCopy before the "
1685
                    "array is deallocated, i.e. before the last call to Py_DECREF.") < 0) {
1686 1
                Py_DECREF(ret);
1687
                return NULL;
1688
            }
1689 1
            Py_INCREF(arr);
1690 1
            if (PyArray_SetWritebackIfCopyBase(ret, arr) < 0) {
1691 0
                Py_DECREF(ret);
1692
                return NULL;
1693
            }
1694 1
            PyArray_ENABLEFLAGS(ret, NPY_ARRAY_UPDATEIFCOPY);
1695
            PyArray_CLEARFLAGS(ret, NPY_ARRAY_WRITEBACKIFCOPY);
1696
        }
1697 1
        else if (flags & NPY_ARRAY_WRITEBACKIFCOPY) {
1698 1
            Py_INCREF(arr);
1699 1
            if (PyArray_SetWritebackIfCopyBase(ret, arr) < 0) {
1700 0
                Py_DECREF(ret);
1701
                return NULL;
1702
            }
1703
        }
1704
    }
1705
    /*
1706
     * If no copy then take an appropriate view if necessary, or
1707
     * just return a reference to ret itself.
1708
     */
1709
    else {
1710 1
        int needview = ((flags & NPY_ARRAY_ENSUREARRAY) &&
1711 1
                        !PyArray_CheckExact(arr));
1712

1713 1
        Py_DECREF(newtype);
1714 1
        if (needview) {
1715 1
            PyTypeObject *subtype = NULL;
1716

1717 1
            if (flags & NPY_ARRAY_ENSUREARRAY) {
1718 1
                subtype = &PyArray_Type;
1719
            }
1720

1721 1
            ret = (PyArrayObject *)PyArray_View(arr, NULL, subtype);
1722 1
            if (ret == NULL) {
1723
                return NULL;
1724
            }
1725
        }
1726
        else {
1727 1
            Py_INCREF(arr);
1728 1
            ret = arr;
1729
        }
1730
    }
1731

1732
    return (PyObject *)ret;
1733
}
1734

1735
/*NUMPY_API */
1736
NPY_NO_EXPORT PyObject *
1737 1
PyArray_FromStructInterface(PyObject *input)
1738
{
1739 1
    PyArray_Descr *thetype = NULL;
1740
    PyArrayInterface *inter;
1741
    PyObject *attr;
1742 1
    char endian = NPY_NATBYTE;
1743

1744 1
    attr = PyArray_LookupSpecial_OnInstance(input, "__array_struct__");
1745 1
    if (attr == NULL) {
1746 1
        if (PyErr_Occurred()) {
1747
            return NULL;
1748
        } else {
1749 1
            return Py_NotImplemented;
1750
        }
1751
    }
1752 1
    if (!PyCapsule_CheckExact(attr)) {
1753 1
        if (PyType_Check(input) && PyObject_HasAttrString(attr, "__get__")) {
1754
            /*
1755
             * If the input is a class `attr` should be a property-like object.
1756
             * This cannot be interpreted as an array, but is a valid.
1757
             * (Needed due to the lookup being on the instance rather than type)
1758
             */
1759 1
            Py_DECREF(attr);
1760
            return Py_NotImplemented;
1761
        }
1762
        goto fail;
1763
    }
1764 1
    inter = PyCapsule_GetPointer(attr, NULL);
1765 1
    if (inter == NULL) {
1766
        goto fail;
1767
    }
1768 1
    if (inter->two != 2) {
1769
        goto fail;
1770
    }
1771 1
    if ((inter->flags & NPY_ARRAY_NOTSWAPPED) != NPY_ARRAY_NOTSWAPPED) {
1772 1
        endian = NPY_OPPBYTE;
1773 1
        inter->flags &= ~NPY_ARRAY_NOTSWAPPED;
1774
    }
1775

1776 1
    if (inter->flags & NPY_ARR_HAS_DESCR) {
1777 0
        if (PyArray_DescrConverter(inter->descr, &thetype) == NPY_FAIL) {
1778 0
            thetype = NULL;
1779 0
            PyErr_Clear();
1780
        }
1781
    }
1782

1783 1
    if (thetype == NULL) {
1784 1
        PyObject *type_str = PyUnicode_FromFormat(
1785 1
            "%c%c%d", endian, inter->typekind, inter->itemsize);
1786 1
        if (type_str == NULL) {
1787 0
            Py_DECREF(attr);
1788
            return NULL;
1789
        }
1790 1
        int ok = PyArray_DescrConverter(type_str, &thetype);
1791 1
        Py_DECREF(type_str);
1792 1
        if (ok != NPY_SUCCEED) {
1793 0
            Py_DECREF(attr);
1794
            return NULL;
1795
        }
1796
    }
1797

1798 1
    PyObject *ret = PyArray_NewFromDescrAndBase(
1799
            &PyArray_Type, thetype,
1800 1
            inter->nd, inter->shape, inter->strides, inter->data,
1801
            inter->flags, NULL, input);
1802 1
    Py_DECREF(attr);
1803
    return ret;
1804

1805 0
 fail:
1806 0
    PyErr_SetString(PyExc_ValueError, "invalid __array_struct__");
1807 0
    Py_DECREF(attr);
1808
    return NULL;
1809
}
1810

1811
/*
1812
 * Checks if the object in descr is the default 'descr' member for the
1813
 * __array_interface__ dictionary with 'typestr' member typestr.
1814
 */
1815
NPY_NO_EXPORT int
1816 1
_is_default_descr(PyObject *descr, PyObject *typestr) {
1817 1
    if (!PyList_Check(descr) || PyList_GET_SIZE(descr) != 1) {
1818
        return 0;
1819
    }
1820 1
    PyObject *tuple = PyList_GET_ITEM(descr, 0);
1821 1
    if (!(PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2)) {
1822
        return 0;
1823
    }
1824 1
    PyObject *name = PyTuple_GET_ITEM(tuple, 0);
1825 1
    if (!(PyUnicode_Check(name) && PyUnicode_GetLength(name) == 0)) {
1826
        return 0;
1827
    }
1828 1
    PyObject *typestr2 = PyTuple_GET_ITEM(tuple, 1);
1829 1
    return PyObject_RichCompareBool(typestr, typestr2, Py_EQ);
1830
}
1831

1832
/*NUMPY_API*/
1833
NPY_NO_EXPORT PyObject *
1834 1
PyArray_FromInterface(PyObject *origin)
1835
{
1836 1
    PyObject *iface = NULL;
1837 1
    PyObject *attr = NULL;
1838 1
    PyObject *base = NULL;
1839
    PyArrayObject *ret;
1840 1
    PyArray_Descr *dtype = NULL;
1841 1
    char *data = NULL;
1842
    Py_buffer view;
1843
    int i, n;
1844
    npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS];
1845 1
    int dataflags = NPY_ARRAY_BEHAVED;
1846

1847 1
    iface = PyArray_LookupSpecial_OnInstance(origin, "__array_interface__");
1848

1849 1
    if (iface == NULL) {
1850 1
        if (PyErr_Occurred()) {
1851 0
            PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
1852
        }
1853
        return Py_NotImplemented;
1854
    }
1855 1
    if (!PyDict_Check(iface)) {
1856 1
        if (PyType_Check(origin) && PyObject_HasAttrString(iface, "__get__")) {
1857
            /*
1858
             * If the input is a class `iface` should be a property-like object.
1859
             * This cannot be interpreted as an array, but is a valid.
1860
             * (Needed due to the lookup being on the instance rather than type)
1861
             */
1862 1
            Py_DECREF(iface);
1863
            return Py_NotImplemented;
1864
        }
1865

1866 0
        Py_DECREF(iface);
1867 0
        PyErr_SetString(PyExc_ValueError,
1868
                "Invalid __array_interface__ value, must be a dict");
1869 0
        return NULL;
1870
    }
1871

1872
    /* Get type string from interface specification */
1873 1
    attr = _PyDict_GetItemStringWithError(iface, "typestr");
1874 1
    if (attr == NULL) {
1875 1
        Py_DECREF(iface);
1876 1
        if (!PyErr_Occurred()) {
1877 1
            PyErr_SetString(PyExc_ValueError,
1878
                    "Missing __array_interface__ typestr");
1879
        }
1880
        return NULL;
1881
    }
1882

1883
    /* allow bytes for backwards compatibility */
1884 1
    if (!PyBytes_Check(attr) && !PyUnicode_Check(attr)) {
1885 0
        PyErr_SetString(PyExc_TypeError,
1886
                    "__array_interface__ typestr must be a string");
1887 0
        goto fail;
1888
    }
1889

1890
    /* Get dtype from type string */
1891 1
    if (PyArray_DescrConverter(attr, &dtype) != NPY_SUCCEED) {
1892
        goto fail;
1893
    }
1894

1895
    /*
1896
     * If the dtype is NPY_VOID, see if there is extra information in
1897
     * the 'descr' attribute.
1898
     */
1899 1
    if (dtype->type_num == NPY_VOID) {
1900 1
        PyObject *descr = _PyDict_GetItemStringWithError(iface, "descr");
1901 1
        if (descr == NULL && PyErr_Occurred()) {
1902
            goto fail;
1903
        }
1904 1
        PyArray_Descr *new_dtype = NULL;
1905 1
        if (descr != NULL) {
1906 1
            int is_default = _is_default_descr(descr, attr);
1907 1
            if (is_default < 0) {
1908
                goto fail;
1909
            }
1910 1
            if (!is_default) {
1911 1
                if (PyArray_DescrConverter2(descr, &new_dtype) != NPY_SUCCEED) {
1912
                    goto fail;
1913
                }
1914 1
                if (new_dtype != NULL) {
1915 1
                    Py_DECREF(dtype);
1916 1
                    dtype = new_dtype;
1917
                }
1918
            }
1919

1920
        }
1921

1922
    }
1923

1924
    /* Get shape tuple from interface specification */
1925 1
    attr = _PyDict_GetItemStringWithError(iface, "shape");
1926 1
    if (attr == NULL) {
1927 1
        if (PyErr_Occurred()) {
1928
            return NULL;
1929
        }
1930
        /* Shape must be specified when 'data' is specified */
1931 1
        PyObject *data = _PyDict_GetItemStringWithError(iface, "data");
1932 1
        if (data == NULL && PyErr_Occurred()) {
1933
            return NULL;
1934
        }
1935 1
        else if (data != NULL) {
1936 0
            Py_DECREF(iface);
1937 0
            PyErr_SetString(PyExc_ValueError,
1938
                    "Missing __array_interface__ shape");
1939 0
            return NULL;
1940
        }
1941
        /* Assume shape as scalar otherwise */
1942
        else {
1943
            /* NOTE: pointers to data and base should be NULL */
1944 1
            n = dims[0] = 0;
1945
        }
1946
    }
1947
    /* Make sure 'shape' is a tuple */
1948 1
    else if (!PyTuple_Check(attr)) {
1949 1
        PyErr_SetString(PyExc_TypeError,
1950
                "shape must be a tuple");
1951 1
        goto fail;
1952
    }
1953
    /* Get dimensions from shape tuple */
1954
    else {
1955 1
        n = PyTuple_GET_SIZE(attr);
1956 1
        for (i = 0; i < n; i++) {
1957 1
            PyObject *tmp = PyTuple_GET_ITEM(attr, i);
1958 1
            dims[i] = PyArray_PyIntAsIntp(tmp);
1959 1
            if (error_converting(dims[i])) {
1960
                goto fail;
1961
            }
1962
        }
1963
    }
1964

1965
    /* Get data buffer from interface specification */
1966 1
    attr = _PyDict_GetItemStringWithError(iface, "data");
1967 1
    if (attr == NULL && PyErr_Occurred()){
1968
        return NULL;
1969
    }
1970

1971
    /* Case for data access through pointer */
1972 1
    if (attr && PyTuple_Check(attr)) {
1973
        PyObject *dataptr;
1974 1
        if (PyTuple_GET_SIZE(attr) != 2) {
1975 0
            PyErr_SetString(PyExc_TypeError,
1976
                    "__array_interface__ data must be a 2-tuple with "
1977
                    "(data pointer integer, read-only flag)");
1978 0
            goto fail;
1979
        }
1980 1
        dataptr = PyTuple_GET_ITEM(attr, 0);
1981 1
        if (PyLong_Check(dataptr)) {
1982 1
            data = PyLong_AsVoidPtr(dataptr);
1983 1
            if (data == NULL && PyErr_Occurred()) {
1984
                goto fail;
1985
            }
1986
        }
1987
        else {
1988 0
            PyErr_SetString(PyExc_TypeError,
1989
                    "first element of __array_interface__ data tuple "
1990
                    "must be an integer.");
1991 0
            goto fail;
1992
        }
1993 1
        if (PyObject_IsTrue(PyTuple_GET_ITEM(attr,1))) {
1994 0
            dataflags &= ~NPY_ARRAY_WRITEABLE;
1995
        }
1996
        base = origin;
1997
    }
1998

1999
    /* Case for data access through buffer */
2000 1
    else if (attr) {
2001 1
        if (attr != Py_None) {
2002
            base = attr;
2003
        }
2004
        else {
2005 0
            base = origin;
2006
        }
2007 1
        if (PyObject_GetBuffer(base, &view,
2008
                    PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) {
2009 1
            PyErr_Clear();
2010 1
            if (PyObject_GetBuffer(base, &view,
2011
                        PyBUF_SIMPLE) < 0) {
2012
                goto fail;
2013
            }
2014
            dataflags &= ~NPY_ARRAY_WRITEABLE;
2015
        }
2016 1
        data = (char *)view.buf;
2017
        /*
2018
         * In Python 3 both of the deprecated functions PyObject_AsWriteBuffer and
2019
         * PyObject_AsReadBuffer that this code replaces release the buffer. It is
2020
         * up to the object that supplies the buffer to guarantee that the buffer
2021
         * sticks around after the release.
2022
         */
2023 1
        PyBuffer_Release(&view);
2024

2025
        /* Get offset number from interface specification */
2026 1
        attr = _PyDict_GetItemStringWithError(iface, "offset");
2027 1
        if (attr == NULL && PyErr_Occurred()) {
2028
            goto fail;
2029
        }
2030 1
        else if (attr) {
2031 1
            npy_longlong num = PyLong_AsLongLong(attr);
2032 1
            if (error_converting(num)) {
2033 0
                PyErr_SetString(PyExc_TypeError,
2034
                        "__array_interface__ offset must be an integer");
2035 0
                goto fail;
2036
            }
2037 1
            data += num;
2038
        }
2039
    }
2040

2041 1
    ret = (PyArrayObject *)PyArray_NewFromDescrAndBase(
2042
            &PyArray_Type, dtype,
2043
            n, dims, NULL, data,
2044
            dataflags, NULL, base);
2045
    /*
2046
     * Ref to dtype was stolen by PyArray_NewFromDescrAndBase
2047
     * Prevent DECREFing dtype in fail codepath by setting to NULL
2048
     */
2049 1
    dtype = NULL;
2050 1
    if (ret == NULL) {
2051
        goto fail;
2052
    }
2053 1
    if (data == NULL) {
2054 1
        if (PyArray_SIZE(ret) > 1) {
2055 1
            PyErr_SetString(PyExc_ValueError,
2056
                    "cannot coerce scalar to array with size > 1");
2057 1
            Py_DECREF(ret);
2058
            goto fail;
2059
        }
2060 1
        if (PyArray_SETITEM(ret, PyArray_DATA(ret), origin) < 0) {
2061 0
            Py_DECREF(ret);
2062
            goto fail;
2063
        }
2064
    }
2065 1
    attr = _PyDict_GetItemStringWithError(iface, "strides");
2066 1
    if (attr == NULL && PyErr_Occurred()){
2067
        return NULL;
2068
    }
2069 1
    if (attr != NULL && attr != Py_None) {
2070 1
        if (!PyTuple_Check(attr)) {
2071 1
            PyErr_SetString(PyExc_TypeError,
2072
                    "strides must be a tuple");
2073 1
            Py_DECREF(ret);
2074
            goto fail;
2075
        }
2076 1
        if (n != PyTuple_GET_SIZE(attr)) {
2077 1
            PyErr_SetString(PyExc_ValueError,
2078
                    "mismatch in length of strides and shape");
2079 1
            Py_DECREF(ret);
2080
            goto fail;
2081
        }
2082 1
        for (i = 0; i < n; i++) {
2083 1
            PyObject *tmp = PyTuple_GET_ITEM(attr, i);
2084 1
            strides[i] = PyArray_PyIntAsIntp(tmp);
2085 1
            if (error_converting(strides[i])) {
2086 0
                Py_DECREF(ret);
2087
                goto fail;
2088
            }
2089
        }
2090 1
        if (n) {
2091 1
            memcpy(PyArray_STRIDES(ret), strides, n*sizeof(npy_intp));
2092
        }
2093
    }
2094 1
    PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL);
2095 1
    Py_DECREF(iface);
2096
    return (PyObject *)ret;
2097

2098 1
 fail:
2099 1
    Py_XDECREF(dtype);
2100 1
    Py_XDECREF(iface);
2101
    return NULL;
2102
}
2103

2104
/*NUMPY_API
2105
 */
2106
NPY_NO_EXPORT PyObject *
2107 1
PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context)
2108
{
2109
    PyObject *new;
2110
    PyObject *array_meth;
2111

2112 1
    if (context != NULL) {
2113 0
        PyErr_SetString(PyExc_RuntimeError, "'context' must be NULL");
2114 0
        return NULL;
2115
    }
2116 1
    array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__");
2117 1
    if (array_meth == NULL) {
2118 1
        if (PyErr_Occurred()) {
2119 0
            PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
2120
        }
2121
        return Py_NotImplemented;
2122
    }
2123 1
    if (PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__")) {
2124
        /*
2125
         * If the input is a class `array_meth` may be a property-like object.
2126
         * This cannot be interpreted as an array (called), but is a valid.
2127
         * Trying `array_meth.__call__()` on this should not be useful.
2128
         * (Needed due to the lookup being on the instance rather than type)
2129
         */
2130 1
        Py_DECREF(array_meth);
2131
        return Py_NotImplemented;
2132
    }
2133 1
    if (typecode == NULL) {
2134 1
        new = PyObject_CallFunction(array_meth, NULL);
2135
    }
2136
    else {
2137 1
        new = PyObject_CallFunction(array_meth, "O", typecode);
2138
    }
2139 1
    Py_DECREF(array_meth);
2140 1
    if (new == NULL) {
2141
        return NULL;
2142
    }
2143 1
    if (!PyArray_Check(new)) {
2144 1
        PyErr_SetString(PyExc_ValueError,
2145
                        "object __array__ method not "  \
2146
                        "producing an array");
2147 1
        Py_DECREF(new);
2148
        return NULL;
2149
    }
2150
    return new;
2151
}
2152

2153
/*NUMPY_API
2154
* new reference -- accepts NULL for mintype
2155
*/
2156
NPY_NO_EXPORT PyArray_Descr *
2157 1
PyArray_DescrFromObject(PyObject *op, PyArray_Descr *mintype)
2158
{
2159
    PyArray_Descr *dtype;
2160

2161 1
    dtype = mintype;
2162 1
    Py_XINCREF(dtype);
2163

2164 1
    if (PyArray_DTypeFromObject(op, NPY_MAXDIMS, &dtype) < 0) {
2165
        return NULL;
2166
    }
2167

2168 1
    if (dtype == NULL) {
2169 0
        return PyArray_DescrFromType(NPY_DEFAULT_TYPE);
2170
    }
2171
    else {
2172
        return dtype;
2173
    }
2174
}
2175

2176
/* These are also old calls (should use PyArray_NewFromDescr) */
2177

2178
/* They all zero-out the memory as previously done */
2179

2180
/* steals reference to descr -- and enforces native byteorder on it.*/
2181

2182
/*NUMPY_API
2183
  Deprecated, use PyArray_NewFromDescr instead.
2184
*/
2185
NPY_NO_EXPORT PyObject *
2186 0
PyArray_FromDimsAndDataAndDescr(int NPY_UNUSED(nd), int *NPY_UNUSED(d),
2187
                                PyArray_Descr *descr,
2188
                                char *NPY_UNUSED(data))
2189
{
2190 0
    PyErr_SetString(PyExc_NotImplementedError,
2191
                "PyArray_FromDimsAndDataAndDescr: use PyArray_NewFromDescr.");
2192 0
    Py_DECREF(descr);
2193 0
    return NULL;
2194
}
2195

2196
/*NUMPY_API
2197
  Deprecated, use PyArray_SimpleNew instead.
2198
*/
2199
NPY_NO_EXPORT PyObject *
2200 0
PyArray_FromDims(int NPY_UNUSED(nd), int *NPY_UNUSED(d), int NPY_UNUSED(type))
2201
{
2202 0
    PyErr_SetString(PyExc_NotImplementedError,
2203
                "PyArray_FromDims: use PyArray_SimpleNew.");
2204 0
    return NULL;
2205
}
2206

2207
/* end old calls */
2208

2209
/*NUMPY_API
2210
 * This is a quick wrapper around
2211
 * PyArray_FromAny(op, NULL, 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)
2212
 * that special cases Arrays and PyArray_Scalars up front
2213
 * It *steals a reference* to the object
2214
 * It also guarantees that the result is PyArray_Type
2215
 * Because it decrefs op if any conversion needs to take place
2216
 * so it can be used like PyArray_EnsureArray(some_function(...))
2217
 */
2218
NPY_NO_EXPORT PyObject *
2219 1
PyArray_EnsureArray(PyObject *op)
2220
{
2221
    PyObject *new;
2222

2223 1
    if ((op == NULL) || (PyArray_CheckExact(op))) {
2224 1
        new = op;
2225 1
        Py_XINCREF(new);
2226
    }
2227 1
    else if (PyArray_Check(op)) {
2228 0
        new = PyArray_View((PyArrayObject *)op, NULL, &PyArray_Type);
2229
    }
2230 1
    else if (PyArray_IsScalar(op, Generic)) {
2231 0
        new = PyArray_FromScalar(op, NULL);
2232
    }
2233
    else {
2234 1
        new = PyArray_FROM_OF(op, NPY_ARRAY_ENSUREARRAY);
2235
    }
2236 1
    Py_XDECREF(op);
2237 1
    return new;
2238
}
2239

2240
/*NUMPY_API*/
2241
NPY_NO_EXPORT PyObject *
2242 1
PyArray_EnsureAnyArray(PyObject *op)
2243
{
2244 1
    if (op && PyArray_Check(op)) {
2245
        return op;
2246
    }
2247 0
    return PyArray_EnsureArray(op);
2248
}
2249

2250
/*
2251
 * Private implementation of PyArray_CopyAnyInto with an additional order
2252
 * parameter.
2253
 */
2254
NPY_NO_EXPORT int
2255 1
PyArray_CopyAsFlat(PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order)
2256
{
2257 1
    PyArray_StridedUnaryOp *stransfer = NULL;
2258 1
    NpyAuxData *transferdata = NULL;
2259
    NpyIter *dst_iter, *src_iter;
2260

2261
    NpyIter_IterNextFunc *dst_iternext, *src_iternext;
2262
    char **dst_dataptr, **src_dataptr;
2263
    npy_intp dst_stride, src_stride;
2264
    npy_intp *dst_countptr, *src_countptr;
2265
    npy_uint32 baseflags;
2266

2267
    char *dst_data, *src_data;
2268
    npy_intp dst_count, src_count, count;
2269
    npy_intp src_itemsize;
2270
    npy_intp dst_size, src_size;
2271
    int needs_api;
2272

2273 1
    NPY_BEGIN_THREADS_DEF;
2274

2275 1
    if (PyArray_FailUnlessWriteable(dst, "destination array") < 0) {
2276
        return -1;
2277
    }
2278

2279
    /*
2280
     * If the shapes match and a particular order is forced
2281
     * for both, use the more efficient CopyInto
2282
     */
2283 1
    if (order != NPY_ANYORDER && order != NPY_KEEPORDER &&
2284 1
            PyArray_NDIM(dst) == PyArray_NDIM(src) &&
2285 1
            PyArray_CompareLists(PyArray_DIMS(dst), PyArray_DIMS(src),
2286
                                PyArray_NDIM(dst))) {
2287 1
        return PyArray_CopyInto(dst, src);
2288
    }
2289

2290 1
    dst_size = PyArray_SIZE(dst);
2291 1
    src_size = PyArray_SIZE(src);
2292 1
    if (dst_size != src_size) {
2293 0
        PyErr_Format(PyExc_ValueError,
2294
                "cannot copy from array of size %" NPY_INTP_FMT " into an array "
2295
                "of size %" NPY_INTP_FMT, src_size, dst_size);
2296 0
        return -1;
2297
    }
2298

2299
    /* Zero-sized arrays require nothing be done */
2300 1
    if (dst_size == 0) {
2301
        return 0;
2302
    }
2303

2304 1
    baseflags = NPY_ITER_EXTERNAL_LOOP |
2305
                NPY_ITER_DONT_NEGATE_STRIDES |
2306
                NPY_ITER_REFS_OK;
2307

2308
    /*
2309
     * This copy is based on matching C-order traversals of src and dst.
2310
     * By using two iterators, we can find maximal sub-chunks that
2311
     * can be processed at once.
2312
     */
2313 1
    dst_iter = NpyIter_New(dst, NPY_ITER_WRITEONLY | baseflags,
2314
                                order,
2315
                                NPY_NO_CASTING,
2316
                                NULL);
2317 1
    if (dst_iter == NULL) {
2318
        return -1;
2319
    }
2320 1
    src_iter = NpyIter_New(src, NPY_ITER_READONLY | baseflags,
2321
                                order,
2322
                                NPY_NO_CASTING,
2323
                                NULL);
2324 1
    if (src_iter == NULL) {
2325 0
        NpyIter_Deallocate(dst_iter);
2326 0
        return -1;
2327
    }
2328

2329
    /* Get all the values needed for the inner loop */
2330 1
    dst_iternext = NpyIter_GetIterNext(dst_iter, NULL);
2331 1
    dst_dataptr = NpyIter_GetDataPtrArray(dst_iter);
2332
    /* Since buffering is disabled, we can cache the stride */
2333 1
    dst_stride = NpyIter_GetInnerStrideArray(dst_iter)[0];
2334 1
    dst_countptr = NpyIter_GetInnerLoopSizePtr(dst_iter);
2335

2336 1
    src_iternext = NpyIter_GetIterNext(src_iter, NULL);
2337 1
    src_dataptr = NpyIter_GetDataPtrArray(src_iter);
2338
    /* Since buffering is disabled, we can cache the stride */
2339 1
    src_stride = NpyIter_GetInnerStrideArray(src_iter)[0];
2340 1
    src_countptr = NpyIter_GetInnerLoopSizePtr(src_iter);
2341 1
    src_itemsize = PyArray_DESCR(src)->elsize;
2342

2343 1
    if (dst_iternext == NULL || src_iternext == NULL) {
2344 0
        NpyIter_Deallocate(dst_iter);
2345 0
        NpyIter_Deallocate(src_iter);
2346 0
        return -1;
2347
    }
2348

2349 1
    needs_api = NpyIter_IterationNeedsAPI(dst_iter) ||
2350 1
                NpyIter_IterationNeedsAPI(src_iter);
2351

2352
    /*
2353
     * Because buffering is disabled in the iterator, the inner loop
2354
     * strides will be the same throughout the iteration loop.  Thus,
2355
     * we can pass them to this function to take advantage of
2356
     * contiguous strides, etc.
2357
     */
2358 1
    if (PyArray_GetDTypeTransferFunction(
2359 1
                    IsUintAligned(src) && IsAligned(src) &&
2360 1
                    IsUintAligned(dst) && IsAligned(dst),
2361
                    src_stride, dst_stride,
2362
                    PyArray_DESCR(src), PyArray_DESCR(dst),
2363
                    0,
2364
                    &stransfer, &transferdata,
2365
                    &needs_api) != NPY_SUCCEED) {
2366 0
        NpyIter_Deallocate(dst_iter);
2367 0
        NpyIter_Deallocate(src_iter);
2368 0
        return -1;
2369
    }
2370

2371 1
    if (!needs_api) {
2372 1
        NPY_BEGIN_THREADS;
2373
    }
2374

2375 1
    dst_count = *dst_countptr;
2376 1
    src_count = *src_countptr;
2377 1
    dst_data = dst_dataptr[0];
2378 1
    src_data = src_dataptr[0];
2379 1
    int res = 0;
2380
    for(;;) {
2381
        /* Transfer the biggest amount that fits both */
2382 1
        count = (src_count < dst_count) ? src_count : dst_count;
2383 1
        if (stransfer(
2384
                dst_data, dst_stride, src_data, src_stride,
2385
                count, src_itemsize, transferdata) < 0) {
2386
            res = -1;
2387
            break;
2388
        }
2389

2390
        /* If we exhausted the dst block, refresh it */
2391 1
        if (dst_count == count) {
2392 1
            res = dst_iternext(dst_iter);
2393 1
            if (!res) {
2394
                break;
2395
            }
2396 0
            dst_count = *dst_countptr;
2397 0
            dst_data = dst_dataptr[0];
2398
        }
2399
        else {
2400 1
            dst_count -= count;
2401 1
            dst_data += count*dst_stride;
2402
        }
2403

2404
        /* If we exhausted the src block, refresh it */
2405 1
        if (src_count == count) {
2406 1
            res = src_iternext(src_iter);
2407 1
            if (!res) {
2408
                break;
2409
            }
2410 1
            src_count = *src_countptr;
2411 1
            src_data = src_dataptr[0];
2412
        }
2413
        else {
2414 0
            src_count -= count;
2415 0
            src_data += count*src_stride;
2416
        }
2417
    }
2418

2419 1
    NPY_END_THREADS;
2420

2421 1
    NPY_AUXDATA_FREE(transferdata);
2422 1
    NpyIter_Deallocate(dst_iter);
2423 1
    NpyIter_Deallocate(src_iter);
2424 1
    if (res > 0) {
2425
        /* The iteration stopped successfully, do not report an error */
2426
        return 0;
2427
    }
2428 1
    return res;
2429
}
2430

2431
/*NUMPY_API
2432
 * Copy an Array into another array -- memory must not overlap
2433
 * Does not require src and dest to have "broadcastable" shapes
2434
 * (only the same number of elements).
2435
 *
2436
 * TODO: For NumPy 2.0, this could accept an order parameter which
2437
 *       only allows NPY_CORDER and NPY_FORDER.  Could also rename
2438
 *       this to CopyAsFlat to make the name more intuitive.
2439
 *
2440
 * Returns 0 on success, -1 on error.
2441
 */
2442
NPY_NO_EXPORT int
2443 1
PyArray_CopyAnyInto(PyArrayObject *dst, PyArrayObject *src)
2444
{
2445 1
    return PyArray_CopyAsFlat(dst, src, NPY_CORDER);
2446
}
2447

2448
/*NUMPY_API
2449
 * Copy an Array into another array.
2450
 * Broadcast to the destination shape if necessary.
2451
 *
2452
 * Returns 0 on success, -1 on failure.
2453
 */
2454
NPY_NO_EXPORT int
2455 1
PyArray_CopyInto(PyArrayObject *dst, PyArrayObject *src)
2456
{
2457 1
    return PyArray_AssignArray(dst, src, NULL, NPY_UNSAFE_CASTING);
2458
}
2459

2460
/*NUMPY_API
2461
 * Move the memory of one array into another, allowing for overlapping data.
2462
 *
2463
 * Returns 0 on success, negative on failure.
2464
 */
2465
NPY_NO_EXPORT int
2466 1
PyArray_MoveInto(PyArrayObject *dst, PyArrayObject *src)
2467
{
2468 1
    return PyArray_AssignArray(dst, src, NULL, NPY_UNSAFE_CASTING);
2469
}
2470

2471
/*NUMPY_API
2472
 * PyArray_CheckAxis
2473
 *
2474
 * check that axis is valid
2475
 * convert 0-d arrays to 1-d arrays
2476
 */
2477
NPY_NO_EXPORT PyObject *
2478 1
PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
2479
{
2480
    PyObject *temp1, *temp2;
2481 1
    int n = PyArray_NDIM(arr);
2482

2483 1
    if (*axis == NPY_MAXDIMS || n == 0) {
2484 1
        if (n != 1) {
2485 1
            temp1 = PyArray_Ravel(arr,0);
2486 1
            if (temp1 == NULL) {
2487 0
                *axis = 0;
2488 0
                return NULL;
2489
            }
2490 1
            if (*axis == NPY_MAXDIMS) {
2491 1
                *axis = PyArray_NDIM((PyArrayObject *)temp1)-1;
2492
            }
2493
        }
2494
        else {
2495 1
            temp1 = (PyObject *)arr;
2496 1
            Py_INCREF(temp1);
2497 1
            *axis = 0;
2498
        }
2499 1
        if (!flags && *axis == 0) {
2500
            return temp1;
2501
        }
2502
    }
2503
    else {
2504 1
        temp1 = (PyObject *)arr;
2505 1
        Py_INCREF(temp1);
2506
    }
2507 1
    if (flags) {
2508 1
        temp2 = PyArray_CheckFromAny((PyObject *)temp1, NULL,
2509
                                     0, 0, flags, NULL);
2510 1
        Py_DECREF(temp1);
2511 1
        if (temp2 == NULL) {
2512
            return NULL;
2513
        }
2514
    }
2515
    else {
2516
        temp2 = (PyObject *)temp1;
2517
    }
2518 1
    n = PyArray_NDIM((PyArrayObject *)temp2);
2519 1
    if (check_and_adjust_axis(axis, n) < 0) {
2520 1
        Py_DECREF(temp2);
2521
        return NULL;
2522
    }
2523
    return temp2;
2524
}
2525

2526
/*NUMPY_API
2527
 * Zeros
2528
 *
2529
 * steals a reference to type. On failure or when dtype->subarray is
2530
 * true, dtype will be decrefed.
2531
 * accepts NULL type
2532
 */
2533
NPY_NO_EXPORT PyObject *
2534 1
PyArray_Zeros(int nd, npy_intp const *dims, PyArray_Descr *type, int is_f_order)
2535
{
2536
    PyArrayObject *ret;
2537

2538 1
    if (!type) {
2539 1
        type = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
2540
    }
2541

2542 1
    ret = (PyArrayObject *)PyArray_NewFromDescr_int(
2543
            &PyArray_Type, type,
2544
            nd, dims, NULL, NULL,
2545
            is_f_order, NULL, NULL,
2546
            1, 0);
2547

2548 1
    if (ret == NULL) {
2549
        return NULL;
2550
    }
2551

2552
    /* handle objects */
2553 1
    if (PyDataType_REFCHK(PyArray_DESCR(ret))) {
2554 1
        if (_zerofill(ret) < 0) {
2555 0
            Py_DECREF(ret);
2556
            return NULL;
2557
        }
2558
    }
2559

2560

2561
    return (PyObject *)ret;
2562

2563
}
2564

2565
/*NUMPY_API
2566
 * Empty
2567
 *
2568
 * accepts NULL type
2569
 * steals a reference to type
2570
 */
2571
NPY_NO_EXPORT PyObject *
2572 1
PyArray_Empty(int nd, npy_intp const *dims, PyArray_Descr *type, int is_f_order)
2573
{
2574
    PyArrayObject *ret;
2575

2576 1
    if (!type) type = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
2577

2578
    /*
2579
     * PyArray_NewFromDescr steals a ref,
2580
     * but we need to look at type later.
2581
     * */
2582 1
    Py_INCREF(type);
2583

2584 1
    ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type,
2585
                                                type, nd, dims,
2586
                                                NULL, NULL,
2587
                                                is_f_order, NULL);
2588 1
    if (ret != NULL && PyDataType_REFCHK(type)) {
2589 1
        PyArray_FillObjectArray(ret, Py_None);
2590 1
        if (PyErr_Occurred()) {
2591 0
            Py_DECREF(ret);
2592 0
            Py_DECREF(type);
2593
            return NULL;
2594
        }
2595
    }
2596

2597 1
    Py_DECREF(type);
2598
    return (PyObject *)ret;
2599
}
2600

2601
/*
2602
 * Like ceil(value), but check for overflow.
2603
 *
2604
 * Return 0 on success, -1 on failure. In case of failure, set a PyExc_Overflow
2605
 * exception
2606
 */
2607
static npy_intp
2608 1
_arange_safe_ceil_to_intp(double value)
2609
{
2610
    double ivalue;
2611

2612 1
    ivalue = npy_ceil(value);
2613
    /* condition inverted to handle NaN */
2614 1
    if (npy_isnan(ivalue)) {
2615 1
        PyErr_SetString(PyExc_ValueError,
2616
            "arange: cannot compute length");
2617 1
        return -1;
2618
    }
2619 1
    if (!(NPY_MIN_INTP <= ivalue && ivalue <= NPY_MAX_INTP)) {
2620 1
        PyErr_SetString(PyExc_OverflowError,
2621
                "arange: overflow while computing length");
2622 1
        return -1;
2623
    }
2624

2625 1
    return (npy_intp)ivalue;
2626
}
2627

2628

2629
/*NUMPY_API
2630
  Arange,
2631
*/
2632
NPY_NO_EXPORT PyObject *
2633 0
PyArray_Arange(double start, double stop, double step, int type_num)
2634
{
2635
    npy_intp length;
2636
    PyArrayObject *range;
2637
    PyArray_ArrFuncs *funcs;
2638
    PyObject *obj;
2639
    int ret;
2640
    double delta, tmp_len;
2641 0
    NPY_BEGIN_THREADS_DEF;
2642

2643 0
    delta = stop - start;
2644 0
    tmp_len = delta/step;
2645

2646
    /* Underflow and divide-by-inf check */
2647 0
    if (tmp_len == 0.0 && delta != 0.0) {
2648 0
        if (npy_signbit(tmp_len)) {
2649 0
            length = 0;
2650
        }
2651
        else {
2652 0
            length = 1;
2653
        }
2654
    }
2655
    else {
2656 0
        length = _arange_safe_ceil_to_intp(tmp_len);
2657 0
        if (error_converting(length)) {
2658
            return NULL;
2659
        }
2660
    }
2661

2662 0
    if (length <= 0) {
2663 0
        length = 0;
2664 0
        return PyArray_New(&PyArray_Type, 1, &length, type_num,
2665
                           NULL, NULL, 0, 0, NULL);
2666
    }
2667 0
    range = (PyArrayObject *)PyArray_New(&PyArray_Type, 1, &length, type_num,
2668
                        NULL, NULL, 0, 0, NULL);
2669 0
    if (range == NULL) {
2670
        return NULL;
2671
    }
2672 0
    funcs = PyArray_DESCR(range)->f;
2673

2674
    /*
2675
     * place start in the buffer and the next value in the second position
2676
     * if length > 2, then call the inner loop, otherwise stop
2677
     */
2678 0
    obj = PyFloat_FromDouble(start);
2679 0
    ret = funcs->setitem(obj, PyArray_DATA(range), range);
2680 0
    Py_DECREF(obj);
2681 0
    if (ret < 0) {
2682
        goto fail;
2683
    }
2684 0
    if (length == 1) {
2685
        return (PyObject *)range;
2686
    }
2687 0
    obj = PyFloat_FromDouble(start + step);
2688 0
    ret = funcs->setitem(obj, PyArray_BYTES(range)+PyArray_ITEMSIZE(range),
2689
                         range);
2690 0
    Py_DECREF(obj);
2691 0
    if (ret < 0) {
2692
        goto fail;
2693
    }
2694 0
    if (length == 2) {
2695
        return (PyObject *)range;
2696
    }
2697 0
    if (!funcs->fill) {
2698 0
        PyErr_SetString(PyExc_ValueError,
2699
                "no fill-function for data-type.");
2700 0
        Py_DECREF(range);
2701
        return NULL;
2702
    }
2703 0
    NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(range));
2704 0
    funcs->fill(PyArray_DATA(range), length, range);
2705 0
    NPY_END_THREADS;
2706 0
    if (PyErr_Occurred()) {
2707
        goto fail;
2708
    }
2709
    return (PyObject *)range;
2710

2711 0
 fail:
2712 0
    Py_DECREF(range);
2713
    return NULL;
2714
}
2715

2716
/*
2717
 * the formula is len = (intp) ceil((stop - start) / step);
2718
 */
2719
static npy_intp
2720 1
_calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, int cmplx)
2721
{
2722
    npy_intp len, tmp;
2723
    PyObject *zero, *val;
2724
    int next_is_nonzero, val_is_zero;
2725
    double value;
2726

2727 1
    *next = PyNumber_Subtract(stop, start);
2728 1
    if (!(*next)) {
2729 0
        if (PyTuple_Check(stop)) {
2730 0
            PyErr_Clear();
2731 0
            PyErr_SetString(PyExc_TypeError,
2732
                            "arange: scalar arguments expected "\
2733
                            "instead of a tuple.");
2734
        }
2735
        return -1;
2736
    }
2737

2738 1
    zero = PyLong_FromLong(0);
2739 1
    if (!zero) {
2740 0
        Py_DECREF(*next);
2741 0
        *next = NULL;
2742 0
        return -1;
2743
    }
2744

2745 1
    next_is_nonzero = PyObject_RichCompareBool(*next, zero, Py_NE);
2746 1
    if (next_is_nonzero == -1) {
2747 0
        Py_DECREF(zero);
2748 0
        Py_DECREF(*next);
2749 0
        *next = NULL;
2750 0
        return -1;
2751
    }
2752 1
    val = PyNumber_TrueDivide(*next, step);
2753 1
    Py_DECREF(*next);
2754 1
    *next = NULL;
2755

2756 1
    if (!val) {
2757 1
        Py_DECREF(zero);
2758
        return -1;
2759
    }
2760

2761 1
    val_is_zero = PyObject_RichCompareBool(val, zero, Py_EQ);
2762 1
    Py_DECREF(zero);
2763 1
    if (val_is_zero == -1) {
2764 0
        Py_DECREF(val);
2765
        return -1;
2766
    }
2767

2768 1
    if (cmplx && PyComplex_Check(val)) {
2769 0
        value = PyComplex_RealAsDouble(val);
2770 0
        if (error_converting(value)) {
2771 0
            Py_DECREF(val);
2772
            return -1;
2773
        }
2774 0
        len = _arange_safe_ceil_to_intp(value);
2775 0
        if (error_converting(len)) {
2776 0
            Py_DECREF(val);
2777
            return -1;
2778
        }
2779 0
        value = PyComplex_ImagAsDouble(val);
2780 0
        Py_DECREF(val);
2781 0
        if (error_converting(value)) {
2782
            return -1;
2783
        }
2784 0
        tmp = _arange_safe_ceil_to_intp(value);
2785 0
        if (error_converting(tmp)) {
2786
            return -1;
2787
        }
2788 0
        len = PyArray_MIN(len, tmp);
2789
    }
2790
    else {
2791 1
        value = PyFloat_AsDouble(val);
2792 1
        Py_DECREF(val);
2793 1
        if (error_converting(value)) {
2794
            return -1;
2795
        }
2796

2797
        /* Underflow and divide-by-inf check */
2798 1
        if (val_is_zero && next_is_nonzero) {
2799 1
            if (npy_signbit(value)) {
2800
                len = 0;
2801
            }
2802
            else {
2803
                len = 1;
2804
            }
2805
        }
2806
        else {
2807 1
            len = _arange_safe_ceil_to_intp(value);
2808 1
            if (error_converting(len)) {
2809
                return -1;
2810
            }
2811
        }
2812
    }
2813

2814 1
    if (len > 0) {
2815 1
        *next = PyNumber_Add(start, step);
2816 1
        if (!*next) {
2817
            return -1;
2818
        }
2819
    }
2820
    return len;
2821
}
2822

2823
/*NUMPY_API
2824
 *
2825
 * ArangeObj,
2826
 *
2827
 * this doesn't change the references
2828
 */
2829
NPY_NO_EXPORT PyObject *
2830 1
PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr *dtype)
2831
{
2832
    PyArrayObject *range;
2833
    PyArray_ArrFuncs *funcs;
2834
    PyObject *next, *err;
2835
    npy_intp length;
2836 1
    PyArray_Descr *native = NULL;
2837
    int swap;
2838 1
    NPY_BEGIN_THREADS_DEF;
2839

2840
    /* Datetime arange is handled specially */
2841 1
    if ((dtype != NULL && (dtype->type_num == NPY_DATETIME ||
2842 1
                           dtype->type_num == NPY_TIMEDELTA)) ||
2843 1
            (dtype == NULL && (is_any_numpy_datetime_or_timedelta(start) ||
2844 1
                              is_any_numpy_datetime_or_timedelta(stop) ||
2845 1
                              is_any_numpy_datetime_or_timedelta(step)))) {
2846 1
        return (PyObject *)datetime_arange(start, stop, step, dtype);
2847
    }
2848

2849 1
    if (!dtype) {
2850
        PyArray_Descr *deftype;
2851
        PyArray_Descr *newtype;
2852

2853
        /* intentionally made to be at least NPY_LONG */
2854 1
        deftype = PyArray_DescrFromType(NPY_LONG);
2855 1
        newtype = PyArray_DescrFromObject(start, deftype);
2856 1
        Py_DECREF(deftype);
2857 1
        if (newtype == NULL) {
2858
            return NULL;
2859
        }
2860 1
        deftype = newtype;
2861 1
        if (stop && stop != Py_None) {
2862 1
            newtype = PyArray_DescrFromObject(stop, deftype);
2863 1
            Py_DECREF(deftype);
2864 1
            if (newtype == NULL) {
2865
                return NULL;
2866
            }
2867
            deftype = newtype;
2868
        }
2869 1
        if (step && step != Py_None) {
2870 1
            newtype = PyArray_DescrFromObject(step, deftype);
2871 1
            Py_DECREF(deftype);
2872 1
            if (newtype == NULL) {
2873
                return NULL;
2874
            }
2875
            deftype = newtype;
2876
        }
2877
        dtype = deftype;
2878
    }
2879
    else {
2880 1
        Py_INCREF(dtype);
2881
    }
2882 1
    if (!step || step == Py_None) {
2883 1
        step = PyLong_FromLong(1);
2884
    }
2885
    else {
2886 1
        Py_XINCREF(step);
2887
    }
2888 1
    if (!stop || stop == Py_None) {
2889 1
        stop = start;
2890 1
        start = PyLong_FromLong(0);
2891
    }
2892
    else {
2893 1
        Py_INCREF(start);
2894
    }
2895
    /* calculate the length and next = start + step*/
2896 1
    length = _calc_length(start, stop, step, &next,
2897 1
                          PyTypeNum_ISCOMPLEX(dtype->type_num));
2898 1
    err = PyErr_Occurred();
2899 1
    if (err) {
2900 1
        Py_DECREF(dtype);
2901 1
        if (err && PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
2902 1
            PyErr_SetString(PyExc_ValueError, "Maximum allowed size exceeded");
2903
        }
2904
        goto fail;
2905
    }
2906 1
    if (length <= 0) {
2907 1
        length = 0;
2908 1
        range = (PyArrayObject *)PyArray_SimpleNewFromDescr(1, &length, dtype);
2909 1
        Py_DECREF(step);
2910 1
        Py_DECREF(start);
2911
        return (PyObject *)range;
2912
    }
2913

2914
    /*
2915
     * If dtype is not in native byte-order then get native-byte
2916
     * order version.  And then swap on the way out.
2917
     */
2918 1
    if (!PyArray_ISNBO(dtype->byteorder)) {
2919 1
        native = PyArray_DescrNewByteorder(dtype, NPY_NATBYTE);
2920 1
        swap = 1;
2921
    }
2922
    else {
2923
        native = dtype;
2924
        swap = 0;
2925
    }
2926

2927 1
    range = (PyArrayObject *)PyArray_SimpleNewFromDescr(1, &length, native);
2928 1
    if (range == NULL) {
2929
        goto fail;
2930
    }
2931

2932
    /*
2933
     * place start in the buffer and the next value in the second position
2934
     * if length > 2, then call the inner loop, otherwise stop
2935
     */
2936 1
    funcs = PyArray_DESCR(range)->f;
2937 1
    if (funcs->setitem(start, PyArray_DATA(range), range) < 0) {
2938
        goto fail;
2939
    }
2940 1
    if (length == 1) {
2941
        goto finish;
2942
    }
2943 1
    if (funcs->setitem(next, PyArray_BYTES(range)+PyArray_ITEMSIZE(range),
2944
                       range) < 0) {
2945
        goto fail;
2946
    }
2947 1
    if (length == 2) {
2948
        goto finish;
2949
    }
2950 1
    if (!funcs->fill) {
2951 0
        PyErr_SetString(PyExc_ValueError, "no fill-function for data-type.");
2952 0
        Py_DECREF(range);
2953
        goto fail;
2954
    }
2955 1
    NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(range));
2956 1
    funcs->fill(PyArray_DATA(range), length, range);
2957 1
    NPY_END_THREADS;
2958 1
    if (PyErr_Occurred()) {
2959
        goto fail;
2960
    }
2961 1
 finish:
2962
    /* TODO: This swapping could be handled on the fly by the nditer */
2963 1
    if (swap) {
2964
        PyObject *new;
2965 1
        new = PyArray_Byteswap(range, 1);
2966 1
        Py_DECREF(new);
2967 1
        Py_DECREF(PyArray_DESCR(range));
2968
        /* steals the reference */
2969 1
        ((PyArrayObject_fields *)range)->descr = dtype;
2970
    }
2971 1
    Py_DECREF(start);
2972 1
    Py_DECREF(step);
2973 1
    Py_DECREF(next);
2974
    return (PyObject *)range;
2975

2976 0
 fail:
2977 1
    Py_DECREF(start);
2978 1
    Py_DECREF(step);
2979 1
    Py_XDECREF(next);
2980
    return NULL;
2981
}
2982

2983
/* This array creation function steals the reference to dtype. */
2984
static PyArrayObject *
2985 1
array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nread)
2986
{
2987
    PyArrayObject *r;
2988
    npy_off_t start, numbytes;
2989
    int elsize;
2990

2991 1
    if (num < 0) {
2992 1
        int fail = 0;
2993 1
        start = npy_ftell(fp);
2994 1
        if (start < 0) {
2995 0
            fail = 1;
2996
        }
2997 1
        if (npy_fseek(fp, 0, SEEK_END) < 0) {
2998 0
            fail = 1;
2999
        }
3000 1
        numbytes = npy_ftell(fp);
3001 1
        if (numbytes < 0) {
3002 0
            fail = 1;
3003
        }
3004 1
        numbytes -= start;
3005 1
        if (npy_fseek(fp, start, SEEK_SET) < 0) {
3006
            fail = 1;
3007
        }
3008 1
        if (fail) {
3009 0
            PyErr_SetString(PyExc_IOError,
3010
                            "could not seek in file");
3011 0
            Py_DECREF(dtype);
3012
            return NULL;
3013
        }
3014 1
        num = numbytes / dtype->elsize;
3015
    }
3016

3017
    /*
3018
     * Array creation may move sub-array dimensions from the dtype to array
3019
     * dimensions, so we need to use the original element size when reading.
3020
     */
3021 1
    elsize = dtype->elsize;
3022

3023 1
    r = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype, 1, &num,
3024
                                              NULL, NULL, 0, NULL);
3025 1
    if (r == NULL) {
3026
        return NULL;
3027
    }
3028

3029 1
    NPY_BEGIN_ALLOW_THREADS;
3030 1
    *nread = fread(PyArray_DATA(r), elsize, num, fp);
3031 1
    NPY_END_ALLOW_THREADS;
3032 1
    return r;
3033
}
3034

3035
/*
3036
 * Create an array by reading from the given stream, using the passed
3037
 * next_element and skip_separator functions.
3038
 * As typical for array creation functions, it steals the reference to dtype.
3039
 */
3040
#define FROM_BUFFER_SIZE 4096
3041
static PyArrayObject *
3042 1
array_from_text(PyArray_Descr *dtype, npy_intp num, char const *sep, size_t *nread,
3043
                void *stream, next_element next, skip_separator skip_sep,
3044
                void *stream_data)
3045
{
3046
    PyArrayObject *r;
3047
    npy_intp i;
3048
    char *dptr, *clean_sep, *tmp;
3049 1
    int err = 0;
3050 1
    int stop_reading_flag = 0;  /* -1 means end reached; -2 a parsing error */
3051 1
    npy_intp thisbuf = 0;
3052
    npy_intp size;
3053
    npy_intp bytes, totalbytes;
3054

3055 1
    size = (num >= 0) ? num : FROM_BUFFER_SIZE;
3056

3057
    /*
3058
     * Array creation may move sub-array dimensions from the dtype to array
3059
     * dimensions, so we need to use the original dtype when reading.
3060
     */
3061 1
    Py_INCREF(dtype);
3062

3063 1
    r = (PyArrayObject *)
3064
        PyArray_NewFromDescr(&PyArray_Type, dtype, 1, &size,
3065
                             NULL, NULL, 0, NULL);
3066 1
    if (r == NULL) {
3067 0
        Py_DECREF(dtype);
3068
        return NULL;
3069
    }
3070

3071 1
    clean_sep = swab_separator(sep);
3072 1
    if (clean_sep == NULL) {
3073
        err = 1;
3074
        goto fail;
3075
    }
3076

3077 1
    NPY_BEGIN_ALLOW_THREADS;
3078 1
    totalbytes = bytes = size * dtype->elsize;
3079 1
    dptr = PyArray_DATA(r);
3080 1
    for (i = 0; num < 0 || i < num; i++) {
3081 1
        stop_reading_flag = next(&stream, dptr, dtype, stream_data);
3082 1
        if (stop_reading_flag < 0) {
3083
            break;
3084
        }
3085 1
        *nread += 1;
3086 1
        thisbuf += 1;
3087 1
        dptr += dtype->elsize;
3088 1
        if (num < 0 && thisbuf == size) {
3089 0
            totalbytes += bytes;
3090 0
            tmp = PyDataMem_RENEW(PyArray_DATA(r), totalbytes);
3091 0
            if (tmp == NULL) {
3092
                err = 1;
3093
                break;
3094
            }
3095 0
            ((PyArrayObject_fields *)r)->data = tmp;
3096 0
            dptr = tmp + (totalbytes - bytes);
3097 0
            thisbuf = 0;
3098
        }
3099 1
        stop_reading_flag = skip_sep(&stream, clean_sep, stream_data);
3100 1
        if (stop_reading_flag < 0) {
3101 1
            if (num == i + 1) {
3102
                /* if we read as much as requested sep is optional */
3103 1
                stop_reading_flag = -1;
3104
            }
3105
            break;
3106
        }
3107
    }
3108 1
    if (num < 0) {
3109 1
        const size_t nsize = PyArray_MAX(*nread,1)*dtype->elsize;
3110

3111 1
        if (nsize != 0) {
3112 1
            tmp = PyDataMem_RENEW(PyArray_DATA(r), nsize);
3113 1
            if (tmp == NULL) {
3114
                err = 1;
3115
            }
3116
            else {
3117 1
                PyArray_DIMS(r)[0] = *nread;
3118 1
                ((PyArrayObject_fields *)r)->data = tmp;
3119
            }
3120
        }
3121
    }
3122 1
    NPY_END_ALLOW_THREADS;
3123

3124 1
    free(clean_sep);
3125

3126 1
    if (stop_reading_flag == -2) {
3127 1
        if (PyErr_Occurred()) {
3128
            /* If an error is already set (unlikely), do not create new one */
3129 0
            Py_DECREF(r);
3130 0
            Py_DECREF(dtype);
3131
            return NULL;
3132
        }
3133
        /* 2019-09-12, NumPy 1.18 */
3134 1
        if (DEPRECATE(
3135
                "string or file could not be read to its end due to unmatched "
3136
                "data; this will raise a ValueError in the future.") < 0) {
3137
            goto fail;
3138
        }
3139
    }
3140

3141 1
fail:
3142 1
    Py_DECREF(dtype);
3143 1
    if (err == 1) {
3144 0
        PyErr_NoMemory();
3145
    }
3146 1
    if (PyErr_Occurred()) {
3147 1
        Py_DECREF(r);
3148
        return NULL;
3149
    }
3150
    return r;
3151
}
3152
#undef FROM_BUFFER_SIZE
3153

3154
/*NUMPY_API
3155
 *
3156
 * Given a ``FILE *`` pointer ``fp``, and a ``PyArray_Descr``, return an
3157
 * array corresponding to the data encoded in that file.
3158
 *
3159
 * The reference to `dtype` is stolen (it is possible that the passed in
3160
 * dtype is not held on to).
3161
 *
3162
 * The number of elements to read is given as ``num``; if it is < 0, then
3163
 * then as many as possible are read.
3164
 *
3165
 * If ``sep`` is NULL or empty, then binary data is assumed, else
3166
 * text data, with ``sep`` as the separator between elements. Whitespace in
3167
 * the separator matches any length of whitespace in the text, and a match
3168
 * for whitespace around the separator is added.
3169
 *
3170
 * For memory-mapped files, use the buffer interface. No more data than
3171
 * necessary is read by this routine.
3172
 */