From 1ca125f2099ba7f12d6af33525160358669e4cdd Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Tue, 1 Oct 2024 16:48:45 +0200 Subject: [PATCH 1/2] Use locking='best-effort' instead of True + add some typing --- src/silx/io/h5py_utils.py | 58 +++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/silx/io/h5py_utils.py b/src/silx/io/h5py_utils.py index 84a29f965c..f472294545 100644 --- a/src/silx/io/h5py_utils.py +++ b/src/silx/io/h5py_utils.py @@ -1,5 +1,5 @@ # /*########################################################################## -# Copyright (C) 2016-2023 European Synchrotron Radiation Facility +# Copyright (C) 2016-2024 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ # THE SOFTWARE. # # ############################################################################*/ +from __future__ import annotations """ This module provides utility methods on top of h5py, mainly to handle parallel writing and reading. @@ -34,6 +35,7 @@ import sys import traceback import logging +from typing import Sequence import h5py from .._version import calc_hexversion @@ -79,7 +81,13 @@ def _libver_low_bound_is_v108(libver) -> bool: return low == "v108" -def _hdf5_file_locking(mode="r", locking=None, swmr=None, libver=None, **_): +def _hdf5_file_locking( + mode: str | None = "r", + locking: bool | str | None = None, + swmr: bool | None = None, + libver: str | Sequence[str] | None = None, + **_ + ) -> str | bool | None: """Concurrent access by disabling file locking is not supported in these cases: @@ -88,17 +96,15 @@ def _hdf5_file_locking(mode="r", locking=None, swmr=None, libver=None, **_): * libver > v108 and file already locked: does not work * windows and HDF5_HAS_LOCKING_ARGUMENT and file already locked: does not work - :param str or None mode: read-only by default - :param bool or None locking: by default it is disabled for `mode='r'` - and `swmr=False` and enabled for all - other modes. - :param bool or None swmr: try both modes when `mode='r'` and `swmr=None` - :param None or str or tuple libver: - :returns bool: + :param mode: read-only by default + :param locking: by default it is disabled for `mode='r'` + and `swmr=False` and enabled for all other modes. + :param swmr: try both modes when `mode='r'` and `swmr=None` + :param libver: """ - if locking is None: - locking = bool(mode != "r" or swmr) - if not locking: + if locking is None and mode == "r" and not swmr: + locking = False + if locking in (False, "false"): if mode != "r": raise ValueError("Locking is mandatory for HDF5 writing") if swmr: @@ -341,25 +347,25 @@ class File(h5py.File): def __init__( self, - filename, - mode=None, - locking=None, - enable_file_locking=None, - swmr=None, - libver=None, + filename: str, + mode: str | None = None, + locking: bool | str | None = None, + enable_file_locking: bool | None = None, + swmr: bool | None = None, + libver: str | Sequence[str] | None = None, **kwargs, ): r"""The arguments `locking` and `swmr` should not be specified explicitly for normal use cases. - :param str filename: - :param str or None mode: read-only by default - :param bool or None locking: by default it is disabled for `mode='r'` - and `swmr=False` and enabled for all - other modes. - :param bool or None enable_file_locking: deprecated - :param bool or None swmr: try both modes when `mode='r'` and `swmr=None` - :param None or str or tuple libver: + :param filename: + :param mode: read-only by default + :param locking: by default it is disabled for `mode='r'` + and `swmr=False` and enabled for all + other modes. + :param enable_file_locking: deprecated + :param swmr: try both modes when `mode='r'` and `swmr=None` + :param libver: :param \**kwargs: see `h5py.File.__init__` """ # File locking behavior has changed in recent versions of libhdf5 From 254ede793c852cdf5544c83b429e97c72a5352b0 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Tue, 8 Oct 2024 09:13:01 +0200 Subject: [PATCH 2/2] Apply suggestions from code review --- src/silx/io/h5py_utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/silx/io/h5py_utils.py b/src/silx/io/h5py_utils.py index f472294545..b6f937a16b 100644 --- a/src/silx/io/h5py_utils.py +++ b/src/silx/io/h5py_utils.py @@ -98,7 +98,8 @@ def _hdf5_file_locking( :param mode: read-only by default :param locking: by default it is disabled for `mode='r'` - and `swmr=False` and enabled for all other modes. + and `swmr=False` and enabled when supported + for all other modes. :param swmr: try both modes when `mode='r'` and `swmr=None` :param libver: """ @@ -361,8 +362,8 @@ def __init__( :param filename: :param mode: read-only by default :param locking: by default it is disabled for `mode='r'` - and `swmr=False` and enabled for all - other modes. + and `swmr=False` and enabled when supported + for all other modes. :param enable_file_locking: deprecated :param swmr: try both modes when `mode='r'` and `swmr=None` :param libver: