From 360c2816b3f1a0523a1d1485b8b3901b20725c55 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 24 Nov 2021 17:46:26 +0530 Subject: [PATCH 1/4] bpo-25066: Added repr for multiprocessing.Event --- Lib/multiprocessing/synchronize.py | 4 ++++ Lib/test/_test_multiprocessing.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index d0be48f1fd7a8f..2cbd5538193310 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -353,6 +353,10 @@ def wait(self, timeout=None): return True return False + def __repr__(self) -> str: + cls = self.__class__ + set_status = 'set' if self.is_set() else 'unset' + return f"<{cls.__module__}.{cls.__name__} at {id(self):#x} {set_status}>" # # Barrier # diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 3bc5b8f3d79b02..0c7013ff1e74ee 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -1645,7 +1645,20 @@ def test_event(self): self.assertEqual(wait(), True) p.join() -# + def test_repr(self) -> None: + event = self.Event() + if self.TYPE in ('processes', 'threads'): + self.assertRegex(repr(event), r"<(\w+\.)+Event at .* unset>") + event.set() + self.assertRegex(repr(event), r"<(\w+\.)+Event at .* set>") + event.clear() + self.assertRegex(repr(event), r"<(\w+\.)+Event at .* unset>") + elif self.TYPE == 'manager': + self.assertRegex(repr(event), r" Date: Wed, 24 Nov 2021 12:25:43 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst b/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst new file mode 100644 index 00000000000000..29fd3c2dd9f1b8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst @@ -0,0 +1 @@ +Added repr for ``multiprocessing.Event``, patch by Kumar Aditya. \ No newline at end of file From 9214db214cd5754c22c78fa3f4eb69f1587ae43c Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Thu, 25 Nov 2021 17:36:28 +0530 Subject: [PATCH 3/4] code review --- Lib/multiprocessing/synchronize.py | 3 +-- Lib/test/_test_multiprocessing.py | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index 2cbd5538193310..42624b543601a1 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -354,9 +354,8 @@ def wait(self, timeout=None): return False def __repr__(self) -> str: - cls = self.__class__ set_status = 'set' if self.is_set() else 'unset' - return f"<{cls.__module__}.{cls.__name__} at {id(self):#x} {set_status}>" + return f"<{type(self).__qualname__} at {id(self):#x} {set_status}>" # # Barrier # diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 0c7013ff1e74ee..b2d656ab428975 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -1647,12 +1647,12 @@ def test_event(self): def test_repr(self) -> None: event = self.Event() - if self.TYPE in ('processes', 'threads'): - self.assertRegex(repr(event), r"<(\w+\.)+Event at .* unset>") + if self.TYPE == 'processes': + self.assertRegex(repr(event), r"") event.set() - self.assertRegex(repr(event), r"<(\w+\.)+Event at .* set>") + self.assertRegex(repr(event), r"") event.clear() - self.assertRegex(repr(event), r"<(\w+\.)+Event at .* unset>") + self.assertRegex(repr(event), r"") elif self.TYPE == 'manager': self.assertRegex(repr(event), r" Date: Thu, 9 Dec 2021 12:10:15 +0000 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst --- .../next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst b/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst index 29fd3c2dd9f1b8..df19d041644c28 100644 --- a/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst +++ b/Misc/NEWS.d/next/Library/2021-11-24-12-25-42.bpo-25066.YIcIkn.rst @@ -1 +1 @@ -Added repr for ``multiprocessing.Event``, patch by Kumar Aditya. \ No newline at end of file +Added a :meth:`__repr__` method to :class:`multiprocessing.Event` objects, patch by Kumar Aditya. \ No newline at end of file