Skip to content

Commit 6ecc7b1

Browse files
committed
Use a separate API
1 parent d37f6a3 commit 6ecc7b1

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

Doc/library/threading.rst

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,23 @@ This module defines the following functions:
138138
.. versionadded:: 3.4
139139

140140

141-
.. function:: settrace(func, *, running_threads=False)
141+
.. function:: settrace(func)
142142

143143
.. index:: single: trace function
144144

145145
Set a trace function for all threads started from the :mod:`threading` module.
146146
The *func* will be passed to :func:`sys.settrace` for each thread, before its
147147
:meth:`~Thread.run` method is called.
148148

149-
If *running_threads* is set, the tracing function will be installed in all running
150-
threads.
149+
.. function:: settrace_all_threads(func)
151150

152-
.. versionchanged:: 3.12
153-
The *running_threads* parameter was added.
151+
Set a trace function for all threads started from the :mod:`threading` module
152+
and all Python threads that are currently executing.
153+
154+
The *func* will be passed to :func:`sys.settrace` for each thread, before its
155+
:meth:`~Thread.run` method is called.
156+
157+
.. versionadded:: 3.12
154158

155159
.. function:: gettrace()
156160

@@ -163,19 +167,23 @@ This module defines the following functions:
163167
.. versionadded:: 3.10
164168

165169

166-
.. function:: setprofile(func, *, running_threads=False)
170+
.. function:: setprofile(func)
167171

168172
.. index:: single: profile function
169173

170174
Set a profile function for all threads started from the :mod:`threading` module.
171175
The *func* will be passed to :func:`sys.setprofile` for each thread, before its
172176
:meth:`~Thread.run` method is called.
173177

174-
If *running_threads* is set, the profile function will be installed in all running
175-
threads.
178+
.. function:: setprofile_all_threads(func)
179+
180+
Set a profile function for all threads started from the :mod:`threading` module
181+
and all Python threads that are currently executing.
182+
183+
The *func* will be passed to :func:`sys.setprofile` for each thread, before its
184+
:meth:`~Thread.run` method is called.
176185

177-
.. versionchanged:: 3.12
178-
The *running_threads* parameter was added.
186+
.. versionadded:: 3.12
179187

180188
.. function:: getprofile()
181189

Lib/test/test_threading.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ def callback():
853853
callback()
854854
finally:
855855
sys.settrace(old_trace)
856+
threading.settrace(old_trace)
856857

857858
def test_gettrace(self):
858859
def noop_trace(frame, event, arg):
@@ -883,14 +884,15 @@ def checker():
883884
t = threading.Thread(target=checker)
884885
t.start()
885886
first_check.wait()
886-
threading.settrace(fn, running_threads=True)
887+
threading.settrace_all_threads(fn)
887888
second_check.set()
888889
t.join()
889890
self.assertEqual(trace_funcs, [None, fn])
890891
self.assertEqual(threading.gettrace(), fn)
891892
self.assertEqual(sys.gettrace(), fn)
892893
finally:
893-
threading.settrace(old_trace, running_threads=True)
894+
threading.settrace_all_threads(old_trace)
895+
894896
self.assertEqual(threading.gettrace(), old_trace)
895897
self.assertEqual(sys.gettrace(), old_trace)
896898

@@ -920,14 +922,14 @@ def checker():
920922
t = threading.Thread(target=checker)
921923
t.start()
922924
first_check.wait()
923-
threading.setprofile(fn, running_threads=True)
925+
threading.setprofile_all_threads(fn)
924926
second_check.set()
925927
t.join()
926928
self.assertEqual(profile_funcs, [None, fn])
927929
self.assertEqual(threading.getprofile(), fn)
928930
self.assertEqual(sys.getprofile(), fn)
929931
finally:
930-
threading.setprofile(old_profile, running_threads=True)
932+
threading.setprofile_all_threads(old_profile)
931933

932934
self.assertEqual(threading.getprofile(), old_profile)
933935
self.assertEqual(sys.getprofile(), old_profile)

Lib/threading.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
2929
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
3030
'setprofile', 'settrace', 'local', 'stack_size',
31-
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile']
31+
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
32+
'setprofile_all_threads','settrace_all_threads']
3233

3334
# Rename some stuff so "from threading import *" is safe
3435
_start_new_thread = _thread.start_new_thread
@@ -55,39 +56,47 @@
5556
_profile_hook = None
5657
_trace_hook = None
5758

58-
def setprofile(func, *, running_threads=False):
59+
def setprofile(func):
5960
"""Set a profile function for all threads started from the threading module.
6061
6162
The func will be passed to sys.setprofile() for each thread, before its
6263
run() method is called.
63-
64-
If running_threads is set, the profile function will be installed in all running
65-
threads.
6664
"""
6765
global _profile_hook
6866
_profile_hook = func
6967

70-
if running_threads:
71-
_sys._setprofileallthreads(func)
68+
def setprofile_all_threads(func):
69+
"""Set a profile function for all threads started from the threading module
70+
and all Python threads that are currently executing.
71+
72+
The func will be passed to sys.setprofile() for each thread, before its
73+
run() method is called.
74+
"""
75+
setprofile(func)
76+
_sys._setprofileallthreads(func)
7277

7378
def getprofile():
7479
"""Get the profiler function as set by threading.setprofile()."""
7580
return _profile_hook
7681

77-
def settrace(func, *, running_threads=False):
82+
def settrace(func):
7883
"""Set a trace function for all threads started from the threading module.
7984
8085
The func will be passed to sys.settrace() for each thread, before its run()
8186
method is called.
82-
83-
If running_threads is set, the trace function will be installed in all running
84-
threads.
8587
"""
8688
global _trace_hook
8789
_trace_hook = func
8890

89-
if running_threads:
90-
_sys._settraceallthreads(func)
91+
def settrace_all_threads(func):
92+
"""Set a trace function for all threads started from the threading module
93+
and all Python threads that are currently executing.
94+
95+
The func will be passed to sys.settrace() for each thread, before its run()
96+
method is called.
97+
"""
98+
settrace(func)
99+
_sys._settraceallthreads(func)
91100

92101
def gettrace():
93102
"""Get the trace function as set by threading.settrace()."""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy