Skip to content

Commit 9825bdf

Browse files
bpo-43723: Deprecate camelCase aliases from threading (GH-25174)
The snake_case names have existed since Python 2.6, so there is no reason to keep the old camelCase names around. One similar method, threading.Thread.isAlive, was already removed in Python 3.9 (bpo-37804).
1 parent cc2ffcd commit 9825bdf

File tree

7 files changed

+159
-20
lines changed

7 files changed

+159
-20
lines changed

Doc/faq/library.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,11 @@ Here's a trivial example::
319319
try:
320320
arg = q.get(block=False)
321321
except queue.Empty:
322-
print('Worker', threading.currentThread(), end=' ')
322+
print('Worker', threading.current_thread(), end=' ')
323323
print('queue empty')
324324
break
325325
else:
326-
print('Worker', threading.currentThread(), end=' ')
326+
print('Worker', threading.current_thread(), end=' ')
327327
print('running with argument', arg)
328328
time.sleep(0.5)
329329

Doc/library/idle.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ intended to be the same as executing the same code by the default method,
732732
directly with Python in a text-mode system console or terminal window.
733733
However, the different interface and operation occasionally affect
734734
visible results. For instance, ``sys.modules`` starts with more entries,
735-
and ``threading.activeCount()`` returns 2 instead of 1.
735+
and ``threading.active_count()`` returns 2 instead of 1.
736736

737737
By default, IDLE runs user code in a separate OS process rather than in
738738
the user interface process that runs the shell and editor. In the execution

Doc/library/threading.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ level :mod:`_thread` module. See also the :mod:`queue` module.
1616

1717
.. note::
1818

19-
While they are not listed below, the ``camelCase`` names used for some
20-
methods and functions in this module in the Python 2.x series are still
21-
supported by this module.
19+
In the Python 2.x series, this module contained ``camelCase`` names
20+
for some methods and functions. These are deprecated as of Python 3.10,
21+
but they are still supported for compatibility with Python 2.5 and lower.
2222

2323

2424
.. impl-detail::
@@ -42,6 +42,8 @@ This module defines the following functions:
4242
Return the number of :class:`Thread` objects currently alive. The returned
4343
count is equal to the length of the list returned by :func:`.enumerate`.
4444

45+
The function ``activeCount`` is a deprecated alias for this function.
46+
4547

4648
.. function:: current_thread()
4749

@@ -50,6 +52,8 @@ This module defines the following functions:
5052
:mod:`threading` module, a dummy thread object with limited functionality is
5153
returned.
5254

55+
The function ``currentThread`` is a deprecated alias for this function.
56+
5357

5458
.. function:: excepthook(args, /)
5559

@@ -381,9 +385,11 @@ since it is impossible to detect the termination of alien threads.
381385
.. method:: getName()
382386
setName()
383387

384-
Old getter/setter API for :attr:`~Thread.name`; use it directly as a
388+
Deprecated getter/setter API for :attr:`~Thread.name`; use it directly as a
385389
property instead.
386390

391+
.. deprecated:: 3.10
392+
387393
.. attribute:: ident
388394

389395
The 'thread identifier' of this thread or ``None`` if the thread has not
@@ -433,9 +439,11 @@ since it is impossible to detect the termination of alien threads.
433439
.. method:: isDaemon()
434440
setDaemon()
435441

436-
Old getter/setter API for :attr:`~Thread.daemon`; use it directly as a
442+
Deprecated getter/setter API for :attr:`~Thread.daemon`; use it directly as a
437443
property instead.
438444

445+
.. deprecated:: 3.10
446+
439447

440448
.. _lock-objects:
441449

@@ -771,6 +779,8 @@ item to the buffer only needs to wake up one consumer thread.
771779
calling thread has not acquired the lock when this method is called, a
772780
:exc:`RuntimeError` is raised.
773781

782+
The method ``notifyAll`` is a deprecated alias for this method.
783+
774784

775785
.. _semaphore-objects:
776786

@@ -908,6 +918,8 @@ method. The :meth:`~Event.wait` method blocks until the flag is true.
908918

909919
Return ``True`` if and only if the internal flag is true.
910920

921+
The method ``isSet`` is a deprecated alias for this method.
922+
911923
.. method:: set()
912924

913925
Set the internal flag to true. All threads waiting for it to become true

Doc/whatsnew/3.10.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,27 @@ Deprecated
11291129
``cache=shared`` query parameter.
11301130
(Contributed by Erlend E. Aasland in :issue:`24464`.)
11311131
1132+
* The following ``threading`` methods are now deprecated:
1133+
1134+
* ``threading.currentThread`` => :func:`threading.current_thread`
1135+
1136+
* ``threading.activeCount`` => :func:`threading.active_count`
1137+
1138+
* ``threading.Condition.notifyAll`` =>
1139+
:meth:`threading.Condition.notify_all`
1140+
1141+
* ``threading.Event.isSet`` => :meth:`threading.Event.is_set`
1142+
1143+
* ``threading.Thread.setName`` => :attr:`threading.Thread.name`
1144+
1145+
* ``threading.thread.getName`` => :attr:`threading.Thread.name`
1146+
1147+
* ``threading.Thread.isDaemon`` => :attr:`threading.Thread.daemon`
1148+
1149+
* ``threading.Thread.setDaemon`` => :attr:`threading.Thread.daemon`
1150+
1151+
(Contributed by Jelle Zijlstra in :issue:`21574`.)
1152+
11321153
11331154
Removed
11341155
=======

Lib/test/test_threading.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def test_various_ops(self):
154154

155155
def test_ident_of_no_threading_threads(self):
156156
# The ident still must work for the main thread and dummy threads.
157-
self.assertIsNotNone(threading.currentThread().ident)
157+
self.assertIsNotNone(threading.current_thread().ident)
158158
def f():
159-
ident.append(threading.currentThread().ident)
159+
ident.append(threading.current_thread().ident)
160160
done.set()
161161
done = threading.Event()
162162
ident = []
@@ -447,13 +447,32 @@ def test_old_threading_api(self):
447447
# Just a quick sanity check to make sure the old method names are
448448
# still present
449449
t = threading.Thread()
450-
t.isDaemon()
451-
t.setDaemon(True)
452-
t.getName()
453-
t.setName("name")
450+
with self.assertWarnsRegex(DeprecationWarning,
451+
r'get the daemon attribute'):
452+
t.isDaemon()
453+
with self.assertWarnsRegex(DeprecationWarning,
454+
r'set the daemon attribute'):
455+
t.setDaemon(True)
456+
with self.assertWarnsRegex(DeprecationWarning,
457+
r'get the name attribute'):
458+
t.getName()
459+
with self.assertWarnsRegex(DeprecationWarning,
460+
r'set the name attribute'):
461+
t.setName("name")
462+
454463
e = threading.Event()
455-
e.isSet()
456-
threading.activeCount()
464+
with self.assertWarnsRegex(DeprecationWarning, 'use is_set()'):
465+
e.isSet()
466+
467+
cond = threading.Condition()
468+
cond.acquire()
469+
with self.assertWarnsRegex(DeprecationWarning, 'use notify_all()'):
470+
cond.notifyAll()
471+
472+
with self.assertWarnsRegex(DeprecationWarning, 'use active_count()'):
473+
threading.activeCount()
474+
with self.assertWarnsRegex(DeprecationWarning, 'use current_thread()'):
475+
threading.currentThread()
457476

458477
def test_repr_daemon(self):
459478
t = threading.Thread()

Lib/threading.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,16 @@ def notify_all(self):
388388
"""
389389
self.notify(len(self._waiters))
390390

391-
notifyAll = notify_all
391+
def notifyAll(self):
392+
"""Wake up all threads waiting on this condition.
393+
394+
This method is deprecated, use notify_all() instead.
395+
396+
"""
397+
import warnings
398+
warnings.warn('notifyAll() is deprecated, use notify_all() instead',
399+
DeprecationWarning, stacklevel=2)
400+
self.notify_all()
392401

393402

394403
class Semaphore:
@@ -538,7 +547,16 @@ def is_set(self):
538547
"""Return true if and only if the internal flag is true."""
539548
return self._flag
540549

541-
isSet = is_set
550+
def isSet(self):
551+
"""Return true if and only if the internal flag is true.
552+
553+
This method is deprecated, use notify_all() instead.
554+
555+
"""
556+
import warnings
557+
warnings.warn('isSet() is deprecated, use is_set() instead',
558+
DeprecationWarning, stacklevel=2)
559+
return self.is_set()
542560

543561
def set(self):
544562
"""Set the internal flag to true.
@@ -1146,15 +1164,47 @@ def daemon(self, daemonic):
11461164
self._daemonic = daemonic
11471165

11481166
def isDaemon(self):
1167+
"""Return whether this thread is a daemon.
1168+
1169+
This method is deprecated, use the daemon attribute instead.
1170+
1171+
"""
1172+
import warnings
1173+
warnings.warn('isDaemon() is deprecated, get the daemon attribute instead',
1174+
DeprecationWarning, stacklevel=2)
11491175
return self.daemon
11501176

11511177
def setDaemon(self, daemonic):
1178+
"""Set whether this thread is a daemon.
1179+
1180+
This method is deprecated, use the .daemon property instead.
1181+
1182+
"""
1183+
import warnings
1184+
warnings.warn('setDaemon() is deprecated, set the daemon attribute instead',
1185+
DeprecationWarning, stacklevel=2)
11521186
self.daemon = daemonic
11531187

11541188
def getName(self):
1189+
"""Return a string used for identification purposes only.
1190+
1191+
This method is deprecated, use the name attribute instead.
1192+
1193+
"""
1194+
import warnings
1195+
warnings.warn('getName() is deprecated, get the name attribute instead',
1196+
DeprecationWarning, stacklevel=2)
11551197
return self.name
11561198

11571199
def setName(self, name):
1200+
"""Set the name string for this thread.
1201+
1202+
This method is deprecated, use the name attribute instead.
1203+
1204+
"""
1205+
import warnings
1206+
warnings.warn('setName() is deprecated, set the name attribute instead',
1207+
DeprecationWarning, stacklevel=2)
11581208
self.name = name
11591209

11601210

@@ -1349,7 +1399,16 @@ def current_thread():
13491399
except KeyError:
13501400
return _DummyThread()
13511401

1352-
currentThread = current_thread
1402+
def currentThread():
1403+
"""Return the current Thread object, corresponding to the caller's thread of control.
1404+
1405+
This function is deprecated, use current_thread() instead.
1406+
1407+
"""
1408+
import warnings
1409+
warnings.warn('currentThread() is deprecated, use current_thread() instead',
1410+
DeprecationWarning, stacklevel=2)
1411+
return current_thread()
13531412

13541413
def active_count():
13551414
"""Return the number of Thread objects currently alive.
@@ -1361,7 +1420,16 @@ def active_count():
13611420
with _active_limbo_lock:
13621421
return len(_active) + len(_limbo)
13631422

1364-
activeCount = active_count
1423+
def activeCount():
1424+
"""Return the number of Thread objects currently alive.
1425+
1426+
This function is deprecated, use active_count() instead.
1427+
1428+
"""
1429+
import warnings
1430+
warnings.warn('activeCount() is deprecated, use active_count() instead',
1431+
DeprecationWarning, stacklevel=2)
1432+
return active_count()
13651433

13661434
def _enumerate():
13671435
# Same as enumerate(), but without the lock. Internal use only.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The following ``threading`` methods are now deprecated and should be replaced:
2+
3+
- ``currentThread`` => :func:`threading.current_thread`
4+
5+
- ``activeCount`` => :func:`threading.active_count`
6+
7+
- ``Condition.notifyAll`` => :meth:`threading.Condition.notify_all`
8+
9+
- ``Event.isSet`` => :meth:`threading.Event.is_set`
10+
11+
- ``Thread.setName`` => :attr:`threading.Thread.name`
12+
13+
- ``thread.getName`` => :attr:`threading.Thread.name`
14+
15+
- ``Thread.isDaemon`` => :attr:`threading.Thread.daemon`
16+
17+
- ``Thread.setDaemon`` => :attr:`threading.Thread.daemon`
18+
19+
Patch by Jelle Zijlstra.

0 commit comments

Comments
 (0)