https://github.com/python/cpython/commit/b95d16a338d23e52a4aeb46809fcf8a145bd38b2
commit: b95d16a338d23e52a4aeb46809fcf8a145bd38b2
branch: 3.14
author: Hugo van Kemenade <[email protected]>
committer: gpshead <[email protected]>
date: 2025-11-30T13:06:56-08:00
summary:

[3.14] gh-133146: Add the old public `get_value` method to documentation and 
refactor code. (GH-133301) (#142110)

also uses it within the internals in a few places.
(cherry picked from commit db098a475a47b16d25c88d95dbcf0c6572c68576)

Co-authored-by: Duprat <[email protected]>

files:
M Doc/library/multiprocessing.rst
M Lib/multiprocessing/queues.py
M Lib/multiprocessing/synchronize.py

diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index 714207cb0aefcd..cbc98b256a93a4 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -1577,12 +1577,22 @@ object -- see :ref:`multiprocessing-managers`.
    A solitary difference from its close analog exists: its ``acquire`` method's
    first argument is named *block*, as is consistent with :meth:`Lock.acquire`.
 
+
+   .. method:: get_value()
+
+      Return the current value of semaphore.
+
+      Note that this may raise :exc:`NotImplementedError` on platforms like
+      macOS where ``sem_getvalue()`` is not implemented.
+
+
    .. method:: locked()
 
       Return a boolean indicating whether this object is locked right now.
 
       .. versionadded:: 3.14
 
+
 .. note::
 
    On macOS, ``sem_timedwait`` is unsupported, so calling ``acquire()`` with
diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py
index 925f043900004e..981599acf5ef26 100644
--- a/Lib/multiprocessing/queues.py
+++ b/Lib/multiprocessing/queues.py
@@ -121,7 +121,7 @@ def get(self, block=True, timeout=None):
 
     def qsize(self):
         # Raises NotImplementedError on Mac OSX because of broken 
sem_getvalue()
-        return self._maxsize - self._sem._semlock._get_value()
+        return self._maxsize - self._sem.get_value()
 
     def empty(self):
         return not self._poll()
diff --git a/Lib/multiprocessing/synchronize.py 
b/Lib/multiprocessing/synchronize.py
index 30425047e9801a..9188114ae284c7 100644
--- a/Lib/multiprocessing/synchronize.py
+++ b/Lib/multiprocessing/synchronize.py
@@ -135,11 +135,16 @@ def __init__(self, value=1, *, ctx):
         SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX, ctx=ctx)
 
     def get_value(self):
+        '''Returns current value of Semaphore.
+
+        Raises NotImplementedError on Mac OSX
+        because of broken sem_getvalue().
+        '''
         return self._semlock._get_value()
 
     def __repr__(self):
         try:
-            value = self._semlock._get_value()
+            value = self.get_value()
         except Exception:
             value = 'unknown'
         return '<%s(value=%s)>' % (self.__class__.__name__, value)
@@ -155,7 +160,7 @@ def __init__(self, value=1, *, ctx):
 
     def __repr__(self):
         try:
-            value = self._semlock._get_value()
+            value = self.get_value()
         except Exception:
             value = 'unknown'
         return '<%s(value=%s, maxvalue=%s)>' % \
@@ -247,8 +252,8 @@ def _make_methods(self):
 
     def __repr__(self):
         try:
-            num_waiters = (self._sleeping_count._semlock._get_value() -
-                           self._woken_count._semlock._get_value())
+            num_waiters = (self._sleeping_count.get_value() -
+                           self._woken_count.get_value())
         except Exception:
             num_waiters = 'unknown'
         return '<%s(%s, %s)>' % (self.__class__.__name__, self._lock, 
num_waiters)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to