Skip to content

[3.10] bpo-44584: Deprecate PYTHONTHREADDEBUG env var (GH-27065) #27624

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
Aug 6, 2021
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
4 changes: 3 additions & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -942,10 +942,12 @@ Debug-mode variables

.. envvar:: PYTHONTHREADDEBUG

If set, Python will print threading debug info.
If set, Python will print threading debug info into stdout.

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

.. deprecated-removed:: 3.10 3.12


.. envvar:: PYTHONDUMPREFS

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,11 @@ Deprecated
* NPN features like :meth:`ssl.SSLSocket.selected_npn_protocol` and
:meth:`ssl.SSLContext.set_npn_protocols` are replaced by ALPN.

* The threading debug (:envvar:`PYTHONTHREADDEBUG` environment variable) is
deprecated in Python 3.10 and will be removed in Python 3.12. This feature
requires a :ref:`debug build of Python <debug-build>`.
(Contributed by Victor Stinner in :issue:`44584`.)

.. _whatsnew310-removed:

Removed
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
# on platforms known to behave badly.
platforms_to_skip = ('netbsd5', 'hp-ux11')

# Is Python built with Py_DEBUG macro defined?
Py_DEBUG = hasattr(sys, 'gettotalrefcount')


def restore_default_excepthook(testcase):
testcase.addCleanup(setattr, threading, 'excepthook', threading.excepthook)
Expand Down Expand Up @@ -915,6 +918,16 @@ def noop(): pass
threading.Thread(target=noop).start()
# Thread.join() is not called

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


class ThreadJoinOnShutdown(BaseTestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The threading debug (:envvar:`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. Patch by Victor Stinner.
1 change: 1 addition & 0 deletions Misc/python.man
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ if Python was configured with the
\fB\--with-pydebug\fP build option.
.IP PYTHONTHREADDEBUG
If this environment variable is set, Python will print threading debug info.
The feature is deprecated in Python 3.10 and will be removed in Python 3.12.
.IP PYTHONDUMPREFS
If this environment variable is set, Python will dump objects and reference
counts still alive after shutting down the interpreter.
Expand Down
5 changes: 5 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,8 @@ pyinit_main_reconfigure(PyThreadState *tstate)
static PyStatus
init_interp_main(PyThreadState *tstate)
{
extern void _PyThread_debug_deprecation(void);

assert(!_PyErr_Occurred(tstate));

PyStatus status;
Expand Down Expand Up @@ -1158,6 +1160,9 @@ init_interp_main(PyThreadState *tstate)
#endif
}

// Warn about PYTHONTHREADDEBUG deprecation
_PyThread_debug_deprecation();

assert(!_PyErr_Occurred(tstate));

return _PyStatus_OK();
Expand Down
19 changes: 19 additions & 0 deletions Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ PyThread_init_thread(void)
PyThread__init_thread();
}

void
_PyThread_debug_deprecation(void)
{
#ifdef Py_DEBUG
if (thread_debug) {
// Flush previous dprintf() logs
fflush(stdout);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"The threading debug (PYTHONTHREADDEBUG environment "
"variable) is deprecated and will be removed "
"in Python 3.12",
0))
{
_PyErr_WriteUnraisableMsg("at Python startup", NULL);
}
}
#endif
}

#if defined(_POSIX_THREADS)
# define PYTHREAD_NAME "pthread"
# include "thread_pthread.h"
Expand Down