]> granicus.if.org Git - python/commitdiff
fix Thread.ident when it is the main thread or a dummy thread #5632
authorBenjamin Peterson <benjamin@python.org>
Tue, 31 Mar 2009 21:34:42 +0000 (21:34 +0000)
committerBenjamin Peterson <benjamin@python.org>
Tue, 31 Mar 2009 21:34:42 +0000 (21:34 +0000)
Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS

index cb6f6d2ae918f4a1d8d1ede6e4edf78fb12b914a..463d0d88ff1c0d0448a39b4af57b4a2033070709 100644 (file)
@@ -83,11 +83,24 @@ class ThreadTests(unittest.TestCase):
             t.join(NUMTASKS)
             self.assert_(not t.is_alive())
             self.failIfEqual(t.ident, 0)
+            self.assertFalse(t.ident is None)
             self.assert_(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
         if verbose:
             print 'all tasks done'
         self.assertEqual(numrunning.get(), 0)
 
+    def test_ident_of_no_threading_threads(self):
+        # The ident still must work for the main thread and dummy threads.
+        self.assertFalse(threading.currentThread().ident is None)
+        def f():
+            ident.append(threading.currentThread().ident)
+            done.set()
+        done = threading.Event()
+        ident = []
+        thread.start_new_thread(f, ())
+        done.wait()
+        self.assertFalse(ident[0] is None)
+
     # run with a small(ish) thread stack size (256kB)
     def test_various_ops_small_stack(self):
         if verbose:
index 28a8a2f946e2e978f4e4277a6a302ec282661891..13409db95ba589babe62ba8de98160475e1b891e 100644 (file)
@@ -500,9 +500,12 @@ class Thread(_Verbose):
                 return
             raise
 
+    def _set_ident(self):
+        self.__ident = _get_ident()
+
     def __bootstrap_inner(self):
         try:
-            self.__ident = _get_ident()
+            self._set_ident()
             self.__started.set()
             with _active_limbo_lock:
                 _active[self.__ident] = self
@@ -733,6 +736,7 @@ class _MainThread(Thread):
     def __init__(self):
         Thread.__init__(self, name="MainThread")
         self._Thread__started.set()
+        self._set_ident()
         with _active_limbo_lock:
             _active[_get_ident()] = self
 
@@ -778,6 +782,7 @@ class _DummyThread(Thread):
         del self._Thread__block
 
         self._Thread__started.set()
+        self._set_ident()
         with _active_limbo_lock:
             _active[_get_ident()] = self
 
index 6d6d2925c0632aa6c84c5ee10157da4b05e751cd..6bded6b0f0726676a2c3c65485e916d4d343f6a6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -199,6 +199,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5632: Thread.ident was None for the main thread and threads not created
+  with the threading module.
+
 - Issue #5400: Added patch for multiprocessing on netbsd compilation/support
 
 - Issue #5387: Fixed mmap.move crash by integer overflow.