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 /Lib/threading.py | |
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.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r-- | Lib/threading.py | 22 |
1 files changed, 22 insertions, 0 deletions
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. |