[Python-checkins] cpython (2.7): Issue #22423: Unhandled exception in thread no longer causes unhandled

serhiy.storchaka python-checkins at python.org
Sun Sep 21 21:30:45 CEST 2014


https://hg.python.org/cpython/rev/4baa474b4f31
changeset: 92503:4baa474b4f31
branch: 2.7
parent: 92500:f75f446dba92
user: Serhiy Storchaka <storchaka at gmail.com>
date: Sun Sep 21 22:08:00 2014 +0300
summary:
 Issue #22423: Unhandled exception in thread no longer causes unhandled
AttributeError when sys.stderr is None.
files:
 Lib/test/test_threading.py | 79 ++++++++++++++++++++++++++
 Lib/threading.py | 8 +-
 Misc/NEWS | 4 +
 3 files changed, 87 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -829,6 +829,85 @@
 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)
diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -818,10 +818,10 @@
 # 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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,10 @@
 
 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.
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /