Skip to content

bpo-42251: Add gettrace and getprofile to threading #23125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ This module defines the following functions:
:meth:`~Thread.run` method is called.


.. function:: gettrace()

.. index::
single: trace function
single: debugger

Get the trace function as set by :func:`settrace`.

.. versionadded:: 3.10


.. function:: setprofile(func)

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


.. function:: getprofile()

.. index:: single: profile function

Get the profiler function as set by :func:`setprofile`.

.. versionadded:: 3.10


.. function:: stack_size([size])

Return the thread stack size used when creating new threads. The optional
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ Add :data:`sys.orig_argv` attribute: the list of the original command line
arguments passed to the Python executable.
(Contributed by Victor Stinner in :issue:`23427`.)

threading
---------

Added :func:`threading.gettrace` and :func:`threading.getprofile` to
retrieve the functions set by :func:`threading.settrace` and
:func:`threading.setprofile` respectively.
(Contributed by Mario Corchero in :issue:`42251`.)

types
-----

Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,27 @@ def callback():
finally:
sys.settrace(old_trace)

def test_gettrace(self):
def noop_trace(frame, event, arg):
# no operation
return noop_trace
old_trace = threading.gettrace()
try:
threading.settrace(noop_trace)
trace_func = threading.gettrace()
self.assertEqual(noop_trace,trace_func)
finally:
threading.settrace(old_trace)

def test_getprofile(self):
def fn(*args): pass
old_profile = threading.getprofile()
try:
threading.setprofile(fn)
self.assertEqual(fn, threading.getprofile())
finally:
threading.setprofile(old_profile)

@cpython_only
def test_shutdown_locks(self):
for daemon in (False, True):
Expand Down
10 changes: 9 additions & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
'setprofile', 'settrace', 'local', 'stack_size',
'excepthook', 'ExceptHookArgs']
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile']

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

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

def settrace(func):
"""Set a trace function for all threads started from the threading module.

Expand All @@ -75,6 +79,10 @@ def settrace(func):
global _trace_hook
_trace_hook = func

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

# Synchronization classes

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