1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
import weakref
import numpy as np
import pytest
import asdf
def test_asdffile_tree_cleared_on_close(tmp_path):
"""
closed AsdfFile instances hold private reference to tree
https://github.com/asdf-format/asdf/issues/1558
"""
fn = tmp_path / "test.asdf"
asdf.AsdfFile({"a": np.arange(1000), "b": np.arange(42)}).write_to(fn)
with asdf.open(fn, memmap=False, lazy_load=False) as af:
array_weakref = weakref.ref(af["a"])
array_ref = af["b"]
# we shouldn't be able to access the now closed file
with pytest.raises(OSError, match="Cannot access data"):
af["a"]
with pytest.raises(OSError, match="Cannot access data"):
af["b"]
# we also should not be able to resolve the weak reference
# meaning the tree should have been cleaned up
r = array_weakref()
assert r is None
assert array_ref is not None
|