]> granicus.if.org Git - python/commitdiff
Get "stopped" back into repr(Thread) when appropriate.
authorTim Peters <tim@python.org>
Mon, 9 Sep 2013 23:48:24 +0000 (18:48 -0500)
committerTim Peters <tim@python.org>
Mon, 9 Sep 2013 23:48:24 +0000 (18:48 -0500)
Due to recent changes, a Thread doesn't know that it's over before
someone calls .join() or .is_alive().  That meant repr(Thread)
continued to include "started" (and not "stopped") before one of
those methods was called, even if hours passed since the thread
ended.

Repaired that.

Lib/test/test_threading.py
Lib/threading.py

index 75ae247de7b10508410581e56f7a72e8756fc798..c39d5e26c76d90ca147cb5bad0743c365b05d986 100644 (file)
@@ -573,6 +573,31 @@ class ThreadTests(BaseTestCase):
         # And verify the thread disposed of _tstate_lock.
         self.assertTrue(t._tstate_lock is None)
 
+    def test_repr_stopped(self):
+        # Verify that "stopped" shows up in repr(Thread) appropriately.
+        started = _thread.allocate_lock()
+        finish = _thread.allocate_lock()
+        started.acquire()
+        finish.acquire()
+        def f():
+            started.release()
+            finish.acquire()
+        t = threading.Thread(target=f)
+        t.start()
+        started.acquire()
+        self.assertIn("started", repr(t))
+        finish.release()
+        # "stopped" should appear in the repr in a reasonable amount of time.
+        # Implementation detail:  as of this writing, that's trivially true
+        # if .join() is called, and almost trivially true if .is_alive() is
+        # called.  The detail we're testing here is that "stopped" shows up
+        # "all on its own".
+        LOOKING_FOR = "stopped"
+        for i in range(500):
+            if LOOKING_FOR in repr(t):
+                break
+            time.sleep(0.01)
+        self.assertIn(LOOKING_FOR, repr(t)) # we waited at least 5 seconds
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
index 1ad22a430c8a1749862b419b813e7720906a8564..26d1018d3ede234bc881af49c5c4c8538ae06c0c 100644 (file)
@@ -574,6 +574,7 @@ class Thread:
         status = "initial"
         if self._started.is_set():
             status = "started"
+        self.is_alive() # easy way to get ._is_stopped set when appropriate
         if self._is_stopped:
             status = "stopped"
         if self._daemonic: