]> granicus.if.org Git - python/commitdiff
Issue #22423: Unhandled exception in thread no longer causes unhandled
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 21 Sep 2014 19:08:00 +0000 (22:08 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 21 Sep 2014 19:08:00 +0000 (22:08 +0300)
AttributeError when sys.stderr is None.

Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS

index 86c953f4d65427d0644da703fe77d44e30bd001c..ac41f2d3c4fff2de3bd053a78a4bab4775f08545 100644 (file)
@@ -829,6 +829,85 @@ class ThreadingExceptionTests(BaseTestCase):
         thread.start()
         self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
 
+    def test_print_exception(self):
+        script = r"""if 1:
+            import threading
+            import time
+
+            running = False
+            def run():
+                global running
+                running = True
+                while running:
+                    time.sleep(0.01)
+                1/0
+            t = threading.Thread(target=run)
+            t.start()
+            while not running:
+                time.sleep(0.01)
+            running = False
+            t.join()
+            """
+        rc, out, err = assert_python_ok("-c", script)
+        self.assertEqual(out, '')
+        self.assertIn("Exception in thread", err)
+        self.assertIn("Traceback (most recent call last):", err)
+        self.assertIn("ZeroDivisionError", err)
+        self.assertNotIn("Unhandled exception", err)
+
+    def test_print_exception_stderr_is_none_1(self):
+        script = r"""if 1:
+            import sys
+            import threading
+            import time
+
+            running = False
+            def run():
+                global running
+                running = True
+                while running:
+                    time.sleep(0.01)
+                1/0
+            t = threading.Thread(target=run)
+            t.start()
+            while not running:
+                time.sleep(0.01)
+            sys.stderr = None
+            running = False
+            t.join()
+            """
+        rc, out, err = assert_python_ok("-c", script)
+        self.assertEqual(out, '')
+        self.assertIn("Exception in thread", err)
+        self.assertIn("Traceback (most recent call last):", err)
+        self.assertIn("ZeroDivisionError", err)
+        self.assertNotIn("Unhandled exception", err)
+
+    def test_print_exception_stderr_is_none_2(self):
+        script = r"""if 1:
+            import sys
+            import threading
+            import time
+
+            running = False
+            def run():
+                global running
+                running = True
+                while running:
+                    time.sleep(0.01)
+                1/0
+            sys.stderr = None
+            t = threading.Thread(target=run)
+            t.start()
+            while not running:
+                time.sleep(0.01)
+            running = False
+            t.join()
+            """
+        rc, out, err = assert_python_ok("-c", script)
+        self.assertEqual(out, '')
+        self.assertNotIn("Unhandled exception", err)
+
 
 class LockTests(lock_tests.LockTests):
     locktype = staticmethod(threading.Lock)
index e81471bbb008ae48ff78276ca761a8fea1fb0973..0438e1f5209de9d0708a253d70c35935caa74fec 100644 (file)
@@ -818,10 +818,10 @@ class Thread(_Verbose):
                 # shutdown) use self.__stderr.  Otherwise still use sys (as in
                 # _sys) in case sys.stderr was redefined since the creation of
                 # self.
-                if _sys:
-                    _sys.stderr.write("Exception in thread %s:\n%s\n" %
-                                      (self.name, _format_exc()))
-                else:
+                if _sys and _sys.stderr is not None:
+                    print>>_sys.stderr, ("Exception in thread %s:\n%s" %
+                                         (self.name, _format_exc()))
+                elif self.__stderr is not None:
                     # Do the best job possible w/o a huge amt. of code to
                     # approximate a traceback (code ideas from
                     # Lib/traceback.py)
index b545a5538a7a47dcc9c66cde76e37c4d4f986cbf..2b35629bc682ec13d89d1717cc91c5f6fd6c92e3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,10 @@ Core and Builtins
 
 Library
 -------
+
+- Issue #22423: Unhandled exception in thread no longer causes unhandled
+  AttributeError when sys.stderr is None.
+
 - Issue #22419: Limit the length of incoming HTTP request in wsgiref server to
   65536 bytes and send a 414 error code for higher lengths. Patch contributed
   by Devin Cook.