bpo-24391: Better reprs for threading objects.
Add reprs for Semaphore, BoundedSemaphore, Event, and Barrier.
This commit is contained in:
parent
8bd216dfed
commit
9377c22974
|
@ -453,6 +453,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: unset at .*>")
|
||||
evt.set()
|
||||
self.assertRegex(repr(evt), r"<\w+\.Event: set at .*>")
|
||||
|
||||
|
||||
class ConditionTests(BaseTestCase):
|
||||
"""
|
||||
|
@ -800,6 +806,15 @@ class SemaphoreTests(BaseSemaphoreTests):
|
|||
sem.acquire()
|
||||
sem.release()
|
||||
|
||||
def test_repr(self):
|
||||
sem = self.semtype(3)
|
||||
self.assertRegex(repr(sem), r"<\w+\.Semaphore: 3 at .*>")
|
||||
sem.acquire()
|
||||
self.assertRegex(repr(sem), r"<\w+\.Semaphore: 2 at .*>")
|
||||
sem.release()
|
||||
sem.release()
|
||||
self.assertRegex(repr(sem), r"<\w+\.Semaphore: 4 at .*>")
|
||||
|
||||
|
||||
class BoundedSemaphoreTests(BaseSemaphoreTests):
|
||||
"""
|
||||
|
@ -814,6 +829,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: 3/3 at .*>")
|
||||
sem.acquire()
|
||||
self.assertRegex(repr(sem), r"<\w+\.BoundedSemaphore: 2/3 at .*>")
|
||||
|
||||
|
||||
class BarrierTests(BaseTestCase):
|
||||
"""
|
||||
|
@ -1006,3 +1027,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: 0/3 at .*>")
|
||||
def f():
|
||||
b.wait(3)
|
||||
bunch = Bunch(f, 2)
|
||||
bunch.wait_for_started()
|
||||
time.sleep(0.2)
|
||||
self.assertRegex(repr(b), r"<\w+\.Barrier: 2/3 at .*>")
|
||||
b.wait(3)
|
||||
bunch.wait_for_finished()
|
||||
self.assertRegex(repr(b), r"<\w+\.Barrier: 0/3 at .*>")
|
||||
b.abort()
|
||||
self.assertRegex(repr(b), r"<\w+\.Barrier: 0/3, broken at .*>")
|
||||
|
|
|
@ -401,6 +401,12 @@ class Semaphore:
|
|||
self._cond = Condition(Lock())
|
||||
self._value = value
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s.%s: %r at %#x>' % (
|
||||
self.__class__.__module__, self.__class__.__name__,
|
||||
self._value, id(self)
|
||||
)
|
||||
|
||||
def acquire(self, blocking=True, timeout=None):
|
||||
"""Acquire a semaphore, decrementing the internal counter by one.
|
||||
|
||||
|
@ -487,6 +493,12 @@ class BoundedSemaphore(Semaphore):
|
|||
Semaphore.__init__(self, value)
|
||||
self._initial_value = value
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s.%s: %r/%r at %#x>' % (
|
||||
self.__class__.__module__, self.__class__.__name__,
|
||||
self._value, self._initial_value, id(self)
|
||||
)
|
||||
|
||||
def release(self, n=1):
|
||||
"""Release a semaphore, incrementing the internal counter by one or more.
|
||||
|
||||
|
@ -522,6 +534,12 @@ class Event:
|
|||
self._cond = Condition(Lock())
|
||||
self._flag = False
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s.%s: %s at %#x>' % (
|
||||
self.__class__.__module__, self.__class__.__name__,
|
||||
'set' if self._flag else 'unset', id(self)
|
||||
)
|
||||
|
||||
def _at_fork_reinit(self):
|
||||
# Private method called by Thread._reset_internal_locks()
|
||||
self._cond._at_fork_reinit()
|
||||
|
@ -611,6 +629,13 @@ class Barrier:
|
|||
self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
|
||||
self._count = 0
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s.%s: %r/%r%s at %#x>' % (
|
||||
self.__class__.__module__, self.__class__.__name__,
|
||||
self.n_waiting, self.parties,
|
||||
', broken' if self.broken else '', id(self)
|
||||
)
|
||||
|
||||
def wait(self, timeout=None):
|
||||
"""Wait for the barrier.
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Improved reprs of :mod:`threading` synchronization objects.
|
Loading…
Reference in New Issue