
    F\h&1                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  S SKJr  SqS r " S S\ R                  " SS5      5      r\
R"                  " 5       S	 5       r\
R"                  " 5       S
 5       rS rS r\
R"                  " 5       \R,                  \R.                  S.S j5       rS rSS jrSS jrSS jr SS jr\
R"                  " 5       S 5       rg)    N)source_from_cache)support)make_legacy_pycc                     [         c`  S[        R                  ;   a  Sq g[        R                  (       d  Sq g [
        R                  " [        R                  SSS/5        Sq [         $ [         $ ! [
        R                   a
    Sq  [         $ f = f)a  
Returns True if our sys.executable interpreter requires environment
variables in order to be able to run at all.

This is designed to be used with @unittest.skipIf() to annotate tests
that need to use an assert_python*() function to launch an isolated
mode (-I) or no environment mode (-E) sub-interpreter process.

A normal build & test does not run into this situation but it can happen
when trying to run the standard library test suite from an interpreter that
doesn't have an obvious home with Python's current home finding logic.

Setting PYTHONHOME is one way to get most of the testsuite to run in that
situation.  PYTHONPATH or PYTHONUSERSITE are other common environment
variables that might impact whether or not the interpreter can start.

PYTHONHOMETF-Ez-czimport sys; sys.exit(0))
$__cached_interp_requires_environmentosenvironr   has_subprocess_support
subprocess
check_callsys
executableCalledProcessError     1/usr/lib/python3.13/test/support/script_helper.py interpreter_requires_environmentr      s    $ ,32::%370--380	9!!3>>4#')B#D E
 490//// ,, 	8370 0/	8s   (A. .BBc                       \ rS rSrSrS rSrg)_PythonRunResult=   z2Helper for reporting Python subprocess run resultsc                 H   SnU R                   U R                  pC[        U5      U:  a  SX2* S -   n[        U5      U:  a  SXB* S -   nUR                  SS5      R	                  5       nUR                  SS5      R	                  5       n[        SU R                  UUU4-  5      e)z4Provide helpful details about failed subcommand runsi0u  s   (... truncated stdout ...)Ns   (... truncated stderr ...)asciireplacezRProcess return code is %d
command line: %r

stdout:
---
%s
---

stderr:
---
%s
---)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenr   r   s        r   fail_PythonRunResult.fail@   s     88TXXSs8f/#gh-?Cs8f/#gh-?Cjj),335jj),335 # !% # # %% & 	&r   r   N)__name__
__module____qualname____firstlineno____doc__r&   __static_attributes__r   r   r   r   r   =   s
    <&r   r   )r"   r   r   c            	      :   [        5       nUR                  SS5      nUR                  SS5      nSU;   a  UR                  S5      nOU(       + =(       a    U(       + n[        R                  SS/nU(       a  X6-   nU(       a  UR	                  S5        OU(       d  U(       d  UR	                  S5        UR                  S	S5      (       a-  0 n[        R
                  S
:X  a  [        R                  S   US'   O[        R                  R                  5       nSU;  a  SUS'   UR                  U5        UR                  U 5        [        R                  " U[        R                  [        R                  [        R                  XtS9nU    UR                  5       u  pUR                  5         [        R                   " 5          SSS5        UR"                  n[%        UW	W
5      U4$ ! UR                  5         [        R                   " 5         f = f! , (       d  f       NS= f)a  Used to implement assert_python_*.

*args are the command line flags to pass to the python interpreter.
**env_vars keyword arguments are environment variables to set on the process.

If __run_using_command= is supplied, it must be a list of
command line arguments to prepend to the command line used.
Useful when you want to run another command that should launch the
python interpreter via its own arguments. ["/bin/echo", "--"] for
example could print the unquoted python command line instead of
run it.
__run_using_commandN__cwd
__isolated-Xfaulthandlerz-Ir   
__cleanenvwin32
SYSTEMROOTTERM )stdinstdoutstderrenvcwd)r   popr   r   appendplatformr
   r   copyupdateextendr   PopenPIPEcommunicatekill_cleanup
returncoder   )argsenv_varsenv_requiredrun_using_commandr=   isolatedr$   r<   procr   r   r"   s               r   run_python_until_endrP   ]   s    45L %:DA
,,w
%Cx<<-<4$4n5H$/ 	l ||L$''<<7" !#

< 8C jjoo XFJJxOODHJOO *
 +D 
	"'')HCIIK! 
 
BBS)833 IIK! 
s$   HG"%H"'H		H
Hc                    [        U0 UD6u  p4UR                  (       a  U (       d  UR                  (       d  U (       d  UR                  U5        U$ N)rP   r"   r&   )expected_successrJ   rK   resr$   s        r   _assert_pythonrU      s7    ($;(;MC#SVV<LJr   c                       [        S/U Q70 UD6$ )a`  
Assert that running the interpreter with `args` and optional environment
variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
stderr) tuple.

If the __cleanenv keyword is set, env_vars is used as a fresh environment.

Python is started in isolated mode (command line option -I),
except if the __isolated keyword is set to False.
TrU   rJ   rK   s     r   assert_python_okrY      s     $2222r   c                       [        S/U Q70 UD6$ )z
Assert that running the interpreter with `args` and optional environment
variables `env_vars` fails (rc != 0) and return a (return code, stdout,
stderr) tuple.

See assert_python_ok() for more options.
FrW   rX   s     r   assert_python_failurer[      s     %3$3(33r   )r:   r;   c                 2   [         R                  /n[        5       (       d  UR                  S5        UR	                  U5        UR                  S[        [        R                  5      5      nSUS'   [        R                  " U4[        R                  XS.UD6$ )zRun a Python subprocess with the given arguments.

kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
object.
r   r<   vt100r7   )r9   r:   r;   )r   r   r   r?   rC   
setdefaultdictr
   r   r   rD   rE   )r:   r;   rJ   kwr$   r<   s         r   spawn_pythonra      s     H+--OOD --tBJJ/
0CCKH "JOO#)" " "r   c                     U R                   R                  5         U R                  R                  5       nU R                  R                  5         U R	                  5         [
        R                  " 5         U$ )z?Run the given Popen process until completion and return stdout.)r9   closer:   readwaitr   rH   )pdatas     r   kill_pythonrh      sH    GGMMO88==?DHHNN FFHKr   c                    UnU(       d  U[         R                  S-   -  n[         R                  R                  X5      n[	        U[
        5      (       a&  [        USSS9 nUR                  U5        S S S 5        O&[        US5       nUR                  U5        S S S 5        [        R                  " 5         U$ ! , (       d  f       N%= f! , (       d  f       N6= f)Npywzutf-8)encodingwb)
r
   extseppathjoin
isinstancestropenwrite	importlibinvalidate_caches)
script_dirscript_basenamesourceomit_suffixscript_filenamescript_namescript_files          r   make_scriptr~      s    %O299t++'',,z;K&#+sW5f% 65 +t$f% %! 65 %$s   B7C7
C
Cc                 N   SS K nU[        R                  -   S-   n[        R                  R	                  X5      nUR                  US5       nUc  UR                  [        R                  5      n[        U5      S:  a?  US   S:X  a6  [        [        U5      5      n	[        R                  R                  U	5      nU	nO[        R                  R                  U5      nUR                  X#5        S S S 5        U[        R                  R	                  Xc5      4$ ! , (       d  f       N/= f)Nr   ziprk      __pycache__)zipfiler
   rn   ro   rp   ZipFilesplitsepr   r   r   basenamert   )
zip_dirzip_basenamer|   name_in_zipr   zip_filenamezip_namezip_fileparts
legacy_pycs
             r   make_zip_scriptr      s    		)%/Lww||G2H	3	'8%%bff-E5zQ59#=,->{-KL
 gg..z:( gg..{;{0 
( RWW\\(888 
(	's   B!D
D$c                 J    [         R                  " U 5        [        U SU5        g )N__init__)r
   mkdirr~   )pkg_dirinit_sources     r   make_pkgr     s    HHW[1r   c                 .   SS K n/ n[        U SS5      n	UR                  U	5        [        R                  R                  U	5      n
[        XU5      nUR                  U5        U(       a<  [        R                  " U	SS9n	[        R                  " USS9nUR                  X45        [        SUS-   5       Vs/ s H&  n[        R                  R                  U/U-  5      PM(     nn[        R                  R                  US   [        R                  R                  U5      5      nU[        R                  -   S-   n[        R                  R                  X5      nUR                  US	5       nU H5  n[        R                  R                  UU
5      nUR                  U	U5        M7     UR                  X5        S S S 5        U H  n[        R                  " U5        M     U[        R                  R                  UU5      4$ s  snf ! , (       d  f       NT= f)
Nr   r   r8   T)doraise   r   rk   )r   r~   r?   r
   ro   r   
py_compilecompilerC   ranger   rp   rn   r   rt   unlink)r   r   pkg_namerx   ry   depthcompiledr   r   	init_nameinit_basenamer|   i	pkg_namesscript_name_in_zipr   r   r   nameinit_name_in_zips                       r   make_zip_pkgr     s   FGZ4I
MM)GG$$Y/Mg?K
MM+&&y$?	 ((dCy./49!U1W4EF4EqhZ\*4EIFimRWW5E5Ek5RS		)%/Lww||G2H	3	'8D!ww||D-@NN9&67  	{7	 
(
 
		$  RWW\\(,>??? G 
(	's   5-H+AH
Hc                    [         R                  (       a  S nS[        R                  R	                  U 5       3n[        5         [        U" U5      SS9  [        R                  SSSSU S	/n[        R                  " U5      n[        U" U S
UR                   35      SS9  UR                  (       a  [        U S35      eg [        SU S	5        g )Nc                     SU  S3$ )Nz===== z ======r   )texts    r   titlerun_test_script.<locals>.title3  s    D6))r   zscript T)flushr   r2   r3   z-uz-vz completed: exit code z failed)r   verboser
   ro   r   printr   r   r   runrI   r!   rY   )scriptr   r   rJ   rO   s        r   run_test_scriptr   /  s     	* ))&123eDk& dND&$O~~d#etf24??2CDE	?? D6!122  	vt,r   )FrR   )r8   )r   F)collectionsru   r   r
   os.pathr   r   importlib.utilr   testr   test.support.import_helperr   r	   r   
namedtupler   requires_subprocessrP   rU   rY   r[   rE   STDOUTra   rh   r~   r   r   r   r   r   r   r   <module>r      s     
 	    ,  6 (, $%0P&{--.@*>@ &@ 	@4 @4F 	 34 	)z7H7H " ".	 9*2 ,1@> 	- -r   