]> granicus.if.org Git - python/commitdiff
prevent writing to stderr from messing up the exception state (closes #14474)
authorBenjamin Peterson <benjamin@python.org>
Mon, 2 Apr 2012 15:15:17 +0000 (11:15 -0400)
committerBenjamin Peterson <benjamin@python.org>
Mon, 2 Apr 2012 15:15:17 +0000 (11:15 -0400)
Lib/test/test_thread.py
Misc/NEWS
Modules/threadmodule.c

index 544e70d2fdf7ab3a599def978979628c01c8530f..387fa030cee4f7a4bc9a39fabbb4cd003205f920 100644 (file)
@@ -130,6 +130,30 @@ class ThreadRunningTests(BasicThreadTest):
             time.sleep(0.01)
         self.assertEqual(thread._count(), orig)
 
+    def test_save_exception_state_on_error(self):
+        # See issue #14474
+        def task():
+            started.release()
+            sys.stderr = stderr
+            raise SyntaxError
+        def mywrite(self, *args):
+            try:
+                raise ValueError
+            except ValueError:
+                pass
+            real_write(self, *args)
+        c = thread._count()
+        started = thread.allocate_lock()
+        with test_support.captured_output("stderr") as stderr:
+            real_write = stderr.write
+            stderr.write = mywrite
+            started.acquire()
+            thread.start_new_thread(task, ())
+            started.acquire()
+            while thread._count() > c:
+                pass
+        self.assertIn("Traceback", stderr.getvalue())
+
 
 class Barrier:
     def __init__(self, num_threads):
index 15c943c1c05de67324da03d352296b6407fd1879..8593a10b1ce0c99ac2c7798751134f1f18b18a70 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
 Core and Builtins
 -----------------
 
+- Issue #14474: Save and restore exception state in thread.start_new_thread()
+  while writing error message if the thread leaves a unhandled exception.
+
 - Issue #13019: Fix potential reference leaks in bytearray.extend().  Patch
   by Suman Saha.
 
index 4e41085ed9261b5e782823430cb10d25a3044534..73ee382df36a0bb0e0823d36c93c35efed303edf 100644 (file)
@@ -618,6 +618,8 @@ t_bootstrap(void *boot_raw)
             PyErr_Clear();
         else {
             PyObject *file;
+            PyObject *exc, *value, *tb;
+            PyErr_Fetch(&exc, &value, &tb);
             PySys_WriteStderr(
                 "Unhandled exception in thread started by ");
             file = PySys_GetObject("stderr");
@@ -625,6 +627,7 @@ t_bootstrap(void *boot_raw)
                 PyFile_WriteObject(boot->func, file, 0);
             else
                 PyObject_Print(boot->func, stderr, 0);
+            PyErr_Restore(exc, value, tb);
             PySys_WriteStderr("\n");
             PyErr_PrintEx(0);
         }