mirror of https://github.com/python/cpython
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
|
@ -455,6 +455,12 @@ class EventTests(BaseTestCase):
|
||||||
with evt._cond:
|
with evt._cond:
|
||||||
self.assertFalse(evt._cond.acquire(False))
|
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):
|
class ConditionTests(BaseTestCase):
|
||||||
"""
|
"""
|
||||||
|
@ -802,6 +808,15 @@ class SemaphoreTests(BaseSemaphoreTests):
|
||||||
sem.acquire()
|
sem.acquire()
|
||||||
sem.release()
|
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):
|
class BoundedSemaphoreTests(BaseSemaphoreTests):
|
||||||
"""
|
"""
|
||||||
|
@ -816,6 +831,12 @@ class BoundedSemaphoreTests(BaseSemaphoreTests):
|
||||||
sem.release()
|
sem.release()
|
||||||
self.assertRaises(ValueError, 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):
|
class BarrierTests(BaseTestCase):
|
||||||
"""
|
"""
|
||||||
|
@ -1008,3 +1029,18 @@ class BarrierTests(BaseTestCase):
|
||||||
b = self.barriertype(1)
|
b = self.barriertype(1)
|
||||||
b.wait()
|
b.wait()
|
||||||
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>")
|
||||||
|
|
|
@ -418,6 +418,11 @@ class Semaphore:
|
||||||
self._cond = Condition(Lock())
|
self._cond = Condition(Lock())
|
||||||
self._value = value
|
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):
|
def acquire(self, blocking=True, timeout=None):
|
||||||
"""Acquire a semaphore, decrementing the internal counter by one.
|
"""Acquire a semaphore, decrementing the internal counter by one.
|
||||||
|
|
||||||
|
@ -504,6 +509,11 @@ class BoundedSemaphore(Semaphore):
|
||||||
Semaphore.__init__(self, value)
|
Semaphore.__init__(self, value)
|
||||||
self._initial_value = 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):
|
def release(self, n=1):
|
||||||
"""Release a semaphore, incrementing the internal counter by one or more.
|
"""Release a semaphore, incrementing the internal counter by one or more.
|
||||||
|
|
||||||
|
@ -539,6 +549,11 @@ class Event:
|
||||||
self._cond = Condition(Lock())
|
self._cond = Condition(Lock())
|
||||||
self._flag = False
|
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):
|
def _at_fork_reinit(self):
|
||||||
# Private method called by Thread._reset_internal_locks()
|
# Private method called by Thread._reset_internal_locks()
|
||||||
self._cond._at_fork_reinit()
|
self._cond._at_fork_reinit()
|
||||||
|
@ -637,6 +652,13 @@ class Barrier:
|
||||||
self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
|
self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
|
||||||
self._count = 0
|
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):
|
def wait(self, timeout=None):
|
||||||
"""Wait for the barrier.
|
"""Wait for the barrier.
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Improved reprs of :mod:`threading` synchronization objects:
|
||||||
|
:class:`~threading.Semaphore`, :class:`~threading.BoundedSemaphore`,
|
||||||
|
:class:`~threading.Event` and :class:`~threading.Barrier`.
|
Loading…
Reference in New Issue