diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_dummy_thread.py | 8 | ||||
-rw-r--r-- | Lib/test/lock_tests.py | 8 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_locks.py | 3 | ||||
-rw-r--r-- | Lib/threading.py | 10 |
4 files changed, 26 insertions, 3 deletions
diff --git a/Lib/_dummy_thread.py b/Lib/_dummy_thread.py index b67cfb9..36e5f38 100644 --- a/Lib/_dummy_thread.py +++ b/Lib/_dummy_thread.py @@ -140,6 +140,14 @@ class LockType(object): def locked(self): return self.locked_status + def __repr__(self): + return "<%s %s.%s object at %s>" % ( + "locked" if self.locked_status else "unlocked", + self.__class__.__module__, + self.__class__.__qualname__, + hex(id(self)) + ) + # Used to signal that interrupt_main was called in a "thread" _interrupt = False # True when not executing in a "thread" diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index 1cbcea2..136f1c3 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -82,7 +82,13 @@ class BaseLockTests(BaseTestCase): def test_repr(self): lock = self.locktype() - repr(lock) + self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>") + del lock + + def test_locked_repr(self): + lock = self.locktype() + lock.acquire() + self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>") del lock def test_acquire_destroy(self): diff --git a/Lib/test/test_importlib/test_locks.py b/Lib/test/test_importlib/test_locks.py index 0805054..4c01177 100644 --- a/Lib/test/test_importlib/test_locks.py +++ b/Lib/test/test_importlib/test_locks.py @@ -31,6 +31,9 @@ if threading is not None: test_timeout = None # _release_save() unsupported test_release_save_unacquired = None + # lock status in repr unsupported + test_repr = None + test_locked_repr = None LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock for kind, splitinit in init.items()} diff --git a/Lib/threading.py b/Lib/threading.py index 3407083..c7c4478 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -106,8 +106,14 @@ class _RLock: owner = _active[owner].name except KeyError: pass - return "<%s owner=%r count=%d>" % ( - self.__class__.__name__, owner, self._count) + return "<%s %s.%s object owner=%r count=%d at %s>" % ( + "locked" if self._block.locked() else "unlocked", + self.__class__.__module__, + self.__class__.__qualname__, + owner, + self._count, + hex(id(self)) + ) def acquire(self, blocking=True, timeout=-1): """Acquire a lock, blocking or non-blocking. |