Commit dd282117 authored by Baptiste Esteban's avatar Baptiste Esteban
Browse files

Check ref counting + add test

parent df600776
Pipeline #26699 passed with stage
in 21 minutes and 17 seconds
......@@ -39,12 +39,12 @@ namespace pln
pybind11::object to_numpy(mln::ndbuffer_image img)
{
auto& api = pybind11::detail::npy_api::get();
auto data = pybind11::handle();
int flags = ~pybind11::detail::npy_api::NPY_ARRAY_OWNDATA_ & pybind11::detail::npy_api::NPY_ARRAY_WRITEABLE_;
pybind11::object data = pybind11::none();
int flags = pybind11::detail::npy_api::NPY_ARRAY_OWNDATA_ | pybind11::detail::npy_api::NPY_ARRAY_WRITEABLE_;
if (img.__data())
{
data = pybind11::cast(img.__data()).inc_ref();
flags &= pybind11::detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_;
data = pybind11::reinterpret_borrow<pybind11::object>(pybind11::cast(img.__data()));
flags |= pybind11::detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_;
}
/* For the moment, restrict RGB8 image to 2D image */
......@@ -67,7 +67,7 @@ namespace pln
if (!res)
throw std::runtime_error("Unable to create the numpy array in ndimage -> array");
if (data)
api.PyArray_SetBaseObject_(res.ptr(), data.inc_ref().ptr());
api.PyArray_SetBaseObject_(res.ptr(), data.release().ptr());
return res;
}
......
......@@ -20,19 +20,23 @@ fi
export ASAN_OPTIONS="log_path=log.tmp:halt_on_error=false"
$@
return_code=$?
log_path=$(echo log.tmp*)
if [ $return_code -ne 0 ]
then
if [ "$log_path" != "log.tmp*" ]
then
rm $log_path
fi
exit 1
fi
unset ASAN_OPTIONS
log_path=$(echo log.tmp*)
if [ "$log_path" = "log.tmp*" ]
then
exit 0
fi
output_lines="$(grep "pln" $log_path)"
output_lines="$(grep "pln|mln" $log_path)"
n_lines=$(echo -n $output_lines | wc -c)
if [ $n_lines = 0 ]
then
......
......@@ -85,7 +85,23 @@ class TestNumpyImage(unittest.TestCase):
with self.assertRaises(ValueError) as context:
pln.id(img)
self.assertTrue(ERROR_MSG in str(context.exception))
def test_ref_count(self):
import gc
def change_scope():
img = pln.iota()
img[0, 0] = 1303
return pln.id(img)
img = pln.iota()
img2 = pln.id(img)
img3 = change_scope()
img[0, 0] = 1136
self.assertTrue(img[0, 0] == img2[0, 0] == 1136)
gc.collect()
self.assertTrue(img3[0, 0] == 1303)
del img
gc.collect()
self.assertTrue(img2[0, 0] == 1136)
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment