]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close #12028: Make threading._get_ident() public, rename it to
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 30 May 2011 21:02:52 +0000 (23:02 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 30 May 2011 21:02:52 +0000 (23:02 +0200)
threading.get_ident() and document it. This function was used by
_thread.get_ident().

12 files changed:
Doc/library/signal.rst
Doc/library/threading.rst
Lib/logging/__init__.py
Lib/reprlib.py
Lib/test/lock_tests.py
Lib/test/test_capi.py
Lib/test/test_signal.py
Lib/test/test_sys.py
Lib/test/test_threaded_import.py
Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS

index 68a5d2c0149c3417c51b0af21bf288a4a4b4cd63..f2a37cc65a666bf9c4da4d7b6ce56fafa4347b50 100644 (file)
@@ -187,10 +187,9 @@ The :mod:`signal` module defines the following functions:
    Send the signal *signum* to the thread *thread_id*, another thread in the same
    process as the caller.  The signal is asynchronously directed to thread.
 
-   *thread_id* can be read from the :attr:`~threading.Thread.ident` attribute
-   of :attr:`threading.Thread`.  For example,
-   ``threading.current_thread().ident`` gives the identifier of the current
-   thread.
+   Use :func:`threading.get_ident()` or the :attr:`~threading.Thread.ident`
+   attribute of :attr:`threading.Thread` to get a 'thread identifier' for
+   *thread_id*.
 
    If *signum* is 0, then no signal is sent, but error checking is still
    performed; this can be used to check if a thread is still running.
index df47045ffb5d334ebdab847dbcb04696744288ed..504a2fb1d36a60a341044fbdde6cb08ab92d95b7 100644 (file)
@@ -48,6 +48,17 @@ This module defines the following functions and objects:
    returned.
 
 
+.. function:: get_ident()
+
+   Return the 'thread identifier' of the current thread.  This is a nonzero
+   integer.  Its value has no direct meaning; it is intended as a magic cookie
+   to be used e.g. to index a dictionary of thread-specific data.  Thread
+   identifiers may be recycled when a thread exits and another thread is
+   created.
+
+   .. versionadded:: 3.3
+
+
 .. function:: enumerate()
 
    Return a list of all :class:`Thread` objects currently alive.  The list
@@ -332,10 +343,10 @@ impossible to detect the termination of alien threads.
    .. 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.
+      been started.  This is a nonzero integer.  See the :func:`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.
 
    .. method:: is_alive()
 
index 3a8a6391ec50f44553bb7bfbb7f9ca30c82a84d3..1a4b24122606b3113a9f728ad9dd1dc9c992ce4d 100644 (file)
@@ -41,10 +41,9 @@ except ImportError: #pragma: no cover
     codecs = None
 
 try:
-    import _thread as thread
     import threading
 except ImportError: #pragma: no cover
-    thread = None
+    threading = None
 
 __author__  = "Vinay Sajip <vinay_sajip@red-dove.com>"
 __status__  = "production"
@@ -199,7 +198,7 @@ def _checkLevel(level):
 #the lock would already have been acquired - so we need an RLock.
 #The same argument applies to Loggers and Manager.loggerDict.
 #
-if thread:
+if threading:
     _lock = threading.RLock()
 else: #pragma: no cover
     _lock = None
@@ -278,8 +277,8 @@ class LogRecord(object):
         self.created = ct
         self.msecs = (ct - int(ct)) * 1000
         self.relativeCreated = (self.created - _startTime) * 1000
-        if logThreads and thread:
-            self.thread = thread.get_ident()
+        if logThreads and threading:
+            self.thread = threading.get_ident()
             self.threadName = threading.current_thread().name
         else: # pragma: no cover
             self.thread = None
@@ -773,7 +772,7 @@ class Handler(Filterer):
         """
         Acquire a thread lock for serializing access to the underlying I/O.
         """
-        if thread:
+        if threading:
             self.lock = threading.RLock()
         else: #pragma: no cover
             self.lock = None
index f8033604da28da12dcad7d164580be638e51904a..092874a18feb4a570303eed89d5d10b28a5c96e9 100644 (file)
@@ -5,7 +5,7 @@ __all__ = ["Repr", "repr", "recursive_repr"]
 import builtins
 from itertools import islice
 try:
-    from _thread import get_ident
+    from threading import get_ident
 except ImportError:
     from _dummy_thread import get_ident
 
index 126f97cb31a0cbed261159754bbabf09b8c70d11..7bcc43607fe57d29e788fc510ab1e64085be923b 100644 (file)
@@ -4,7 +4,7 @@ Various tests for synchronization primitives.
 
 import sys
 import time
-from _thread import start_new_thread, get_ident, TIMEOUT_MAX
+from _thread import start_new_thread, TIMEOUT_MAX
 import threading
 import unittest
 
@@ -31,7 +31,7 @@ class Bunch(object):
         self.finished = []
         self._can_exit = not wait_before_exit
         def task():
-            tid = get_ident()
+            tid = threading.get_ident()
             self.started.append(tid)
             try:
                 f()
index 327ac66501447ccbefea441a9b2cef12b8c75f87..72364747f8dcf0ddb5e3c1589ed86864a578184e 100644 (file)
@@ -190,18 +190,17 @@ def test_main():
         idents = []
 
         def callback():
-            idents.append(_thread.get_ident())
+            idents.append(threading.get_ident())
 
         _testcapi._test_thread_state(callback)
         a = b = callback
         time.sleep(1)
         # Check our main thread is in the list exactly 3 times.
-        if idents.count(_thread.get_ident()) != 3:
+        if idents.count(threading.get_ident()) != 3:
             raise support.TestFailed(
                         "Couldn't find main thread correctly in the list")
 
     if threading:
-        import _thread
         import time
         TestThreadState()
         t = threading.Thread(target=TestThreadState)
index cdd3f3e3a4d50a8ee4eefa1d7f9866b25b8ab088..4bf2d2dedfead8f5f4b040520244f652fa1ff5bf 100644 (file)
@@ -557,7 +557,7 @@ class PendingSignalsTests(unittest.TestCase):
 
     def kill(self, signum):
         if self.has_pthread_kill:
-            tid = threading.current_thread().ident
+            tid = threading.get_ident()
             signal.pthread_kill(tid, signum)
         else:
             pid = os.getpid()
@@ -589,7 +589,7 @@ class PendingSignalsTests(unittest.TestCase):
                          'need signal.pthread_kill()')
     def test_pthread_kill(self):
         signum = signal.SIGUSR1
-        current = threading.current_thread().ident
+        current = threading.get_ident()
 
         old_handler = signal.signal(signum, self.handler)
         self.addCleanup(signal.signal, signum, old_handler)
index 04bdfefc8b2e8a259be9628d47117cbe7b1774b0..bc0f34c0b929e0358435535906b66ccf3d554945 100644 (file)
@@ -343,7 +343,7 @@ class SysModuleTest(unittest.TestCase):
     # Test sys._current_frames() in a WITH_THREADS build.
     @test.support.reap_threads
     def current_frames_with_threads(self):
-        import threading, _thread
+        import threading
         import traceback
 
         # Spawn a thread that blocks at a known place.  Then the main
@@ -357,7 +357,7 @@ class SysModuleTest(unittest.TestCase):
             g456()
 
         def g456():
-            thread_info.append(_thread.get_ident())
+            thread_info.append(threading.get_ident())
             entered_g.set()
             leave_g.wait()
 
@@ -373,7 +373,7 @@ class SysModuleTest(unittest.TestCase):
 
         d = sys._current_frames()
 
-        main_id = _thread.get_ident()
+        main_id = threading.get_ident()
         self.assertIn(main_id, d)
         self.assertIn(thread_id, d)
 
index 7791935fdf170d2d0963bc87dc2f64b391eae3b9..6919d21e1dc387f213189a1fe157cdacbef25288 100644 (file)
@@ -30,7 +30,7 @@ def task(N, done, done_tasks, errors):
     except Exception as e:
         errors.append(e.with_traceback(None))
     finally:
-        done_tasks.append(thread.get_ident())
+        done_tasks.append(threading.get_ident())
         finished = len(done_tasks) == N
         if finished:
             done.set()
index c22d965495e6e1e382654e759157c015693180e9..12e596e053f9dab5824a33793b5861209099b44d 100644 (file)
@@ -173,7 +173,7 @@ class ThreadTests(BaseTestCase):
         exception = ctypes.py_object(AsyncExc)
 
         # First check it works when setting the exception from the same thread.
-        tid = _thread.get_ident()
+        tid = threading.get_ident()
 
         try:
             result = set_async_exc(ctypes.c_long(tid), exception)
@@ -202,7 +202,7 @@ class ThreadTests(BaseTestCase):
 
         class Worker(threading.Thread):
             def run(self):
-                self.id = _thread.get_ident()
+                self.id = threading.get_ident()
                 self.finished = False
 
                 try:
index fafe7792f5653a59c6234e6ff838a8fd45004718..1f638b43c3a454f416d69d1461c9ce0e465974e6 100644 (file)
@@ -24,7 +24,7 @@ __all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
 # Rename some stuff so "from threading import *" is safe
 _start_new_thread = _thread.start_new_thread
 _allocate_lock = _thread.allocate_lock
-_get_ident = _thread.get_ident
+get_ident = _thread.get_ident
 ThreadError = _thread.error
 try:
     _CRLock = _thread.RLock
@@ -52,7 +52,7 @@ if __debug__:
                 format = format % args
                 # Issue #4188: calling current_thread() can incur an infinite
                 # recursion if it has to create a DummyThread on the fly.
-                ident = _get_ident()
+                ident = get_ident()
                 try:
                     name = _active[ident].name
                 except KeyError:
@@ -110,7 +110,7 @@ class _RLock(_Verbose):
                 self.__class__.__name__, owner, self._count)
 
     def acquire(self, blocking=True, timeout=-1):
-        me = _get_ident()
+        me = get_ident()
         if self._owner == me:
             self._count = self._count + 1
             if __debug__:
@@ -130,7 +130,7 @@ class _RLock(_Verbose):
     __enter__ = acquire
 
     def release(self):
-        if self._owner != _get_ident():
+        if self._owner != get_ident():
             raise RuntimeError("cannot release un-acquired lock")
         self._count = count = self._count - 1
         if not count:
@@ -166,7 +166,7 @@ class _RLock(_Verbose):
         return (count, owner)
 
     def _is_owned(self):
-        return self._owner == _get_ident()
+        return self._owner == get_ident()
 
 _PyRLock = _RLock
 
@@ -714,7 +714,7 @@ class Thread(_Verbose):
             raise
 
     def _set_ident(self):
-        self._ident = _get_ident()
+        self._ident = get_ident()
 
     def _bootstrap_inner(self):
         try:
@@ -787,7 +787,7 @@ class Thread(_Verbose):
                 try:
                     # We don't call self._delete() because it also
                     # grabs _active_limbo_lock.
-                    del _active[_get_ident()]
+                    del _active[get_ident()]
                 except:
                     pass
 
@@ -823,7 +823,7 @@ class Thread(_Verbose):
 
         try:
             with _active_limbo_lock:
-                del _active[_get_ident()]
+                del _active[get_ident()]
                 # There must not be any python code between the previous line
                 # and after the lock is released.  Otherwise a tracing function
                 # could try to acquire the lock again in the same thread, (in
@@ -1006,9 +1006,8 @@ class _DummyThread(Thread):
 
 def current_thread():
     try:
-        return _active[_get_ident()]
+        return _active[get_ident()]
     except KeyError:
-        ##print "current_thread(): no current thread for", _get_ident()
         return _DummyThread()
 
 currentThread = current_thread
@@ -1062,7 +1061,7 @@ def _after_fork():
             if thread is current:
                 # There is only one active thread. We reset the ident to
                 # its new value since it can have changed.
-                ident = _get_ident()
+                ident = get_ident()
                 thread._ident = ident
                 # Any condition variables hanging off of the active thread may
                 # be in an invalid state, so we reinitialize them.
index e5e5aa9e50ce78f5338ee3bde0c21e247cd6d318..152acba40ef5c3fdd0cf211c9a79156b4e16624e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -177,6 +177,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #12028: Make threading._get_ident() public, rename it to
+  threading.get_ident() and document it. This function was used by
+  _thread.get_ident().
+
 - Issue #12171: IncrementalEncoder.reset() of CJK codecs (multibytecodec) calls
   encreset() instead of decreset().