@@ -1143,21 +1143,21 @@ def import_threading():
11431143 self .assertEqual (out , b'' )
11441144 self .assertEqual (err , b'' )
11451145
1146- def test_start_new_thread_at_exit (self ):
1146+ def test_start_new_thread_at_finalization (self ):
11471147 code = """if 1:
1148- import atexit
11491148 import _thread
11501149
11511150 def f():
11521151 print("shouldn't be printed")
11531152
1154- def exit_handler():
1155- _thread.start_new_thread(f, ())
1156-
1157- atexit.register(exit_handler)
1153+ class AtFinalization:
1154+ def __del__(self):
1155+ print("OK")
1156+ _thread.start_new_thread(f, ())
1157+ at_finalization = AtFinalization()
11581158 """
11591159 _ , out , err = assert_python_ok ("-c" , code )
1160- self .assertEqual (out , b'' )
1160+ self .assertEqual (out . strip () , b"OK" )
11611161 self .assertIn (b"can't create new thread at interpreter shutdown" , err )
11621162
11631163class ThreadJoinOnShutdown (BaseTestCase ):
@@ -1281,6 +1281,30 @@ def main():
12811281 rc , out , err = assert_python_ok ('-c' , script )
12821282 self .assertFalse (err )
12831283
1284+ def test_thread_from_thread (self ):
1285+ script = """if True:
1286+ import threading
1287+ import time
1288+
1289+ def thread2():
1290+ time.sleep(0.05)
1291+ print("OK")
1292+
1293+ def thread1():
1294+ time.sleep(0.05)
1295+ t2 = threading.Thread(target=thread2)
1296+ t2.start()
1297+
1298+ t = threading.Thread(target=thread1)
1299+ t.start()
1300+ # do not join() -- the interpreter waits for non-daemon threads to
1301+ # finish.
1302+ """
1303+ rc , out , err = assert_python_ok ('-c' , script )
1304+ self .assertEqual (err , b"" )
1305+ self .assertEqual (out .strip (), b"OK" )
1306+ self .assertEqual (rc , 0 )
1307+
12841308 @skip_unless_reliable_fork
12851309 def test_reinit_tls_after_fork (self ):
12861310 # Issue #13817: fork() would deadlock in a multithreaded program with
0 commit comments