Skip to content

Commit 0001a1b

Browse files
authored
bpo-42251: Add gettrace and getprofile to threading (GH-23125)
This allows to retrieve the functions that were set in these two, which might differ from sys.gettrace and sys.getprofile within a thread.
1 parent db6434c commit 0001a1b

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

Doc/library/threading.rst

+20
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ This module defines the following functions:
121121
:meth:`~Thread.run` method is called.
122122

123123

124+
.. function:: gettrace()
125+
126+
.. index::
127+
single: trace function
128+
single: debugger
129+
130+
Get the trace function as set by :func:`settrace`.
131+
132+
.. versionadded:: 3.10
133+
134+
124135
.. function:: setprofile(func)
125136

126137
.. index:: single: profile function
@@ -130,6 +141,15 @@ This module defines the following functions:
130141
:meth:`~Thread.run` method is called.
131142

132143

144+
.. function:: getprofile()
145+
146+
.. index:: single: profile function
147+
148+
Get the profiler function as set by :func:`setprofile`.
149+
150+
.. versionadded:: 3.10
151+
152+
133153
.. function:: stack_size([size])
134154

135155
Return the thread stack size used when creating new threads. The optional

Doc/whatsnew/3.10.rst

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ Add :data:`sys.orig_argv` attribute: the list of the original command line
224224
arguments passed to the Python executable.
225225
(Contributed by Victor Stinner in :issue:`23427`.)
226226

227+
threading
228+
---------
229+
230+
Added :func:`threading.gettrace` and :func:`threading.getprofile` to
231+
retrieve the functions set by :func:`threading.settrace` and
232+
:func:`threading.setprofile` respectively.
233+
(Contributed by Mario Corchero in :issue:`42251`.)
234+
227235
types
228236
-----
229237

Lib/test/test_threading.py

+21
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,27 @@ def callback():
765765
finally:
766766
sys.settrace(old_trace)
767767

768+
def test_gettrace(self):
769+
def noop_trace(frame, event, arg):
770+
# no operation
771+
return noop_trace
772+
old_trace = threading.gettrace()
773+
try:
774+
threading.settrace(noop_trace)
775+
trace_func = threading.gettrace()
776+
self.assertEqual(noop_trace,trace_func)
777+
finally:
778+
threading.settrace(old_trace)
779+
780+
def test_getprofile(self):
781+
def fn(*args): pass
782+
old_profile = threading.getprofile()
783+
try:
784+
threading.setprofile(fn)
785+
self.assertEqual(fn, threading.getprofile())
786+
finally:
787+
threading.setprofile(old_profile)
788+
768789
@cpython_only
769790
def test_shutdown_locks(self):
770791
for daemon in (False, True):

Lib/threading.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
2929
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
3030
'setprofile', 'settrace', 'local', 'stack_size',
31-
'excepthook', 'ExceptHookArgs']
31+
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile']
3232

3333
# Rename some stuff so "from threading import *" is safe
3434
_start_new_thread = _thread.start_new_thread
@@ -65,6 +65,10 @@ def setprofile(func):
6565
global _profile_hook
6666
_profile_hook = func
6767

68+
def getprofile():
69+
"""Get the profiler function as set by threading.setprofile()."""
70+
return _profile_hook
71+
6872
def settrace(func):
6973
"""Set a trace function for all threads started from the threading module.
7074
@@ -75,6 +79,10 @@ def settrace(func):
7579
global _trace_hook
7680
_trace_hook = func
7781

82+
def gettrace():
83+
"""Get the trace function as set by threading.settrace()."""
84+
return _trace_hook
85+
7886
# Synchronization classes
7987

8088
Lock = _allocate_lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added :func:`threading.gettrace` and :func:`threading.getprofile` to
2+
retrieve the functions set by :func:`threading.settrace` and
3+
:func:`threading.setprofile` respectively. Patch by Mario Corchero.

0 commit comments

Comments
 (0)