Skip to content

Commit 0cdd445

Browse files
committed
Remove indirection in threading (issue python#10968).
The public names (Thread, Condition, etc.) used to be factory functions returning instances of hidden classes (_Thread, _Condition, etc.), because (if Guido recalls correctly) this code pre-dates the ability to subclass extension types. It is now possible to inherit from Thread and other classes, without having to import the private underscored names like multiprocessing did. A doc update will follow: a patch is under discussion on the issue.
1 parent 9bce311 commit 0cdd445

3 files changed

Lines changed: 15 additions & 34 deletions

File tree

Lib/multiprocessing/dummy/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from multiprocessing import TimeoutError, cpu_count
5252
from multiprocessing.dummy.connection import Pipe
5353
from threading import Lock, RLock, Semaphore, BoundedSemaphore
54-
from threading import Event
54+
from threading import Event, Condition
5555
from queue import Queue
5656

5757
#
@@ -84,17 +84,6 @@ def exitcode(self):
8484
#
8585
#
8686

87-
class Condition(threading._Condition):
88-
# XXX
89-
if sys.version_info < (3, 0):
90-
notify_all = threading._Condition.notify_all.__func__
91-
else:
92-
notify_all = threading._Condition.notify_all
93-
94-
#
95-
#
96-
#
97-
9887
Process = DummyProcess
9988
current_process = threading.current_thread
10089
current_process()._children = weakref.WeakKeyDictionary()

Lib/threading.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ def _is_owned(self):
172172
_PyRLock = _RLock
173173

174174

175-
def Condition(*args, **kwargs):
176-
return _Condition(*args, **kwargs)
177-
178-
class _Condition(_Verbose):
175+
class Condition(_Verbose):
179176

180177
def __init__(self, lock=None, verbose=None):
181178
_Verbose.__init__(self, verbose)
@@ -308,10 +305,7 @@ def notify_all(self):
308305
notifyAll = notify_all
309306

310307

311-
def Semaphore(*args, **kwargs):
312-
return _Semaphore(*args, **kwargs)
313-
314-
class _Semaphore(_Verbose):
308+
class Semaphore(_Verbose):
315309

316310
# After Tim Peters' semaphore class, but not quite the same (no maximum)
317311

@@ -366,25 +360,19 @@ def __exit__(self, t, v, tb):
366360
self.release()
367361

368362

369-
def BoundedSemaphore(*args, **kwargs):
370-
return _BoundedSemaphore(*args, **kwargs)
371-
372-
class _BoundedSemaphore(_Semaphore):
363+
class BoundedSemaphore(Semaphore):
373364
"""Semaphore that checks that # releases is <= # acquires"""
374365
def __init__(self, value=1, verbose=None):
375-
_Semaphore.__init__(self, value, verbose)
366+
Semaphore.__init__(self, value, verbose)
376367
self._initial_value = value
377368

378369
def release(self):
379370
if self._value >= self._initial_value:
380371
raise ValueError("Semaphore released too many times")
381-
return _Semaphore.release(self)
372+
return Semaphore.release(self)
382373

383374

384-
def Event(*args, **kwargs):
385-
return _Event(*args, **kwargs)
386-
387-
class _Event(_Verbose):
375+
class Event(_Verbose):
388376

389377
# After Tim Peters' event class (without is_posted())
390378

@@ -918,10 +906,7 @@ def setName(self, name):
918906

919907
# The timer class was contributed by Itamar Shtull-Trauring
920908

921-
def Timer(*args, **kwargs):
922-
return _Timer(*args, **kwargs)
923-
924-
class _Timer(Thread):
909+
class Timer(Thread):
925910
"""Call a function after a specified number of seconds:
926911
927912
t = Timer(30.0, f, args=[], kwargs={})

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ Core and Builtins
237237
Library
238238
-------
239239

240+
- Issue #10968: Remove indirection in threading. The public names (Thread,
241+
Condition, etc.) used to be factory functions returning instances of hidden
242+
classes (_Thread, _Condition, etc.), because (if Guido recalls correctly) this
243+
code pre-dates the ability to subclass extension types. It is now possible to
244+
inherit from Thread and other classes, without having to import the private
245+
underscored names like multiprocessing did.
246+
240247
- Issue #9723: Add shlex.quote functions, to escape filenames and command
241248
lines.
242249

0 commit comments

Comments
 (0)