a
    zeK                     @   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
re	jrdZdZqed nHejdrdZdZn2ejdkrdZn"ejdkrdZnejdkrdZdZd	Zeejdvoee doe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 )*    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cygwinFZ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   1/usr/lib/python3.9/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s0|| |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__joinr/   classmethodr.   r6   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 rBt d attatj	rt
dt  t
dt t
dt t
dt t
dt t
dt t
dt 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"    
rF   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   r6   r.   assertEqual)
selfr,   expected_fs_encodingexpected_stream_encodingexpected_warningsr)   r   Zencoding_detailsr5   Zexpected_detailsr   r   r   _check_child_encoding_details   s    
z5_LocaleHandlingTestCase._check_child_encoding_detailsN)r7   r8   r9   rM   r   r   r   r   rG      s   rG   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 ]r}tD ]h}|dkr>|dkr>q(| j||d4 | }|||< | j|||d dd	 W d    q(1 s0    Y  q(q d S )
Nr	   r   r    r!   r"   r   r    r!   r    r   )env_varZconfigured_localeF)rL   r)   )maxDiffr<   subTestcopyrM   )rI   rJ   rK   base_var_dictrS   locale_to_setvar_dictr   r   r   )test_external_target_locale_configuration   s.    zBLocaleConfigurationTests.test_external_target_locale_configurationN)r7   r8   r9   r;   rP   rZ   r   r   r   r   rN      s   
rN   ZPY_COERCE_C_LOCALEz(C locale coercion disabled at build timec                   @   sF   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rH||d< | jd|dL trf|}|}	nd}d}	tj	r|t
gkrd}| |||||	 W d   n1 s0    Y  tD ]b}
dD ]X}| j||
|d	2 | }|
||< | ||||| W d   q1 s0    Y  qqd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   rQ   r   T)Zdefault_localer   rR   )rS   Znominal_localer   )rT   r<   rD   rC   LEGACY_LOCALE_WARNINGupdaterU   rE   r   
is_androidr?   rM   rB   rV   )rI   r*   r+   coerce_c_localerL   r)   Z
extra_varsrW   Z_expected_warningsZ_coercion_expectedrX   rS   rY   r   r   r   _check_c_locale_coercion  s^    
"z,LocaleCoercionTests._check_c_locale_coercionc                 C   s   | j ddd d d S )Nr	   r_   r`   rI   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	   ra   rb   )rI   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_   rL   )r`   r?   rc   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)   )r`   rD   rC   rc   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   Frk   rh   )r_   r"   rL   r)   )r`   rD   rC   r\   rc   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 n4 t jyf } z| t| W Y d }~n
d }~0 0 |dkrz| d |tv r| 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))re   r   r   T)stdoutenvtext)locale	setlocaler!   Z
addCleanupErrorZskipTeststrTARGET_LOCALESr&   osenviron
subprocessrunsys
executablePIPErH   rn   r4   )rI   Zold_loclocecodero   r   r   r   r   #test_PYTHONCOERCECLOCALE_set_to_one  s$    $
z7LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_one)NT)
r7   r8   r9   r`   rd   rg   ri   rl   rm   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__)-rq   rv   rx   rz   Z	sysconfigrO   collectionsr   testr   Ztest.support.script_helperr   rB   rC   rD   rE   ru   platform
startswithr^   r#   r=   boolhasattrr   r   _fieldsZ_EncodingDetailsr   r\   r>   r<   r%   r?   rF   ZTestCaserG   rN   Zcpython_onlyZ
skipUnlessZget_config_varr[   r   r7   mainr   r   r   r   <module>   st   



:	- 
