o
    GXhK                     @   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mZ d dlm	Z	 d dl
mZ ddgZdZdZdZg d	Zejd
rLe	jrFdZdZn*ed n$ejdrWdZdZnejdkr_dZnejdkrgdZn	ejdkrpdZdZd	Zeejdvoee doee dZdd ZdZedeZG dd deZdZdZdada da!dd Z"G dd  d ej#Z$G d!d" d"e$Z%e	j&e'e(d#d$G d%d& d&e$Z)d'd( Z*e+d)kre,  dS dS )*    N)
namedtuple)support)run_python_until_endCzinvalid.asciiasciiT)zC.UTF-8zC.utf8UTF-8linuxutf-8ZPOSIXZaixz	iso8859-1darwincygwinFvxworks)r
   r   nl_langinfoCODESETc                 C   s6   d}t r|d7 }|| }td|dd\}}|jdkS )Nz=import locale; print(locale.setlocale(locale.LC_CTYPE, '{}'))z>; import sys; sys.exit(not locale.nl_langinfo(locale.CODESET))-c PYTHONCOERCECLOCALEr   )_check_nl_langinfo_CODESETformatr   rc)Zlocale_nameZcmd_fmtcmdresultpy_cmd r   E/opt/python-3.10.19/usr/lib/python3.10/test/test_c_locale_coercion.py_set_locale_in_subprocessW   s   

r   zBfsencoding stdin_info stdout_info stderr_info lang lc_ctype lc_allEncodingDetailsc                   @   s2   e Zd Zdg dZedd Zedd ZdS )r   ;)zimport sys, osz"print(sys.getfilesystemencoding())z2print(sys.stdin.encoding + ':' + sys.stdin.errors)z4print(sys.stdout.encoding + ':' + sys.stdout.errors)z4print(sys.stderr.encoding + ':' + sys.stderr.errors)z(print(os.environ.get('LANG', 'not set'))z,print(os.environ.get('LC_CTYPE', 'not set'))z*print(os.environ.get('LC_ALL', 'not set'))c                 C   s|   |d }d| dg }|| d |dd}|rt}n|dd}|dd}	|||	f}
t| |g||
R   S )	z;Returns expected child process details for a given encodingz:{}   surrogateescapebackslashreplaceLANGznot setLC_CTYPELC_ALL)r   appendgetCLI_COERCION_TARGETdict_asdict)clscoercion_expectedfs_encodingstream_encodingenv_vars_streamZstream_infoZexpected_langZexpected_lc_ctypeZexpected_lc_allZenv_infor   r   r   get_expected_detailsr   s   
z$EncodingDetails.get_expected_detailsc                 C   sl   t ddd| jfi |\}}|jdks|| |jd }t| |  }|j	d
  }||fS )aj  Retrieves fsencoding and standard stream details from a child process

        Returns (encoding_details, stderr_lines):

        - encoding_details: EncodingDetails for eager decoding
        - stderr_lines: result of calling splitlines() on the stderr output

        The child is run in isolated mode if the current interpreter supports
        that.
        z-Xzutf8=0r   r   r   )r   CHILD_PROCESS_SCRIPTr   Zfailoutdecode
splitlinesr'   r(   errrstrip)r)   r-   r   r   Zstdout_linesZchild_encoding_detailsstderr_linesr   r   r   get_child_details   s   



z!EncodingDetails.get_child_detailsN)__name__
__module____qualname__joinr0   classmethodr/   r7   r   r   r   r   r   e   s    
zPython runtime initialized with LC_CTYPE=C (a locale with default ASCII encoding), which may cause Unicode compatibility problems. Using C.UTF-8, C.utf8, or UTF-8 (if available) as alternative Unicode-compatible locales is recommended.zPython detected LC_CTYPE=C: LC_CTYPE coerced to {} (set another locale or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).c                  C   s   t d urd S g a tD ]} t| rt |  q
t r!t d attatj	rWt
dt  t
dt t
dt t
dt t
dt t
dt t
dt d S d S )	Nr   zAVAILABLE_TARGETS = z EXPECTED_C_LOCALE_EQUIVALENTS = z$EXPECTED_C_LOCALE_STREAM_ENCODING = z EXPECTED_C_LOCALE_FS_ENCODING = z$EXPECT_COERCION_IN_DEFAULT_LOCALE = z_C_UTF8_LOCALES = z_check_nl_langinfo_CODESET = )AVAILABLE_TARGETS_C_UTF8_LOCALESr   r$   r&   CLI_COERCION_WARNING_FMTr   CLI_COERCION_WARNINGr   verboseprintEXPECTED_C_LOCALE_EQUIVALENTS!EXPECTED_C_LOCALE_STREAM_ENCODINGEXPECTED_C_LOCALE_FS_ENCODING!EXPECT_COERCION_IN_DEFAULT_LOCALEr   )Ztarget_localer   r   r   setUpModule   s&   

rG   c                   @   s   e Zd Zdd ZdS )_LocaleHandlingTestCasec           
      C   sJ   t |}|\}}t ||||}	| ||	 |du rg }| || dS )a5  Check the C locale handling for the given process environment

        Parameters:
            expected_fs_encoding: expected sys.getfilesystemencoding() result
            expected_stream_encoding: expected encoding for standard streams
            expected_warning: stderr output to expect (if any)
        N)r   r7   r/   assertEqual)
selfr-   expected_fs_encodingexpected_stream_encodingexpected_warningsr*   r   Zencoding_detailsr6   Zexpected_detailsr   r   r   _check_child_encoding_details   s   
z5_LocaleHandlingTestCase._check_child_encoding_detailsN)r8   r9   r:   rN   r   r   r   r   rH      s    rH   c                   @   s    e Zd Zedd Zdd ZdS )LocaleConfigurationTestsc                 C   s   t stdd S )Nz No C-with-UTF-8 locale available)r=   unittestZSkipTest)r)   r   r   r   
setUpClass   s   
z#LocaleConfigurationTests.setUpClassc              
   C   s   d | _ d}d}ddddd}dD ]9}tD ]4}|dkr|dkrq| j||d | }|||< | j|||d dd	 W d    n1 sCw   Y  qqd S )
Nr	   r   r!   r"   r#   r   r!   r"   r!   r   )env_varZconfigured_localeF)rM   r*   )maxDiffr=   subTestcopyrN   )rJ   rK   rL   base_var_dictrT   locale_to_setvar_dictr   r   r   )test_external_target_locale_configuration   s6   zBLocaleConfigurationTests.test_external_target_locale_configurationN)r8   r9   r:   r<   rQ   r[   r   r   r   r   rO      s    
rO   ZPY_COERCE_C_LOCALEz(C locale coercion disabled at build timec                   @   sJ   e Zd Z		dddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dS )LocaleCoercionTestsNTc              
   K   s  d| _ tst}t}d}|rtg}ddddd}|| |dur$||d< | jd|d& tr3|}|}	nd}d}	tj	rA|t
gkrAd}| |||||	 W d   n1 sTw   Y  tD ]0}
dD ]+}| j||
|d	 | }|
||< | ||||| W d   n1 sw   Y  q_q[dS )
a  Check the C locale handling for various configurations

        Parameters:
            fs_encoding: expected sys.getfilesystemencoding() result
            stream_encoding: expected encoding for standard streams
            coerce_c_locale: setting to use for PYTHONCOERCECLOCALE
              None: don't set the variable at all
              str: the value set in the child's environment
            expected_warnings: expected warning lines on stderr
            extra_vars: additional environment variables to set in subprocess
        NFr   rR   r   T)Zdefault_localer   rS   )rT   Znominal_localer   )rU   r=   rE   rD   LEGACY_LOCALE_WARNINGupdaterV   rF   r   
is_androidr@   rN   rC   rW   )rJ   r+   r,   coerce_c_localerM   r*   Z
extra_varsrX   Z_expected_warningsZ_coercion_expectedrY   rT   rZ   r   r   r   _check_c_locale_coercion  sf   

z,LocaleCoercionTests._check_c_locale_coercionc                 C   s   | j ddd d d S )Nr	   r`   ra   rJ   r   r   r    test_PYTHONCOERCECLOCALE_not_seti  s   z4LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_setc                 C   s   dD ]
}| j dd|d qd S )N)r   1trueZfalser	   rb   rc   )rJ   Zsettingr   r   r   !test_PYTHONCOERCECLOCALE_not_zerom  s   z5LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_zeroc                 C   s   | j dddtgd d S )Nr	   warn)r`   rM   )ra   r@   rd   r   r   r   $test_PYTHONCOERCECLOCALE_set_to_warns  s   
z8LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_warnc                 C   s*   | j ttddd | j ttdddd d S )N0F)r`   r*   r   r`   r#   r*   )ra   rE   rD   rd   r   r   r   $test_PYTHONCOERCECLOCALE_set_to_zeroz  s   
z8LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_zeroc                 C   s0   | j ttd ddd | j ttddtgdd d S )Nr   Frl   ri   )r`   r#   rM   r*   )ra   rE   rD   r]   rd   r   r   r   test_LC_ALL_set_to_C  s   
z(LocaleCoercionTests.test_LC_ALL_set_to_Cc              
   C   s   t t jd }| t jt j| z	t t jd}W n t jy3 } z| t| W Y d }~nd }~ww |dkr=| d |tv rH| d|  d}tt	j
dd}tjtjd|gtj|d	d
}| |j | d S )Nr   r   z.test requires LC_CTYPE locale different than Czcoerced LC_CTYPE locale: %sz=import locale; print(locale.setlocale(locale.LC_CTYPE, None))rf   r   r   T)stdoutenvtext)locale	setlocaler"   Z
addCleanupErrorZskipTeststrTARGET_LOCALESr'   osenviron
subprocessrunsys
executablePIPErI   ro   r5   )rJ   old_loclocecoderp   r   r   r   r   #test_PYTHONCOERCECLOCALE_set_to_one  s(   
z7LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_one)NT)
r8   r9   r:   ra   re   rh   rj   rm   rn   r   r   r   r   r   r\     s    	
Jr\   c                   C   s   t   d S )N)r   reap_childrenr   r   r   r   tearDownModule  s   r   __main__)-rr   rw   ry   r{   Z	sysconfigrP   collectionsr   testr   Ztest.support.script_helperr   rC   rD   rE   rF   rv   platform
startswithr_   r$   r>   boolhasattrr   r   _fieldsZ_EncodingDetailsr   r]   r?   r=   r&   r@   rG   ZTestCaserH   rO   Zcpython_onlyZ
skipUnlessZget_config_varr\   r   r8   mainr   r   r   r   <module>   sx   




:	- 