diff --git a/Modules/Bridge/NumPy/include/itkPyBuffer.hxx b/Modules/Bridge/NumPy/include/itkPyBuffer.hxx index 0252850b214..69ba3ec27e0 100644 --- a/Modules/Bridge/NumPy/include/itkPyBuffer.hxx +++ b/Modules/Bridge/NumPy/include/itkPyBuffer.hxx @@ -20,6 +20,7 @@ #include "itkImportImageContainer.h" +#include // For unique_ptr. namespace itk { @@ -81,11 +82,12 @@ PyBuffer::_GetImageViewFromArray(PyObject * arr, PyObject * shape, PyObj return nullptr; } + [[maybe_unused]] const std::unique_ptr bufferScopeGuard(&pyBuffer, + &PyBuffer_Release); + const Py_ssize_t bufferLength = pyBuffer.len; void * const buffer = pyBuffer.buf; - PyBuffer_Release(&pyBuffer); - PyObject * const shapeseq = PySequence_Fast(shape, "expected sequence"); const unsigned int dimension = PySequence_Size(shape); @@ -109,7 +111,6 @@ PyBuffer::_GetImageViewFromArray(PyObject * arr, PyObject * shape, PyObj if (bufferLength < 0 || static_cast(bufferLength) != len) { PyErr_SetString(PyExc_RuntimeError, "Size mismatch of image and Buffer."); - PyBuffer_Release(&pyBuffer); SWIG_Py_DECREF(shapeseq); return nullptr; } @@ -150,7 +151,6 @@ PyBuffer::_GetImageViewFromArray(PyObject * arr, PyObject * shape, PyObj output->SetNumberOfComponentsPerPixel(numberOfComponents); SWIG_Py_DECREF(shapeseq); - PyBuffer_Release(&pyBuffer); return output; } diff --git a/Modules/Bridge/NumPy/include/itkPyVectorContainer.hxx b/Modules/Bridge/NumPy/include/itkPyVectorContainer.hxx index 5916ea1c973..51026b9addb 100644 --- a/Modules/Bridge/NumPy/include/itkPyVectorContainer.hxx +++ b/Modules/Bridge/NumPy/include/itkPyVectorContainer.hxx @@ -18,6 +18,7 @@ #ifndef itkPyVectorContainer_hxx #define itkPyVectorContainer_hxx +#include // For unique_ptr. #include namespace itk @@ -61,6 +62,9 @@ PyVectorContainer::_vector_container_from_array(Py return nullptr; } + [[maybe_unused]] const std::unique_ptr bufferScopeGuard(&pyBuffer, + &PyBuffer_Release); + const Py_ssize_t bufferLength = pyBuffer.len; const void * const buffer = pyBuffer.buf; @@ -75,7 +79,6 @@ PyVectorContainer::_vector_container_from_array(Py if (bufferLength < 0 || static_cast(bufferLength) != len) { PyErr_SetString(PyExc_RuntimeError, "Size mismatch of vector and Buffer."); - PyBuffer_Release(&pyBuffer); return nullptr; } const auto * const data = static_cast(buffer); @@ -85,7 +88,6 @@ PyVectorContainer::_vector_container_from_array(Py { output->SetElement(ii, data[ii]); } - PyBuffer_Release(&pyBuffer); return output; } diff --git a/Modules/Bridge/NumPy/include/itkPyVnl.hxx b/Modules/Bridge/NumPy/include/itkPyVnl.hxx index 23749902aa3..e80ce5a78a1 100644 --- a/Modules/Bridge/NumPy/include/itkPyVnl.hxx +++ b/Modules/Bridge/NumPy/include/itkPyVnl.hxx @@ -18,6 +18,7 @@ #ifndef itkPyVnl_hxx #define itkPyVnl_hxx +#include // For unique_ptr. #include namespace itk @@ -60,6 +61,9 @@ PyVnl::_GetVnlVectorFromArray(PyObject * arr, PyObject * shape) -> con return VectorType(); } + [[maybe_unused]] const std::unique_ptr bufferScopeGuard(&pyBuffer, + &PyBuffer_Release); + const Py_ssize_t bufferLength = pyBuffer.len; const void * const buffer = pyBuffer.buf; @@ -74,12 +78,10 @@ PyVnl::_GetVnlVectorFromArray(PyObject * arr, PyObject * shape) -> con if (bufferLength < 0 || static_cast(bufferLength) != len) { PyErr_SetString(PyExc_RuntimeError, "Size mismatch of vector and Buffer."); - PyBuffer_Release(&pyBuffer); return VectorType(); } const auto * const data = static_cast(buffer); VectorType output(data, numberOfElements); - PyBuffer_Release(&pyBuffer); return output; } @@ -127,6 +129,9 @@ PyVnl::_GetVnlMatrixFromArray(PyObject * arr, PyObject * shape) -> con return MatrixType(); } + [[maybe_unused]] const std::unique_ptr bufferScopeGuard(&pyBuffer, + &PyBuffer_Release); + const Py_ssize_t bufferLength = pyBuffer.len; const void * const buffer = pyBuffer.buf; @@ -145,13 +150,11 @@ PyVnl::_GetVnlMatrixFromArray(PyObject * arr, PyObject * shape) -> con if (bufferLength < 0 || static_cast(bufferLength) != len) { PyErr_SetString(PyExc_RuntimeError, "Size mismatch of matrix and Buffer."); - PyBuffer_Release(&pyBuffer); return MatrixType(); } const auto * const data = static_cast(buffer); MatrixType output(data, size[0], size[1]); - PyBuffer_Release(&pyBuffer); return output; }