From 61362ecdae746722147590994eeb2b602135de68 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 25 Nov 2024 06:31:59 +0100 Subject: [PATCH] Get aiorwlock.__version__ from package metadata (#437) --- README.rst | 3 +++ aiorwlock/__init__.py | 18 ++++++++++++++---- tests/test_version.py | 6 ++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/test_version.py diff --git a/README.rst b/README.rst index 682dafd..9ac312a 100644 --- a/README.rst +++ b/README.rst @@ -24,6 +24,9 @@ frequently searched is an ideal candidate for the use of a read-write lock. However, if updates become frequent then the data spends most of its time being exclusively locked and there is little, if any increase in concurrency. +**Note:** a task that *acquires* the lock should be used for *releasing* it. +Locking from one task and releasing from another one generates ``RuntimeError``. + Implementation is almost direct port from this patch_. diff --git a/aiorwlock/__init__.py b/aiorwlock/__init__.py index 8b1164f..075ef03 100644 --- a/aiorwlock/__init__.py +++ b/aiorwlock/__init__.py @@ -3,8 +3,18 @@ from collections import deque from typing import Any, Deque, List, Tuple -__version__ = "1.4.0" -__all__ = ('RWLock',) +__all__ = ('RWLock', '__version__') + + +def __getattr__(name: str) -> object: + global __version__ + + if name == "__version__": + from importlib.metadata import version + __version__ = version("aiorwlock") + return __version__ + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") # implementation based on: @@ -145,8 +155,8 @@ def _release(self, lock_type: int) -> None: try: self._owning.remove((me, lock_type)) - except ValueError: - raise RuntimeError('Cannot release an un-acquired lock') + except ValueError as exc: + raise RuntimeError('Cannot release an un-acquired lock') from exc if lock_type == self._RL: self._r_state -= 1 else: diff --git a/tests/test_version.py b/tests/test_version.py new file mode 100644 index 0000000..cd498f3 --- /dev/null +++ b/tests/test_version.py @@ -0,0 +1,6 @@ +import aiorwlock + + +def test_version() -> None: + ver = aiorwlock.__version__ + assert isinstance(ver, str)