[Python-checkins] bpo-44584: Deprecate PYTHONTHREADDEBUG env var (GH-27065)

miss-islington webhook-mailer at python.org
Fri Aug 6 07:33:16 EDT 2021


https://github.com/python/cpython/commit/a11158ecef8cff795f7db8f4047cbd20cc9cf37e
commit: a11158ecef8cff795f7db8f4047cbd20cc9cf37e
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-08-06T04:32:37-07:00
summary:

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 4d77691172aae81bdcbb0ea75839d0e896c43781)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-08-12-18-56.bpo-44584.qKnSqV.rst
M Doc/using/cmdline.rst
M Doc/whatsnew/3.10.rst
M Lib/test/test_threading.py
M Misc/python.man
M Python/pylifecycle.c
M Python/thread.c

diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
index 25e05d413de877..f22548facd0f92 100644
--- a/Doc/using/cmdline.rst
+++ b/Doc/using/cmdline.rst
@@ -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
 
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 5af1693e90cacf..d9a90fb63f18ed 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -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
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 23fe2d3f4d32e2..c51de6f4b85537 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -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)
@@ -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):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-08-12-18-56.bpo-44584.qKnSqV.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-08-12-18-56.bpo-44584.qKnSqV.rst
new file mode 100644
index 00000000000000..4afb33b047d4be
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-08-12-18-56.bpo-44584.qKnSqV.rst	
@@ -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.
diff --git a/Misc/python.man b/Misc/python.man
index 10cb807c38a02c..45a49271d4dfe3 100644
--- a/Misc/python.man
+++ b/Misc/python.man
@@ -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.
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index d31a9c15fd1ec5..eeaf20b4617a20 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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;
@@ -1158,6 +1160,9 @@ init_interp_main(PyThreadState *tstate)
 #endif
     }
 
+    // Warn about PYTHONTHREADDEBUG deprecation
+    _PyThread_debug_deprecation();
+
     assert(!_PyErr_Occurred(tstate));
 
     return _PyStatus_OK();
diff --git a/Python/thread.c b/Python/thread.c
index a10f5728dc0ceb..dfe28b6bdb680d 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -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"



More information about the Python-checkins mailing list