Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.utils.open: do not lock the file #3939

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/silx/gui/hdf5/test/test_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from silx.gui import hdf5
from silx.gui.utils.testutils import SignalListener
from silx.io import commonh5
from silx.io import h5py_utils
import weakref

import h5py
Expand All @@ -53,7 +54,7 @@ def useH5File(request, tmpdir_factory):
tmp = tmpdir_factory.mktemp("test_hdf5")
request.cls.filename = os.path.join(tmp, "data.h5")
# create h5 data
with h5py.File(request.cls.filename, "w") as f:
with h5py_utils.File(request.cls.filename, "w") as f:
g = f.create_group("arrays")
g.create_dataset("scalar", data=10)
yield
Expand Down Expand Up @@ -86,7 +87,7 @@ def h5TempFile(self):
fd, tmp_name = tempfile.mkstemp(suffix=".h5")
os.close(fd)
# create h5 data
h5file = h5py.File(tmp_name, "w")
h5file = h5py_utils.File(tmp_name, "w")
g = h5file.create_group("arrays")
g.create_dataset("scalar", data=10)
h5file.close()
Expand Down Expand Up @@ -159,7 +160,7 @@ def testRemoveObject(self):
self.assertEqual(model.rowCount(qt.QModelIndex()), 0)

def testSynchronizeObject(self):
h5 = h5py.File(self.filename, mode="r")
h5 = h5py_utils.File(self.filename, mode="r")
model = hdf5.Hdf5TreeModel()
model.insertH5pyObject(h5)
self.assertEqual(model.rowCount(qt.QModelIndex()), 1)
Expand Down Expand Up @@ -234,7 +235,7 @@ def testNotCloseFile(self):
"""A file inserted as an h5py object is not open (then not closed)
internally."""
try:
h5File = h5py.File(self.filename, mode="r")
h5File = h5py_utils.File(self.filename, mode="r")
model = hdf5.Hdf5TreeModel()
self.assertEqual(model.rowCount(qt.QModelIndex()), 0)
model.insertH5pyObject(h5File)
Expand Down Expand Up @@ -368,7 +369,7 @@ class TestHdf5TreeModelSignals(TestCaseQt):
def setUp(self):
TestCaseQt.setUp(self)
self.model = hdf5.Hdf5TreeModel()
self.h5 = h5py.File(self.filename, mode='r')
self.h5 = h5py_utils.File(self.filename, mode='r')
self.model.insertH5pyObject(self.h5)

self.listener = SignalListener()
Expand All @@ -394,7 +395,7 @@ def waitForPendingOperations(self, model):
raise RuntimeError("Still waiting for a pending operation")

def testInsert(self):
h5 = h5py.File(self.filename, mode='r')
h5 = h5py_utils.File(self.filename, mode='r')
self.model.insertH5pyObject(h5)
self.assertEqual(self.listener.callCount(), 0)

Expand Down Expand Up @@ -572,7 +573,7 @@ def useH5Model(request, tmpdir_factory):
extH5FileName = os.path.join(tmp, "base__external.h5")
extDatFileName = os.path.join(tmp, "base__external.dat")

externalh5 = h5py.File(extH5FileName, mode="w")
externalh5 = h5py_utils.File(extH5FileName, mode="w")
externalh5["target/dataset"] = 50
externalh5["target/link"] = h5py.SoftLink("/target/dataset")
externalh5["/ext/vds0"] = [0, 1]
Expand All @@ -581,7 +582,7 @@ def useH5Model(request, tmpdir_factory):

numpy.array([0,1,10,10,2,3]).tofile(extDatFileName)

h5 = h5py.File(filename, mode="w")
h5 = h5py_utils.File(filename, mode="w")
h5["group/dataset"] = 50
h5["link/soft_link"] = h5py.SoftLink("/group/dataset")
h5["link/soft_link_to_group"] = h5py.SoftLink("/group")
Expand All @@ -604,7 +605,7 @@ def useH5Model(request, tmpdir_factory):
h5["/ext"].create_dataset("raw", shape=(2,2), dtype=int, external=external)
h5.close()

with h5py.File(filename, mode="r") as h5File:
with h5py_utils.File(filename, mode="r") as h5File:
# Create model
request.cls.model = hdf5.Hdf5TreeModel()
request.cls.model.insertH5pyObject(h5File)
Expand Down
6 changes: 2 additions & 4 deletions src/silx/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

from silx.utils.proxy import Proxy
from .url import DataUrl
from . import h5py_utils
from .._version import calc_hexversion

import h5py
Expand Down Expand Up @@ -482,10 +483,7 @@ def _open_local_file(filename):
"File '%s' can't be read as a numpy file." % filename))

if h5py.is_hdf5(filename):
try:
return h5py.File(filename, "r")
except OSError:
return h5py.File(filename, "r", libver='latest', swmr=True)
return h5py_utils.File(filename, "r")

try:
from . import fabioh5
Expand Down