changeset: 76068:60ad83716733 branch: 3.2 parent: 76059:d746ffc34e0f user: Benjamin Peterson date: Mon Apr 02 11:15:17 2012 -0400 files: Lib/test/test_thread.py Misc/NEWS Modules/_threadmodule.c description: prevent writing to stderr from messing up the exception state (closes #14474) diff -r d746ffc34e0f -r 60ad83716733 Lib/test/test_thread.py --- a/Lib/test/test_thread.py Sun Apr 01 18:49:54 2012 -0400 +++ b/Lib/test/test_thread.py Mon Apr 02 11:15:17 2012 -0400 @@ -128,6 +128,30 @@ 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): diff -r d746ffc34e0f -r 60ad83716733 Misc/NEWS --- a/Misc/NEWS Sun Apr 01 18:49:54 2012 -0400 +++ b/Misc/NEWS Mon Apr 02 11:15:17 2012 -0400 @@ -10,6 +10,9 @@ 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. diff -r d746ffc34e0f -r 60ad83716733 Modules/_threadmodule.c --- a/Modules/_threadmodule.c Sun Apr 01 18:49:54 2012 -0400 +++ b/Modules/_threadmodule.c Mon Apr 02 11:15:17 2012 -0400 @@ -994,14 +994,17 @@ 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); } }

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