
    dafq                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ  ej                   d      Z ej                   d      Zd d	lmZ d
 Zd Zd0dZe j.                  d        Z G d dej2                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! ejD                  d       G d d e             Z# G d! d"e      Z$ G d# d$e      Z% G d% d&e      Z& G d' d(e      Z' G d) d*e      Z( G d+ d,e      Z) G d- d.e      Z*e+d/k(  r ejX                          yy)1    N)dedent)support)import_helper)threading_helper)	os_helper_xxsubinterpreters_xxinterpchannels)interpretersc                     t        j                         \  }}| j                  dd      }t        d| d| d      }|t	        |d      fS )N
z
                z-
        import contextlib
        with open(zg, 'w', encoding='utf-8') as spipe:
            with contextlib.redirect_stdout(spipe):
                z	
        utf-8encoding)ospipereplacer   open)scriptrwindentedwrappeds        1/root/Python-3.12.4/Lib/test/test_interpreters.py_captured_scriptr      s^    779DAq~~d$89H # 
 		 G DW---    c                      t        j                         D ]#  } | j                  dk(  r	 | j                          % y # t        $ r Y 2w xY wNr   )r
   list_allidcloseRuntimeErrorinterps    r   clean_up_interpretersr$       sJ    '') 99>	LLN	
  		s   <	AAc                     t        |      \  }}|5  | j                  ||       |j                         cd d d        S # 1 sw Y   y xY w)Nchannels)r   runread)r#   requestr'   r   rpipes        r   _run_outputr,   *   sA    $W-MFE	 

6H
-zz|  s	   #>Ac              #   "   K   t        j                         \  } fd}t        j                  |      }|j	                          d  t        |d      5 }|j                  d       d d d        |j                          y # 1 sw Y   xY ww)Nc                  B     j                  t        d d             y )Nz7
            # wait for "signal"
            with open(z5) as rpipe:
                rpipe.read()
            )r(   r   )r#   r   s   r   r(   z_running.<locals>.run4   s*    

6 c   	r   targetr   done)r   r   	threadingThreadstartr   writejoin)r#   r   r(   tspiper   s   `    @r   _runningr9   1   sm     779DAq 	$AGGI		a FFFH s   ABB*BBBc                       e Zd Zd Zd Zy)TestBasec                 j    t        j                         \  fd}| j                  |       fS )Nc                      	 t        j                         	 t        j                          y # t        $ r Y "w xY w# t        $ r Y y w xY wN)r   r    	Exception)r   r   s   r   cleanupz!TestBase.os_pipe.<locals>.cleanupI   sJ        s   / > 	;;	A
	A
)r   r   
addCleanup)selfr@   r   r   s     @@r   os_pipezTestBase.os_pipeG   s.    wwy1	 	 !tr   c                     t                y r>   )r$   rB   s    r   tearDownzTestBase.tearDownU   s    r   N)__name__
__module____qualname__rC   rF    r   r   r;   r;   E   s     r   r;   c                   *    e Zd Zd Zd Zd Zd Zd Zy)CreateTestsc                     t        j                         }| j                  |t         j                         | j	                  |t        j
                                y r>   )r
   createassertIsInstanceInterpreterassertInr   rB   r#   s     r   test_in_mainzCreateTests.test_in_main[   s=    $$&fl&>&>?fl3356r   c                 "   t        j                         d fd}t        j                  |      }5  |j                          d d d        |j	                          | j                  t        j                                y # 1 sw Y   >xY w)Nc                  n    t        j                          j                          j                          y r>   )r
   rN   acquirerelease)r#   locks   r   fz%CreateTests.test_in_thread.<locals>.fc   s#    !((*FLLNLLNr   r/   )r2   Lockr3   r4   r6   rQ   r
   r   )rB   rY   r7   r#   rX   s      @@r   test_in_threadzCreateTests.test_in_thread`   sl    ~~	
 A& 	GGI		fl3356	 	s   BBc                    t        j                         \  }t        j                         }t        |t	        d            }t        j
                  t        |            }| j                  t        j                         |||g       y )Nz
            from test.support import interpreters
            interp = interpreters.create()
            print(interp.id)
            )r
   r   rN   r,   r   rP   intassertEqual)rB   mainr#   outinterp2s        r   test_in_subinterpreterz"CreateTests.test_in_subinterpretern   sn    %%'$$&&& * # 
 **3s84..042IJr   c                 d   t        t        j                               }g }t        d      D ]'  }t        j                         }|j                  |       ) |D ]  }|j                           t        j                         }| j                  t        t        j                               ||hz         y N   )setr
   r   rangerN   appendr    r^   )rB   before
interp_lst_interpsr#   s         r   test_after_destroy_allz"CreateTests.test_after_destroy_ally   s    \**,-
q 	'A"))+Gg&	' ! 	FLLN	 $$&\2245v7HIr   c                    t        t        j                               }t        j                         }t        j                         }t        j                         }|j	                          |j	                          t        j                         }| j                  t        t        j                               |||hz         y r>   )rf   r
   r   rN   r    r^   )rB   ri   interp1ra   interp3r#   s         r   test_after_destroy_somez#CreateTests.test_after_destroy_some   s    \**,-%%'%%'%%'$$&\2245v&@Q7QRr   N)rG   rH   rI   rS   r[   rb   rm   rq   rJ   r   r   rL   rL   Y   s    7
7	KJSr   rL   c                       e Zd Zd Zd Zy)GetCurrentTestsc                 x    t        j                         }t        j                         }| j                  ||       y r>   )r
   get_mainget_currentr^   )rB   r_   currents      r   	test_mainzGetCurrentTests.test_main   s.    $$&**,$'r   c                     t         j                         }t        j                         }t	        |t        d            }t        j                  t        |            }| j                  ||       y )Nz
            from test.support import interpreters
            cur = interpreters.get_current()
            print(cur.id)
            )	_interpretersru   r
   rN   r,   r   rP   r]   assertNotEqual)rB   r_   r#   r`   rw   s        r   test_subinterpreterz#GetCurrentTests.test_subinterpreter   s\    %%'$$&&& * # 
 **3s84GT*r   N)rG   rH   rI   rx   r|   rJ   r   r   rs   rs      s    (
	+r   rs   c                       e Zd Zd Zd Zd Zy)ListAllTestsc                 b    t        j                         }| j                  dt        |             y N   )r
   r   r^   len)rB   rl   s     r   test_initialzListAllTests.test_initial   s$    '')CL)r   c                 N   t        j                         }t        j                         }t        j                         }g }t        j                         D ]  }|j	                  |j
                          | j                  ||j
                  |j
                  |j
                  g       y r>   )r
   rv   rN   r   rh   r   r^   rB   r_   firstsecondidsr#   s         r   test_after_creatingz ListAllTests.test_after_creating   s~    '')##%$$&"++- 	"FJJvyy!	" 	tww&))<=r   c                 X   t        j                         }t        j                         }t        j                         }|j                          g }t        j                         D ]  }|j                  |j                          | j                  ||j                  |j                  g       y r>   )r
   rv   rN   r    r   rh   r   r^   r   s         r   test_after_destroyingz"ListAllTests.test_after_destroying   s    '')##%$$&"++- 	"FJJvyy!	" 	tww		23r   N)rG   rH   rI   r   r   r   rJ   r   r   r~   r~      s    *	>
4r   r~   c                       e Zd Zd Zd Zd Zd Z ej                  d      d        Z	 ej                  d      d        Z
d Z ej                  d      d	        Zd
 Zd Zd Zy)TestInterpreterAttrsc                 x   t        j                         }t        j                         }t        j                         }| j	                  |j
                  t        j                         | j	                  |j
                  t        j                         | j	                  |j
                  t        j                         y r>   )r
   ru   rv   rN   rO   r   rz   InterpreterID)rB   r_   rw   r#   s       r   test_id_typez!TestInterpreterAttrs.test_id_type   sz    $$&**,$$&dgg}'B'BCgjj-*E*EFfii)D)DEr   c                 d    t        j                         }| j                  |j                  d       y r   )r
   ru   r^   r   rB   r_   s     r   test_main_idz!TestInterpreterAttrs.test_main_id   s$    $$&!$r   c                     t        j                  d      }| j                  |j                  d       | j	                  t
              5  t        j                  d       d d d        y # 1 sw Y   y xY wNr   1)r
   rP   r^   r   assertRaises	TypeErrorrR   s     r   test_custom_idz#TestInterpreterAttrs.test_custom_id   sW    ))!,A&y) 	*$$S)	* 	* 	*   A&&A/c                     t        j                  d      }| j                  t              5  d|_        d d d        y # 1 sw Y   y xY wNr      )r
   rP   r   AttributeErrorr   rR   s     r   test_id_readonlyz%TestInterpreterAttrs.test_id_readonly   s<    ))!,~. 	FI	 	 		   <Aznot ready yet (see bpo-32604)c                 b    t        j                         }| j                  |j                         y r>   )r
   ru   assertFalseisolatedr   s     r   test_main_isolatedz'TestInterpreterAttrs.test_main_isolated   s"    $$&'r   c                 b    t        j                         }| j                  |j                         y r>   )r
   rN   r   r   rR   s     r   $test_subinterpreter_isolated_defaultz9TestInterpreterAttrs.test_subinterpreter_isolated_default   s"    $$&)r   c                     t        j                  d      }t        j                  d      }| j                  |j                         | j	                  |j                         y )NTr   F)r
   rN   
assertTruer   r   rB   ro   ra   s      r   %test_subinterpreter_isolated_explicitz:TestInterpreterAttrs.test_subinterpreter_isolated_explicit   sH    %%t4%%u5(()))*r   c                 d    t        j                  d      }| j                  |j                         y r   )r
   rP   r   r   rR   s     r   test_custom_isolated_defaultz1TestInterpreterAttrs.test_custom_isolated_default   s$    ))!,)r   c                     t        j                  dd      }t        j                  dd      }| j                  |j                         | j	                  |j                         y )Nr   Tr   F)r
   rP   r   r   r   r   s      r   test_custom_isolated_explicitz2TestInterpreterAttrs.test_custom_isolated_explicit   sL    **1t<**1u=(()))*r   c                     t        j                  d      }| j                  t              5  d|_        d d d        y # 1 sw Y   y xY w)Nr   T)r
   rP   r   r   r   rR   s     r   test_isolated_readonlyz+TestInterpreterAttrs.test_isolated_readonly   s<    ))!,~. 	#"FO	# 	# 	#r   c                     t        j                         }t        j                         }| j                  ||       | j                  ||       y r>   )r
   rN   r^   r{   r   s      r   test_equalityz"TestInterpreterAttrs.test_equality   s>    %%'%%''*GW-r   N)rG   rH   rI   r   r   r   r   unittestskipr   r   r   r   r   r   r   rJ   r   r   r   r      s    F%*
 X]]23( 4( X]]23* 4*+ X]]23* 4*+#
.r   r   c                   f    e Zd Zd Z ej
                  d      d        Zd Zd Zd Z	d Z
d Zd	 Zy
)TestInterpreterIsRunningc                 j    t        j                         }| j                  |j                                y r>   )r
   ru   r   
is_runningr   s     r   rx   z"TestInterpreterIsRunning.test_main	  s#    $$&)*r   Fails on FreeBSDc                 &   t        j                         }| j                  |j                                t	        |      5  | j                  |j                                d d d        | j                  |j                                y # 1 sw Y   )xY wr>   )r
   rN   r   r   r9   r   rR   s     r   r|   z,TestInterpreterIsRunning.test_subinterpreter  sp    $$&**,-f 	1OOF--/0	1**,-	1 	1s    BBc                    | j                         \  }}t        j                         }|j                  d| d       | j	                  |j                                | j                  t        j                  |d      d       y )Nz4if True:
            import os
            os.write(z, b'x')
            r      x)	rC   r
   rN   r(   r   r   r^   r   r)   )rB   r   r   r#   s       r   test_finishedz&TestInterpreterIsRunning.test_finished  sr    ||~1$$&

 S  	 	**,-A-r   c                     t        j                         }t        |t        d|j                   d            }| j                  |j                         d       y )Nz`
            import _xxsubinterpreters as _interpreters
            if _interpreters.is_running(zZ):
                print(True)
            else:
                print(False)
            True)r
   rN   r,   r   r   r^   strip)rB   r#   r`   s      r   test_from_subinterpreterz1TestInterpreterIsRunning.test_from_subinterpreter   sS    $$&&& .))/ 4* #  	f-r   c                     t        j                         }|j                          | j                  t              5  |j                          d d d        y # 1 sw Y   y xY wr>   )r
   rN   r    r   r!   r   rR   s     r   test_already_destroyedz/TestInterpreterIsRunning.test_already_destroyed+  sH    $$&|, 	 	  	  	    AAc                     t        j                  d      }| j                  t              5  |j	                          d d d        y # 1 sw Y   y xY wN@B )r
   rP   r   r!   r   rR   s     r   test_does_not_existz,TestInterpreterIsRunning.test_does_not_exist1  sA    )))4|, 	 	  	  	    AAc                     t        j                  d      }| j                  t              5  |j	                          d d d        y # 1 sw Y   y xY wN)r
   rP   r   
ValueErrorr   rR   s     r   test_bad_idz$TestInterpreterIsRunning.test_bad_id6  sA    ))"-z* 	 	  	  	 r   c                    | j                         \  }}| j                         \  }}d}d}t        j                         }|j                  d| d|d| d|d	       | j	                  |j                                t        j                  ||       |j                  d       | j                  t        j                  |d	      |       y )
N   D   Fqif True:
            import os
            import threading

            def task():
                v = os.read(!, 1)
                assert v == 
                os.write(, R)
            t = threading.Thread(target=task)
            t.start()
            t.join()r   )
rC   r
   rN   r(   r   r   r   r5   r^   r)   )rB   r_interpw_interpr_threadw_threadDONEFINISHEDr#   s           r   !test_with_only_background_threadsz:TestInterpreterIsRunning.test_with_only_background_threads;  s    !\\^(!\\^($$&

 
 &J '!H %"2h\ 2
 
	 	**,-
4 

:1-x8r   N)rG   rH   rI   rx   r   r   r|   r   r   r   r   r   r   rJ   r   r   r   r     sE    + X]]%&. '..	.  
 
9r   r   c                   x    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Z ej                  d
      d        Zd Zy)TestInterpreterClosec                    t        j                         }t        j                         }t        j                         }t        j                         }| j                  t	        t        j
                               ||||h       |j                          | j                  t	        t        j
                               |||h       y r>   )r
   ru   rN   r^   rf   r   r    )rB   r_   ro   ra   rp   s        r   
test_basiczTestInterpreterClose.test_basicW  s    $$&%%'%%'%%'\2245':	<\22451	3r   c                    t        t        j                               }t               }t        d      D ]'  }t        j                         }|j                  |       ) | j                  t        t        j                               ||z         |D ]  }|j                           | j                  t        t        j                               |       y rd   )rf   r
   r   rg   rN   addr^   r    )rB   ri   rl   rk   r#   s        r   test_allzTestInterpreterClose.test_allb  s    \**,-%q 	 A!((*FKK	  	\2245v7GH 	FLLN	\2245v>r   c                      t        j                         \   j                  t              5  j	                          d d d         fd}t        j                  |      }|j                          |j                          y # 1 sw Y   FxY w)Nc                  z    j                  t              5   j                          d d d        y # 1 sw Y   y xY wr>   )r   r!   r    )r_   rB   s   r   rY   z)TestInterpreterClose.test_main.<locals>.fr  s/    ""<0 

  s   1:r/   )	r
   r   r   r!   r    r2   r3   r4   r6   )rB   rY   r7   r_   s   `  @r   rx   zTestInterpreterClose.test_mainm  sh    %%'|, 	JJL		 A&				 	s   BBc                     t        j                         }|j                          | j                  t              5  |j                          d d d        y # 1 sw Y   y xY wr>   )r
   rN   r    r   r!   rR   s     r   r   z+TestInterpreterClose.test_already_destroyedz  sE    $$&|, 	LLN	 	 	r   c                     t        j                  d      }| j                  t              5  |j	                          d d d        y # 1 sw Y   y xY wr   )r
   rP   r   r!   r    rR   s     r   r   z(TestInterpreterClose.test_does_not_exist  s>    )))4|, 	LLN	 	 	r   c                     t        j                  d      }| j                  t              5  |j	                          d d d        y # 1 sw Y   y xY wr   )r
   rP   r   r   r    rR   s     r   r   z TestInterpreterClose.test_bad_id  s>    ))"-z* 	LLN	 	 	r   c           	      N   t        j                         \  }t        j                         }t        |t	        dt        |j                         d            }| j                  |j                         d       | j                  t        t        j                               ||h       y )Nza
            from test.support import interpreters
            interp = interpreters.Interpreter(z)
            try:
                interp.close()
            except RuntimeError:
                print('failed')
            failed)
r
   r   rN   r,   r   r]   r   r^   r   rf   )rB   r_   r#   r`   s       r   test_from_currentz&TestInterpreterClose.test_from_current  s    %%'$$&&& .//2699~.> ?* #  	h/\2245f~Fr   c                    t        j                         \  }t        j                         }t        j                         }| j                  t	        t        j                               |||h       |j                  t        d|j                   d             | j                  t	        t        j                               ||h       y )Nzf
            from test.support import interpreters
            interp2 = interpreters.Interpreter(int(zs))
            interp2.close()
            interp3 = interpreters.create()
            interp3.close()
            )r
   r   rN   r^   rf   r(   r   r   )rB   r_   ro   ra   s       r   test_from_siblingz&TestInterpreterClose.test_from_sibling  s    %%'%%'%%'\22451	3F  44;JJ< @  	 	\2245gGr   c                     t        j                         fd}t        j                  |      }|j	                          |j                          y )Nc                  &     j                          y r>   )r    r"   s   r   rY   z6TestInterpreterClose.test_from_other_thread.<locals>.f  s    LLNr   r/   )r
   rN   r2   r3   r4   r6   )rB   rY   r7   r#   s      @r   test_from_other_threadz+TestInterpreterClose.test_from_other_thread  s:    $$&	 A&			r   r   c                 J   t        j                         \  }t        j                         }t        |      5  | j	                  t
              5  |j                          d d d        | j                  |j                                d d d        y # 1 sw Y   1xY w# 1 sw Y   y xY wr>   )	r
   r   rN   r9   r   r!   r    r   r   )rB   r_   r#   s      r   test_still_runningz'TestInterpreterClose.test_still_running  s    %%'$$&f 	1""<0 OOF--/0	1 	1 	1 	1s#   BB'BB	BB"c                    | j                         \  }}| j                         \  }}d}t        j                         }|j                  d| d|d       |j	                          | j                  t        j                  |d      |       y )Nr   a  if True:
            import os
            import threading
            import time

            done = False

            def notify_fini():
                global done
                done = True
                t.join()
            threading._register_atexit(notify_fini)

            def task():
                while not done:
                    time.sleep(0.1)
                os.write(r   r   r   )rC   r
   rN   r(   r    r^   r   r)   )rB   r   r   r   r   r   r#   s          r   test_subthreads_still_runningz2TestInterpreterClose.test_subthreads_still_running  s    !\\^(!\\^($$&

   #2h\ 2! 	( 	1-x8r   N)rG   rH   rI   r   r   rx   r   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   U  sV    	3	?

GH X]]%&1 '19r   r   c                       e Zd Zd Zd Z ej                         d        Z ej                  d      d        Z
d Zd Zd Zd	 Zd
 Zy)TestInterpreterRunc                     t        j                         }t        d      \  }}|5  |j                  |       |j	                         }d d d        | j                  d       y # 1 sw Y   xY w)Nprint("it worked!", end="")
it worked!)r
   rN   r   r(   r)   r^   )rB   r#   r   filer`   s        r   test_successzTestInterpreterRun.test_success  s`    $$&'(EF 	JJv))+C	 	l+		 	s   "A""A+c                 6   t        j                         t        d      \  }|5  fd}t        j                  |      }|j                          |j                          |j                         }d d d        | j                  d       y # 1 sw Y   xY w)Nr   c                  (     j                         y r>   )r(   )r#   r   s   r   rY   z,TestInterpreterRun.test_in_thread.<locals>.f  s    

6"r   r/   r   )	r
   rN   r   r2   r3   r4   r6   r)   r^   )rB   r   rY   r7   r`   r#   r   s        @@r   r[   z!TestInterpreterRun.test_in_thread  s~    $$&'(EF 	#   *AGGIFFH))+C	 	l+	 	s   ABBc                    t        j                         }dd l}|j                  dd      5 }|j	                  d       |j                          d}t        d|j                   d| d	      }|j                  |       |j                  d       |j                         }| j                  ||       d d d        y # 1 sw Y   y xY w)
Nr   zw+r   r    zspam spam spam spam spamz
                import os
                try:
                    os.fork()
                except RuntimeError:
                    with open('zE', 'w', encoding='utf-8') as out:
                        out.write('z')
                )r
   rN   tempfileNamedTemporaryFiler5   flushr   namer(   seekr)   r^   )rB   r#   r  r   expectedr   contents          r   	test_forkzTestInterpreterRun.test_fork  s    $$&(((@ 	0DJJrNJJL1H ! 
 !%		{ +$$,: . F JJvIIaLiikGWh/#	0 	0 	0s   BB99Cr   c                     t        j                         }t        |      5  | j                  t              5  |j                  d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nprint("spam"))r
   rN   r9   r   r!   r(   rR   s     r   test_already_runningz'TestInterpreterRun.test_already_running	  sa    $$&f 	,""<0 ,

?+,	, 	,, ,	, 	,s"   A%AA%A"	A%%A.c                     t        j                  d      }| j                  t              5  |j	                  d       d d d        y # 1 sw Y   y xY w)Nr   r  )r
   rP   r   r!   r(   rR   s     r   r   z&TestInterpreterRun.test_does_not_exist  sA    )))4|, 	(JJ'	( 	( 	(   AAc                     t        j                  d      }| j                  t              5  |j	                  d       d d d        y # 1 sw Y   y xY w)Nr   r  )r
   rP   r   r   r(   rR   s     r   r   zTestInterpreterRun.test_bad_id  sA    ))"-z* 	(JJ'	( 	( 	(r  c                     t        j                         }| j                  t              5  |j	                  d       d d d        y # 1 sw Y   y xY w)N
   r
   rN   r   r   r(   rR   s     r   test_bad_scriptz"TestInterpreterRun.test_bad_script  s>    $$&y) 	JJrN	 	 	   AAc                     t        j                         }| j                  t              5  |j	                  d       d d d        y # 1 sw Y   y xY w)Ns   print("spam")r  rR   s     r   test_bytes_for_scriptz(TestInterpreterRun.test_bytes_for_script  s@    $$&y) 	)JJ'(	) 	) 	)r  c                 .   | j                         \  }}| j                         \  }}d}d}d}t        j                         }|j                  d| d|d| d|d| d|d	       |j                  d
| d|d	       t	        j
                  ||       |j                  d       | j                  t	        j                  |d      |       | j                  t	        j                  |d      |       | j                  t	        j                  |d      |       y )N   Rr   r   r   r   r   r   z[)
            t = threading.Thread(target=task)
            t.start()
            os.write(z)
            zif True:
            os.write(r   r   )rC   r
   rN   r(   r   r5   r^   r)   )	rB   r   r   r   r   RANr   r   r#   s	            r   *test_with_background_threads_still_runningz=TestInterpreterRun.test_with_background_threads_still_running$  s"   !\\^(!\\^($$&

 
 &J '!H %"2h\ 2 Zr# ) 	 	

 Zr# ) 	 	4 

:1-s31-s31-x8r   N)rG   rH   rI   r   r[   r   requires_forkr
  r   r   r  r   r   r  r  r  rJ   r   r   r   r     sd    ,, W0 0, X]]%&, ',(
(

)
9r   r   zNthese are crashing, likely just due just to _xxsubinterpreters (see gh-105699)c                   l    e Zd Z ej                  d      d        Z ej                  d      d        Zy)StressTestscpuc                 r    g }t        d      D ]'  }t        j                         }|j                  |       ) y )Nd   )rg   r
   rN   rh   )rB   aliverk   r#   s       r   test_create_many_sequentialz'StressTests.test_create_many_sequentialL  s4    s 	!A!((*FLL 	!r   c                     g fdfdt        d      D        }t        j                  |      5  	 d d d        y # 1 sw Y   y xY w)Nc                  P    t        j                         } j                  |        y r>   )r
   rN   rh   )r#   r"  s    r   taskz3StressTests.test_create_many_threaded.<locals>.taskV  s    !((*FLL r   c              3   J   K   | ]  }t        j                           yw)r/   N)r2   r3   ).0rk   r&  s     r   	<genexpr>z8StressTests.test_create_many_threaded.<locals>.<genexpr>Y  s     EQ9##400Es    #   )rg   r   start_threads)rB   threadsr"  r&  s     @@r   test_create_many_threadedz%StressTests.test_create_many_threadedS  sD    	! F%*E++G4 		 	 	s	   >AN)rG   rH   rI   r   requires_resourcer#  r-  rJ   r   r   r  r  F  sD     Wu%! &! Wu% &r   r  c                        e Zd ZdZej
                   fd       ZdddZdZdZ	d Z
d Zd	 Z ej                         dd
d       Zd Z xZS )StartupTestsr   c              '     K   t        |   | 5 }| xj                  dz  c_        	 | | j                  r(| j                  dk(  rt	        dd       nt	        d       d| _        	 d d d        y # | j                  r/| j                  dk(  rt	        dd       d| _        w t	        d       d| _        w xY w# 1 sw Y   y xY ww)Nr   z### end subtest debug ###r  )endF)supersubTest_subtest_count_debugged_in_subtestprint)rB   argsctx	__class__s      r   r4  zStartupTests.subTeste  s     W_d# 	2s1$
2	,,**a/ 9rB9:,1)	2 	2
 ,,**a/ 9rB -2) 9:,1)	2 	2s3   CB7A0;B7'	C0AB44B77C <CNheaderc                X   |r| j                  d| d       |rn|j                  t        j                        r-| j                  |d t	        t        j                                 n"| j                  |       | j                  d       | j                  d       y | j                  |       y )Nz--- z ---z<no newline>------)_debugendswithr   linesepr   )rB   msgr<  s      r   debugzStartupTests.debugu  s|    KK$vhd+,<<

+KK$5c"**o%5 67KK$KK/KK!KKr   Fc                     | j                   st                d| _         | j                  +	 | j                  sd| _        t        d       t        |       y t        |       y )NTz### start subtest debug ###)	_debuggedr7  _subtestr6  )rB   rB  s     r   r?  zStartupTests._debug  sJ    ~~G!DN==$0004D-78c
#Jr   c                     dd l }|j                  d      }t        j                  j	                  |      }| j                  t        j                  |       |S )Nr   test_interpreters_)prefix)r  mkdtempr   pathrealpathrA   r   rmtree)rB   r  tmps      r   create_temp_dirzStartupTests.create_temp_dir  sF    &:;ggs#	((#.
r   c                *   t        j                  j                  | }t         j                  j                  |      }|rt        j                  |d       t        |dd      5 }|j                  t        |             d d d        |S # 1 sw Y   |S xY w)NT)exist_okr   r   r   )r   rK  r6   dirnamemakedirsr   r5   r   )rB   textrK  filenamerR  outfiles         r   write_scriptzStartupTests.write_script  so    77<<&''//(+KK$/(C'2 	(gMM&,'	(	(s   #BBcwdc                   dd l }dd l}t        |t              r|j	                  |      }t
        j                  g|}	 |j                  ||dd      }|j                   dk(  s |j"                  dk7  sJ |j                          |j"                  dk7  rqt$        j&                  ra| j                  d	|j                  |d
d         d       | j                  |j(                  d       | j                  |j                   d       | j+                  |j"                  d       | j+                  |j                   d       |j(                  S # t        $ r}| j                  d|j                  |              t        |t              rL|j                  s@t        j                  j                  |d         rd}nd}| j                  |d    d|         d }~ww xY w)Nr   T)rY  capture_outputrT  z# cmd: existszdoes not exist r  z
# python3 r   z failed:stdoutr;  stderr)shlex
subprocess
isinstancestrsplitsys
executabler(   r?   rC  r6   FileNotFoundErrorrU  r   rK  r\  r_  
returncoder   verboser^  r^   )rB   argvrY  r`  ra  procexcr\  s           r   
run_pythonzStartupTests.run_python  s    	dC ;;t$D&&	>>#	 " D {{b DOOq$8E$++E8??aGOOJJEJJtABx$8#9BCJJt{{8J4JJt{{8J4!,b){{!  	JJD!1 234#01#,,77>>$q'*%F-F

d1gYax01	s   E 	GB GGc                    d}| j                         }| j                  |ddd       | j                  |dd|       | j                  |dd|       | j                  |d|       d|fd|fd	|fd
|fdg}|D ]x  \  }}| j                  d|       5  | j                  ||      }t	        j
                  |      }|d   |d   }	}| j                  |	|       | j                  |	|       d d d        z y # 1 sw Y   xY w)Na  
            import sys
            from test.support import interpreters

            orig = sys.path[0]

            interp = interpreters.create()
            interp.run(f"""if True:
                import json
                import sys
                print(json.dumps({{
                    'main': {orig!r},
                    'sub': sys.path[0],
                }}, indent=4), flush=True)
                """)
            pkgz__init__.pyr  )rT  z__main__.pyz	script.pyz	-m scriptz-m pkgz-m pkg.script)z-c "import script"r  zpython3 rX  r_   sub)rO  rW  r4  rm  jsonloadsr^   )
rB   r   rY  casesrj  r  r`   datasp0_mainsp0_subs
             r   test_sys_path_0zStartupTests.test_sys_path_0  s0   , ""$#um"=#um&A#uk?#{8 ##sOc"&
 $ 	4ND(/0 4oodo4zz#$(L$u+'  (3  (34 4	44 4s   AC33C<	)rG   rH   rI   r5  
contextlibcontextmanagerr4  rC  rE  r6  r?  rO  rW  r   requires_subprocessrm  rw  __classcell__)r:  s   @r   r0  r0  ^  sp    
 N2 2 $(  I  !W  "&*  #B+4r   r0  c                       e Zd Zd Zy)FinalizationTestsc                    dd l }t        j                  ddg}|j                  |dd      }| j	                  d|j
                         |j                  dk(  r`t        j                  rPt                t        d       t        d|j                          t        d	|j
                          t        d
       | j                  |j                  d       y )Nr   z-czif True:
            import _xxsubinterpreters as _interpreters
            interpid = _interpreters.create()
            raise Exception
            T)r[  rT  	Tracebackz --- cmd unexpected succeeded ---zstdout:
zstderr:
r>  r   )ra  re  rf  r(   rQ   r_  rh  r   ri  r7  r^  r^   )rB   ra  rj  rk  s       r   test_gh_109793z FinalizationTests.test_gh_109793  s     ' 
 ~~d4d~Ck4;;/??aGOOG45Idkk]+,Idkk]+,(O!,r   N)rG   rH   rI   r  rJ   r   r   r}  r}    s    -r   r}  c                       e Zd Zd Zd Zy)TestIsShareablec                     g d}|D ]B  }| j                  |      5  t        j                  |      }| j                  |       d d d        D y # 1 sw Y   OxY w)N)N   spamspamr  i)r4  r
   is_shareabler   )rB   
shareablesobj	shareables       r   test_default_shareablesz'TestIsShareable.test_default_shareables  s\    
  	+Cc" +(55c:		*+ +	++ +s   'AA	c                 V    G d d      } G d dt               }ddt        dt        t        t               t	               d| |d	       |d
      g}|D ]I  }| j                  t        |            5  | j                  t        j                  |             d d d        K y # 1 sw Y   VxY w)Nc                       e Zd Zd Zd Zy)2TestIsShareable.test_not_shareable.<locals>.Cheesec                     || _         y r>   r  )rB   r  s     r   __init__z;TestIsShareable.test_not_shareable.<locals>.Cheese.__init__  s	     	r   c                     | j                   S r>   r  rE   s    r   __str__z:TestIsShareable.test_not_shareable.<locals>.Cheese.__str__  s    yy r   N)rG   rH   rI   r  r  rJ   r   r   Cheeser    s    !!r   r  c                       e Zd ZdZy)4TestIsShareable.test_not_shareable.<locals>.SubByteszA subclass of a shareable type.N)rG   rH   rI   __doc__rJ   r   r   SubBytesr    s    1r   r  TF.g      Y@Wensleydaler  )
bytesNotImplementedtypeobjectr?   r4  reprr   r
   r  )rB   r  r  not_shareablesr  s        r   test_not_shareablez"TestIsShareable.test_not_shareable  s    	! 	!	2u 	2
 }%!" " 	4Cd3i( 4   --c244 4	44 4s   /%BB(	N)rG   rH   rI   r  r  rJ   r   r   r  r    s    +4r   r  c                       e Zd Zd Zd Zy)TestChannelsc                     t        j                         \  }}| j                  |t         j                         | j                  |t         j                         y r>   )r
   create_channelrO   RecvChannelSendChannel)rB   r   ss      r   test_createzTestChannels.test_create9  s@    **,1a!9!9:a!9!9:r   c                 (   | j                  t        j                         g        t               }t	        d      D ]'  }t        j
                         }|j                  |       ) t        t        j                               }| j                  ||       y rd   )r^   r
   list_all_channelsrf   rg   r  r   )rB   createdrk   chafters        r   test_list_allzTestChannels.test_list_all>  ss    7792>%q 	A,,.BKKO	 L2245(r   N)rG   rH   rI   r  r  rJ   r   r   r  r  7  s    ;
)r   r  c                   $    e Zd Zd Zd Zd Zd Zy)TestRecvChannelAttrsc                     t        j                         \  }}| j                  |j                  t        j
                         y r>   r
   r  rO   r   	_channels	ChannelID)rB   rchrk   s      r   r   z!TestRecvChannelAttrs.test_id_typeJ  s.    ,,.Qcffi&9&9:r   c                     t        j                  d      }| j                  |j                  d       | j	                  t
              5  t        j                  d       d d d        y # 1 sw Y   y xY wr   )r
   r  r^   r   r   r   rB   r  s     r   r   z#TestRecvChannelAttrs.test_custom_idN  W    &&q)#y) 	*$$S)	* 	* 	*r   c                     t        j                  d      }| j                  t              5  d|_        d d d        y # 1 sw Y   y xY wr   )r
   r  r   r   r   r  s     r   r   z%TestRecvChannelAttrs.test_id_readonlyU  <    &&q)~. 	CF	 	 	r   c                     t        j                         \  }}t        j                         \  }}| j                  ||       | j                  ||       y r>   r
   r  r^   r{   )rB   ch1rk   ch2s       r   r   z"TestRecvChannelAttrs.test_equalityZ  sF    ,,.Q,,.Qc"C%r   NrG   rH   rI   r   r   r   r   rJ   r   r   r  r  H      ;*
&r   r  c                   $    e Zd Zd Zd Zd Zd Zy)TestSendChannelAttrsc                     t        j                         \  }}| j                  |j                  t        j
                         y r>   r  )rB   rk   schs      r   r   z!TestSendChannelAttrs.test_id_typec  s.    ,,.3cffi&9&9:r   c                     t        j                  d      }| j                  |j                  d       | j	                  t
              5  t        j                  d       d d d        y # 1 sw Y   y xY wr   )r
   r  r^   r   r   r   rB   r  s     r   r   z#TestSendChannelAttrs.test_custom_idg  r  r   c                     t        j                  d      }| j                  t              5  d|_        d d d        y # 1 sw Y   y xY wr   )r
   r  r   r   r   r  s     r   r   z%TestSendChannelAttrs.test_id_readonlyn  r  r   c                     t        j                         \  }}t        j                         \  }}| j                  ||       | j                  ||       y r>   r  )rB   rk   r  r  s       r   r   z"TestSendChannelAttrs.test_equalitys  sF    ,,.3,,.3c"C%r   Nr  rJ   r   r   r  r  a  r  r   r  c                       e Zd Zd Zd Z ej                  d      d        Zd Zd Z	d Z
d Z ej                  d      d	        Zd
 Zd Zd Zd Zd Zd Zy)TestSendRecvc                     t        j                         \  }}d}|j                  |       |j                         }| j	                  ||       | j                  ||       y Nr  )r
   r  send_nowaitrecvr^   assertIsNotrB   r   r  origr  s        r   test_send_recv_mainz TestSendRecv.test_send_recv_main|  sP    **,1	dffhd#d#r   c                 `    t        j                         }|j                  t        d             y )Na>  
            from test.support import interpreters
            r, s = interpreters.create_channel()
            orig = b'spam'
            s.send_nowait(orig)
            obj = r.recv()
            assert obj == orig, 'expected: obj == orig'
            assert obj is not orig, 'expected: obj is not orig'
            r
   rN   r(   r   rR   s     r   test_send_recv_same_interpreterz,TestSendRecv.test_send_recv_same_interpreter  s(    $$&

6   	r   zbroken (see BPO-...)c           	         t        j                         \  }}t        j                         \  }}d}|j                  |       t        t        j                         t        dt        |       d      t        ||            }|j                         }| j                  |d       | j                  t        |      t        |             y )Nr  z
                obj1 = r.recv()
                assert obj1 == b'spam', 'expected: obj1 == orig1'
                # When going to another interpreter we get a copy.
                assert id(obj1) != , 'expected: obj1 is not orig1'
                orig2 = b'eggs'
                print(id(orig2))
                s.send_nowait(orig2)
                r   r  r&      eggs)r
   r  r  r,   rN   r   r   dictr  r^   r{   r]   rB   r1s1r2s2orig1r`   obj2s           r   %test_send_recv_different_interpretersz2TestSendRecv.test_send_recv_different_interpreters  s    ,,.B,,.B
u! $ %'uI; /	  B"% wwyw'BtHc#h/r   c                 <   t        j                         \  fd}t        j                  |      }|j	                          d}j                  |       |j                          j                         }| j                  ||       | j                  ||       y )Nc                      	 	 j                         } 	 j                  |        y # t        j                  $ r t        j                  d       Y nw xY wS)Ng?)r  r
   ChannelEmptyErrortimesleepsend)r  r   r  s    r   rY   z8TestSendRecv.test_send_recv_different_threads.<locals>.f  sM    $&&(C FF3K $55 $JJsO$	 s   ' (AAr/   r  )
r
   r  r2   r3   r4   r  r6   r  r^   r  )rB   rY   r7   r  r  r   r  s        @@r    test_send_recv_different_threadsz-TestSendRecv.test_send_recv_different_threads  sy    **,1	 A&			t	ffhd#d#r   c                     t        j                         \  }}d}|j                  |       |j                         }| j	                  ||       | j                  ||       y r  )r
   r  r  recv_nowaitr^   r  r  s        r   test_send_recv_nowait_mainz'TestSendRecv.test_send_recv_nowait_main  sP    **,1	dmmod#d#r   c                 v    t        j                         \  }}|j                  d       }| j                  |       y r>   )r
   r  r  assertIsNone)rB   r   rk   r  s       r   'test_send_recv_nowait_main_with_defaultz4TestSendRecv.test_send_recv_nowait_main_with_default  s0    **,1mmD!#r   c                 `    t        j                         }|j                  t        d             y )Na  
            from test.support import interpreters
            r, s = interpreters.create_channel()
            orig = b'spam'
            s.send_nowait(orig)
            obj = r.recv_nowait()
            assert obj == orig, 'expected: obj == orig'
            # When going back to the same interpreter we get the same object.
            assert obj is not orig, 'expected: obj is not orig'
            r  rR   s     r   &test_send_recv_nowait_same_interpreterz3TestSendRecv.test_send_recv_nowait_same_interpreter  s(    $$&

6 	 	 		r   c           	         t        j                         \  }}t        j                         \  }}d}|j                  |       t        t        j                         t        dt        |       d      t        ||            }|j                         }| j                  |d       | j                  t        |      t        |             y )Nr  z
                obj1 = r.recv_nowait()
                assert obj1 == b'spam', 'expected: obj1 == orig1'
                # When going to another interpreter we get a copy.
                assert id(obj1) != r  r  r&   r  )r
   r  r  r,   rN   r   r   r  r  r^   r{   r]   r  s           r   ,test_send_recv_nowait_different_interpretersz9TestSendRecv.test_send_recv_nowait_different_interpreters  s    ,,.B,,.B
u! $ %'uI; /	  B"% ~~w'BtHc#h/r   c                     t        j                  d      }| j                  t         j                        5  |j	                          d d d        y # 1 sw Y   y xY wr   )r
   r  r   ChannelNotFoundErrorr  rB   r  s     r    test_recv_channel_does_not_existz-TestSendRecv.test_recv_channel_does_not_exist  sD    %%i0|@@A 	GGI	 	 	   AAc                     t        j                  d      }| j                  t         j                        5  |j	                  d       d d d        y # 1 sw Y   y xY wNr   r  )r
   r  r   r  r  r  s     r    test_send_channel_does_not_existz-TestSendRecv.test_send_channel_does_not_exist  sG    %%i0|@@A 	GGG	 	 	   AAc                     t        j                  d      }| j                  t         j                        5  |j	                          d d d        y # 1 sw Y   y xY wr   )r
   r  r   r  r  r  s     r   'test_recv_nowait_channel_does_not_existz4TestSendRecv.test_recv_nowait_channel_does_not_exist  sE    %%i0|@@A 	NN	 	 	r  c                     t        j                  d      }| j                  t         j                        5  |j	                  d       d d d        y # 1 sw Y   y xY wr  )r
   r  r   r  r  r  s     r   'test_send_nowait_channel_does_not_existz4TestSendRecv.test_send_nowait_channel_does_not_exist  sG    %%i0|@@A 	$NN7#	$ 	$ 	$r  c                     t        j                         \  }}| j                  t         j                        5  |j	                          d d d        y # 1 sw Y   y xY wr>   )r
   r  r   r  r  )rB   r  rk   s      r   test_recv_nowait_emptyz#TestSendRecv.test_recv_nowait_empty  sG    ++-A|==> 	NN	 	 	s   AAc                 p   t               }t        j                         \  }}|j                  |      }|j	                  d        |j	                  d       |j	                  d       |j	                  d       |j                  |      }|j                  |      }|j                         }|j                  |      }|j                  |      }	| j                  ||       | j                  |d        | j                  |d       | j                  |d       | j                  |d       | j                  |	|       y )Nr   r  r  )r  r
   r  r  r  assertIsr^   )
rB   defaultr  r  obj1r  obj3obj4obj5obj6s
             r   test_recv_nowait_defaultz%TestSendRecv.test_recv_nowait_default  s    (..0Sw'  w'w' w'w'dG$dD!q!w'w'dG$r   N)rG   rH   rI   r  r  r   r   r  r  r  r  r  r  r  r  r  r  r  r
  rJ   r   r   r  r  z  s~    $
 X]])*0 +0.$,$ X]])*0 +0.


$

%r   r  __main__r>   )-rx  rq  r   os.pathre  r2   textwrapr   r   r  testr   test.supportr   r   r   import_modulerz   r  r
   r   r$   r,   ry  r9   TestCaser;   rL   rs   r~   r   r   r   r   r   r  r0  r}  r  r  r  r  r  rG   r_   rJ   r   r   <module>r     s     	  
      & ) "+++,@A'M''(;<	 %	.  & x    (9S( 9Sx+h +&48 4<>.8 >.BK9x K9\A98 A9Hh9 h9Z _`(  a.P48 P4f- -(/4h /4d)8 )"&8 &2&8 &2e%8 e%P zHMMO r   