U g)@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z da ddZGdddeddZd d Zd d Zd dZddZejejdddZddZd!ddZd"ddZd#ddZd$dd ZdS)%N)source_from_cache)make_legacy_pycstrip_python_stderrcCsVtdkrRdtjkrdadSzttjdddgWntjk 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 PYTHONHOMET-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron subprocessZ check_callsys executableZCalledProcessErrorr r 0/usr/lib/python3.8/test/support/script_helper.py interpreter_requires_environments    rc@seZdZdZddZdS)_PythonRunResultz2Helper for reporting Python subprocess run resultscCsd}|j|j}}t||kr0d|| d}t||krNd|| d}|dd}|dd}td|j|||fdS)z4Provide helpful details about failed subcommand runsi@s(... truncated stdout ...)Ns(... truncated stderr ...)asciireplacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenrrr r r fail;s   z_PythonRunResult.failN)__name__ __module__ __qualname____doc__rr r r r r8sr)rrrc Os&t}|dd}d|kr&|d}n | o0| }tjddg}|rN|dn|s`|s`|d|ddri}tjdkrtjd |d <n tj}d |krd |d <| || |t j |t j t j t j ||d }|*z|\}} W5|t XW5QRX|j} t| } t| || |fS) NZ__cwdZ __isolatedz-XZ faulthandlerz-IrZ __cleanenvZwin32Z SYSTEMROOTTERM)stdinstdoutstderrenvcwd)rpopr r appendplatformrrcopyupdateextendr PopenPIPEkill_cleanupZ communicate returncoderr) argsenv_varsZ env_requiredr'isolatedrr&procrrrr r r run_python_until_endXsB            r7cOs0t||\}}|jr|s"|js,|s,|||S)N)r7rr)Zexpected_successr3r4resrr r r _assert_pythons 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)Tr9r3r4r r r assert_python_oks 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)Fr:r;r r r assert_python_failuresr=)r$r%cOsXtjg}ts|d|||dttj}d|d<t j |ft 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. rr&Zvt100r!)r#r$r%) r r rr)r- setdefaultdictrrr r.r/)r$r%r3kwrr&r r r spawn_pythons   rAcCs2|j|j}|j|t|S)z?Run the given Popen process until completion and return stdout.)r#closer$readwaitr r1)pdatar r r kill_pythons    rGFc CsT|}|s|tjd7}tj||}t|ddd}||W5QRXt|S)Npywzutf-8)encoding)rextseppathjoinopenwrite importlibinvalidate_caches)Z script_dirscript_basenamesourceZ omit_suffixZscript_filename script_nameZ script_filer r r make_scriptsrUc Cs|tjd}tj||}t|dh}|dkr|tj}t|dkrt|ddkrtt t |}tj |}|}n tj |}| ||W5QRX|tj||fS)NziprI __pycache__) rrKrLrMzipfileZipFilesplitseprrrbasenamerO) zip_dir zip_basenamerTZ name_in_zip zip_filenamezip_namezip_filepartsZ legacy_pycr r r make_zip_scripts    rer"cCst|t|d|dS)N__init__)rmkdirrU)Zpkg_dirZ init_sourcer r r make_pkgs rhc s,g}t|dd}||tj|} t|||} || |rjtj|dd}tj| dd} ||| ffddtd|dD} tj | dtj| } |tj d } tj || }t |d 6}| D]}tj || }| ||q| | | W5QRX|D]}t|q|tj || fS) Nrfr"T)doraisecsg|]}tjg|qSr )rr]rM).0ipkg_namer r sz make_zip_pkg..rirVrI)rUr)rrLr^ py_compilecompiler-rangerMrKrZr[rOunlink)r_r`rnrRrSZdepthZcompiledrtZ init_nameZ init_basenamerTZ pkg_namesZscript_name_in_ziprarbrcnameZinit_name_in_zipr rmr make_zip_pkgs,     rv)F)N)r")riF) collectionsrPr ros.pathr rqrZimportlib.utilrZ test.supportrrrr namedtuplerr7r9r<r=r/ZSTDOUTrArGrUrerhrvr r r r s4 $ 4