a zeH@sdZddlZddlZddlZddlZddlZddlZddZdZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZdZGdddejZGdddeZGdddeZGdddeZedkredS)z>Tests for the unparse.py script in the Tools/parser directory.NcCs6t|}|WdS1s(0YdS)znRead and return the contents of a Python source file (as a string), taking into account the file encoding.N)tokenizeopenread)filenamestreamrE/opt/bitninja-python-dojo/embedded/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@sFeZdZddZddZefddZddd Zdd d Zdd d Z dS) ASTTestCasecCs|t|t|dSN) assertEqualastdump)selfast1ast2rrrassertASTEqualwszASTTestCase.assertASTEqualcKsl|j||dJtj|fi|}t|}tj|fi|}|||Wdn1s^0YdS)N)code1Zast_parse_kwargs)subTestrparseunparser)rrkwargsrcode2rrrrcheck_ast_roundtripzs  zASTTestCase.check_ast_roundtripcCs@|j|d ||tj|Wdn1s20YdS)N)node)rZ assertRaisesrr)rrZraisesrrr check_invalidszASTTestCase.check_invalidNcCs |p|}tt|}||fSr )rrrrrrrrr get_sourceszASTTestCase.get_sourcecCsN|||\}}|j||d|||Wdn1s@0YdSN)rr)rrr rrrrcheck_src_roundtripszASTTestCase.check_src_roundtripcCsN|||\}}|j||d|||Wdn1s@0YdSr)rrZassertNotEqualrrrrcheck_src_dont_roundtripsz$ASTTestCase.check_src_dont_roundtrip)N)N)N) __name__ __module__ __qualname__rr ValueErrorrrr r!rrrrr vs    r c@steZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+dSdTZ,dUdVZ-dWdXZ.dYdZZ/d[S)\UnparseTestCasecCs,|d|d|d|ddS)Nz f'{f"{0}"*3}'z f'{f"{y}"*3}'zf''zf"""'end' "quote\""""rrrrr test_fstringss   zUnparseTestCase.test_fstringscCsJ|d|d|d|d|d|d|ddS)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(rrrtest_fstrings_complicateds      z)UnparseTestCase.test_fstrings_complicatedcCs"|d|d|ddS)Nzu'foo'zr'foo'zb'foo'r'r(rrr test_stringss  zUnparseTestCase.test_stringscCs|ddS)Nz del x, y, zr'r(rrrtest_del_statementsz"UnparseTestCase.test_del_statementcCs|d|ddS)Nz45 << 2z13 >> 7r'r(rrr test_shiftss zUnparseTestCase.test_shiftscCs|tdSr )rfor_elser(rrr test_for_elseszUnparseTestCase.test_for_elsecCs|tdSr )r while_elser(rrrtest_while_elseszUnparseTestCase.test_while_elsecCs6|d|d|d|d|ddS)Nz(-1)**7z(-1.)**8z(-1j)**6znot True or FalsezTrue or not Falser'r(rrrtest_unary_parenss     z!UnparseTestCase.test_unary_parenscCs|ddS)Nz 3 .__abs__()r'r(rrrtest_integer_parenssz#UnparseTestCase.test_integer_parenscCs,|d|d|d|ddS)NZ1e1000z-1e1000Z1e1000jz-1e1000jr'r(rrrtest_huge_floats   zUnparseTestCase.test_huge_floatc Cs.|tttjtddtddS)Nnanvaluez1e1000 - 1e1000)rrrrConstantfloatr(rrrtest_nanszUnparseTestCase.test_nancCs |td|tddS)Nil)rstrr(rrr test_min_intszUnparseTestCase.test_min_intcCs,|d|d|d|ddS)NZ7jz-7jZ0jz-0jr'r(rrrtest_imaginary_literalss   z'UnparseTestCase.test_imaginary_literalscCs|ddS)Nz(lambda: int)()r'r(rrrtest_lambda_parenthesessz'UnparseTestCase.test_lambda_parenthesescCs|d|ddS)Nz 1 < 4 <= 5za is b is c is not dr'r(rrrtest_chained_comparisonss 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(rrrtest_function_argumentss           z'UnparseTestCase.test_function_argumentscCs|tdSr )rrelative_importr(rrrtest_relative_importsz$UnparseTestCase.test_relative_importcCs|tdSr )r nonlocal_exr(rrr test_nonlocalszUnparseTestCase.test_nonlocalcCs|tdSr )r raise_fromr(rrrtest_raise_fromszUnparseTestCase.test_raise_fromcCs|ddS)Nzb'123'r'r(rrr test_bytesszUnparseTestCase.test_bytescCs6|d|d|d|d|ddS)Nzdef f(a : int): passzdef f(a: int = 5): passzdef f(*args: [int]): passzdef f(**kwargs: dict): passzdef f() -> None: passr'r(rrrtest_annotationss     z UnparseTestCase.test_annotationscCs|ddS)Nz{'a', 'b', 'c'}r'r(rrrtest_set_literalsz UnparseTestCase.test_set_literalc Cs*|tttjgdtddS)N)eltsz{*()})rrrrSetr(rrrtest_empty_setszUnparseTestCase.test_empty_setcCs|ddS)Nz{x for x in range(5)}r'r(rrrtest_set_comprehension sz&UnparseTestCase.test_set_comprehensioncCs|ddS)Nz{x: x*x for x in range(10)}r'r(rrrtest_dict_comprehensionsz'UnparseTestCase.test_dict_comprehensioncCs|tdSr )rclass_decoratorr(rrrtest_class_decoratorssz%UnparseTestCase.test_class_decoratorscCs|ddS)Nz(class A(metaclass=type, *[], **{}): passr'r(rrrtest_class_definitionsz%UnparseTestCase.test_class_definitioncCs|t|tdSr )relif1elif2r(rrr test_elifss zUnparseTestCase.test_elifscCs|tdSr )rtry_except_finallyr(rrrtest_try_except_finallysz'UnparseTestCase.test_try_except_finallycCs,|d|d|d|ddS)Nza, *b, c = seqza, (*b, c) = seqza, *b[0], c = seqza, *(b, c) = seqr'r(rrrtest_starred_assignments   z'UnparseTestCase.test_starred_assignmentcCs|tdSr )r with_simpler(rrrtest_with_simple$sz UnparseTestCase.test_with_simplecCs|tdSr )rwith_asr(rrr test_with_as'szUnparseTestCase.test_with_ascCs|tdSr )rwith_two_itemsr(rrrtest_with_two_items*sz#UnparseTestCase.test_with_two_itemscCs|d|ddS)Nz{**{'y': 2}, 'x': 1}z{**{'y': 2}, **{'x': 1}}r'r(rrrtest_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|ddS)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 |tjdtjddddS)NX)id)exccause)rrZRaiseNamer(rrrtest_invalid_raiseCsz"UnparseTestCase.test_invalid_raisecCs |tjtjddgddS)Ndr8)values)rrZ JoinedStrr:r(rrrtest_invalid_fstring_constantFsz-UnparseTestCase.test_invalid_fstring_constantcCs(|tjtjdddtddddS)Nar9kindY)r9 conversion format_spec)rrFormattedValuer:ordr(rrrtest_invalid_fstring_conversionIs z/UnparseTestCase.test_invalid_fstring_conversioncCs|tjtjddddS)Nz\\r8)rrrqr:r(rrrtest_invalid_fstring_backslashRsz.UnparseTestCase.test_invalid_fstring_backslashcCs|tjdddS)Nr8)rrZ YieldFromr(rrrtest_invalid_yield_fromUsz'UnparseTestCase.test_invalid_yield_fromcCs$d}|D]}|d|dqdS)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 docstringrrrtest_docstringsXszUnparseTestCase.test_docstringscCs0|tjdddd|tjdddddS)N)rlz(1,))r|z (1, 2, 3))r rr:r(rrrtest_constant_tupleslsz$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_typerrrtest_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 passTZ type_commentsr'rZ statementrrrtest_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 passTrr'rrrrtest_type_ignores z UnparseTestCase.test_type_ignoreN)0r"r#r$r)r,r-r.r/r1r3r4r5r6r<r>r?r@rArBrDrFrHrIrJrKrNrOrPrRrSrVrXrYr[r]r_r`rargrjrsrtrur{rrrrrrrrr&sZ  r&c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)CosmeticTestCasezATest if there are cosmetic issues caused by unnecessary 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|ddS)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(rrrtest_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): passrr(rrrtest_class_bases_and_keywordss         z.CosmeticTestCase.test_class_bases_and_keywordscCs6|d|d|d|d|ddS)Nr*r+zf'{x}\n'zf'''{""" """}\n'''zf'''{f"""{x} """}\n'''rr(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 )rryprefixrzrrrr{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""")rrr!)rZdocstrings_negativernegativesrcrrrtest_docstrings_negative_casess   z/CosmeticTestCase.test_docstrings_negative_casescCs8dD]}||dqdD]}||dqdS)N)+-~1)notz 1r)rrrrrtest_unary_op_factorsz%CosmeticTestCase.test_unary_op_factorcCs"|d|d|ddS)Nza[1]za[1, 2]z a[(1, *a)]rr(rrrras  zCosmeticTestCase.test_slicesN) r"r#r$__doc__rrr)r{rrrarrrrrs rc@sLeZdZdZeejdZeedfZ hdZ dZ e ddZ dd ZdS) DirectoryTestCasez:Test roundtrip behaviour on all files in Lib and Lib/test.z..test>ztest_syntax.pyztest_fstring.pyztest_grammar.pyztest_compile.pyz test_ast.pyztest_asdl_parser.pyNcsdjdurjSddjD}tjdsZfdd|D}tt|d}t||B}|_|S)NcSs0g|](}|dD]}|jds|qqS)z*.pyZbad)globname startswithresolve).0Z directoryitemrrr s z3DirectoryTestCase.files_to_test..Zcpucsh|]}|jjvr|qSr)rrun_always_files)rrclsrr s z2DirectoryTestCase.files_to_test.. ) _files_to_testtest_directoriesrsupportZis_resource_enabledsetrandomsamplelist)ritemsZtests_to_run_alwaysrrr files_to_test s   zDirectoryTestCase.files_to_testc Csj|D]\}tjjr&td||j|d"t|}||Wdq1sZ0YqdS)NzTesting )r) rrrverboseprintabsoluterr r)rrsourcerrr test_files,s  zDirectoryTestCase.test_files)r"r#r$rpathlibPath__file__parentZlib_dirrrr classmethodrrrrrrrs  r__main__)rZunittestZ test.supportrrrrrr r0r2rCrErGrQrTrUrWrZr\r^rZTestCaser r&rrr"mainrrrrs8     i3