[Python-checkins] bpo-42251: Add gettrace and getprofile to threading (GH-23125)
pablogsal
webhook-mailer at python.org
Wed Nov 4 04:28:11 EST 2020
https://github.com/python/cpython/commit/0001a1b69ecda47b0406daa88c2943877580bcae
commit: 0001a1b69ecda47b0406daa88c2943877580bcae
branch: master
author: Mario Corchero <mcorcherojim at bloomberg.net>
committer: pablogsal <Pablogsal at gmail.com>
date: 2020-11-04T09:27:43Z
summary:
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.
files:
A Misc/NEWS.d/next/Library/2020-11-03-14-15-35.bpo-42251.6TC32V.rst
M Doc/library/threading.rst
M Doc/whatsnew/3.10.rst
M Lib/test/test_threading.py
M Lib/threading.py
diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst
index 7eb12fe116bd2..e05486f7d0849 100644
--- a/Doc/library/threading.rst
+++ b/Doc/library/threading.rst
@@ -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
@@ -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
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 60dee0c6bd165..89fc300778290 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -224,6 +224,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
-----
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 2f0f3ae0946a5..e0e5406ac26a1 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -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):
diff --git a/Lib/threading.py b/Lib/threading.py
index 06c77f70fe74f..d4fe649e4f04b 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -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
@@ -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.
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2020-11-03-14-15-35.bpo-42251.6TC32V.rst b/Misc/NEWS.d/next/Library/2020-11-03-14-15-35.bpo-42251.6TC32V.rst
new file mode 100644
index 0000000000000..7435c837a2cbe
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-03-14-15-35.bpo-42251.6TC32V.rst
@@ -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.
More information about the Python-checkins
mailing list