Skip to content

Commit a11158e

Browse files
bpo-44584: Deprecate PYTHONTHREADDEBUG env var (GH-27065)
The threading debug (PYTHONTHREADDEBUG environment variable) is deprecated in Python 3.10 and will be removed in Python 3.12. This feature requires a debug build of Python. (cherry picked from commit 4d77691) Co-authored-by: Victor Stinner <[email protected]>
1 parent 0a642d5 commit a11158e

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
lines changed

Doc/using/cmdline.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,12 @@ Debug-mode variables
942942

943943
.. envvar:: PYTHONTHREADDEBUG
944944

945-
If set, Python will print threading debug info.
945+
If set, Python will print threading debug info into stdout.
946946

947947
Need a :ref:`debug build of Python <debug-build>`.
948948

949+
.. deprecated-removed:: 3.10 3.12
950+
949951

950952
.. envvar:: PYTHONDUMPREFS
951953

Doc/whatsnew/3.10.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,11 @@ Deprecated
16871687
* NPN features like :meth:`ssl.SSLSocket.selected_npn_protocol` and
16881688
:meth:`ssl.SSLContext.set_npn_protocols` are replaced by ALPN.
16891689
1690+
* The threading debug (:envvar:`PYTHONTHREADDEBUG` environment variable) is
1691+
deprecated in Python 3.10 and will be removed in Python 3.12. This feature
1692+
requires a :ref:`debug build of Python <debug-build>`.
1693+
(Contributed by Victor Stinner in :issue:`44584`.)
1694+
16901695
.. _whatsnew310-removed:
16911696
16921697
Removed

Lib/test/test_threading.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
# on platforms known to behave badly.
3333
platforms_to_skip = ('netbsd5', 'hp-ux11')
3434

35+
# Is Python built with Py_DEBUG macro defined?
36+
Py_DEBUG = hasattr(sys, 'gettotalrefcount')
37+
3538

3639
def restore_default_excepthook(testcase):
3740
testcase.addCleanup(setattr, threading, 'excepthook', threading.excepthook)
@@ -915,6 +918,16 @@ def noop(): pass
915918
threading.Thread(target=noop).start()
916919
# Thread.join() is not called
917920

921+
@unittest.skipUnless(Py_DEBUG, 'need debug build (Py_DEBUG)')
922+
def test_debug_deprecation(self):
923+
# bpo-44584: The PYTHONTHREADDEBUG environment variable is deprecated
924+
rc, out, err = assert_python_ok("-Wdefault", "-c", "pass",
925+
PYTHONTHREADDEBUG="1")
926+
msg = (b'DeprecationWarning: The threading debug '
927+
b'(PYTHONTHREADDEBUG environment variable) '
928+
b'is deprecated and will be removed in Python 3.12')
929+
self.assertIn(msg, err)
930+
918931

919932
class ThreadJoinOnShutdown(BaseTestCase):
920933

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The threading debug (:envvar:`PYTHONTHREADDEBUG` environment variable) is
2+
deprecated in Python 3.10 and will be removed in Python 3.12. This feature
3+
requires a debug build of Python. Patch by Victor Stinner.

Misc/python.man

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ if Python was configured with the
550550
\fB\--with-pydebug\fP build option.
551551
.IP PYTHONTHREADDEBUG
552552
If this environment variable is set, Python will print threading debug info.
553+
The feature is deprecated in Python 3.10 and will be removed in Python 3.12.
553554
.IP PYTHONDUMPREFS
554555
If this environment variable is set, Python will dump objects and reference
555556
counts still alive after shutting down the interpreter.

Python/pylifecycle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,8 @@ pyinit_main_reconfigure(PyThreadState *tstate)
10571057
static PyStatus
10581058
init_interp_main(PyThreadState *tstate)
10591059
{
1060+
extern void _PyThread_debug_deprecation(void);
1061+
10601062
assert(!_PyErr_Occurred(tstate));
10611063

10621064
PyStatus status;
@@ -1158,6 +1160,9 @@ init_interp_main(PyThreadState *tstate)
11581160
#endif
11591161
}
11601162

1163+
// Warn about PYTHONTHREADDEBUG deprecation
1164+
_PyThread_debug_deprecation();
1165+
11611166
assert(!_PyErr_Occurred(tstate));
11621167

11631168
return _PyStatus_OK();

Python/thread.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ PyThread_init_thread(void)
7575
PyThread__init_thread();
7676
}
7777

78+
void
79+
_PyThread_debug_deprecation(void)
80+
{
81+
#ifdef Py_DEBUG
82+
if (thread_debug) {
83+
// Flush previous dprintf() logs
84+
fflush(stdout);
85+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
86+
"The threading debug (PYTHONTHREADDEBUG environment "
87+
"variable) is deprecated and will be removed "
88+
"in Python 3.12",
89+
0))
90+
{
91+
_PyErr_WriteUnraisableMsg("at Python startup", NULL);
92+
}
93+
}
94+
#endif
95+
}
96+
7897
#if defined(_POSIX_THREADS)
7998
# define PYTHREAD_NAME "pthread"
8099
# include "thread_pthread.h"

0 commit comments

Comments
 (0)