B
    u9a?K  ã               @   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Zd dlmZ d dl	Z
d dlmZmZ ddgZdZdZdZdd	d
gZej d¡r¤e
jjr˜dZdZqØe d¡ n4ej d¡rºdZdZnejdkrÊdZnejdkrØdZdZeejdk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j)e *e +d$¡d%¡G d&d'„ d'e'ƒƒƒZ,d(d)„ Z-e.d*kr¤e-ƒ  dS )+é    N)Ú
namedtuple)Úrun_python_until_endÚ interpreter_requires_environmentÚCzinvalid.asciiÚasciiTzC.UTF-8zC.utf8zUTF-8Úlinuxzutf-8ZPOSIXZaixz	iso8859-1ÚdarwinÚcygwinF)zC.UTF-8zC.utf8zUTF-8)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))z-cÚ )ÚPYTHONCOERCECLOCALEr   )Ú_check_nl_langinfo_CODESETÚformatr   Úrc)Zlocale_nameZcmd_fmtÚcmdÚresultÚpy_cmd© r   ú,/usr/lib/python3.7/test_c_locale_coercion.pyÚ_set_locale_in_subprocessV   s    
r   zBfsencoding stdin_info stdout_info stderr_info lang lc_ctype lc_allÚEncodingDetailsc            
   @   sJ   e Zd Zd dddddddd	g¡Zed
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¡ ¡ }|rFt ¡ }n| dd¡ ¡ }| dd¡ ¡ }	|||	f}
t| |f||
žŽ  ¡ ƒS )	z;Returns expected child process details for a given encodingz:{}é   ÚsurrogateescapeÚbackslashreplaceÚLANGznot setÚLC_CTYPEÚLC_ALL)r   ÚappendÚgetÚlowerÚ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_detailsq   s    

z$EncodingDetails.get_expected_detailsc             C   s$   |   dd¡} |   dd¡} |  ¡ } | S )zçAdjust the output to handle platform specific idiosyncrasies

        * Some platforms report ASCII as ANSI_X3.4-1968
        * Some platforms report ASCII as US-ASCII
        * Some platforms report UTF-8 instead of utf-8
        s   ANSI_X3.4-1968s   asciis   US-ASCII)Úreplacer!   )Údatar   r   r   Ú_handle_output_variationsƒ   s    z)EncodingDetails._handle_output_variationsc             C   sr   t ddd| jf|Ž\}}|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=0z-cr   r   )r   ÚCHILD_PROCESS_SCRIPTr   Zfailr.   ÚoutÚdecodeÚ
splitlinesr#   r$   ÚerrÚrstrip)r%   r)   r   r   Zadjusted_outputZstdout_linesZchild_encoding_detailsÚstderr_linesr   r   r   Úget_child_details   s    


z!EncodingDetails.get_child_detailsN)
Ú__name__Ú
__module__Ú__qualname__Újoinr/   Úclassmethodr+   Ústaticmethodr.   r6   r   r   r   r   r   d   s   zëPython 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.zŠPython detected LC_CTYPE=C: LC_CTYPE coerced to {} (set another locale or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).c              C   sJ   t d k	rd S g a xtD ]} t| ƒrt  | ¡ qW t rFt d at t¡ad S )Nr   )ÚAVAILABLE_TARGETSÚ_C_UTF8_LOCALESr   r   r"   ÚCLI_COERCION_WARNING_FMTr   ÚCLI_COERCION_WARNING)Ztarget_localer   r   r   ÚsetUpModule½   s    
rA   c               @   s   e Zd Zdd„ ZdS )Ú_LocaleHandlingTestCasec       
      C   sJ   t  |¡}|\}}t  ||||¡}	|  ||	¡ |dk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   rH   r   r   r   r   rB   Ò   s   rB   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œ}xjdD ]b}x\tD ]T}|dkrB|dkrBq,| j||d* | ¡ }|||< | j|||d dd	 W d Q R X q,W q"W d S )
Nzutf-8r   )r   r   r   r   )r   r   r   zUTF-8)Úenv_varZconfigured_localeF)rG   r&   )ÚmaxDiffr=   ÚsubTestÚcopyrH   )rD   rE   rF   Úbase_var_dictrL   Úlocale_to_setÚvar_dictr   r   r   Ú)test_external_target_locale_configurationú   s(    


zBLocaleConfigurationTests.test_external_target_locale_configurationN)r7   r8   r9   r;   rK   rS   r   r   r   r   rI   ð   s   rI   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k	rH||d< | jd|dD trf|}|}	nd}d}	tj	j
r„|tgkr„d}|  |||||	¡ W dQ R X xXtD ]P}
xJdD ]B}| j||
|d	( | ¡ }|
||< |  |||||¡ W dQ R X q°W q¦W 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   )r   r   r   r   r   T)Zdefault_localer   )r   r   )rL   Znominal_localer   )rM   r=   ÚEXPECTED_C_LOCALE_FS_ENCODINGÚ!EXPECTED_C_LOCALE_STREAM_ENCODINGÚLEGACY_LOCALE_WARNINGÚupdaterN   Ú!EXPECT_COERCION_IN_DEFAULT_LOCALEÚtestÚsupportÚ
is_androidr@   rH   ÚEXPECTED_C_LOCALE_EQUIVALENTSrO   )rD   r'   r(   Úcoerce_c_localerG   r&   Z
extra_varsrP   Z_expected_warningsZ_coercion_expectedrQ   rL   rR   r   r   r   Ú_check_c_locale_coercion#  sR    





z,LocaleCoercionTests._check_c_locale_coercionc             C   s   | j ddd d d S )Nzutf-8)r^   )r_   )rD   r   r   r   Ú test_PYTHONCOERCECLOCALE_not_setm  s    z4LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_setc             C   s"   xdD ]}| j dd|d qW d S )N)r   Ú1ÚtrueZfalsezutf-8)r^   )r_   )rD   Zsettingr   r   r   Ú!test_PYTHONCOERCECLOCALE_not_zeroq  s    
z5LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_zeroc             C   s   | j dddtgd d S )Nzutf-8Úwarn)r^   rG   )r_   r@   )rD   r   r   r   Ú$test_PYTHONCOERCECLOCALE_set_to_warnw  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_   rU   rV   )rD   r   r   r   Ú$test_PYTHONCOERCECLOCALE_set_to_zero~  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   F)r^   r   r&   rd   )r^   r   rG   r&   )r_   rU   rV   rW   )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|¡ t  t jd¡}|dkr@|  d¡ |tkrV|  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))ra   )r   z-cT)ÚstdoutÚenvÚtext)ÚlocaleÚ	setlocaler   Z
addCleanupZskipTestÚTARGET_LOCALESr#   ÚosÚenvironÚ
subprocessÚrunÚsysÚ
executableÚPIPErC   ri   r4   )rD   Zold_locÚlocÚcoderj   r   r   r   r   Ú#test_PYTHONCOERCECLOCALE_set_to_oneš  s    
z7LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_one)NT)
r7   r8   r9   r_   r`   rc   re   rg   rh   rx   r   r   r   r   rT     s   	 
FrT   c               C   s   t j tt¡ t j ¡  d S )N)rZ   r[   Zrun_unittestrI   rT   Zreap_childrenr   r   r   r   Ú	test_main¯  s    ry   Ú__main__)/rl   ro   Zshutilrq   rs   Z	sysconfigrJ   Úcollectionsr   Ztest.supportrZ   Ztest.support.script_helperr   r   r]   rV   rU   rY   rn   ÚplatformÚ
startswithr[   r\   r   r>   ÚboolÚhasattrr   r   Ú_fieldsZ_EncodingDetailsr   rW   r?   r=   r"   r@   rA   ZTestCaserB   rI   Zcpython_onlyZ
skipUnlessZget_config_varrT   ry   r7   r   r   r   r   Ú<module>   sf   





H-
 
