Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a12974

Browse files
author
Victor Stinner
committed
Close #12028: Make threading._get_ident() public, rename it to
threading.get_ident() and document it. This function was used by _thread.get_ident().
1 parent d976098 commit 2a12974

12 files changed

Lines changed: 50 additions & 39 deletions

File tree

Doc/library/signal.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,9 @@ The :mod:`signal` module defines the following functions:
187187
Send the signal *signum* to the thread *thread_id*, another thread in the same
188188
process as the caller. The signal is asynchronously directed to thread.
189189

190-
*thread_id* can be read from the :attr:`~threading.Thread.ident` attribute
191-
of :attr:`threading.Thread`. For example,
192-
``threading.current_thread().ident`` gives the identifier of the current
193-
thread.
190+
Use :func:`threading.get_ident()` or the :attr:`~threading.Thread.ident`
191+
attribute of :attr:`threading.Thread` to get a 'thread identifier' for
192+
*thread_id*.
194193

195194
If *signum* is 0, then no signal is sent, but error checking is still
196195
performed; this can be used to check if a thread is still running.

Doc/library/threading.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ This module defines the following functions and objects:
4848
returned.
4949

5050

51+
.. function:: get_ident()
52+
53+
Return the 'thread identifier' of the current thread. This is a nonzero
54+
integer. Its value has no direct meaning; it is intended as a magic cookie
55+
to be used e.g. to index a dictionary of thread-specific data. Thread
56+
identifiers may be recycled when a thread exits and another thread is
57+
created.
58+
59+
.. versionadded:: 3.3
60+
61+
5162
.. function:: enumerate()
5263

5364
Return a list of all :class:`Thread` objects currently alive. The list
@@ -332,10 +343,10 @@ impossible to detect the termination of alien threads.
332343
.. attribute:: ident
333344

334345
The 'thread identifier' of this thread or ``None`` if the thread has not
335-
been started. This is a nonzero integer. See the
336-
:func:`thread.get_ident()` function. Thread identifiers may be recycled
337-
when a thread exits and another thread is created. The identifier is
338-
available even after the thread has exited.
346+
been started. This is a nonzero integer. See the :func:`get_ident()`
347+
function. Thread identifiers may be recycled when a thread exits and
348+
another thread is created. The identifier is available even after the
349+
thread has exited.
339350

340351
.. method:: is_alive()
341352

Lib/logging/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141
codecs = None
4242

4343
try:
44-
import _thread as thread
4544
import threading
4645
except ImportError: #pragma: no cover
47-
thread = None
46+
threading = None
4847

4948
__author__ = "Vinay Sajip <[email protected]>"
5049
__status__ = "production"
@@ -199,7 +198,7 @@ def _checkLevel(level):
199198
#the lock would already have been acquired - so we need an RLock.
200199
#The same argument applies to Loggers and Manager.loggerDict.
201200
#
202-
if thread:
201+
if threading:
203202
_lock = threading.RLock()
204203
else: #pragma: no cover
205204
_lock = None
@@ -278,8 +277,8 @@ def __init__(self, name, level, pathname, lineno,
278277
self.created = ct
279278
self.msecs = (ct - int(ct)) * 1000
280279
self.relativeCreated = (self.created - _startTime) * 1000
281-
if logThreads and thread:
282-
self.thread = thread.get_ident()
280+
if logThreads and threading:
281+
self.thread = threading.get_ident()
283282
self.threadName = threading.current_thread().name
284283
else: # pragma: no cover
285284
self.thread = None
@@ -773,7 +772,7 @@ def createLock(self):
773772
"""
774773
Acquire a thread lock for serializing access to the underlying I/O.
775774
"""
776-
if thread:
775+
if threading:
777776
self.lock = threading.RLock()
778777
else: #pragma: no cover
779778
self.lock = None

Lib/reprlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import builtins
66
from itertools import islice
77
try:
8-
from _thread import get_ident
8+
from threading import get_ident
99
except ImportError:
1010
from _dummy_thread import get_ident
1111

Lib/test/lock_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sys
66
import time
7-
from _thread import start_new_thread, get_ident, TIMEOUT_MAX
7+
from _thread import start_new_thread, TIMEOUT_MAX
88
import threading
99
import unittest
1010

@@ -31,7 +31,7 @@ def __init__(self, f, n, wait_before_exit=False):
3131
self.finished = []
3232
self._can_exit = not wait_before_exit
3333
def task():
34-
tid = get_ident()
34+
tid = threading.get_ident()
3535
self.started.append(tid)
3636
try:
3737
f()

Lib/test/test_capi.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,17 @@ def TestThreadState():
190190
idents = []
191191

192192
def callback():
193-
idents.append(_thread.get_ident())
193+
idents.append(threading.get_ident())
194194

195195
_testcapi._test_thread_state(callback)
196196
a = b = callback
197197
time.sleep(1)
198198
# Check our main thread is in the list exactly 3 times.
199-
if idents.count(_thread.get_ident()) != 3:
199+
if idents.count(threading.get_ident()) != 3:
200200
raise support.TestFailed(
201201
"Couldn't find main thread correctly in the list")
202202

203203
if threading:
204-
import _thread
205204
import time
206205
TestThreadState()
207206
t = threading.Thread(target=TestThreadState)

Lib/test/test_signal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def can_test_blocked_signals(self, skip):
557557

558558
def kill(self, signum):
559559
if self.has_pthread_kill:
560-
tid = threading.current_thread().ident
560+
tid = threading.get_ident()
561561
signal.pthread_kill(tid, signum)
562562
else:
563563
pid = os.getpid()
@@ -589,7 +589,7 @@ def test_sigpending(self):
589589
'need signal.pthread_kill()')
590590
def test_pthread_kill(self):
591591
signum = signal.SIGUSR1
592-
current = threading.current_thread().ident
592+
current = threading.get_ident()
593593

594594
old_handler = signal.signal(signum, self.handler)
595595
self.addCleanup(signal.signal, signum, old_handler)

Lib/test/test_sys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def test_current_frames(self):
343343
# Test sys._current_frames() in a WITH_THREADS build.
344344
@test.support.reap_threads
345345
def current_frames_with_threads(self):
346-
import threading, _thread
346+
import threading
347347
import traceback
348348

349349
# Spawn a thread that blocks at a known place. Then the main
@@ -357,7 +357,7 @@ def f123():
357357
g456()
358358

359359
def g456():
360-
thread_info.append(_thread.get_ident())
360+
thread_info.append(threading.get_ident())
361361
entered_g.set()
362362
leave_g.wait()
363363

@@ -373,7 +373,7 @@ def g456():
373373

374374
d = sys._current_frames()
375375

376-
main_id = _thread.get_ident()
376+
main_id = threading.get_ident()
377377
self.assertIn(main_id, d)
378378
self.assertIn(thread_id, d)
379379

Lib/test/test_threaded_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def task(N, done, done_tasks, errors):
3030
except Exception as e:
3131
errors.append(e.with_traceback(None))
3232
finally:
33-
done_tasks.append(thread.get_ident())
33+
done_tasks.append(threading.get_ident())
3434
finished = len(done_tasks) == N
3535
if finished:
3636
done.set()

Lib/test/test_threading.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class AsyncExc(Exception):
173173
exception = ctypes.py_object(AsyncExc)
174174

175175
# First check it works when setting the exception from the same thread.
176-
tid = _thread.get_ident()
176+
tid = threading.get_ident()
177177

178178
try:
179179
result = set_async_exc(ctypes.c_long(tid), exception)
@@ -202,7 +202,7 @@ class AsyncExc(Exception):
202202

203203
class Worker(threading.Thread):
204204
def run(self):
205-
self.id = _thread.get_ident()
205+
self.id = threading.get_ident()
206206
self.finished = False
207207

208208
try:

0 commit comments

Comments
 (0)