diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-09-29 10:07:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-29 10:07:58 (GMT) |
commit | eed32df5b6b989caf125d829301546db58b529dd (patch) | |
tree | f348047d4945b33cf077988167e5bf290b1eb6ea | |
parent | b6fe8572509b77d2002eaddf99d718e9b4835684 (diff) | |
download | cpython-eed32df5b6b989caf125d829301546db58b529dd.zip cpython-eed32df5b6b989caf125d829301546db58b529dd.tar.gz cpython-eed32df5b6b989caf125d829301546db58b529dd.tar.bz2 |
bpo-24391: Better reprs for threading objects. (GH-20534)
Add reprs for Semaphore, BoundedSemaphore, Event, and Barrier.
-rw-r--r-- | Lib/test/lock_tests.py | 36 | ||||
-rw-r--r-- | Lib/threading.py | 22 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-05-30-10-48-04.bpo-24391.ZCTnhX.rst | 3 |
3 files changed, 61 insertions, 0 deletions
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index dffb7d4..d826293 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -455,6 +455,12 @@ class EventTests(BaseTestCase): with evt._cond: self.assertFalse(evt._cond.acquire(False)) + def test_repr(self): + evt = self.eventtype() + self.assertRegex(repr(evt), r"<\w+\.Event at .*: unset>") + evt.set() + self.assertRegex(repr(evt), r"<\w+\.Event at .*: set>") + class ConditionTests(BaseTestCase): """ @@ -802,6 +808,15 @@ class SemaphoreTests(BaseSemaphoreTests): sem.acquire() sem.release() + def test_repr(self): + sem = self.semtype(3) + self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=3>") + sem.acquire() + self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=2>") + sem.release() + sem.release() + self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=4>") + class BoundedSemaphoreTests(BaseSemaphoreTests): """ @@ -816,6 +831,12 @@ class BoundedSemaphoreTests(BaseSemaphoreTests): sem.release() self.assertRaises(ValueError, sem.release) + def test_repr(self): + sem = self.semtype(3) + self.assertRegex(repr(sem), r"<\w+\.BoundedSemaphore at .*: value=3/3>") + sem.acquire() + self.assertRegex(repr(sem), r"<\w+\.BoundedSemaphore at .*: value=2/3>") + class BarrierTests(BaseTestCase): """ @@ -1008,3 +1029,18 @@ class BarrierTests(BaseTestCase): b = self.barriertype(1) b.wait() b.wait() + + def test_repr(self): + b = self.barriertype(3) + self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=0/3>") + def f(): + b.wait(3) + bunch = Bunch(f, 2) + bunch.wait_for_started() + time.sleep(0.2) + self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=2/3>") + b.wait(3) + bunch.wait_for_finished() + self.assertRegex(repr(b), r"<\w+\.Barrier at .*: waiters=0/3>") + b.abort() + self.assertRegex(repr(b), r"<\w+\.Barrier at .*: broken>") diff --git a/Lib/threading.py b/Lib/threading.py index 9b0419c..e9962d1 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -418,6 +418,11 @@ class Semaphore: self._cond = Condition(Lock()) self._value = value + def __repr__(self): + cls = self.__class__ + return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:" + f" value={self._value}>") + def acquire(self, blocking=True, timeout=None): """Acquire a semaphore, decrementing the internal counter by one. @@ -504,6 +509,11 @@ class BoundedSemaphore(Semaphore): Semaphore.__init__(self, value) self._initial_value = value + def __repr__(self): + cls = self.__class__ + return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:" + f" value={self._value}/{self._initial_value}>") + def release(self, n=1): """Release a semaphore, incrementing the internal counter by one or more. @@ -539,6 +549,11 @@ class Event: self._cond = Condition(Lock()) self._flag = False + def __repr__(self): + cls = self.__class__ + status = 'set' if self._flag else 'unset' + return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: {status}>" + def _at_fork_reinit(self): # Private method called by Thread._reset_internal_locks() self._cond._at_fork_reinit() @@ -637,6 +652,13 @@ class Barrier: self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken self._count = 0 + def __repr__(self): + cls = self.__class__ + if self.broken: + return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: broken>" + return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:" + f" waiters={self.n_waiting}/{self.parties}>") + def wait(self, timeout=None): """Wait for the barrier. diff --git a/Misc/NEWS.d/next/Library/2020-05-30-10-48-04.bpo-24391.ZCTnhX.rst b/Misc/NEWS.d/next/Library/2020-05-30-10-48-04.bpo-24391.ZCTnhX.rst new file mode 100644 index 0000000..15add15 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-30-10-48-04.bpo-24391.ZCTnhX.rst @@ -0,0 +1,3 @@ +Improved reprs of :mod:`threading` synchronization objects: +:class:`~threading.Semaphore`, :class:`~threading.BoundedSemaphore`, +:class:`~threading.Event` and :class:`~threading.Barrier`. |