NUMPY_API * Makes a copy of the iterator */
| 555 | * Makes a copy of the iterator |
| 556 | */ |
| 557 | NPY_NO_EXPORT NpyIter * |
| 558 | NpyIter_Copy(NpyIter *iter) |
| 559 | { |
| 560 | npy_uint32 itflags = NIT_ITFLAGS(iter); |
| 561 | int ndim = NIT_NDIM(iter); |
| 562 | int iop, nop = NIT_NOP(iter); |
| 563 | int out_of_memory = 0; |
| 564 | |
| 565 | npy_intp size; |
| 566 | NpyIter *newiter; |
| 567 | PyArrayObject **objects; |
| 568 | PyArray_Descr **dtypes; |
| 569 | |
| 570 | /* Allocate memory for the new iterator */ |
| 571 | size = NIT_SIZEOF_ITERATOR(itflags, ndim, nop); |
| 572 | newiter = (NpyIter*)PyObject_Malloc(size); |
| 573 | |
| 574 | /* Copy the raw values to the new iterator */ |
| 575 | memcpy(newiter, iter, size); |
| 576 | |
| 577 | /* Take ownership of references to the operands and dtypes */ |
| 578 | objects = NIT_OPERANDS(newiter); |
| 579 | dtypes = NIT_DTYPES(newiter); |
| 580 | for (iop = 0; iop < nop; ++iop) { |
| 581 | Py_INCREF(objects[iop]); |
| 582 | Py_INCREF(dtypes[iop]); |
| 583 | } |
| 584 | |
| 585 | /* Allocate buffers and make copies of the transfer data if necessary */ |
| 586 | if (itflags & NPY_ITFLAG_BUFFER) { |
| 587 | NpyIter_BufferData *bufferdata; |
| 588 | npy_intp buffersize, itemsize; |
| 589 | char **buffers; |
| 590 | |
| 591 | bufferdata = NIT_BUFFERDATA(newiter); |
| 592 | buffers = NBF_BUFFERS(bufferdata); |
| 593 | buffersize = NBF_BUFFERSIZE(bufferdata); |
| 594 | NpyIter_TransferInfo *transferinfo = NBF_TRANSFERINFO(bufferdata); |
| 595 | |
| 596 | for (iop = 0; iop < nop; ++iop) { |
| 597 | if (buffers[iop] != NULL) { |
| 598 | if (out_of_memory) { |
| 599 | buffers[iop] = NULL; |
| 600 | } |
| 601 | else { |
| 602 | itemsize = dtypes[iop]->elsize; |
| 603 | buffers[iop] = PyArray_malloc(itemsize*buffersize); |
| 604 | if (buffers[iop] == NULL) { |
| 605 | out_of_memory = 1; |
| 606 | } |
| 607 | else { |
| 608 | if (PyDataType_FLAGCHK(dtypes[iop], NPY_NEEDS_INIT)) { |
| 609 | memset(buffers[iop], '\0', itemsize*buffersize); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 |
no test coverage detected