MCPcopy Create free account
hub / github.com/numpy/numpy / npyiter_clear_buffers

Function npyiter_clear_buffers

numpy/core/src/multiarray/nditer_api.c:2609–2654  ·  view source on GitHub ↗

* This function clears any references still held by the buffers and should * only be used to discard buffers if an error occurred. * * @param iter Iterator */

Source from the content-addressed store, hash-verified

2607 * @param iter Iterator
2608 */
2609NPY_NO_EXPORT void
2610npyiter_clear_buffers(NpyIter *iter)
2611{
2612 int nop = iter->nop;
2613 NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
2614
2615 if (NBF_SIZE(bufferdata) == 0) {
2616 /* if the buffers are empty already, there is nothing to do */
2617 return;
2618 }
2619
2620 /*
2621 * The iterator may be using a dtype with references (as of writing this
2622 * means Python objects, but does not need to stay that way).
2623 * In that case, further cleanup may be necessary and we clear buffers
2624 * explicitly.
2625 */
2626 PyObject *type, *value, *traceback;
2627 PyErr_Fetch(&type, &value, &traceback);
2628
2629 /* Cleanup any buffers with references */
2630 char **buffers = NBF_BUFFERS(bufferdata);
2631
2632 NpyIter_TransferInfo *transferinfo = NBF_TRANSFERINFO(bufferdata);
2633 PyArray_Descr **dtypes = NIT_DTYPES(iter);
2634 npyiter_opitflags *op_itflags = NIT_OPITFLAGS(iter);
2635 for (int iop = 0; iop < nop; ++iop, ++buffers) {
2636 if (transferinfo[iop].clear.func == NULL ||
2637 !(op_itflags[iop]&NPY_OP_ITFLAG_USINGBUFFER)) {
2638 continue;
2639 }
2640 if (*buffers == 0) {
2641 continue;
2642 }
2643 int itemsize = dtypes[iop]->elsize;
2644 if (transferinfo[iop].clear.func(NULL,
2645 dtypes[iop], *buffers, NBF_SIZE(bufferdata), itemsize,
2646 transferinfo[iop].clear.auxdata) < 0) {
2647 /* This should never fail; if it does write it out */
2648 PyErr_WriteUnraisable(NULL);
2649 }
2650 }
2651 /* Signal that the buffers are empty */
2652 NBF_SIZE(bufferdata) = 0;
2653 PyErr_Restore(type, value, traceback);
2654}
2655
2656
2657/*

Callers 1

NpyIter_DeallocateFunction · 0.85

Calls 1

funcMethod · 0.45

Tested by

no test coverage detected