Custom lock/rlock classes owning the protected data - #5102
Open
happz wants to merge 2 commits into
Open
Conversation
LecrisUT
approved these changes
Aug 17, 2026
LecrisUT
left a comment
Member
There was a problem hiding this comment.
Is the intent to move all of the treading locks to these and deny the plain threading.Lock usage?
Also is this part of a chain? Could use the context and see some more in-action usage to confirm typing works and all that.
Contributor
Author
Eventually, yes.
Mostly drafts, nothing presentable. But the typing works. diff --git a/tmt/utils/__init__.py b/tmt/utils/__init__.py
index 8663ea655..d8c78ab20 100644
--- a/tmt/utils/__init__.py
+++ b/tmt/utils/__init__.py
@@ -6220,7 +6220,8 @@ def is_url(url: str) -> bool:
# Handle the thread synchronization for the `catch_warnings(...)` context manager
-_catch_warning_lock = RLock()
+import tmt.utils._threading
+_catch_warnings = tmt.utils._threading.RLock(warnings.catch_warnings)
ActionType = Literal['default', 'error', 'ignore', 'always', 'module', 'once']
@@ -6246,6 +6247,7 @@ def catch_warnings_safe(
...
"""
- with _catch_warning_lock, warnings.catch_warnings():
- warnings.simplefilter(action=action, category=category)
- yield
+ with _catch_warnings as catch_warnings:
+ with catch_warnings():
+ warnings.simplefilter(action=action, category=category)
+ yield |
happz
force-pushed
the
threading-locks-own-data
branch
3 times, most recently
from
August 22, 2026 12:56
7de5ff3 to
6ad7862
Compare
lukaszachy
approved these changes
Aug 26, 2026
happz
force-pushed
the
threading-locks-own-data
branch
from
August 26, 2026 14:40
6ad7862 to
b9311ee
Compare
Compared to the well-known `threading.Lock`, the locks added here wrap
the data they protect. It is still possible to modify the data, since
Python does not have actual private attributes, but it's no longer
possible to do so by accident or by omission:
```python
# With the classical approach...
_SOME_SHARED_DATA_LOCK = threading.Lock()
SOME_SHARED_DATA = set()
# ... nothing stops me from changing the set without taking the lock:
SOME_SHARED_DATA.add('foo')
# On the other hand, with our custom locks, the set does not exist as
# a value I could modify directly...
SOME_SHARED_DATA: Lock[set[str]] = Lock(set())
# ... and it's "lent" to me only if the wrapper acquires the lock on my
# behalf:
with SOME_SHARED_DATA as the_wrapped_set:
the_wrapped_set.add('foo')
```
happz
force-pushed
the
threading-locks-own-data
branch
from
September 2, 2026 07:18
b9311ee to
ac8997d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Compared to the well-known
threading.Lock, the locks added here wrap the data they protect. It is still possible to modify the data, since Python does not have actual private attributes, but it's no longer possible to do so by accident or by omission:Pull Request Checklist