B
    18\?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re
jjrdZdZqed n4ejdrdZdZnejdkrdZnejdkrdZdZeejdk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j)e*e+d$d%G d&d' d'e'Z,d(d) Z-e.d*kre-  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POSIX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   _/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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 )zAdjust 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   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__joinr0   classmethodr,   staticmethodr/   r8   r   r   r   r   r   d   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   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    
rC   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   r8   r,   assertEqual)
selfr*   expected_fs_encodingexpected_stream_encodingexpected_warningsr'   r   Zencoding_detailsr7   Zexpected_detailsr   r   r   _check_child_encoding_details   s    
z5_LocaleHandlingTestCase._check_child_encoding_detailsN)r9   r:   r;   rJ   r   r   r   r   rD      s   rD   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?   unittest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)rI   r'   )maxDiffr?   subTestcopyrJ   )rF   rG   rH   base_var_dictrO   locale_to_setvar_dictr   r   r   )test_external_target_locale_configuration   s(    


zBLocaleConfigurationTests.test_external_target_locale_configurationN)r9   r:   r;   r=   rN   rV   r   r   r   r   rK      s   rK   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rd}| |||||	 W dQ R X xXtD ]P}
xJdD ]B}| j||
|d	( | }|
||< | ||||| W dQ R X qW qW 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   )rO   Znominal_localer   )rP   r?   EXPECTED_C_LOCALE_FS_ENCODING!EXPECTED_C_LOCALE_STREAM_ENCODINGLEGACY_LOCALE_WARNINGupdaterQ   !EXPECT_COERCION_IN_DEFAULT_LOCALEtestsupport
is_androidrB   rJ   EXPECTED_C_LOCALE_EQUIVALENTSrR   )rF   r(   r)   coerce_c_localerI   r'   Z
extra_varsrS   Z_expected_warningsZ_coercion_expectedrT   rO   rU   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)rb   )rc   )rF   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truefalsezutf-8)rb   )rc   )rF   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)rb   rI   )rc   rB   )rF   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)rb   r'   r   )rb   r   r'   )rc   rY   rZ   )rF   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)rb   r   r'   rj   )rb   r   rI   r'   )rc   rY   rZ   r[   )rF   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))re   )r   z-cT)stdoutenvtext)locale	setlocaler   
addCleanupskipTestTARGET_LOCALESr$   osenviron
subprocessrunsys
executablePIPErE   ro   r6   )rF   Zold_locloccoderp   r   r   r   r   #test_PYTHONCOERCECLOCALE_set_to_one  s    
z7LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_one)NT)
r9   r:   r;   rc   rd   ri   rk   rm   rn   r   r   r   r   r   rX     s   	 
FrX   c               C   s   t jtt t j  d S )N)r^   r_   run_unittestrK   rX   reap_childrenr   r   r   r   	test_main  s    r   __main__)/rr   rw   shutilry   r{   	sysconfigrL   collectionsr   Ztest.supportr^   Ztest.support.script_helperr   r   ra   rZ   rY   r]   rv   platform
startswithr_   r`   r    r@   boolhasattrr   r   _fieldsZ_EncodingDetailsr   r[   rA   r?   r#   rB   rC   TestCaserD   rK   cpython_only
skipUnlessget_config_varrX   r   r9   r   r   r   r   <module>   sf   





H-
 
