3 �L�j�)�@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZdadd�ZGdd�dejdd#��Zd d �Zd d�Zdd�Zdd�Zejejd�dd�Zdd�Zd$dd�Zd%dd�Zd&dd�Zd'd!d"�ZdS)(�N)�source_from_cache)�make_legacy_pyc�strip_python_stderrc CsVtdkrRdtjkrdadSytjtjdddg�Wntjk rLdaYnXdatS)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. NZ PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)�$__cached_interp_requires_environment�os�environ� subprocessZ check_call�sys� executableZCalledProcessError�r r �2/usr/lib64/python3.6/test/support/script_helper.py� interpreter_requires_environments   r c@seZdZdZdd�ZdS)�_PythonRunResultz2Helper for reporting Python subprocess run resultscCs�d }|j|j}}t|�|kr0d|| d�}t|�|krNd|| d�}|jdd�j�}|jdd�j�}td|j|||f��dS) z4Provide helpful details about failed subcommand runs�P�ds(... truncated stdout ...)Ns(... truncated stderr ...)�ascii�replacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---i@)�out�err�len�decode�rstrip�AssertionError�rc)�self�cmd_line�maxlenrrr r r �fail>s   z_PythonRunResult.failN)�__name__� __module__� __qualname__�__doc__rr r r r r;srrrrc Ost�}d|kr|jd�}n | o$| }tjddg}|rB|jd�n| rX| rX|jd�|jdd�r�i}tjdkr�tjd|d<n tjj�}d |kr�d |d <|j |�|j |�t j |t j t j t j |d �}|�*z|j�\}}Wd|j�t j�XWdQRX|j} t|�}t| ||�|fS) NZ __isolatedz-XZ faulthandlerz-Iz-EZ __cleanenvZwin32Z SYSTEMROOT�TERM�)�stdin�stdout�stderr�env)r �popr r �append�platformrr�copy�update�extendr�Popen�PIPEZ communicate�kill�_cleanup� returncoderr) �args�env_varsZ env_required�isolatedrr'�procrrrr r r �run_python_until_end[s:            r7cOs4t||�\}}|jr|s&|j r0| r0|j|�|S)N)r7rr)Zexpected_successr3r4�resrr r r �_assert_python�s r9cOstd|�|�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)T)r9)r3r4r r r �assert_python_ok�s r:cOstd|�|�S)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. F)F)r9)r3r4r r r �assert_python_failure�sr;)r%r&cOsXtjg}t�s|jd�|j|�|jdttj��}d|d<t j |ft j ||d�|��S)z�Run a Python subprocess with the given arguments. kw is extra keyword args to pass to subprocess.Popen. Returns a Popen object. z-Er'Zvt100r")r$r%r&) r r r r)r-� setdefault�dictrrrr.r/)r%r&r3�kwrr'r r r � spawn_python�s   r?cCs2|jj�|jj�}|jj�|j�tj�|S)z?Run the given Popen process until completion and return stdout.)r$�closer%�read�waitrr1)�p�datar r r � kill_python�s    rEFcCsP|}|s|tjd7}tjj||�}t|ddd�}|j|�|j�tj�|S)N�py�wzutf-8)�encoding) r�extsep�path�join�open�writer@� importlib�invalidate_caches)Z script_dir�script_basename�sourceZ omit_suffixZscript_filename� script_nameZ script_filer r r � make_script�s rSc Cs�|tjd}tjj||�}tj|d�}|dkr~|jtj�}t|�dkrr|ddkrrt t |��}tjj |�}|}n tjj |�}|j ||�|j �|tjj||�fS)N�ziprG�� __pycache__�����)rrIrJrK�zipfile�ZipFile�split�seprrr�basenamerMr@) �zip_dir� zip_basenamerRZ name_in_zip� zip_filename�zip_name�zip_file�partsZ legacy_pycr r r �make_zip_script�s      rcr#cCstj|�t|d|�dS)N�__init__)r�mkdirrS)Zpkg_dirZ init_sourcer r r �make_pkg�s rf�cs0g}t|dd�}|j|�tjj|�} t|||�} |j| �|rjtj|dd�}tj| dd�} |j|| f��fdd�td|d�D�} tjj | d tjj| ��} |tj d} tjj || �}t j |d �}x&| D]}tjj || �}|j ||�q�W|j | | �|j�x|D]}tj|��q W|tjj || �fS) Nrdr#T)�doraisecsg|]}tjj�g|��qSr )rr[rK)�.0�i)�pkg_namer r � �sz make_zip_pkg..rgrTrG�����)rSr)rrJr\� py_compile�compiler-�rangerKrIrXrYrMr@�unlink)r]r^rkrPrQZdepthZcompiledrqZ init_nameZ init_basenamerRZ pkg_namesZscript_name_in_zipr_r`ra�nameZinit_name_in_zipr )rkr � make_zip_pkg�s.         rs)rrr)F)N)r#)rgF) � collectionsrNr rZos.pathZtempfilerrn� contextlibZshutilrX�importlib.utilrZ test.supportrrrr � namedtuplerr7r9r:r;r/ZSTDOUTr?rErSrcrfrsr r r r �s4 $3