cpython: 8609d7fcdcc7 (original) (raw)
Mercurial > cpython
changeset 76066:8609d7fcdcc7 2.7
prevent writing to stderr from messing up the exception state (closes #14474) [#14474]
Benjamin Peterson benjamin@python.org | |
---|---|
date | Mon, 02 Apr 2012 11:15:17 -0400 |
parents | d3a82a26c705 |
children | d552016fc07c |
files | Lib/test/test_thread.py Misc/NEWS Modules/threadmodule.c |
diffstat | 3 files changed, 30 insertions(+), 0 deletions(-)[+] [-] Lib/test/test_thread.py 24 Misc/NEWS 3 Modules/threadmodule.c 3 |
line wrap: on
line diff
--- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -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[](#l1.8)
def task():[](#l1.9)
started.release()[](#l1.10)
sys.stderr = stderr[](#l1.11)
raise SyntaxError[](#l1.12)
def mywrite(self, *args):[](#l1.13)
try:[](#l1.14)
raise ValueError[](#l1.15)
except ValueError:[](#l1.16)
pass[](#l1.17)
real_write(self, *args)[](#l1.18)
c = thread._count()[](#l1.19)
started = thread.allocate_lock()[](#l1.20)
with test_support.captured_output("stderr") as stderr:[](#l1.21)
real_write = stderr.write[](#l1.22)
stderr.write = mywrite[](#l1.23)
started.acquire()[](#l1.24)
thread.start_new_thread(task, ())[](#l1.25)
started.acquire()[](#l1.26)
while thread._count() > c:[](#l1.27)
pass[](#l1.28)
self.assertIn("Traceback", stderr.getvalue())[](#l1.29)
+ class Barrier: def init(self, num_threads):
--- 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()
--- a/Modules/threadmodule.c +++ b/Modules/threadmodule.c @@ -618,6 +618,8 @@ t_bootstrap(void *boot_raw) PyErr_Clear(); else { PyObject *file;
PyObject *exc, *value, *tb;[](#l3.7)
PyErr_Fetch(&exc, &value, &tb);[](#l3.8) PySys_WriteStderr([](#l3.9) "Unhandled exception in thread started by ");[](#l3.10) file = PySys_GetObject("stderr");[](#l3.11)
@@ -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);[](#l3.16) PySys_WriteStderr("\n");[](#l3.17) PyErr_PrintEx(0);[](#l3.18) }[](#l3.19)