File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -128,6 +128,30 @@ def task():
128128 time .sleep (0.01 )
129129 self .assertEqual (thread ._count (), orig )
130130
131+ def test_save_exception_state_on_error (self ):
132+ # See issue #14474
133+ def task ():
134+ started .release ()
135+ sys .stderr = stderr
136+ raise SyntaxError
137+ def mywrite (self , * args ):
138+ try :
139+ raise ValueError
140+ except ValueError :
141+ pass
142+ real_write (self , * args )
143+ c = thread ._count ()
144+ started = thread .allocate_lock ()
145+ with support .captured_output ("stderr" ) as stderr :
146+ real_write = stderr .write
147+ stderr .write = mywrite
148+ started .acquire ()
149+ thread .start_new_thread (task , ())
150+ started .acquire ()
151+ while thread ._count () > c :
152+ pass
153+ self .assertIn ("Traceback" , stderr .getvalue ())
154+
131155
132156class Barrier :
133157 def __init__ (self , num_threads ):
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 3?
1010Core and Builtins
1111-----------------
1212
13+ - Issue #14474: Save and restore exception state in thread.start_new_thread()
14+ while writing error message if the thread leaves a unhandled exception.
15+
1316- Issue #13019: Fix potential reference leaks in bytearray.extend(). Patch
1417 by Suman Saha.
1518
Original file line number Diff line number Diff line change @@ -1001,14 +1001,17 @@ t_bootstrap(void *boot_raw)
10011001 PyErr_Clear ();
10021002 else {
10031003 PyObject * file ;
1004+ PyObject * exc , * value , * tb ;
10041005 PySys_WriteStderr (
10051006 "Unhandled exception in thread started by " );
1007+ PyErr_Fetch (& exc , & value , & tb );
10061008 file = PySys_GetObject ("stderr" );
10071009 if (file != NULL && file != Py_None )
10081010 PyFile_WriteObject (boot -> func , file , 0 );
10091011 else
10101012 PyObject_Print (boot -> func , stderr , 0 );
10111013 PySys_WriteStderr ("\n" );
1014+ PyErr_Restore (exc , value , tb );
10121015 PyErr_PrintEx (0 );
10131016 }
10141017 }
You can’t perform that action at this time.
0 commit comments