Skip to content

Commit 5472ce2

Browse files
committed
Refactor code
1 parent 6e2c56c commit 5472ce2

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

Lib/multiprocessing/synchronize.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ class _MacOSXSemaphore(SemLock):
139139
"""
140140

141141
def __init__(self, kind, value, maxvalue, *, ctx):
142-
util.debug(f"_MacOSXSemaphore:: creation of a {self.__class__.__name__}"\
143-
f"with '{value = }'")
142+
if not isinstance(self, Semaphore):
143+
raise TypeError("_MacOSXSemaphore can only be used "
144+
"as base class of Semaphore class")
145+
self._count = ctx.Value('h', value)
144146
SemLock.__init__(self, kind, value, maxvalue, ctx=ctx)
145-
self._count = ctx.Value('L', value) # May be more than 'L' ?
146147

147148
def _acquire(self, *args, **kwargs) -> bool:
148149
if self._semlock.acquire(*args, **kwargs):
149150
with self._count:
150-
util.debug(f"_MacOSXSemaphore: acquire {repr(self)}")
151151
self._count.value -= 1
152152
return True
153153
return False
@@ -156,27 +156,24 @@ def _release(self):
156156
with self._count:
157157
self._count.value += 1
158158
self._semlock.release()
159-
util.debug(f"_MacOSXSemaphore: release {repr(self)}")
160159

161160
def _release_bounded(self):
162-
if self._count.value + 1 > self._semlock.maxvalue:
163-
raise ValueError(f"Cannot exceed initial value of"\
164-
f" {self._semlock.maxvalue!a}")
165-
self._release()
161+
with self._count:
162+
if self._count.value + 1 > self._semlock.maxvalue:
163+
raise ValueError(f"Cannot exceed initial value of"\
164+
f" {self._semlock.maxvalue!a}")
165+
self._release()
166166

167167
def _get_value(self) -> int:
168168
return self._count.value
169169

170170
def _make_methods(self):
171171
super()._make_methods()
172-
util.debug("_MacOSXSemaphore: _make_methods call")
173172
self.acquire = self._acquire
174173
if isinstance(self, BoundedSemaphore):
175174
self.release = self._release_bounded
176175
elif isinstance(self, Semaphore):
177176
self.release = self._release
178-
else:
179-
raise RuntimeError("Class dedicated only to Semaphore or BoundedSemaphore OSX")
180177
self.get_value = self._get_value
181178

182179
def __setstate__(self, state):

0 commit comments

Comments
 (0)