
    dafj0                         d 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	m
Z
 ddlmZmZ  e	         G d dej                        Zy)	z%Tests to cover the Tools/i18n package    N)dedent)assert_python_ok)skip_if_missingtoolsdir)temp_cwdtemp_dirc                   t   e Zd ZdZej
                  j                  edd      Zd Z	d Z
d Zd Z ej                  ej                   j#                  d      d	      d
        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*d# Z+d$ Z,d% Z-d& Z.d' Z/y())Test_pygettextzTests for the pygettext.py tooli18nzpygettext.pyc                     i }|j                  d      D ]O  }|r|j                  d      r|j                  d      }|j                  dd      \  }}|j                         ||<   Q |S )z: utility: return the header of a .po file as a dictionary 
)#msgidmsgstr":   )split
startswithstrip)selfdataheaderslinekeyvals         4/root/Python-3.12.4/Lib/test/test_tools/test_i18n.py
get_headerzTest_pygettext.get_header   sh    JJt$ 	'D4??+BC::c?Dzz#a(HC99;GCL	'     c                    g }d}g }|j                  d      D ]  }|rW|j                  d      r!|j                  |j                  d             n%|j                  dj	                  |             g }d}\|j                  d      sn|t        d      d }|j                  |j                  d             d} |r |j                  dj	                  |             |S )z= utility: return all msgids in .po file as a list of strings Fr   r   zmsgid NT)r   r   appendr   joinlen)r   r   msgidsreading_msgid	cur_msgidr   s         r   
get_msgidszTest_pygettext.get_msgids    s    	JJt$ 	4D??3'$$TZZ_5MM$))I"67 "I$)Mx(CMN+  C1 $	4 dii	23r   c                 b   d}t        d      5 }t        |dd      5 }|j                  |       ddd       t        | j                  d|       t        dd      5 }|j                         }ddd       ddd       | j                        S # 1 sw Y   _xY w# 1 sw Y   .xY w# 1 sw Y   2xY w)z: utility: return all msgids extracted from module_content ztest_docstrings.pyNwutf-8encodingz-Dmessages.pot)r   openwriter   scriptreadr'   )r   module_contentfilenamecwdfpr   s         r   extract_docstrings_from_strz*Test_pygettext.extract_docstrings_from_str8   s    'd^ 	!shg6 )"()T[[$9nw7 !2wwy!		! t$$) )! !		! 	!s9   B%B,B%B,B%B	B%B"	B%%B.c                 8   t        d      5 }t        | j                         t        dd      5 }|j	                         }ddd       | j                        }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d	|       | j                  d
|       | j                  d|       | j                  d|       | j                  d|       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zMake sure the required fields are in the header, according to:
           http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
        Nr-   r*   r+   zProject-Id-VersionPOT-Creation-DatezPO-Revision-DatezLast-TranslatorzLanguage-TeamzMIME-VersionzContent-TypezContent-Transfer-EncodingzGenerated-By)r   r   r0   r.   r1   r   assertIn)r   r4   r5   r   headers        r   test_headerzTest_pygettext.test_headerC   s     d^ 	2sT[[)nw7 !2wwy!__T*FMM.7MM-v6MM,f5MM+V4MM/62MM.&1MM.&1MM5v>MM.&1	2 	2! !	2 	2s#   #DD B;DD		DDaixzbpo-29972: broken test on AIXc                 n   ddl m } t        d      5 }t        | j                         t	        dd      5 }|j                         }ddd       | j                        }|d   }|j                  d      r|dt        d        }|j                  |d	       ddd       y# 1 sw Y   ZxY w# 1 sw Y   yxY w)
z; Match the date format from xgettext for POT-Creation-Date r   )datetimeNr-   r*   r+   r8   z\nz%Y-%m-%d %H:%M%z)
r>   r   r   r0   r.   r1   r   endswithr#   strptime)r   r>   r4   r5   r   r:   creationDates          r   test_POT_Creation_Datez%Test_pygettext.test_POT_Creation_Date]   s     	&d^ 	@sT[[)nw7 !2wwy!__T*F!"56L $$U++Lc%j[9 l,>?	@ 	@! !	@ 	@s#   #B+BAB+B(	$B++B4c                     dD ]K  }| j                  |      5  | j                  t        d|z              }| j                  d|       d d d        M y # 1 sw Y   XxY w)Nz	"""doc"""z
r'''doc'''zR'doc'zu"doc"zE                def foo(bar):
                    %s
                docsubTestr6   r   r9   r   rE   r$   s      r   test_funcdocstringz!Test_pygettext.test_funcdocstringp   o    B 	-Cc" -99& B B ;  eV,- -	-- -   0AA	c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz5        def foo(bar):
            b"""doc"""
        rE   r6   r   assertFalser   r$   r   s      r   test_funcdocstring_bytesz'Test_pygettext.test_funcdocstring_bytesy   B    11& : 3  	VFEu~%FGF
   	A A c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz5        def foo(bar):
            f"""doc"""
        rE   rM   rO   s      r   test_funcdocstring_fstringz)Test_pygettext.test_funcdocstring_fstring   rQ   rR   c                     dD ]K  }| j                  |      5  | j                  t        d|z              }| j                  d|       d d d        M y # 1 sw Y   XxY w)NrD   z@                class C:
                    %s
                rE   rF   rH   s      r   test_classdocstringz"Test_pygettext.test_classdocstring   rJ   rK   c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz0        class C:
            b"""doc"""
        rE   rM   rO   s      r   test_classdocstring_bytesz(Test_pygettext.test_classdocstring_bytes   rQ   rR   c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz0        class C:
            f"""doc"""
        rE   rM   rO   s      r   test_classdocstring_fstringz*Test_pygettext.test_classdocstring_fstring   rQ   rR   c                     dD ]K  }| j                  |      5  | j                  t        d|z              }| j                  d|       d d d        M y # 1 sw Y   XxY w)NrD   z#                %s
                rE   rF   rH   s      r   test_moduledocstringz#Test_pygettext.test_moduledocstring   sm    B 	-Cc" -99& BB ;  eV,	- -	-- -rK   c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz        b"""doc"""
        rE   rM   rO   s      r   test_moduledocstring_bytesz)Test_pygettext.test_moduledocstring_bytes   B    11& : 3  	VFEu~%FGFrR   c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz        f"""doc"""
        rE   rM   rO   s      r   test_moduledocstring_fstringz+Test_pygettext.test_moduledocstring_fstring   r_   rR   c                 J    | j                  d      }| j                  d|       y )Nz_("""doc""" r'str' u"ing")	docstring)r6   r9   r   r$   s     r   
test_msgidzTest_pygettext.test_msgid   s#    1102k6*r   c                 x    | j                  d      }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz_(b"""doc""")rE   r6   rN   rO   s      r   test_msgid_byteszTest_pygettext.test_msgid_bytes   4    11/BVFEu~%FGF   	77c                 x    | j                  d      }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz_(f"""doc""")rE   rg   rO   s      r   test_msgid_fstringz!Test_pygettext.test_msgid_fstring   ri   rj   c                 \    | j                  t        d            }| j                  d|       y)z3 Test docstrings for functions with annotated args z9        def foo(bar: str):
            """doc"""
        rE   Nr6   r   r9   rd   s     r   !test_funcdocstring_annotated_argsz0Test_pygettext.test_funcdocstring_annotated_args   .    11& : 3  	eV$r   c                 \    | j                  t        d            }| j                  d|       y)z: Test docstrings for functions with annotated return type z;        def foo(bar) -> str:
            """doc"""
        rE   Nrn   rd   s     r   #test_funcdocstring_annotated_returnz2Test_pygettext.test_funcdocstring_annotated_return   rp   r   c                 \    | j                  t        d            }| j                  d|       y)z6 Test docstring for functions with default arg values z7        def foo(bar=()):
            """doc"""
        rE   Nrn   rd   s     r    test_funcdocstring_defvalue_argsz/Test_pygettext.test_funcdocstring_defvalue_args   rp   r   c                     | j                  t        d            }| j                  d|       | j                  d|       | j                  d|       y)z Test docstring extraction for multiple functions combining
        annotated args, annotated return types and default arg values
        z        def foo1(bar: tuple=()) -> str:
            """doc1"""

        def foo2(bar: List[1:2]) -> (lambda x: x):
            """doc2"""

        def foo3(bar: 'func'=lambda x: x) -> {1: 2}:
            """doc3"""
        doc1doc2doc3Nrn   rd   s     r   !test_funcdocstring_multiple_funcsz0Test_pygettext.test_funcdocstring_multiple_funcs   sL     11& 	: 	3 	 	ff%ff%ff%r   c                 \    | j                  t        d            }| j                  d|       y)ze Test docstring extraction for a class with colons occurring within
        the parentheses.
        z\        class D(L[1:2], F({1: 2}), metaclass=M(lambda x: x)):
            """doc"""
        rE   Nrn   rd   s     r   test_classdocstring_early_colonz.Test_pygettext.test_classdocstring_early_colon   s0     11& : 3  	eV$r   c                 \    | j                  t        d            }| j                  d|       y )Nz"        f"{_('foo bar')}"
        foo barrn   rd   s     r   test_calls_in_fstringsz%Test_pygettext.test_calls_in_fstrings   .    11& : 3  	i(r   c                 \    | j                  t        d            }| j                  d|       y )Nz#        rf"{_('foo bar')}"
        r}   rn   rd   s     r   test_calls_in_fstrings_rawz)Test_pygettext.test_calls_in_fstrings_raw   r   r   c                 \    | j                  t        d            }| j                  d|       y )Nz+        f"""{f'{_("foo bar")}'}"""
        r}   rn   rd   s     r   test_calls_in_fstrings_nestedz,Test_pygettext.test_calls_in_fstrings_nested   r   r   c                 \    | j                  t        d            }| j                  d|       y )Nz&        f"{obj._('foo bar')}"
        r}   rn   rd   s     r    test_calls_in_fstrings_attributez/Test_pygettext.test_calls_in_fstrings_attribute  r   r   c                 \    | j                  t        d            }| j                  d|       y )Nz*        f"{type(str)('foo bar')}"
        r}   r6   r   assertNotInrd   s     r   (test_calls_in_fstrings_with_call_on_callz7Test_pygettext.test_calls_in_fstrings_with_call_on_call  s0    11& : 3  	F+r   c                 \    | j                  t        d            }| j                  d|       y )Nz6        f"{_('foo {bar}').format(bar='baz')}"
        	foo {bar}rn   rd   s     r   "test_calls_in_fstrings_with_formatz1Test_pygettext.test_calls_in_fstrings_with_format  s.    11& : 3  	k6*r   c                     | j                  t        d            }| j                  |D cg c]	  }d|v s| c}       y c c}w )Nz%        f"{_(f'foo {bar}')}"
        r   rM   rO   s      r   )test_calls_in_fstrings_with_wrong_input_1z8Test_pygettext.test_calls_in_fstrings_with_wrong_input_1  sC    11& : 3  	VLE{e7K%LMLrR   c                 \    | j                  t        d            }| j                  d|       y )Nz        f"{_(1)}"
        r   r   rd   s     r   )test_calls_in_fstrings_with_wrong_input_2z8Test_pygettext.test_calls_in_fstrings_with_wrong_input_2  s0    11& : 3  	F#r   c                     | j                  t        d            }| j                  d|       | j                  d|       y )Nz%        f"{_('foo', 'bar')}"
        foobarr   rd   s     r   (test_calls_in_fstring_with_multiple_argsz7Test_pygettext.test_calls_in_fstring_with_multiple_args#  s@    11& : 3  	''r   c                     | j                  t        d            }| j                  d|       | j                  d|       | j                  d|       y )Nz)        f"{_('foo', bar='baz')}"
        r   r   bazr   rd   s     r   'test_calls_in_fstring_with_keyword_argsz6Test_pygettext.test_calls_in_fstring_with_keyword_args*  sP    11& : 3  	'''r   c                     | j                  t        d            }| j                  d|       | j                  d|       y )Nz*        f"{_(f'foo') + _('bar')}"
        r   r   )r6   r   r   r9   rd   s     r   5test_calls_in_fstring_with_partially_wrong_expressionzDTest_pygettext.test_calls_in_fstring_with_partially_wrong_expression2  s>    11& : 3  	'eV$r   c           	         d}d}d}t        d      5  t        d      5 }t        j                  t        j                  j                  |d             t        t        j                  j                  |dd      dd	      5 }|j                  d
|d       ddd       t        j                  t        j                  j                  |d             t        t        j                  j                  |dd      dd	      5 }|j                  d
|d       ddd       t        j                  t        j                  j                  |d             t        t        j                  j                  |dd      dd	      5 }|j                  d
|d       ddd       t        | j                  |       t        dd	      5 }|j                         }ddd       | j                  d| d       | j                  d| d|       | j                  ||       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y    xY w# 1 sw Y   xY w# 1 sw Y   ~xY w# 1 sw Y   DxY w# 1 sw Y   yxY w)zVMake sure the directories are inspected for source files
           bpo-31920
        zText to translate1zText to translate2zText to ignoreNpypkgzpymod.pyr)   r*   r+   z_()zpkg.pyz	pymod2.pyCVSz	pymod3.pyr-   zmsgid "r   )r   r   osmkdirpathr"   r.   r/   r   r0   r1   r9   r   )r   text1text2text3sdirsfiler5   r   s           r   test_files_listzTest_pygettext.test_files_list9  s    %$ d^ 	*Xd^ 	*tHHRWW\\$01bggll4*=s&( -+0b	O,- HHRWW\\$12bggll4;?&( -+0b	O,- HHRWW\\$./bggll4<c&( -+0b	O,- T[[$/nw7 !2wwy!MMGE7!,d3MMGE7!,d3UD)%	* 	* 	*- -- -- -! !	* 	* 	* 	*s   I"A!I?H$A(I=H1A(I;H>+I<I
AII"$H.)I1H;6I>II
III	I""I+N)0__name__
__module____qualname____doc__r   r   r"   r   r0   r   r'   r6   r;   unittestskipIfsysplatformr   rB   rI   rP   rT   rV   rX   rZ   r\   r^   ra   re   rh   rl   ro   rr   rt   ry   r{   r~   r   r   r   r   r   r   r   r   r   r   r    r   r   r
   r
      s    )WW\\(6>:F	0	%24 X__S\\,,U346@6@"-HH-HH-HH+
HH%%%&$%)))),+N$((%*r   r
   )r   r   r   r   textwrapr   test.support.script_helperr   test.test_toolsr   r   test.support.os_helperr   r   TestCaser
   r   r   r   <module>r      s9    + 	 
   7 5 5  B*X&& B*r   