Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 135b6d8

Browse files
committed
Close #13550: Remove the debug machinery from the threading module: remove
verbose arguments from all threading classes and functions.
1 parent 643cd68 commit 135b6d8

2 files changed

Lines changed: 21 additions & 134 deletions

File tree

Lib/threading.py

Lines changed: 18 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,6 @@
3434
del _thread
3535

3636

37-
# Debug support (adapted from ihooks.py).
38-
39-
_VERBOSE = False
40-
41-
if __debug__:
42-
43-
class _Verbose(object):
44-
45-
def __init__(self, verbose=None):
46-
if verbose is None:
47-
verbose = _VERBOSE
48-
self._verbose = verbose
49-
50-
def _note(self, format, *args):
51-
if self._verbose:
52-
format = format % args
53-
# Issue #4188: calling current_thread() can incur an infinite
54-
# recursion if it has to create a DummyThread on the fly.
55-
ident = get_ident()
56-
try:
57-
name = _active[ident].name
58-
except KeyError:
59-
name = "<OS thread %d>" % ident
60-
format = "%s: %s\n" % (name, format)
61-
_sys.stderr.write(format)
62-
63-
else:
64-
# Disable this when using "python -O"
65-
class _Verbose(object):
66-
def __init__(self, verbose=None):
67-
pass
68-
def _note(self, *args):
69-
pass
70-
7137
# Support for profile and trace hooks
7238

7339
_profile_hook = None
@@ -85,17 +51,14 @@ def settrace(func):
8551

8652
Lock = _allocate_lock
8753

88-
def RLock(verbose=None, *args, **kwargs):
89-
if verbose is None:
90-
verbose = _VERBOSE
91-
if (__debug__ and verbose) or _CRLock is None:
92-
return _PyRLock(verbose, *args, **kwargs)
54+
def RLock(*args, **kwargs):
55+
if _CRLock is None:
56+
return _PyRLock(*args, **kwargs)
9357
return _CRLock(*args, **kwargs)
9458

95-
class _RLock(_Verbose):
59+
class _RLock:
9660

97-
def __init__(self, verbose=None):
98-
_Verbose.__init__(self, verbose)
61+
def __init__(self):
9962
self._block = _allocate_lock()
10063
self._owner = None
10164
self._count = 0
@@ -113,18 +76,11 @@ def acquire(self, blocking=True, timeout=-1):
11376
me = get_ident()
11477
if self._owner == me:
11578
self._count = self._count + 1
116-
if __debug__:
117-
self._note("%s.acquire(%s): recursive success", self, blocking)
11879
return 1
11980
rc = self._block.acquire(blocking, timeout)
12081
if rc:
12182
self._owner = me
12283
self._count = 1
123-
if __debug__:
124-
self._note("%s.acquire(%s): initial success", self, blocking)
125-
else:
126-
if __debug__:
127-
self._note("%s.acquire(%s): failure", self, blocking)
12884
return rc
12985

13086
__enter__ = acquire
@@ -136,11 +92,6 @@ def release(self):
13692
if not count:
13793
self._owner = None
13894
self._block.release()
139-
if __debug__:
140-
self._note("%s.release(): final release", self)
141-
else:
142-
if __debug__:
143-
self._note("%s.release(): non-final release", self)
14495

14596
def __exit__(self, t, v, tb):
14697
self.release()
@@ -150,12 +101,8 @@ def __exit__(self, t, v, tb):
150101
def _acquire_restore(self, state):
151102
self._block.acquire()
152103
self._count, self._owner = state
153-
if __debug__:
154-
self._note("%s._acquire_restore()", self)
155104

156105
def _release_save(self):
157-
if __debug__:
158-
self._note("%s._release_save()", self)
159106
if self._count == 0:
160107
raise RuntimeError("cannot release un-acquired lock")
161108
count = self._count
@@ -171,10 +118,9 @@ def _is_owned(self):
171118
_PyRLock = _RLock
172119

173120

174-
class Condition(_Verbose):
121+
class Condition:
175122

176-
def __init__(self, lock=None, verbose=None):
177-
_Verbose.__init__(self, verbose)
123+
def __init__(self, lock=None):
178124
if lock is None:
179125
lock = RLock()
180126
self._lock = lock
@@ -233,23 +179,16 @@ def wait(self, timeout=None):
233179
if timeout is None:
234180
waiter.acquire()
235181
gotit = True
236-
if __debug__:
237-
self._note("%s.wait(): got it", self)
238182
else:
239183
if timeout > 0:
240184
gotit = waiter.acquire(True, timeout)
241185
else:
242186
gotit = waiter.acquire(False)
243187
if not gotit:
244-
if __debug__:
245-
self._note("%s.wait(%s): timed out", self, timeout)
246188
try:
247189
self._waiters.remove(waiter)
248190
except ValueError:
249191
pass
250-
else:
251-
if __debug__:
252-
self._note("%s.wait(%s): got it", self, timeout)
253192
return gotit
254193
finally:
255194
self._acquire_restore(saved_state)
@@ -265,19 +204,9 @@ def wait_for(self, predicate, timeout=None):
265204
else:
266205
waittime = endtime - _time()
267206
if waittime <= 0:
268-
if __debug__:
269-
self._note("%s.wait_for(%r, %r): Timed out.",
270-
self, predicate, timeout)
271207
break
272-
if __debug__:
273-
self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.",
274-
self, predicate, timeout, waittime)
275208
self.wait(waittime)
276209
result = predicate()
277-
else:
278-
if __debug__:
279-
self._note("%s.wait_for(%r, %r): Success.",
280-
self, predicate, timeout)
281210
return result
282211

283212
def notify(self, n=1):
@@ -286,11 +215,7 @@ def notify(self, n=1):
286215
__waiters = self._waiters
287216
waiters = __waiters[:n]
288217
if not waiters:
289-
if __debug__:
290-
self._note("%s.notify(): no waiters", self)
291218
return
292-
self._note("%s.notify(): notifying %d waiter%s", self, n,
293-
n!=1 and "s" or "")
294219
for waiter in waiters:
295220
waiter.release()
296221
try:
@@ -304,14 +229,13 @@ def notify_all(self):
304229
notifyAll = notify_all
305230

306231

307-
class Semaphore(_Verbose):
232+
class Semaphore:
308233

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

311-
def __init__(self, value=1, verbose=None):
236+
def __init__(self, value=1):
312237
if value < 0:
313238
raise ValueError("semaphore initial value must be >= 0")
314-
_Verbose.__init__(self, verbose)
315239
self._cond = Condition(Lock())
316240
self._value = value
317241

@@ -324,9 +248,6 @@ def acquire(self, blocking=True, timeout=None):
324248
while self._value == 0:
325249
if not blocking:
326250
break
327-
if __debug__:
328-
self._note("%s.acquire(%s): blocked waiting, value=%s",
329-
self, blocking, self._value)
330251
if timeout is not None:
331252
if endtime is None:
332253
endtime = _time() + timeout
@@ -337,9 +258,6 @@ def acquire(self, blocking=True, timeout=None):
337258
self._cond.wait(timeout)
338259
else:
339260
self._value = self._value - 1
340-
if __debug__:
341-
self._note("%s.acquire: success, value=%s",
342-
self, self._value)
343261
rc = True
344262
self._cond.release()
345263
return rc
@@ -349,9 +267,6 @@ def acquire(self, blocking=True, timeout=None):
349267
def release(self):
350268
self._cond.acquire()
351269
self._value = self._value + 1
352-
if __debug__:
353-
self._note("%s.release: success, value=%s",
354-
self, self._value)
355270
self._cond.notify()
356271
self._cond.release()
357272

@@ -361,8 +276,8 @@ def __exit__(self, t, v, tb):
361276

362277
class BoundedSemaphore(Semaphore):
363278
"""Semaphore that checks that # releases is <= # acquires"""
364-
def __init__(self, value=1, verbose=None):
365-
Semaphore.__init__(self, value, verbose)
279+
def __init__(self, value=1):
280+
Semaphore.__init__(self, value)
366281
self._initial_value = value
367282

368283
def release(self):
@@ -371,12 +286,11 @@ def release(self):
371286
return Semaphore.release(self)
372287

373288

374-
class Event(_Verbose):
289+
class Event:
375290

376291
# After Tim Peters' event class (without is_posted())
377292

378-
def __init__(self, verbose=None):
379-
_Verbose.__init__(self, verbose)
293+
def __init__(self):
380294
self._cond = Condition(Lock())
381295
self._flag = False
382296

@@ -426,13 +340,13 @@ def wait(self, timeout=None):
426340
# since the previous cycle. In addition, a 'resetting' state exists which is
427341
# similar to 'draining' except that threads leave with a BrokenBarrierError,
428342
# and a 'broken' state in which all threads get the exception.
429-
class Barrier(_Verbose):
343+
class Barrier:
430344
"""
431345
Barrier. Useful for synchronizing a fixed number of threads
432346
at known synchronization points. Threads block on 'wait()' and are
433347
simultaneously once they have all made that call.
434348
"""
435-
def __init__(self, parties, action=None, timeout=None, verbose=None):
349+
def __init__(self, parties, action=None, timeout=None):
436350
"""
437351
Create a barrier, initialised to 'parties' threads.
438352
'action' is a callable which, when supplied, will be called
@@ -441,7 +355,6 @@ def __init__(self, parties, action=None, timeout=None, verbose=None):
441355
If a 'timeout' is provided, it is uses as the default for
442356
all subsequent 'wait()' calls.
443357
"""
444-
_Verbose.__init__(self, verbose)
445358
self._cond = Condition(Lock())
446359
self._action = action
447360
self._timeout = timeout
@@ -602,7 +515,7 @@ def _newname(template="Thread-%d"):
602515

603516
# Main class for threads
604517

605-
class Thread(_Verbose):
518+
class Thread:
606519

607520
__initialized = False
608521
# Need to store a reference to sys.exc_info for printing
@@ -615,9 +528,8 @@ class Thread(_Verbose):
615528
#XXX __exc_clear = _sys.exc_clear
616529

617530
def __init__(self, group=None, target=None, name=None,
618-
args=(), kwargs=None, verbose=None, *, daemon=None):
531+
args=(), kwargs=None, *, daemon=None):
619532
assert group is None, "group argument must be None for now"
620-
_Verbose.__init__(self, verbose)
621533
if kwargs is None:
622534
kwargs = {}
623535
self._target = target
@@ -664,8 +576,6 @@ def start(self):
664576

665577
if self._started.is_set():
666578
raise RuntimeError("threads can only be started once")
667-
if __debug__:
668-
self._note("%s.start(): starting thread", self)
669579
with _active_limbo_lock:
670580
_limbo[self] = self
671581
try:
@@ -715,24 +625,17 @@ def _bootstrap_inner(self):
715625
with _active_limbo_lock:
716626
_active[self._ident] = self
717627
del _limbo[self]
718-
if __debug__:
719-
self._note("%s._bootstrap(): thread started", self)
720628

721629
if _trace_hook:
722-
self._note("%s._bootstrap(): registering trace hook", self)
723630
_sys.settrace(_trace_hook)
724631
if _profile_hook:
725-
self._note("%s._bootstrap(): registering profile hook", self)
726632
_sys.setprofile(_profile_hook)
727633

728634
try:
729635
self.run()
730636
except SystemExit:
731-
if __debug__:
732-
self._note("%s._bootstrap(): raised SystemExit", self)
637+
pass
733638
except:
734-
if __debug__:
735-
self._note("%s._bootstrap(): unhandled exception", self)
736639
# If sys.stderr is no more (most likely from interpreter
737640
# shutdown) use self._stderr. Otherwise still use sys (as in
738641
# _sys) in case sys.stderr was redefined since the creation of
@@ -763,9 +666,6 @@ def _bootstrap_inner(self):
763666
# hog; deleting everything else is just for thoroughness
764667
finally:
765668
del exc_type, exc_value, exc_tb
766-
else:
767-
if __debug__:
768-
self._note("%s._bootstrap(): normal return", self)
769669
finally:
770670
# Prevent a race in
771671
# test_threading.test_no_refcycle_through_target when
@@ -832,29 +732,18 @@ def join(self, timeout=None):
832732
if self is current_thread():
833733
raise RuntimeError("cannot join current thread")
834734

835-
if __debug__:
836-
if not self._stopped:
837-
self._note("%s.join(): waiting until thread stops", self)
838-
839735
self._block.acquire()
840736
try:
841737
if timeout is None:
842738
while not self._stopped:
843739
self._block.wait()
844-
if __debug__:
845-
self._note("%s.join(): thread stopped", self)
846740
else:
847741
deadline = _time() + timeout
848742
while not self._stopped:
849743
delay = deadline - _time()
850744
if delay <= 0:
851-
if __debug__:
852-
self._note("%s.join(): timed out", self)
853745
break
854746
self._block.wait(delay)
855-
else:
856-
if __debug__:
857-
self._note("%s.join(): thread stopped", self)
858747
finally:
859748
self._block.release()
860749

@@ -947,14 +836,9 @@ def __init__(self):
947836
def _exitfunc(self):
948837
self._stop()
949838
t = _pickSomeNonDaemonThread()
950-
if t:
951-
if __debug__:
952-
self._note("%s: waiting for other threads", self)
953839
while t:
954840
t.join()
955841
t = _pickSomeNonDaemonThread()
956-
if __debug__:
957-
self._note("%s: exiting", self)
958842
self._delete()
959843

960844
def _pickSomeNonDaemonThread():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ Core and Builtins
511511
Library
512512
-------
513513

514+
- Issue #13550: Remove the debug machinery from the threading module: remove
515+
verbose arguments from all threading classes and functions.
516+
514517
- Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary,
515518
WeakValueDictionary) to return a better approximation when some objects
516519
are dead or dying. Moreover, the implementation is now O(1) rather than

0 commit comments

Comments
 (0)