From ed3fa64e0c31c0802b00b1f69a54258ca0fbac69 Mon Sep 17 00:00:00 2001 From: "Yves.Duprat" Date: Wed, 16 Apr 2025 11:29:23 +0200 Subject: [PATCH 1/8] Initial commit --- Lib/multiprocessing/synchronize.py | 4 +++- Lib/test/_test_multiprocessing.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index 771f1db8813852..e97c88aaf4de6a 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -91,7 +91,9 @@ def _make_methods(self): self.release = self._semlock.release def locked(self): - return self._semlock._count() != 0 + '''Return True if SemLock is acquired. + ''' + return self._semlock._is_zero() def __enter__(self): return self._semlock.__enter__() diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 1cd5704905f95c..698a04ffe46d2d 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -1492,6 +1492,24 @@ def test_lock(self): self.assertFalse(lock.locked()) self.assertRaises((ValueError, threading.ThreadError), lock.release) + @classmethod + def _test_lock_locked_2processes(cls, lock, event): + lock.acquire() + event.set() + + def test_lock_locked_2processes(self): + if self.TYPE != 'processes': + self.skipTest('test not appropriate for {}'.format(self.TYPE)) + + lock = self.Lock() + event = self.Event() + p = self.Process(target=self._test_lock_locked_2processes, + args=(lock, event)) + p.start() + event.wait() + self.assertTrue(lock.locked()) + p.join() + @staticmethod def _acquire_release(lock, timeout, l=None, n=1): for _ in range(n): @@ -1561,6 +1579,20 @@ def test_rlock(self): self.assertFalse(lock.locked()) self.assertRaises((AssertionError, RuntimeError), lock.release) + def test_rlock_locked_2processes(self): + if self.TYPE != 'processes': + self.skipTest('test not appropriate for {}'.format(self.TYPE)) + + rlock = self.RLock() + event = self.Event() + # target is the same as for the test_lock_locked_2processes test. + p = self.Process(target=self._test_lock_locked_2processes, + args=(rlock, event)) + p.start() + event.wait() + self.assertTrue(rlock.locked()) + p.join() + def test_lock_context(self): with self.Lock() as locked: self.assertTrue(locked) From bd56973c120122526f5c86da8d26b5f8a9b93579 Mon Sep 17 00:00:00 2001 From: Duprat Date: Wed, 16 Apr 2025 12:33:39 +0200 Subject: [PATCH 2/8] Suppress docstring Co-authored-by: Peter Bierma --- Lib/multiprocessing/synchronize.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index e97c88aaf4de6a..30425047e9801a 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -91,8 +91,6 @@ def _make_methods(self): self.release = self._semlock.release def locked(self): - '''Return True if SemLock is acquired. - ''' return self._semlock._is_zero() def __enter__(self): From b1bf4b836999fd0589bd4460a842abf4c73dfe17 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Apr 2025 11:45:04 +0000 Subject: [PATCH 3/8] =?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 --- .../next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst diff --git a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst new file mode 100644 index 00000000000000..59c76fa6f33f0a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst @@ -0,0 +1,2 @@ +Fix the public :meth:`locked`of ``multiprocessing.SemLock`` class. +Also adding 2 tests for the derivated :class:`multiprocessing.Lock`and :class:`multiprocessing.RLock` classes. From a604ff09fe8357b496be058255eca2d6d640c34b Mon Sep 17 00:00:00 2001 From: Duprat Date: Wed, 16 Apr 2025 13:47:50 +0200 Subject: [PATCH 4/8] Fix nits --- .../Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst index 59c76fa6f33f0a..d5d184a94bc075 100644 --- a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst +++ b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst @@ -1,2 +1,2 @@ -Fix the public :meth:`locked`of ``multiprocessing.SemLock`` class. -Also adding 2 tests for the derivated :class:`multiprocessing.Lock`and :class:`multiprocessing.RLock` classes. +Fix the public :meth:`locked` of ``multiprocessing.SemLock`` class. +Also adding 2 tests for the derivated :class:`multiprocessing.Lock` and :class:`multiprocessing.RLock` classes. From e03cd0b4dcc904c53adace35c084902bd0288810 Mon Sep 17 00:00:00 2001 From: Duprat Date: Wed, 16 Apr 2025 13:48:58 +0200 Subject: [PATCH 5/8] Fix nits From 56153025f0413246e2a41938dadf91ec0e052e99 Mon Sep 17 00:00:00 2001 From: Duprat Date: Wed, 16 Apr 2025 14:52:09 +0200 Subject: [PATCH 6/8] Fix bad docmentation reference --- .../next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst index d5d184a94bc075..6aee8a03605ea2 100644 --- a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst +++ b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst @@ -1,2 +1,2 @@ -Fix the public :meth:`locked` of ``multiprocessing.SemLock`` class. +Fix the public `locked` method of ``multiprocessing.SemLock`` class. Also adding 2 tests for the derivated :class:`multiprocessing.Lock` and :class:`multiprocessing.RLock` classes. From 5d216065bc3ae477f802c7830abb8134575390ce Mon Sep 17 00:00:00 2001 From: Duprat Date: Wed, 16 Apr 2025 14:53:25 +0200 Subject: [PATCH 7/8] Last fix --- .../next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst index 6aee8a03605ea2..862db02b819ee3 100644 --- a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst +++ b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst @@ -1,2 +1,2 @@ -Fix the public `locked` method of ``multiprocessing.SemLock`` class. +Fix the public ``locked`` method of ``multiprocessing.SemLock`` class. Also adding 2 tests for the derivated :class:`multiprocessing.Lock` and :class:`multiprocessing.RLock` classes. From 787636c93bdef70aeefc456fd2fa29728d1b9f98 Mon Sep 17 00:00:00 2001 From: "Yves.Duprat" Date: Thu, 17 Apr 2025 00:23:18 +0200 Subject: [PATCH 8/8] Add an assert on lock sta te --- Lib/test/_test_multiprocessing.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 698a04ffe46d2d..be6efc49e9489e 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -1493,8 +1493,9 @@ def test_lock(self): self.assertRaises((ValueError, threading.ThreadError), lock.release) @classmethod - def _test_lock_locked_2processes(cls, lock, event): + def _test_lock_locked_2processes(cls, lock, event, res): lock.acquire() + res.value = lock.locked() event.set() def test_lock_locked_2processes(self): @@ -1503,11 +1504,13 @@ def test_lock_locked_2processes(self): lock = self.Lock() event = self.Event() + res = self.Value('b', 0) p = self.Process(target=self._test_lock_locked_2processes, - args=(lock, event)) + args=(lock, event, res)) p.start() event.wait() self.assertTrue(lock.locked()) + self.assertTrue(res.value) p.join() @staticmethod @@ -1585,12 +1588,14 @@ def test_rlock_locked_2processes(self): rlock = self.RLock() event = self.Event() + res = Value('b', 0) # target is the same as for the test_lock_locked_2processes test. p = self.Process(target=self._test_lock_locked_2processes, - args=(rlock, event)) + args=(rlock, event, res)) p.start() event.wait() self.assertTrue(rlock.locked()) + self.assertTrue(res.value) p.join() def test_lock_context(self):