NUMPY_API * Deallocate an iterator. * * To correctly work when an error is in progress, we have to check * `PyErr_Occurred()`. This is necessary when buffers are not finalized * or WritebackIfCopy is used. We could avoid that check by exposing a new * function which is passed in whether or not a Python error is already set. */
| 676 | * function which is passed in whether or not a Python error is already set. |
| 677 | */ |
| 678 | NPY_NO_EXPORT int |
| 679 | NpyIter_Deallocate(NpyIter *iter) |
| 680 | { |
| 681 | int success = PyErr_Occurred() == NULL; |
| 682 | |
| 683 | npy_uint32 itflags; |
| 684 | /*int ndim = NIT_NDIM(iter);*/ |
| 685 | int iop, nop; |
| 686 | PyArray_Descr **dtype; |
| 687 | PyArrayObject **object; |
| 688 | npyiter_opitflags *op_itflags; |
| 689 | |
| 690 | if (iter == NULL) { |
| 691 | return success; |
| 692 | } |
| 693 | |
| 694 | itflags = NIT_ITFLAGS(iter); |
| 695 | nop = NIT_NOP(iter); |
| 696 | dtype = NIT_DTYPES(iter); |
| 697 | object = NIT_OPERANDS(iter); |
| 698 | op_itflags = NIT_OPITFLAGS(iter); |
| 699 | |
| 700 | /* Deallocate any buffers and buffering data */ |
| 701 | if (itflags & NPY_ITFLAG_BUFFER) { |
| 702 | /* Ensure no data is held by the buffers before they are cleared */ |
| 703 | if (success) { |
| 704 | if (npyiter_copy_from_buffers(iter) < 0) { |
| 705 | success = NPY_FAIL; |
| 706 | } |
| 707 | } |
| 708 | else { |
| 709 | npyiter_clear_buffers(iter); |
| 710 | } |
| 711 | |
| 712 | NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter); |
| 713 | char **buffers; |
| 714 | |
| 715 | /* buffers */ |
| 716 | buffers = NBF_BUFFERS(bufferdata); |
| 717 | for (iop = 0; iop < nop; ++iop, ++buffers) { |
| 718 | PyArray_free(*buffers); |
| 719 | } |
| 720 | |
| 721 | NpyIter_TransferInfo *transferinfo = NBF_TRANSFERINFO(bufferdata); |
| 722 | /* read bufferdata */ |
| 723 | for (iop = 0; iop < nop; ++iop, ++transferinfo) { |
| 724 | NPY_cast_info_xfree(&transferinfo->read); |
| 725 | NPY_cast_info_xfree(&transferinfo->write); |
| 726 | NPY_traverse_info_xfree(&transferinfo->clear); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Deallocate all the dtypes and objects that were iterated and resolve |
| 732 | * any writeback buffers created by the iterator. |
| 733 | */ |
| 734 | for (iop = 0; iop < nop; ++iop, ++dtype, ++object) { |
| 735 | if (op_itflags[iop] & NPY_OP_ITFLAG_HAS_WRITEBACK) { |
no test coverage detected