o
    f,                     @   s   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Zd dlZd dlm	Z	 d dl
mZ d dlmZ dadd ZG dd de dd	Zd
d Zdd Zdd Zdd ZejejdddZdd Zd$ddZd%ddZd&ddZ	d'd d!Zd"d# ZdS )(    N)source_from_cache)support)make_legacy_pycc                   C   sV   t du r)dtjv rda dS zttjdddg W n tjy&   da Y t S w da t S )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.
    N
PYTHONHOMET-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron
subprocess
check_callsys
executableCalledProcessError r   r   1/usr/lib/python3.10/test/support/script_helper.py interpreter_requires_environment   s   


r   c                   @   s   e Zd ZdZdd ZdS )_PythonRunResultz2Helper for reporting Python subprocess run resultsc                 C   s   d}| j | j}}t||krd|| d  }t||kr'd|| d  }|dd }|dd }td| j|||f )z4Provide helpful details about failed subcommand runsi@  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   r   r   r   fail=   s   z_PythonRunResult.failN)__name__
__module____qualname____doc__r   r   r   r   r   r   :   s    r   )r   r   r   c               	   O   sB  t  }|dd }d|v r|d}n| o| }tjddg}|r'|d n	|s0|s0|d |dd rEi }tjdkrDtjd	 |d	< ntj }d
|vrRd|d
< |	| |
|  tj|tjtjtj||d}|" z| \}}	W |  t  n	|  t  w W d    n1 sw   Y  |j}
t|
||	|fS )N__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_requiredr1   isolatedr   r0   procr   r   r   r   r   r   run_python_until_endZ   sD   






rC   c                O   s4   t |i |\}}|jr| s|js| s|| |S N)rC   r   r   )expected_successr>   r?   resr   r   r   r   _assert_python   s   
rG   c                  O      t dg| R i |S )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rG   r>   r?   r   r   r   assert_python_ok   s   rK   c                  O   rH   )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.
    FrI   rJ   r   r   r   assert_python_failure   s   rL   )r.   r/   c                 O   sX   t jg}t s|d || |dttj}d|d< t	j
|ft	j| |d|S )zRun a Python subprocess with the given arguments.

    kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
    object.
    r   r0   vt100r+   )r-   r.   r/   )r   r   r   r3   r7   
setdefaultdictr   r	   r
   r8   r9   )r.   r/   r>   kwr   r0   r   r   r   spawn_python   s   

rQ   c                 C   s2   | j   | j }| j  |   t  |S )z?Run the given Popen process until completion and return stdout.)r-   closer.   readwaitr
   r<   )pdatar   r   r   kill_python   s   


rW   Fc                 C   sh   |}|s|t jd 7 }t j| |}t|ddd}|| W d    n1 s)w   Y  t  |S )Npywzutf-8)encoding)r   extseppathjoinopenwrite	importlibinvalidate_caches)
script_dirscript_basenamesourceomit_suffixscript_filenamescript_namescript_filer   r   r   make_script   s   ri   c           	      C   s   |t j d }t j| |}t|d9}|d u r@|t j}t|dkr:|d dkr:t	t
|}t j|}|}nt j|}||| W d    n1 sPw   Y  |t j||fS )NziprY      __pycache__)r   r[   r\   r]   zipfileZipFilesplitsepr   r   r   basenamer_   )	zip_dirzip_basenamerg   name_in_zipzip_filenamezip_namezip_fileparts
legacy_pycr   r   r   make_zip_script   s   r{   r,   c                 C   s   t |  t| d| d S )N__init__)r   mkdirri   )pkg_dirinit_sourcer   r   r   make_pkg   s   
r      c                    s>  g }t | dd}|| tj|}	t | ||}
||
 |r5tj|dd}tj|
dd}
|||
f  fddtd|d D }tj	|d tj|
}|tj
 d	 }tj	| |}t|d
 }|D ]}tj	||	}||| qg||
| W d    n1 sw   Y  |D ]}t| q|tj	||fS )Nr|   r,   T)doraisec                    s   g | ]}t j g| qS r   )r   rq   r]   ).0ipkg_namer   r   
<listcomp>  s    z make_zip_pkg.<locals>.<listcomp>r   rj   rY   )ri   r3   r   r\   rr   
py_compilecompiler7   ranger]   r[   rn   ro   r_   unlink)rs   rt   r   rc   rd   depthcompiledr   	init_nameinit_basenamerg   	pkg_namesscript_name_in_ziprv   rw   rx   nameinit_name_in_zipr   r   r   make_zip_pkg   s.   

r   c                 C   s   t jrDdd }dtj|  }t  t||dd tjdddd	| d
g}t	|}t|| d|j
 dd |j
rBt| dd S td	| d
 d S )Nc                 S   s   d|  dS )Nz===== z ======r   )textr   r   r   title  s   zrun_test_script.<locals>.titlezscript T)flushr   r&   r'   z-uz-vz completed: exit code z failed)r   verboser   r\   rr   printr   r   r
   runr=   r   rK   )scriptr   r   r>   rB   r   r   r   run_test_script  s   
r   )FrD   )r,   )r   F)collectionsr`   r   r   os.pathr
   r   rn   importlib.utilr   testr   test.support.import_helperr   r   r   
namedtupler   rC   rG   rK   rL   r9   STDOUTrQ   rW   ri   r{   r   r   r   r   r   r   r   <module>   s8   $ 4



