mirror of
https://github.com/python/cpython.git
synced 2025-12-10 11:00:14 +00:00
bpo-24391: Better reprs for threading objects. (GH-20534)
Add reprs for Semaphore, BoundedSemaphore, Event, and Barrier.
This commit is contained in:
parent
b6fe857250
commit
eed32df5b6
3 changed files with 61 additions and 0 deletions
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue