a 5êdgéHã@sÆdZddlZddlZddlZddlZddlZddlZdd„ZdZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZdZGdd„dejƒZGdd„deƒZGdd„deƒZGdd„deƒZedkrÂe ¡dS)z>Tests for the unparse.py script in the Tools/parser directory.éNcCs6t |¡}| ¡WdƒS1s(0YdS)znRead and return the contents of a Python source file (as a string), taking into account the file encoding.N)ÚtokenizeÚopenÚread)ÚfilenameÚstream©rú-/usr/local/lib/python3.9/test/test_unparse.pyÚ read_pyfile s r zQdef f(): for x in range(10): break else: y = 2 z = 3 zIdef g(): while True: break else: y = 2 z = 3 zQfrom . import fred from .. import barney from .australia import shrimp as prawns zzdef f(): x = 1 def g(): nonlocal x x = 2 y = 7 def h(): nonlocal x, y zOtry: 1 / 0 except ZeroDivisionError as e: raise ArithmeticError from e z@f1(arg) @f2 class Foo: pass z=if cond1: suite1 elif cond2: suite2 else: suite3 z,if cond1: suite1 elif cond2: suite2 zctry: suite1 except ex1: suite2 except ex2: suite3 else: suite4 finally: suite5 zwith f(): suite1 zwith f() as x: suite1 z$with f() as x, g() as y: suite1 )Úzclass foo: zdef foo(): zasync def foo(): c@sFeZdZdd„Zdd„Zefdd„Zddd „Zdd d „Zdd d „Z dS)Ú ASTTestCasecCs| t |¡t |¡¡dS©N)Ú assertEqualÚastÚdump)ÚselfÚast1Úast2rrrÚassertASTEqualwszASTTestCase.assertASTEqualcKsl|j||dJtj|fi|¤Ž}t |¡}tj|fi|¤Ž}| ||¡Wdƒn1s^0YdS)N)Úcode1Zast_parse_kwargs)ÚsubTestrÚparseÚunparser)rrÚkwargsrÚcode2rrrrÚcheck_ast_roundtripzs  zASTTestCase.check_ast_roundtripcCs@|j|d | |tj|¡Wdƒn1s20YdS)N)Únode)rZ assertRaisesrr)rrZraisesrrrÚ check_invalidszASTTestCase.check_invalidNcCs |p|}t t |¡¡}||fSr )rrr©rrrrrrÚ get_source…szASTTestCase.get_sourcecCsN| ||¡\}}|j||d| ||¡Wdƒn1s@0YdS©N)rr)rrr rrrrÚcheck_src_roundtripŠszASTTestCase.check_src_roundtripcCsN| ||¡\}}|j||d| ||¡Wdƒn1s@0YdSr)rrZassertNotEqualrrrrÚcheck_src_dont_roundtripsz$ASTTestCase.check_src_dont_roundtrip)N)N)N) Ú__name__Ú __module__Ú __qualname__rrÚ ValueErrorrrr r!rrrrr vs    r c@steZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#dCdD„Z$dEdF„Z%dGdH„Z&dIdJ„Z'dKdL„Z(dMdN„Z)dOdP„Z*dQdR„Z+dSdT„Z,dUdV„Z-dWdX„Z.dYdZ„Z/d[S)\ÚUnparseTestCasecCs,| d¡| d¡| d¡| d¡dS)Nz f'{f"{0}"*3}'z f'{f"{y}"*3}'zf''zf"""'end' "quote\""""©r©rrrrÚ test_fstrings—s   zUnparseTestCase.test_fstringscCsJ| d¡| d¡| d¡| d¡| d¡| d¡| d¡dS)Nz f'''{"'"}'''ú%f'''-{f"""*{f"+{f'.{x}.'}+"}*"""}-'''z4f'''-{f"""*{f"+{f'.{x}.'}+"}*"""}-'single quote\''''zf"""{''' '''}"""zf"""{g(''' ''')}"""z f"a\r\nb"úf"\u2028{'x'}"r'r(rrrÚtest_fstrings_complicatedžs      z)UnparseTestCase.test_fstrings_complicatedcCs"| d¡| d¡| d¡dS)Nzu'foo'zr'foo'zb'foo'r'r(rrrÚ test_strings¨s  zUnparseTestCase.test_stringscCs| d¡dS)Nz del x, y, zr'r(rrrÚtest_del_statement­sz"UnparseTestCase.test_del_statementcCs| d¡| d¡dS)Nz45 << 2z13 >> 7r'r(rrrÚ test_shifts°s zUnparseTestCase.test_shiftscCs| t¡dSr )rÚfor_elser(rrrÚ test_for_else´szUnparseTestCase.test_for_elsecCs| t¡dSr )rÚ while_elser(rrrÚtest_while_else·szUnparseTestCase.test_while_elsecCs6| d¡| d¡| d¡| d¡| d¡dS)Nz(-1)**7z(-1.)**8z(-1j)**6znot True or FalsezTrue or not Falser'r(rrrÚtest_unary_parensºs     z!UnparseTestCase.test_unary_parenscCs| d¡dS)Nz 3 .__abs__()r'r(rrrÚtest_integer_parensÁsz#UnparseTestCase.test_integer_parenscCs,| d¡| d¡| d¡| d¡dS)NZ1e1000z-1e1000Z1e1000jz-1e1000jr'r(rrrÚtest_huge_floatÄs   zUnparseTestCase.test_huge_floatc Cs.| t t tjtdƒd¡¡t d¡¡dS)NÚnan©Úvaluez1e1000 - 1e1000)rrrrÚConstantÚfloatr(rrrÚtest_nanÊsþzUnparseTestCase.test_nancCs | tdƒ¡| tdƒ¡dS)Ni€lûÿÿÿ)rÚstrr(rrrÚ test_min_intÐszUnparseTestCase.test_min_intcCs,| d¡| d¡| d¡| d¡dS)NZ7jz-7jZ0jz-0jr'r(rrrÚtest_imaginary_literalsÔs   z'UnparseTestCase.test_imaginary_literalscCs| d¡dS)Nz(lambda: int)()r'r(rrrÚtest_lambda_parenthesesÚsz'UnparseTestCase.test_lambda_parenthesescCs| d¡| d¡dS)Nz 1 < 4 <= 5za is b is c is not dr'r(rrrÚtest_chained_comparisonsÝs z(UnparseTestCase.test_chained_comparisonscCs|| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d ¡| d ¡| d ¡| d ¡dS) Nz def f(): passzdef f(a): passzdef f(b = 2): passzdef f(a, b): passzdef f(a, b = 2): passzdef f(a = 5, b = 2): passzdef f(*, a = 1, b = 2): passzdef f(*, a = 1, b): passzdef f(*, a, b = 2): passz&def f(a, b = None, *, c, **kwds): passz'def f(a=2, *args, c=5, d, **kwds): passzdef f(*args, **kwargs): passr'r(rrrÚtest_function_argumentsás           z'UnparseTestCase.test_function_argumentscCs| t¡dSr )rÚrelative_importr(rrrÚtest_relative_importïsz$UnparseTestCase.test_relative_importcCs| t¡dSr )rÚ nonlocal_exr(rrrÚ test_nonlocalòszUnparseTestCase.test_nonlocalcCs| t¡dSr )rÚ raise_fromr(rrrÚtest_raise_fromõszUnparseTestCase.test_raise_fromcCs| d¡dS)Nzb'123'r'r(rrrÚ test_bytesøszUnparseTestCase.test_bytescCs6| d¡| d¡| d¡| d¡| d¡dS)Nzdef f(a : int): passzdef f(a: int = 5): passzdef f(*args: [int]): passzdef f(**kwargs: dict): passzdef f() -> None: passr'r(rrrÚtest_annotationsûs     z UnparseTestCase.test_annotationscCs| d¡dS)Nz{'a', 'b', 'c'}r'r(rrrÚtest_set_literalsz UnparseTestCase.test_set_literalc Cs*| t t tjgd¡¡t d¡¡dS)N)Úeltsz{*()})rrrrÚSetr(rrrÚtest_empty_setsþzUnparseTestCase.test_empty_setcCs| d¡dS)Nz{x for x in range(5)}r'r(rrrÚtest_set_comprehension sz&UnparseTestCase.test_set_comprehensioncCs| d¡dS)Nz{x: x*x for x in range(10)}r'r(rrrÚtest_dict_comprehensionsz'UnparseTestCase.test_dict_comprehensioncCs| t¡dSr )rÚclass_decoratorr(rrrÚtest_class_decoratorssz%UnparseTestCase.test_class_decoratorscCs| d¡dS)Nz(class A(metaclass=type, *[], **{}): passr'r(rrrÚtest_class_definitionsz%UnparseTestCase.test_class_definitioncCs| t¡| t¡dSr )rÚelif1Úelif2r(rrrÚ test_elifss zUnparseTestCase.test_elifscCs| t¡dSr )rÚtry_except_finallyr(rrrÚtest_try_except_finallysz'UnparseTestCase.test_try_except_finallycCs,| d¡| d¡| d¡| d¡dS)Nza, *b, c = seqza, (*b, c) = seqza, *b[0], c = seqza, *(b, c) = seqr'r(rrrÚtest_starred_assignments   z'UnparseTestCase.test_starred_assignmentcCs| t¡dSr )rÚ with_simpler(rrrÚtest_with_simple$sz UnparseTestCase.test_with_simplecCs| t¡dSr )rÚwith_asr(rrrÚ test_with_as'szUnparseTestCase.test_with_ascCs| t¡dSr )rÚwith_two_itemsr(rrrÚtest_with_two_items*sz#UnparseTestCase.test_with_two_itemscCs| d¡| d¡dS)Nz{**{'y': 2}, 'x': 1}z{**{'y': 2}, **{'x': 1}}r'r(rrrÚtest_dict_unpacking_in_dict-s z+UnparseTestCase.test_dict_unpacking_in_dictcCsš| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d ¡| d ¡| d ¡| d ¡| d ¡| d¡| d¡dS)Nza[i]za[i,]za[i, j]za[(*a,)]z a[(a:=b)]z a[(a:=b,c)]za[()]za[i:j]za[:j]za[i:]za[i:j:k]za[:j:k]za[i::k]za[i:j,]z a[i:j, k]r'r(rrrÚ test_slices2s              zUnparseTestCase.test_slicescCs | tjdtjddd¡dS)NÚX)Úid)ÚexcÚcause)rrZRaiseÚNamer(rrrÚtest_invalid_raiseCsz"UnparseTestCase.test_invalid_raisecCs | tjtjddgd¡dS)Nédr8)Úvalues)rrZ JoinedStrr:r(rrrÚtest_invalid_fstring_constantFsz-UnparseTestCase.test_invalid_fstring_constantcCs(| tjtjdddtdƒdd¡dS)NÚa©r9ÚkindÚY)r9Ú conversionÚ format_spec)rrÚFormattedValuer:Úordr(rrrÚtest_invalid_fstring_conversionIs ýÿz/UnparseTestCase.test_invalid_fstring_conversioncCs| tjtjddd¡dS)Nz\\r8)rrrqr:r(rrrÚtest_invalid_fstring_backslashRsz.UnparseTestCase.test_invalid_fstring_backslashcCs| tjdd¡dS)Nr8)rrZ YieldFromr(rrrÚtest_invalid_yield_fromUsz'UnparseTestCase.test_invalid_yield_fromcCs$d}|D]}| d|›d¡qdS)N) zthis ends with double quote"z"this includes a """triple quote"""ú z\rú z\tÚ z\nz \r \t \nz$""">>> content = """blabla""" <<<"""z foo\n\x00z' \'\'\'""" ""\'\' \'uðŸâ›Žð©¸½Ã¼Ã©ÅŸ^\\X\\BB⟿z'''r')rÚ docstringsÚ docstringrrrÚtest_docstringsXszUnparseTestCase.test_docstringscCs0| tjdddd¡| tjdddd¡dS)N)érlz(1,))r|ééz (1, 2, 3))r rr:r(rrrÚtest_constant_tupleslsÿz$UnparseTestCase.test_constant_tuplescCsdD]}|j|ddqdS)N)z () -> intz(int, int) -> intz=(Callable[complex], More[Complex(call.to_typevar())]) -> NoneZ func_type)Úmoder')rZ function_typerrrÚtest_function_typersz"UnparseTestCase.test_function_typecCsdD]}|j|ddqdS)N) z a = 5 # type:za = 5 # type: intza = 5 # type: int and morez!def x(): # type: () -> None passz.def x(y): # type: (int) -> None and more passz'async def x(): # type: () -> None passz4async def x(y): # type: (int) -> None and more passzfor x in y: # type: int passz#async for x in y: # type: int passzwith x(): # type: int passz!async with x(): # type: int passT©Z type_commentsr'©rZ statementrrrÚtest_type_commentszs z"UnparseTestCase.test_type_commentscCsdD]}|j|ddqdS)N) za = 5 # type: ignoreza = 5 # type: ignore and morezdef x(): # type: ignore passz'def x(y): # type: ignore and more passz#async def x(): # type: ignore passz-async def x(y): # type: ignore and more passz for x in y: # type: ignore passz&async for x in y: # type: ignore passzwith x(): # type: ignore passz$async with x(): # type: ignore passTr‚r'rƒrrrÚtest_type_ignoreŠs z UnparseTestCase.test_type_ignoreN)0r"r#r$r)r,r-r.r/r1r3r4r5r6r<r>r?r@rArBrDrFrHrIrJrKrNrOrPrRrSrVrXrYr[r]r_r`rargrjrsrtrur{rrr„r…rrrrr&”sZ  r&c@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)ÚCosmeticTestCasez@Test if there are cosmetic issues caused by unnecesary additionscCs| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d ¡| d ¡| d ¡| d ¡| d ¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡dS)Nz(a := b)zawait xz x if x else yz lambda x: xz1 + 1z 1 + 2 / 3z (1 + 2) / 3z(1 + 2) * 3 + 4 * (5 + 2)z(1 + 2) * 3 + 4 * (5 + 2) ** 2z~xzx and yz x and y and zzx and (y and x)z(x and y) and zz(x ** y) ** z ** qzx >> yzx << yzx >> y and x >> zzx + y - z * q ^ t ** kzP * V if P and V else n * R * Tz"lambda P, V, n: P * V == n * R * Tzflag & (other | foo)z not x == yz x == (not y)zyield xz yield from xzcall((yield x))zreturn x + (yield x)©r r(rrrÚtest_simple_expressions_parenss8                           z/CosmeticTestCase.test_simple_expressions_parenscCsh| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d ¡| d ¡dS) Nzclass X: passzclass X(A): passzclass X(A, B, C, D): passzclass X(x=y): passzclass X(metaclass=z): passzclass X(x=y, z=d): passzclass X(A, x=y): passzclass X(A, **kw): passzclass X(*args): passz"class X(*args, **kwargs): passr‡r(rrrÚtest_class_bases_and_keywords»s         z.CosmeticTestCase.test_class_bases_and_keywordscCs6| d¡| d¡| d¡| d¡| d¡dS)Nr*r+zf'{x}\n'zf'''{""" """}\n'''zf'''{f"""{x} """}\n'''r‡r(rrrr)Çs     zCosmeticTestCase.test_fstringscCs.d}tD] }|D]}| |›|›¡qqdS)N) z"""simple doc string"""z7"""A more complex one with some newlines"""z,"""Foo bar baz empty newline"""z"""With some """z"""Foo "bar" baz """z"""\r"""z""""""z """'''"""z """''''''"""u""""ðŸâ›Žð©¸½Ã¼Ã©ÅŸ^\\X\\BB⟿"""z"""end in single 'quote'"""z'''end in double "quote"'''z#"""almost end in double "quote".""")Údocstring_prefixesr )rryÚprefixrzrrrr{Îsz CosmeticTestCase.test_docstringscCs<d}tD].}|D]$}|›|›}| |¡| |¡qqdS)N)za = """false"""z("""false""" + """unless its optimized"""z1 + 1 """false"""z"f"""no, top level but f-fstring""")rŠrr!)rZdocstrings_negativer‹ÚnegativeÚsrcrrrÚtest_docstrings_negative_casesæs   z/CosmeticTestCase.test_docstrings_negative_casescCs8dD]}| |›d¡qdD]}| |›d¡qdS)N)ú+ú-ú~Ú1)Únotz 1r‡)rr‹rrrÚtest_unary_op_factorøsz%CosmeticTestCase.test_unary_op_factorcCs"| d¡| d¡| d¡dS)Nza[1]za[1, 2]z a[(1, *a)]r‡r(rrrraþs  zCosmeticTestCase.test_slicesN) r"r#r$Ú__doc__rˆr‰r)r{rŽr”rarrrrr†šs r†c@sLeZdZdZe e¡jdZeedfZ hd£Z dZ e dd„ƒZ dd „ZdS) ÚDirectoryTestCasez:Test roundtrip behaviour on all files in Lib and Lib/test.z..Útest>ztest_syntax.pyztest_grammar.pyztest_compile.pyz test_ast.pyztest_asdl_parser.pyztest_fstring.pyNcsdˆjdurˆjSdd„ˆjDƒ}tj d¡sZ‡fdd„|Dƒ}tt |d¡ƒ}t||Bƒ}|ˆ_|S)NcSs0g|](}| d¡D]}|j d¡s| ¡‘qqS)z*.pyZbad)ÚglobÚnameÚ startswithÚresolve)Ú.0Z directoryÚitemrrrÚ s ýz3DirectoryTestCase.files_to_test..Zcpucsh|]}|jˆjvr|’qSr)r™Úrun_always_files)rœr©ÚclsrrÚ s ÿz2DirectoryTestCase.files_to_test..é ) Ú_files_to_testÚtest_directoriesr—ÚsupportZis_resource_enabledÚsetÚrandomÚsampleÚlist)r¡ÚitemsZtests_to_run_alwaysrr rÚ files_to_test s þ  zDirectoryTestCase.files_to_testc Csj| ¡D]\}tjjr&td| ¡›ƒ|j|d"t|ƒ}| |¡Wdƒq1sZ0YqdS)NzTesting )r) r¬r—r¦ÚverboseÚprintÚabsoluterr r)rrÚsourcerrrÚ test_files,s  zDirectoryTestCase.test_files)r"r#r$r•ÚpathlibÚPathÚ__file__ÚparentZlib_dirr¥rŸr¤Ú classmethodr¬r±rrrrr–s  r–Ú__main__)r•ZunittestZ test.supportr—r²r¨rrr r0r2rCrErGrQrTrUrWrZr\r^rŠZTestCaser r&r†r–r"ÚmainrrrrÚs8     i3