diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst --- a/Doc/library/threading.rst +++ b/Doc/library/threading.rst @@ -283,16 +283,18 @@ since it is impossible to detect the ter the constructor. .. method:: getName() setName() Old getter/setter API for :attr:`~Thread.name`; use it directly as a property instead. + .. deprecated:: 3.6 + .. attribute:: ident The 'thread identifier' of this thread or ``None`` if the thread has not been started. This is a nonzero integer. See the :func:`_thread.get_ident()` function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited. @@ -316,16 +318,18 @@ since it is impossible to detect the ter The entire Python program exits when no alive non-daemon threads are left. .. method:: isDaemon() setDaemon() Old getter/setter API for :attr:`~Thread.daemon`; use it directly as a property instead. + .. deprecated:: 3.6 + .. impl-detail:: In CPython, due to the :term:`Global Interpreter Lock`, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -184,17 +184,20 @@ New Keywords ``async`` and ``await`` are not recommended to be used as variable, class, function or module names. Introduced by :pep:`492` in Python 3.5, they will become proper keywords in Python 3.7. Deprecated Python modules, functions and methods ------------------------------------------------ -* None yet. +* The old :meth:`~threading.Thread.isDaemon`, + :meth:`~threading.Thread.setDaemon`, :meth:`~threading.Thread.getName` and + :meth:`~threading.Thread.setName` APIs of the :class:`threading.Thread` class + are now deprecated. (Contributed by Berker Peksag in :issue:`24203`.) Deprecated functions and types of the C API ------------------------------------------- * None yet. diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -400,20 +400,32 @@ class ThreadTests(BaseTestCase): self.assertIsNone(weak_raising_cyclic_object(), msg=('%d references still around' % sys.getrefcount(weak_raising_cyclic_object()))) def test_old_threading_api(self): # Just a quick sanity check to make sure the old method names are # still present t = threading.Thread() - t.isDaemon() - t.setDaemon(True) - t.getName() - t.setName("name") + with self.assertWarnsRegex( + DeprecationWarning, 'isDaemon\(\) is deprecated' + ): + t.isDaemon() + with self.assertWarnsRegex( + DeprecationWarning, 'setDaemon\(\) is deprecated' + ): + t.setDaemon(True) + with self.assertWarnsRegex( + DeprecationWarning, 'getName\(\) is deprecated' + ): + t.getName() + with self.assertWarnsRegex( + DeprecationWarning, 'setName\(\) is deprecated' + ): + t.setName("name") t.isAlive() e = threading.Event() e.isSet() threading.activeCount() def test_repr_daemon(self): t = threading.Thread() self.assertFalse('daemon' in repr(t)) diff --git a/Lib/threading.py b/Lib/threading.py --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1,12 +1,13 @@ """Thread module emulating a subset of Java's threading model.""" import sys as _sys import _thread +import warnings as _warnings from time import monotonic as _time from traceback import format_exc as _format_exc from _weakrefset import WeakSet from itertools import islice as _islice, count as _count try: from _collections import deque as _deque except ImportError: @@ -1137,25 +1138,41 @@ class Thread: def daemon(self, daemonic): if not self._initialized: raise RuntimeError("Thread.__init__() not called") if self._started.is_set(): raise RuntimeError("cannot set daemon status of active thread") self._daemonic = daemonic def isDaemon(self): + _warnings.warn( + 'isDaemon() is deprecated. Use daemon attribute instead', + DeprecationWarning, stacklevel=2 + ) return self.daemon def setDaemon(self, daemonic): + _warnings.warn( + 'setDaemon() is deprecated. Use daemon attribute instead', + DeprecationWarning, stacklevel=2 + ) self.daemon = daemonic def getName(self): + _warnings.warn( + 'getName() is deprecated. Use name attribute instead', + DeprecationWarning, stacklevel=2 + ) return self.name def setName(self, name): + _warnings.warn( + 'setName() is deprecated. Use name attribute instead', + DeprecationWarning, stacklevel=2 + ) self.name = name # The timer class was contributed by Itamar Shtull-Trauring class Timer(Thread): """Call a function after a specified number of seconds: t = Timer(30.0, f, args=None, kwargs=None)