o
    HXh                     @   s`  d dl mZ d dlmZ d dlmZ d dlZd dlm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Zd dlZe
jdkZejdkZd ZdZd	Zd
ZdZd	ZdZdZdd Zdd ZG dd dZG dd deej Z!G dd deej Z"G dd dej Z#G dd deej Z$G dd deej Z%G dd deej Z&e'dkre(  dS dS )     )support)import_helper)	os_helperN)
namedtuplentdarwin            l    c                 C   s.   t j| } t j| d }| d S )Nr   _d)ospathbasenamesplitextcasefoldendswith)Zprogramname r   9/opt/python-3.10.19/usr/lib/python3.10/test/test_embed.pydebug_build%   s   r   c                  C   s,   t tj} t| D ]
}|dr| |= q	| S )NZPYTHON)dictr   environlist
startswith)envkeyr   r   r   remove_python_envvars+   s   

r   c                   @   s:   e Zd Zdd Zdd Zdddddddd	Zd
d ZdS )EmbeddingTestsMixinc                 C   s   t jt}t jt jt j|}d}tr/ttjrdndd }||7 }t jtj}nt j	|d}t j	|| | _
}t j|sM| d|  t  | _t | d S )N
_testembedr    z.exeZProgramsz%r doesn't exist)r   r   abspath__file__dirname
MS_WINDOWSr   sys
executablejointest_exeexistsZskipTestgetcwdoldcwdchdir)selfhereZbasepathZexenameextZexepathZexer   r   r   setUp5   s   
zEmbeddingTestsMixin.setUpc                 C   s   t | j d S N)r   r,   r+   r-   r   r   r   tearDownH   s   zEmbeddingTestsMixin.tearDownNr   )r   timeout
returncodeinputcwdc                G   s   	 | j g}|| |d urtr| }tjd |d< tj|tjtjd||d}z|j	||d\}	}
W n   |
  |   |j|krbtjrbtd| d td|	  td|
  td	 | |j|d
|j|
f  |	|
fS )NZ
SYSTEMROOTT)stdoutstderruniversal_newlinesr   r7   )r6   r4   z--- z failed ---zstdout:
zstderr:
z------zbad returncode %d, stderr is %r)r(   extendr$   copyr   r   
subprocessPopenPIPEcommunicate	terminatewaitr5   r   verboseprintassertEqual)r-   r   r4   r5   r6   r7   argscmdpouterrr   r   r   run_embedded_interpreterK   s<   

z,EmbeddingTestsMixin.run_embedded_interpreterc                 c   s$   |  d\}}| |d d}tdd}d}g }| D ]r}|d|kr<| t|d tjdkr7t| |d7 }q| 	t|d	 t
||}|d u rT| || ||  }	tjdkrct|	 | |	j | |	j | |	j ||	 t|d	kr|d }
| |	|
 |V  g }qd S )
NZ&test_repeated_init_and_subinterpretersr    zS^interp (\d+) <(0x[\dA-F]+)>, thread state <(0x[\dA-F]+)>: id\(modules\) = ([\d]+)$Interpzid interp tstate modulesr
   z--- Pass {} ---r      )rK   rE   r   
splitlinesformatlenr   rC   rD   Z
assertLessrematchZassertRegexgroupsZ
assertTrueinterptstatemodulesappend)r-   rI   rJ   Z
interp_patrL   ZnumloopsZcurrent_runlinerR   rT   mainr   r   r   %run_repeated_init_and_subinterpretersn   s@   



z9EmbeddingTestsMixin.run_repeated_init_and_subinterpreters)__name__
__module____qualname__r0   r3   rK   rZ   r   r   r   r   r   4   s    #r   c                   @   sp   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )EmbeddingTestsi  c                 C   s(   |   D ]}|d }| |jd qd S )Nr   0)rZ   rE   id)r-   runrY   r   r   r   test_subinterps_main   s   z#EmbeddingTests.test_subinterps_mainc                 C   sT   |   D ]#}|^}}}t|j}t|D ]\}}| |jt|| d  qqd S )Nr
   )rZ   intr`   	enumeraterE   str)r-   ra   rY   subs_Zmainidisubr   r   r   test_subinterps_different_ids   s   
z,EmbeddingTests.test_subinterps_different_idsc                 C   sj   |   D ].}|^}}}d|v rtd|D ]}| |j|j | |j|j | |j|j qqd S )NZ0x0zplatform prints pointers as 0x0)rZ   unittestZSkipTestZassertNotEqualrT   rU   rV   )r-   ra   rY   rf   rg   ri   r   r   r   test_subinterps_distinct_state   s   
z-EmbeddingTests.test_subinterps_distinct_statec                 C   sP   |  d\}}| |d dd tdtd D }d|d }| || d S )Ntest_repeated_init_and_inittabr    c                 S   s   g | ]}d | dqS )z	--- Pass z ---r   ).0rh   r   r   r   
<listcomp>   s    zAEmbeddingTests.test_repeated_init_and_inittab.<locals>.<listcomp>r
   
)rK   rE   range
INIT_LOOPSr'   )r-   rI   rJ   linesr   r   r   rm      s
   z-EmbeddingTests.test_repeated_init_and_inittabc                 C   s   t tjdd}| jd|d\}}tjdkr t  t| t| d}d}dg d	}|j|||d
}d | _	| 
| | d S )Nzutf-8:surrogateescape)ZPYTHONIOENCODINGtest_forced_io_encodingr   r
   utf-8surrogateescaperp   )z--- Use defaults ---Expected encoding: defaultExpected errors: defaultzstdin: {in_encoding}:{errors}zstdout: {out_encoding}:{errors}'stderr: {out_encoding}:backslashreplacez--- Set errors only ---rx   zExpected errors: ignorezstdin: {in_encoding}:ignorezstdout: {out_encoding}:ignorerz   z--- Set encoding only ---Expected encoding: iso8859-1ry   zstdin: iso8859-1:{errors}zstdout: iso8859-1:{errors}"stderr: iso8859-1:backslashreplacez--- Set encoding and errors ---r{   zExpected errors: replacezstdin: iso8859-1:replacezstdout: iso8859-1:replacer|   )Zin_encodingZout_encodingerrors)r   r   r   rK   r   rC   rD   r'   rO   maxDiffrE   strip)r-   r   rI   rJ   Zexpected_stream_encodingZexpected_errorsexpected_outputr   r   r   rt      s    
z&EmbeddingTests.test_forced_io_encodingc                 C   sr   	 t tjtjtjd}| jd|d\}}tr| j	}n	tjt
 d}d| d}| || | |d d S )N)
PYTHONPATHtest_pre_initialization_apiru   Zspamzsys.executable: rp   r    )r   r   r   pathsepr'   r%   r   rK   r$   r(   r*   assertInrE   )r-   r   rI   rJ   Zexpected_pathr   r   r   r   r      s   z*EmbeddingTests.test_pre_initialization_apic                 C   sL   	 t  }tjtj|d< | jd|d\}}d}| || | |d d S )Nr   #test_pre_initialization_sys_optionsru   zsys.warnoptions: ['once', 'module', 'default']
sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}
warnings.filters[:3]: ['default', 'module', 'once']
r    )	r   r   r   r'   r%   r   rK   r   rE   )r-   r   rI   rJ   r   r   r   r   r   
  s   
z2EmbeddingTests.test_pre_initialization_sys_optionsc                 C   ,   	 |  d\}}| |d | |d d S )Ntest_bpo20891r    rK   rE   r-   rI   rJ   r   r   r   r        zEmbeddingTests.test_bpo20891c                 C   r   )Ntest_initialize_twicer    r   r   r   r   r   r   $  r   z$EmbeddingTests.test_initialize_twicec                 C   s0   	 |  d\}}| | d | |d d S )Ntest_initialize_pymainz6Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']r    rK   rE   rstripr   r   r   r   r   -  s   z%EmbeddingTests.test_initialize_pymainc                 C   s.   |  d\}}| | d | |d d S )Ntest_run_mainz%Py_RunMain(): sys.argv=['-c', 'arg2']r    r   r   r   r   r   r   5  s   zEmbeddingTests.test_run_mainc                 C   s2   d}|  d\}}| |d|  | |d d S )NrM   test_run_main_loopz&Py_RunMain(): sys.argv=['-c', 'arg2']
r    r   )r-   ZnlooprI   rJ   r   r   r   r   :  s   z!EmbeddingTests.test_run_main_loopN)r[   r\   r]   r~   rb   rj   rl   rm   rt   r   r   r   r   r   r   r   r   r   r   r   r^      s    +		r^   c                   @   sD  e Zd ZdZer
dndZe Ze Ze	e
ddddddZer%eddi eeedeedZeeeddddddd	Zg d
Zi de	dddddddddddddddddddddddddededddei ddddgd g d!g d"g d#dd$dd%ed&ed'ed(ed)ed*ed+ed,dd-ejd.di d/dd0dd1dd2dd3dd4dd5dd6dd7dd8dd9dd:dd;ed<ed=dd>dd?dddd@ddddAZeredBdi eeeddCdDZeeeddddddddddEZerdedB< ddddFZdGgZg dHZer)edI edJ er3dKdKedLZni Zg dMZerAedN dZedOdP Z dQdR Z!dSdT Z"dUdV Z#	ddWdXZ$dYdZ Z%d[d\ Z&d]d^ Z'd_d` Z(				ddddaddbdcddZ)dedf Z*dgdh Z+didj Z,dkdl Z-dmdn Z.dodp Z/dqdr Z0dsdt Z1dudv Z2dwdx Z3dydz Z4d{d| Z5d}d~ Z6dd Z7dd Z8dd Z9dd Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@dd ZAdd ZBdd ZCdd ZDdd ZEdd ZFdddZGeHjIdd ZJdd ZKdd ZLeMNeddd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdS )InitConfigTestsi   surrogatepassrw   r   r
   )_config_init	allocator
parse_argvconfigure_localecoerce_c_localeZcoerce_c_locale_warn	utf8_modelegacy_windows_fs_encoding)r   r   r   r   )r   r   isolateduse_environmentr   dev_moder   )r   r   r   r   r   r   r   install_signal_handlersuse_hash_seed	hash_seedfaulthandlertracemallocimport_timeshow_ref_countZ	dump_refsmalloc_statsfilesystem_encodingfilesystem_errorspycache_prefixNprogram_namer   argvr    	orig_argvxoptionswarnoptionspythonpath_envhomer&   base_executableprefixbase_prefixexec_prefixbase_exec_prefixmodule_search_pathsZmodule_search_paths_set
platlibdirsite_importbytes_warningwarn_default_encodinginspectinteractiveoptimization_levelparser_debugwrite_bytecoderC   quietuser_site_directoryconfigure_c_stdiobuffered_stdiostdio_encodingstdio_errorsZskip_source_first_linerun_commandZ
run_moduledefault)run_filenameZ_install_importlibcheck_hash_pycs_modepathconfig_warnings
_init_main_isolated_interpreterlegacy_windows_stdior   )r   r   r   )
r   r   r   r   r   r   r   r   r   r   )ZPy_HasFileSystemDefaultEncodingZPy_HashRandomizationFlagZ$_Py_HasFileSystemDefaultEncodeErrors)ZPy_UTF8Moder   ))ZPy_BytesWarningFlagr   )ZPy_DebugFlagr   )ZPy_DontWriteBytecodeFlagr   T)Z Py_FileSystemDefaultEncodeErrorsr   )ZPy_FileSystemDefaultEncodingr   )ZPy_FrozenFlagr   T)ZPy_IgnoreEnvironmentFlagr   T)ZPy_InspectFlagr   )ZPy_InteractiveFlagr   )ZPy_IsolatedFlagr   )ZPy_NoSiteFlagr   T)ZPy_NoUserSiteDirectoryr   T)ZPy_OptimizeFlagr   )ZPy_QuietFlagr   )ZPy_UnbufferedStdioFlagr   T)ZPy_VerboseFlagrC   ))ZPy_LegacyWindowsFSEncodingFlagr   ))ZPy_LegacyWindowsStdioFlagr   )r   r   python3_dll)r   r   r   r   )r   c                 C   s
   d | _ d S r1   )EXPECTED_CONFIG)clsr   r   r   tearDownClass  s   
zInitConfigTests.tearDownClassc                 C   s<   i }|D ]}d|v r| dd\}}|||< qd||< q|S )N=r
   T)split)r-   Zxoptions_listr   optr   valuer   r   r   main_xoptions  s   

zInitConfigTests.main_xoptionsc                 C   s   t  }td}tjdd|g}tj||tjtjd}|jr)t	d|j
d|j|j
d}zt|W S  tjyH   | d| Y d S w )	Na4  
            import json
            import sys
            import _testinternalcapi

            configs = _testinternalcapi.get_configs()

            data = json.dumps(configs)
            data = data.encode('utf-8')
            sys.stdout.buffer.write(data)
            sys.stdout.buffer.flush()
        z-S-c)r   r8   r9   z)failed to get the default config: stdout=z stderr=rv   fail to decode stdout: )r   textwrapdedentr%   r&   r=   ra   r?   r5   	Exceptionr8   r9   decodejsonloadsJSONDecodeErrorfail)r-   r   coderF   procr8   r   r   r   _get_expected_config_impl  s&   
z)InitConfigTests._get_expected_config_implc                 C   sl   t }|jd u r|  |_i }|j D ] \}}i }| D ]\}}t|tr*| }|||< q|||< q|S r1   )r   r   r   items
isinstancer   r<   )r-   r   configsZ
config_keyZconfig_valueconfigr   r   r   r   r   _get_expected_config<  s   




z$InitConfigTests._get_expected_configc                 C   sD  |   }|d }| D ]\}	}
|
| ju r||	 ||	< q|d }| D ]\}	}
|
| ju r3||	 ||	< q$|d r<|tkrFdD ]}	| j||	< q>|d sO| j|d< |d dkr|d | ju r`d|d< |d	 | ju rl| j|d	< |d
 | ju rwd|d
< |d | ju rd|d< tr| j}n|d | jurtj	
|d }n	tj	t d}|d | ju r||d< |d | ju r||d< |d | ju rd|d< |d }| D ]\}	}
|
| ju r||	 ||	< q|d | jur|d }|d ur|tj	j}g ||d |d< |d ur|d  |d< ||d  | jD ]}	|	|vr||	 ||	< qd S )N
pre_configpath_configr   )r   r   r   r   r   r
   r   rv   r   r   r   rw   r   r   r&   r   z./_testembedr   r   r   )r   r   GET_DEFAULT_CONFIG
API_COMPATIGNORE_CONFIGUTF8_MODE_ERRORSr$   r(   r   r   r!   r'   r*   r   r   r<   COPY_PRE_CONFIG)r-   expected_preconfigexpectedexpected_pathconfigr   apimodify_path_cbr   r   r   r   r   Zdefault_executabler   r   pathsr   r   r   get_expected_configL  sl   







z#InitConfigTests.get_expected_configc                 C   N   t |d }t| D ]\}}|| ju r||d  ||= q| || d S )Nr   r   r   r   r   poprE   )r-   r   r   r   r   r   r   r   r   check_pre_config     
z InitConfigTests.check_pre_configc                 C   r   )Nr   r   )r-   r   r   r   r   r   r   r   r   check_config  r   zInitConfigTests.check_configc           	      C   s   |d }|d }t | j}| jD ]"}t|dkr(|\}}}|| r#dnd||< q|\}}|| ||< q| jD ]"}t|dkrN|\}}}|| rIdnd||< q6|\}}|| ||< q6| |d | d S )Nr   r   r	   r   r
   Zglobal_config)r   DEFAULT_GLOBAL_CONFIGCOPY_GLOBAL_CONFIGrP   COPY_GLOBAL_PRE_CONFIGrE   )	r-   r   r   r   r   itemZ
global_keyZcore_keyZoppositer   r   r   check_global_config  s    




z#InitConfigTests.check_global_configc                 C   sV   |d }| j D ]}|| ||< qtjj|d |d< |d |d< | |d | d S )Nr   r   Zmodule_search_pathr&   Zprogram_full_pathr   )COPY_PATH_CONFIGr   r   r   r'   rE   )r-   r   r   r   r   r   r   r   check_path_config  s   
z!InitConfigTests.check_path_configF)preconfig_apir   ignore_stderrr7   c                C   s  t  }|	d ur||	 |}	|d u r|}|tkr| j}n|tkr$| j}n| j}|d u r-i }t|fi |}|d u r;i }|d u rAi }t| jfi |}|tkrR| j	}n|tkrZ| j
}n| j}t|fi |}| ||||	|| | j||	|d\}}|d u r|d sd}|d ur|
s| | | zt|}W n tjy   | d| Y nw | || | || | | | || |S )N)r   r7   rC   r    r   )r   updateAPI_ISOLATEDPRE_CONFIG_ISOLATED
API_PYTHONPRE_CONFIG_PYTHONPRE_CONFIG_COMPATr   PATH_CONFIGCONFIG_PYTHONCONFIG_ISOLATEDCONFIG_COMPATr   rK   rE   r   r   r   r   r   r   r   r   r  )r-   ZtestnameZexpected_configr   r   r   r9   r   r  r   r  r7   Znew_envZdefault_preconfigZdefault_configrI   rJ   r   r   r   r   check_all_configs  s`   


z!InitConfigTests.check_all_configsc                 C      | j dtd d S )NZtest_init_initialize_configr   r  r   r2   r   r   r   test_init_default_config     z(InitConfigTests.test_init_default_configc                 C   r  )Ntest_preinit_compat_configr  r  r2   r   r   r   r     r  z*InitConfigTests.test_preinit_compat_configc                 C   r  )Ntest_init_compat_configr  r  r2   r   r   r   r    r  z'InitConfigTests.test_init_compat_configc                 C   s@   ddi}ddddgdddddddddd}| j d||td	 d S )
Nr   r
   z./globalvarr   default::BytesWarningr   )r   r   r   r   r   r   r   r   rC   r   r   r   r   test_init_global_configr  r  r-   	preconfigr   r   r   r   r    s&   

z'InitConfigTests.test_init_global_configc                 C   s  t dd}i dddddddd	d
ddddddddddddddddgdg ddd	dg ddg ddd i d!dd"dd#dd$dd%d	d&dd'dd(dd)dd*dd+dd,dd-d.d/| jd0d1d2dd3d}| jd4||td5 d S )6Nr
   r   r   r   r   r   r   {   r   r   r   r   r   r   	iso8859-1r   replacer   Zconf_pycache_prefixr   z./conf_program_namer   r   arg2r   )python3-Wcmdline_warnoption-Xcmdline_xoptionr   passr  r   r   )zconfig_xoption1=3zconfig_xoption2=Zconfig_xoption3r$  r   )r"  r  Zconfig_warnoptionr   pass
r   r   r   r   r   r   rC   r   r   r   r   r   r   Zmy_platlibdirr   r   alwaysr   r   test_init_from_configr  PYMEM_ALLOCATOR_MALLOCr   r  r   r  r   r   r   r(    s   	
 "#$%&'()*+,-./124
6
z%InitConfigTests.test_init_from_configc                 C   s   dt i}i ddddddddd	dd
dddddddddddddddddddddddgd| jd}| jd||td d S ) Nr   r   r
   r   *   r   r   r   r   r   r   r   /my/pathr   env_pycache_prefixr   r   rC   r   r   r  r   r  r   r   r   EnvVarenv_platlibdirr   r   test_init_compat_envr  r)  r  r   r   r   r1  [  sV   	


z$InitConfigTests.test_init_compat_envc                 C   s   t dd}i ddddddddd	dd
dddddddddddddddddddddddgd| jd}| jd||td d S ) Nr
   r  r   r   r+  r   r   r   r   r   r   r   r,  r   r-  r   r   rC   r   r   r  r   r  r   r   r   r.  r/  r0  test_init_python_envr  )r*  r   r  r  r  r   r   r   r2  w  sX   	


z$InitConfigTests.test_init_python_envc                 C   0   t td}t dddgd}| jd||td d S )Nr   r
   r   r   r   r   test_init_env_dev_moder  )r   PYMEM_ALLOCATOR_DEBUGr  r   r  r   r   r   r6       


z&InitConfigTests.test_init_env_dev_modec                 C   r3  )Nr4  r
   r   r5  test_init_env_dev_mode_allocr  )r   r*  r  r   r  r   r   r   r9    r8  z,InitConfigTests.test_init_env_dev_mode_allocc                 C   s,   dt i}dddgd}| jd||td d S )Nr   r
   r   )r   r   r   test_init_dev_moder  )r7  r  r  r  r   r   r   r:    s   

z"InitConfigTests.test_init_dev_modec                 C   sD   dt i}dgg dtjddddgdgd}| jd||td	 d S )
Nr   	script.py)r   r#  devr;  r
   r   r<  )r   r   r   r   r   r   r   test_preinit_parse_argvr  )r7  r   r   r!   r  r  r  r   r   r   r=    s   

	
z'InitConfigTests.test_preinit_parse_argvc                 C   s2   ddi}g d}||dd}| j d||td d S )Nr   r   )r   -E-Ir#  r<  r#  utf8r;  )r   r   r   test_preinit_dont_parse_argvr  r  r  )r-   r  r   r   r   r   r   rA    s   

z,InitConfigTests.test_preinit_dont_parse_argvc                 C       dddd}| j d|td d S )Nr
   r   r   r   r   test_init_isolated_flagr  r  r  r-   r   r   r   r   rE    s
   z'InitConfigTests.test_init_isolated_flagc                 C   rC  )Nr
   r   rD  test_preinit_isolated1r  r  rG  r   r   r   rH    
   z&InitConfigTests.test_preinit_isolated1c                 C   rC  )Nr
   r   rD  test_preinit_isolated2r  r  rG  r   r   r   rJ    rI  z&InitConfigTests.test_preinit_isolated2c                 C   r  )Ntest_preinit_isolated_configr  rB  r2   r   r   r   rK    r  z,InitConfigTests.test_preinit_isolated_configc                 C   r  )Ntest_init_isolated_configr  rB  r2   r   r   r   rL    r  z)InitConfigTests.test_init_isolated_configc                 C   r  )Ntest_preinit_python_configr  rF  r2   r   r   r   rM    r  z*InitConfigTests.test_preinit_python_configc                 C   r  )Ntest_init_python_configr  rF  r2   r   r   r   rN    r  z'InitConfigTests.test_init_python_configc                 C   s    ddd}| j di |td d S )Nr   )r   r   test_init_dont_configure_localer  rF  )r-   r  r   r   r   rO    s   

z/InitConfigTests.test_init_dont_configure_localec                 C   s(   ddd}dd }| j d|t|d d S )Nz./init_read_setZmy_executable)r   r&   c                 S   s   |  dd | d d S )Nr
   Ztest_path_insert1Ztest_path_append)insertrW   )r   r   r   r   modify_path  s   z7InitConfigTests.test_init_read_set.<locals>.modify_pathtest_init_read_set)r   r   rF  )r-   r   rQ  r   r   r   rR    s   
z"InitConfigTests.test_init_read_setc                 C   s.   dg dg dg dd}| j d|td d S )Nr
   )Zconfig_xoptionr$  Zsysadd_xoptionr   )ignore:::cmdline_warnoptionzignore:::sysadd_warnoptionzignore:::config_warnoption)r   r!  rS  r#  r$  )r   r   r   r   test_init_sys_addr  rF  rG  r   r   r   rT    s   z!InitConfigTests.test_init_sys_addc                 C   s8   d}ddgdd|dgd|d dd}| j d	|td
 d S )NRimport _testinternalcapi, json; print(json.dumps(_testinternalcapi.get_configs()))r   r  r   	./python3rp   r   )r   r   r   r   r   test_init_run_mainr  rF  r-   r   r   r   r   r   rW  #  s   
z"InitConfigTests.test_init_run_mainc                 C   s<   d}ddgdd|dgd|d ddd	}| j d
|tdd d S )NrU  r   r  r   rV  rp   r   r   )r   r   r   r   r   r   test_init_mainz)Run Python code before _Py_InitializeMain)r   r9   rF  rX  r   r   r   rY  /  s   

zInitConfigTests.test_init_mainc                 C   s.   dg dg ddddd}| j d|td	 d S )
Nr   )r   arg1-varg3./argv0r>  r   r%  rZ  r[  r\  r^  r&  r   )r   r   r   r   r   r   test_init_parse_argvr  rF  rG  r   r   r   r_  @  s   z$InitConfigTests.test_init_parse_argvc                 C   s4   ddi}dg dg ddd}| j d||td d S )Nr   r   r]  r^  )r   r   r   r   test_init_dont_parse_argvr  rF  )r-   r   r   r   r   r   r`  K  s   

z)InitConfigTests.test_init_dont_parse_argvc                 C   sD   t rd}| j}nd}tr| j}nt|pd}||||d d S )Npythonr   r    )r   r   r&   )r$   r(   MACOSshutilwhichr  )r-   r   r   r&   r   r   r   default_program_nameX  s   z$InitConfigTests.default_program_namec                 C   sX   |   }|d d }|ddddd}| | dtjj|i}| jd|t|dd d S )	Nr   r   r    )r   r   r   r   r   TESTPATHtest_init_setpathTr   r   r  )r   re  r   r   r   r'   r  r   r-   r   r   r   r   r   r   rg  h  s   

z!InitConfigTests.test_init_setpathc              	   C   sT   |   }|d d }|dddddddd}dtjj|i}| jd|t|d	d
 d S )Nr   r   r    Zconf_program_nameZconf_executable)r   r   r   r   r   r   r   r&   rf  test_init_setpath_configTrh  )r   r   r   r   r'   r  r  ri  r   r   r   rj  {  s   
z(InitConfigTests.test_init_setpath_configc              
   C   s   |   }|d u r|d d }|d u r|d d }tr |d d S tj}tj|tjd|j |j	 dtj|tjd|j d|j	 tj|tjd|j d|j	 dgS )Nr   r   r   ra  z.zip.lib-dynload)
r   r$   r%   version_infor   r   r'   r   majorminor)r-   r   r   r   verr   r   r   r     s$   z#InitConfigTests.module_search_pathsc              	   c   s    t  Z}tj|}tr7dd l}tj| j}t	|D ]}||dr6t
tj||tj|| qtj|tj| j}t
| j| t
| j| || _|V  W d    d S 1 sbw   Y  d S )Nr   z*.dll)tempfileTemporaryDirectoryr   r   realpathr$   fnmatchr#   r(   listdirrc  copyfiler'   r   copystat)r-   tmpdirrt  ZexedirfZ	exec_copyr   r   r   tmpdir_with_python  s    
 "z"InitConfigTests.tmpdir_with_pythonc           
      C   s   |   }|d d }tjj|}|D ]}tj|sqtjtj|dr/tj|} n	q| d| | }}| j	||d}|||||||d}| 
| ||d}	| jd|t|	d	 d S )
Nr   r   os.pyzUnable to find home in )r   r   )r   r   r   r   r   r   r   )ZTESTHOMEr   test_init_setpythonhome)r   r   )r   r   r   r   r'   isdirr)   r#   r   r   re  r  r   )
r-   r   r   	paths_strr   r   r   r   Zexpected_pathsr   r   r   r   r|    s4   
	

z'InitConfigTests.test_init_setpythonhomec                 C   s6   |   }|d d }tjj|}||d< d|i}|S )Nr   r   r   r   )r   r   r   r   r'   )r-   r   Zall_configsr   r~  r   r   r   r   copy_paths_by_env  s   z!InitConfigTests.copy_paths_by_envz#Windows does not use pybuilddir.txtc           
   	   C   s   |   [}d}tj||}t| tj|d}t|ddd}|| W d    n1 s1w   Y  |  }||d< | j}|||d}| 	|}	| j
d|t|	d	|d
 W d    d S 1 sbw   Y  d S )Nlibdirzpybuilddir.txtwr@  encodingr   )r   r&   r   r  T)r   r   r  r7   )rz  r   r   r'   mkdiropenwriter   r(   r  r  r   )
r-   rx  Zsubdirr  filenamefpr   r&   r   r   r   r   r   test_init_pybuilddir  s*   


"z$InitConfigTests.test_init_pybuilddirc                 C   s6  |   }t }tj}ts(tj|tj	d|j
 d|j d}t| ntj|d}t| ttjtj|d tj|d}t|ddd	}td
| |d td|d W d    n1 sgw   Y  |  }tsw||d< n,t|D ]#\}}	|dkrtj|tj|	||< q{tj|tj|	||< q{||d< | j}
|}|||
|
|d}i }tr||d< ||d< tj}d|j
 }t|
r|d7 }|d7 }tjtj|
|}||d< | |}| jd||t|d|d W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nra  rk  rl  libr{  z
pyvenv.cfgr  r@  r  z	home = %sfilez$include-system-site-packages = falser   r   )r   r   r   r&   r   r   r   r   z.DLLr   r  T)r   r   r   r  r7   )rz  rq  rr  r%   rm  r$   r   r   r'   r   rn  ro  makedirsrc  rv  r"   r  rD   r   rd   r   r(   r   r#   r  r  r   )r-   rx  Zpyvenv_homerp  Zlib_dynloadr  r  r   indexr   r&   r   r   r   Zdllr   r   r   r   test_init_pyvenv_cfg  sl   


Rz$InitConfigTests.test_init_pyvenv_cfgc           
         s   t d t d} fdd}|d}|d}|d}|d}|d	}|d
}| d }	| | tjj|	d  | | |	d  | | |	d  | | |	d  | | |	d  | | |	d  d S )Nctypes_testinternalcapic                    s   t  j| }d|_ j|_|S )Nr   )getattr	pythonapiargtypesZ	c_wchar_prestype)r   funcr  r   r   get_funcO  s   z8InitConfigTests.test_global_pathconfig.<locals>.get_func
Py_GetPathPy_GetPrefixPy_GetExecPrefixPy_GetProgramNamePy_GetProgramFullPathPy_GetPythonHomer   r   r   r   r   r&   r   )r   import_moduleZget_configsrE   r   r   r   r   )
r-   r  r  r  r  r  r  r  r  r   r   r  r   test_global_pathconfig@  s$   

z&InitConfigTests.test_global_pathconfigc                 C   s<   g d}t td}ddd|g dd}| jd||td d S )N)zignore:::PyConfig_Insert0r   zignore:::env1zignore:::env2zignore:::cmdline1zignore:::cmdline2r  zignore:::PySys_AddWarnOption1zignore:::PySys_AddWarnOption2zignore:::PyConfig_BeforeReadzignore:::PyConfig_AfterReadr4  r
   )r   z-Wignore:::cmdline1z-Wignore:::cmdline2)r   r   r   r   r   test_init_warnoptionsr  )r   r7  r  r  )r-   r   r  r   r   r   r   r  f  s   

	
z%InitConfigTests.test_init_warnoptionsc                 C   s"   dddgd}| j d|td d S )Nr   r   zerror::BytesWarning)r   r   r   test_init_set_configr  rB  rG  r   r   r   r    s   
z$InitConfigTests.test_init_set_configc                 C      |  d d S )Ntest_get_argc_argvrK   r2   r   r   r   r       z"InitConfigTests.test_get_argc_argvr1   )NNNNN)NN)Ur[   r\   r]   r~   r$   r   objectr   r   r   PYMEM_ALLOCATOR_NOT_SETr
  r  r   r  r	  r  r  r   r%   r   r  r  r  r   r   r   r;   r  r  r   classmethodr   r   r   r   r   r   r   r   r  r  r  r  r  r  r(  r1  r2  r6  r9  r:  r=  rA  rE  rH  rJ  rK  rL  rM  rN  rO  rR  rT  rW  rY  r_  r`  re  rg  rj  r   
contextlibcontextmanagerrz  r|  r  rk   ZskipIfr  r  r  r  r  r  r   r   r   r   r   C  s   	
	
!"#$%&')*+,-./01234578:;<=E

	



E
<>			

 

?&	r   c                   @      e Zd Zdd ZdS )SetConfigTestsc                 C   sB   t jdddg}tj|tjtjd}| |jd|j|j|jf d S )Nr?  z-mztest._test_embed_set_config)r8   r9   r   )	r%   r&   r=   ra   r?   rE   r5   r8   r9   )r-   rG   r   r   r   r   test_set_config  s   
zSetConfigTests.test_set_configN)r[   r\   r]   r  r   r   r   r   r        r  c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )AuditingTestsc                 C   r  )Ntest_open_code_hookr  r2   r   r   r   r    r  z!AuditingTests.test_open_code_hookc                 C   r  )N
test_auditr  r2   r   r   r   r    r  zAuditingTests.test_auditc                 C   r  )Ntest_audit_subinterpreterr  r2   r   r   r   r    r  z'AuditingTests.test_audit_subinterpreterc                 C      | j dtjdd d S )Ntest_audit_run_commandr
   r4   r5   rK   r   SHORT_TIMEOUTr2   r   r   r   r       
z$AuditingTests.test_audit_run_commandc                 C   r  )Ntest_audit_run_filer
   r  r  r2   r   r   r   r    r  z!AuditingTests.test_audit_run_filec              	   C   s   t j| jtjd }t|ddd}td|d td|d W d    n1 s)w   Y  zi t d|i}| j	d	t
jd
|d W t | d S t | w )N.pyr  rv   r  z
import sysr  z&sys.__interactivehook__ = lambda: NonePYTHONSTARTUPtest_audit_run_interactivehook
   r4   r5   r   r   r   r'   r+   r   ZTESTFNr  rD   r   rK   r   r  unlinkr-   Zstartupry  r   r   r   r   r    s   z,AuditingTests.test_audit_run_interactivehookc              	   C   s   t j| jtjd }t|ddd}td|d W d    n1 s#w   Y  zi t d|i}| j	dt
jd	|d
 W t | d S t | w )Nr  r  rv   r  r%  r  r  test_audit_run_startupr  r  r  r  r   r   r   r    s   z$AuditingTests.test_audit_run_startupc                 C   r  )Ntest_audit_run_stdinr
   r  r  r2   r   r   r   r    r  z"AuditingTests.test_audit_run_stdinN)r[   r\   r]   r  r  r  r  r  r  r  r  r   r   r   r   r    s    r  c                   @   r  )	MiscTestsc                 C   r  )Ntest_unicode_id_initr  r2   r   r   r   r    s   zMiscTests.test_unicode_id_initN)r[   r\   r]   r  r   r   r   r   r    r  r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )StdPrinterTestsr
   c                 C   s,   t d}|jj}|jf|_|j|_||S )Nr  )r   r  r  PyFile_NewStdPrinterZc_intr  Z	py_objectr  )r-   fdr  r  r   r   r   create_printer  s
   

zStdPrinterTests.create_printerc              
   C   s   d}| j }t|}| tj| t \}}| tj| | tj| zt|| | |}|| W t|| nt|| w t	|d}| 
||dd d S )Nu   unicode:é-€-!
d   r@  backslashreplace)	STDOUT_FDr   dupZ
addCleanupclosepipedup2r  r  readrE   encode)r-   messageZ	stdout_fdZstdout_fd_copyZrfdZwfdprinterdatar   r   r   
test_write  s   

zStdPrinterTests.test_writec                 C   sJ   | j }| |}| | | | | t| |  |  d S r1   )r  r  rE   filenoisattyr   flushr  r-   r  r  r   r   r   test_methods  s   
zStdPrinterTests.test_methodsc                 C   s$   | j }| |}t| t| d S r1   )r  r  r   Zcheck_disallow_instantiationtyper  r   r   r   test_disallow_instantiation  s   
z+StdPrinterTests.test_disallow_instantiationN)r[   r\   r]   r  r  r  r  r  r   r   r   r   r    s    r  __main__))testr   Ztest.supportr   r   rk   collectionsr   r  r   r   rQ   rc  r=   r%   rq  r   r   r$   platformrb  r  r7  r*  r   r  r  rr   ZMAX_HASH_SEEDr   r   r   ZTestCaser^   r   r  r  r  r  r[   rY   r   r   r   r   <module>   sZ   

	n "        T33