Skip to content

Commit

Permalink
Fix memory policy warnings
Browse files Browse the repository at this point in the history
Fix astropy#12324 : Fix the warnings emitted by Numpy when transferring
ownership by setting ``NPY_ARRAY_OWNDATA``:

    RuntimeWarning: Trying to dealloc data, but a memory policy is not
    set.  If you take ownership of the data, you must set a base owning
    the data (e.g. a PyCapsule).

The warning was added in [1], and is now visible only when setting the
NUMPY_WARN_IF_NO_MEM_POLICY environment variable [2]. For more details
and a fix see [3].

[1] numpy/numpy#17582
[2] numpy/numpy#20200
[3] https://github.com/numpy/numpy/blob/main/doc/source/reference/c-api/data_memory.rst#what-happens-when-deallocating-if-there-is-no-policy-set
  • Loading branch information
saimn committed Nov 9, 2022
1 parent b2f33b3 commit cdff855
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
19 changes: 17 additions & 2 deletions astropy/io/fits/src/compressionmodule.c
Expand Up @@ -956,6 +956,13 @@ void open_from_hdu(fitsfile** fileptr, void** buf, size_t* bufsize,
}


/* define a PyCapsule_Destructor, using the correct deallocator for buff */
void free_wrap(void *capsule){
void * obj = PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule));
free(obj);
};


PyObject* compression_compress_hdu(PyObject* self, PyObject* args)
{
PyObject* hdu;
Expand Down Expand Up @@ -1068,8 +1075,16 @@ PyObject* compression_compress_hdu(PyObject* self, PyObject* args)
but it seems like if it fails then the outbuf NEEDS to be freed... */
goto fail;
}
PyArray_ENABLEFLAGS(tmp, NPY_ARRAY_OWNDATA);
/* From this point on outbuf MUST NOT BE FREED! */

// Take responsibility for outbuf by wrapping it in a capsule and
// setting tmp.base to the capsule
PyObject *capsule = PyCapsule_New(outbuf, "wrapped_data",
(PyCapsule_Destructor)&free_wrap);
if (PyArray_SetBaseObject(tmp, capsule) == -1) {
Py_DECREF(tmp);
/* Py_DECREF(capsule); */
goto cleanup;
}

// Leaves refcount of tmp untouched, so its refcount should remain as 1
retval = Py_BuildValue("KN", heapsize, tmp);
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -155,7 +155,7 @@ filterwarnings =
# reading option for now, can be removed once the issue is fixed
ignore:parallel reading does not currently work, so falling back to serial
# numpy configurable allocator now wants memory policy set
ignore:Trying to dealloc data, but a memory policy is not set.
# ignore:Trying to dealloc data, but a memory policy is not set.
# Ignore deprecation warning for asdf in astropy
ignore:ASDF functionality for astropy is being moved.*:astropy.utils.exceptions.AstropyDeprecationWarning:astropy.io.misc.asdf.types
ignore:ASDF functionality for astropy is being moved.*:astropy.utils.exceptions.AstropyDeprecationWarning:astropy.io.misc.asdf.tags.coordinates.frames
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -22,6 +22,7 @@ pypi_filter = https://raw.githubusercontent.com/astropy/ci-helpers/main/pip_pinn
passenv = HOME WINDIR LC_ALL LC_CTYPE CC CI IS_CRON ARCH_ON_CI

setenv =
NUMPY_WARN_IF_NO_MEM_POLICY = 1
# For coverage, we need to pass extra options to the C compiler
cov: CFLAGS = --coverage -fno-inline-functions -O0
image: MPLFLAGS = -m "mpl_image_compare" --mpl --mpl-generate-summary=html --mpl-results-path={toxinidir}/results --mpl-hash-library={toxinidir}/astropy/tests/figures/{envname}.json --mpl-baseline-path=https://raw.githubusercontent.com/astropy/astropy-figure-tests/astropy-main/figures/{envname}/ --remote-data -W ignore::DeprecationWarning
Expand Down

0 comments on commit cdff855

Please sign in to comment.