
    ;eK                     (   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
          r"e	j        rdZdZn`e                    d           nJej                            d          rdZdZn+ej        dk    rdZnej        dk    rdZnej        dk    rdZdZd	Z eej        dvo ee d          o ee d                    Zd ZdZ ede          Z G d de          ZdZdZdada da!d Z" G d dej#                  Z$ G d d e$          Z%e	j&         ej'         ej(        d!          d"           G d# d$e$                                  Z)d% Z*e+d&k    r ej,                     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-8POSIXaixz	iso8859-1darwincygwinFvxworks)r   r	   nl_langinfoCODESETc                     d}t           r|dz  }|                    |           }t          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)locale_namecmd_fmtcmdresultpy_cmds        2/usr/lib/python3.11/test/test_c_locale_coercion.py_set_locale_in_subprocessr    W   sP    MG! TSS
..
%
%C)$LLLNFF9>    zBfsencoding stdin_info stdout_info stderr_info lang lc_ctype lc_allEncodingDetailsc                   h    e Zd Zd                    g d          Zed             Ze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                    |dz   }d|                     d          gz  }|                    |                     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_streamstream_infoexpected_langexpected_lc_ctypeexpected_lc_allenv_infos              r   get_expected_detailsz$EncodingDetails.get_expected_detailsr   s     "E) (9::;;7>>*<==>>> VY77 	D 3 (Z C C",,x;; "3_DCC=k=H===EEGGHHHr!   c                    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   failoutdecode
splitlinesr/   r0   errrstrip)r1   r5   r   r   stdout_lineschild_encoding_detailsstderr_liness          r   get_child_detailsz!EncodingDetails.get_child_details   s     .(D#":
 

 
 yA~~KKz((11<<>>!%cc<&8&@&@&B&B!C!Cz((1188::EEGG%|33r!   N)__name__
__module____qualname__joinr>   classmethodr<   rH    r!   r   r"   r"   e   sp        88 	% 	% 	% 	 	 I I [I" 4 4 [4 4 4r!   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                  F   t           d S g a t          D ]+} t          |           rt                               |            ,t           r,t           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 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   )target_locales    r   setUpModuler[      s8   
 $ ) 4 4$]33 	4$$]333 T/27>>?RSS N:%6::;;;R1NRRSSSZ5VZZ[[[R1NRRSSSZ5VZZ[[[6?66777L.HLLMMMMMN Nr!   c                       e Zd Zd ZdS )_LocaleHandlingTestCasec                     t                               |          }|\  }}t                               ||||          }	|                     ||	           |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"   rH   r<   assertEqual)
selfr5   expected_fs_encodingexpected_stream_encodingexpected_warningsr2   r   encoding_detailsrG   expected_detailss
             r   _check_child_encoding_detailsz5_LocaleHandlingTestCase._check_child_encoding_details   s     !228<<)/&,*?? $	
 
 	)+;<<<$ "'899999r!   N)rI   rJ   rK   rf   rN   r!   r   r]   r]      s#        : : : : :r!   r]   c                   *    e Zd Zed             Zd ZdS )LocaleConfigurationTestsc                 <    t           st          j        d          d S )Nz No C-with-UTF-8 locale available)rP   unittestSkipTest)r1   s    r   
setUpClassz#LocaleConfigurationTests.setUpClass   s+     ! 	H#$FGGG	H 	Hr!   c           
      &   d | _         d}d}ddddd}dD ]{}t          D ]q}|dk    r|dk    r|                     ||          5  |                                }|||<   |                     |||d d	           d d d            n# 1 swxY w Y   r|d S )
Nr
   r   r)   r*   r+   r   r)   r*   r)   r   )env_varconfigured_localeF)rc   r2   )maxDiffrP   subTestcopyrf   )r`   ra   rb   base_var_dictrp   locale_to_setvar_dicts          r   )test_external_target_locale_configurationzBLocaleConfigurationTests.test_external_target_locale_configuration   sQ    &#*  #%	
 
 , 	P 	PG!2 P P f$$')A)A\\'4A " C C P P,1133H(5HW%66x7K7OIMIN	 7 P P P	P P P P P P P P P P P P P P PP	P 	Ps   4BBBN)rI   rJ   rK   rM   rl   rx   rN   r!   r   rh   rh      sF         H H [HP P P P Pr!   rh   PY_COERCE_C_LOCALEz(C locale coercion disabled at build timec                   >    e Zd Z	 	 d
dZd Zd Zd Zd Zd Zd	 Z	dS )LocaleCoercionTestsNTc           
      b   d| _         t          st          }t          }d}|rt          g}ddddd}|                    |           |||d<   |                     d|          5  t          r|}|}	nd}d}	t          j	        r|t          gk    rd}|                     |||||	           ddd           n# 1 swxY w Y   t          D ]i}
dD ]d}|                     ||
|	          5  |                                }|
||<   |                     |||||           ddd           n# 1 swxY w Y   ejd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   rn   r   T)default_localer   ro   )rp   nominal_localer   )rr   rP   rX   rW   LEGACY_LOCALE_WARNINGupdaters   rY   r   
is_androidrS   rf   rV   rt   )r`   r3   r4   coerce_c_localerc   r2   
extra_varsru   _expected_warnings_coercion_expectedrv   rp   rw   s                r   _check_c_locale_coercionz,LocaleCoercionTests._check_c_locale_coercion  so   "   	<7K?O %  <%:$;! #%	
 
 	Z(((&3BM/0 \\.=  ? ? 	C 	C0 +%6"%6""%)"%*"
 " *&+?*@@@%)"..}/:/>/A/A	C C C	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C, ; 	J 	JM/ J J\\'1>6E " G G 
J 
J  -1133H(5HW%66x7B7F7H7H	J J J
J 
J 
J 
J 
J 
J 
J 
J 
J 
J 
J 
J 
J 
J 
JJ	J 	Js%   #AB33B7:B7#3D""D&)D&c                 6    |                      ddd            d S )Nr
   r   r   r`   s    r    test_PYTHONCOERCECLOCALE_not_setz4LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_seti  s#    %%gw%MMMMMr!   c                 @    dD ]}|                      dd|           d S )N)r   1truefalser
   r   r   )r`   settings     r   !test_PYTHONCOERCECLOCALE_not_zeroz5LocaleCoercionTests.test_PYTHONCOERCECLOCALE_not_zerom  s?     2 	U 	UG))'7G)TTTT	U 	Ur!   c                 D    |                      dddt          g           d S )Nr
   warn)r   rc   )r   rS   r   s    r   $test_PYTHONCOERCECLOCALE_set_to_warnz8LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_warns  s;    %%gw6<9M8N 	& 	P 	P 	P 	P 	Pr!   c                     |                      t          t          dd           |                      t          t          ddd           d S )N0F)r   r2   r   r   r+   r2   )r   rX   rW   r   s    r   $test_PYTHONCOERCECLOCALE_set_to_zeroz8LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_zeroz  sc    %%&C&G698= 	& 	? 	? 	?
 	%%&C&G69-08=	 	& 	? 	? 	? 	? 	?r!   c                     |                      t          t          d dd           |                      t          t          ddt          gd           d S )Nr   Fr   r   )r   r+   rc   r2   )r   rX   rW   r   r   s    r   test_LC_ALL_set_to_Cz(LocaleCoercionTests.test_LC_ALL_set_to_C  sl    %%&C&G6:-08=	 	& 	? 	? 	? 	%%&C&G6<-09N8O8= 	& 	? 	? 	? 	? 	?r!   c                    t          j        t           j        d           }|                     t           j        t           j        |           	 t          j        t           j        d          }n># t           j        $ r,}|                     t          |                     Y d }~nd }~ww xY w|dk    r|                     d           |t          v r|                     d|z             d}t          t          j
        d          }t          j        t          j        d|gt          j        |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))r   r   r   T)stdoutenvtext)locale	setlocaler*   
addCleanupErrorskipTeststrTARGET_LOCALESr/   osenviron
subprocessrunsys
executablePIPEr_   r   rD   )r`   old_loclocecoder   r   s          r   #test_PYTHONCOERCECLOCALE_set_to_onez7LocaleCoercionTests.test_PYTHONCOERCECLOCALE_set_to_one  sG   "6?D99(&/7CCC	""6?B77CC| 	" 	" 	"MM#a&&!!!!!!!!	"#::MMJKKK.  MM7#=>>> O2:3777ncndD9$.O!$"&( ( ( 	**,,c22222s   A, ,B';"B""B')NT)
rI   rJ   rK   r   r   r   r   r   r   r   rN   r!   r   r{   r{     s         4837	HJ HJ HJ HJTN N NU U UP P P? ? ?? ? ?3 3 3 3 3r!   r{   c                  ,    t          j                     d S )N)r   reap_childrenrN   r!   r   tearDownModuler     s    r!   __main__)-r   r   r   r   	sysconfigrj   collectionsr   testr   test.support.script_helperr   rV   rW   rX   rY   r   platform
startswithr   r,   rQ   boolhasattrr   r    _fields_EncodingDetailsr"   r   rR   rP   r.   rS   r[   TestCaser]   rh   cpython_only
skipUnlessget_config_varr{   r   rI   mainrN   r!   r   <module>r      sO    				     



      " " " " " "       ; ; ; ; ; ; "%o 6  %, ! '  %) !/// <7## , 	6,3)(/%% 	&,,W5555\U## ,(3%$/!!\X$+!!\X ).%%\Y(/%$+! 1 "TL++ GFM""GFI     O:/99 54 54 54 54 54& 54 54 54t J     N N N<: : : : :h/ : : :<)P )P )P )P )P6 )P )P )PZ 	-Y-.BCC?A AO3 O3 O3 O3 O31 O3 O3A A O3d   zHMOOOOO r!   