]> 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 894a49392eef7befb770f9c87f48156be08ec942..deb4bf36202b87f3de9525fa3ef7ea0450c24366 100644 (file)
@@ -128,6 +128,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 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 2fa911e4bbaaca6d3a5ad4cba9fb78da9288d972..3d9b8225bd403f6bd04da8e4e87763a243a6a0d3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.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 ea038defb1c728a2e0c324fcc885b6b4f58a4459..5f76a7b6c2ff1c5ecdbc3289f8d43a7300c3d9cd 100644 (file)
@@ -994,14 +994,17 @@ t_bootstrap(void *boot_raw)
             PyErr_Clear();
         else {
             PyObject *file;
+            PyObject *exc, *value, *tb;
             PySys_WriteStderr(
                 "Unhandled exception in thread started by ");
+            PyErr_Fetch(&exc, &value, &tb);
             file = PySys_GetObject("stderr");
             if (file != NULL && file != Py_None)
                 PyFile_WriteObject(boot->func, file, 0);
             else
                 PyObject_Print(boot->func, stderr, 0);
             PySys_WriteStderr("\n");
+            PyErr_Restore(exc, value, tb);
             PyErr_PrintEx(0);
         }
     }