a  ze[@sdZddlmZddlmZmZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZdd lmZdd lmZGd d d ejZGd ddejZGdddejZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZ GdddeZ!Gdd d eZ"Gd!d"d"eZ#Gd#d$d$eZ$Gd%d&d&eZ%Gd'd(d(eZ&Gd)d*d*eZ'Gd+d,d,ejZ(Gd-d.d.eZ)Gd/d0d0eZ*Gd1d2d2eZ+Gd3d4d4ejZ,d5d6Z-e.d7kre/dS)8aFTest suite for 2to3's parser and grammar files. This is the place to add tests for changes to 2to3's grammar, such as those merging the grammars for Python 2 and 3. In addition to specific tests for parts of the grammar we've changed, we also make sure we can parse the test_grammar.py files from both Python 2 and Python 3. )support)driverdriver_no_print_statementN)r)tokenize) ParseError)python_symbolsc@seZdZddZdS) TestDrivercCsJd}t|}||jdjdjtj||jdjdjtjdS)Nzprint 1 print 2 rr)r parse_string assertEqualZchildrentypesymsZ print_stmt)selfstrM/opt/bitninja-python-dojo/embedded/lib/python3.9/lib2to3/tests/test_parser.py test_formfeed"s zTestDriver.test_formfeedN)__name__ __module__ __qualname__rrrrrr sr c@s>eZdZddZddZeejdudddZ d d Z dS) TestPgen2CachingcCstjtjddddS)NFTsaveforce) pgen2_driver load_grammarr grammar_pathrrrrtest_load_grammar_from_txt_file*sz0TestPgen2Caching.test_load_grammar_from_txt_filec Cst}zztj|tjtj}t tj|t |}t j |ddd| tj|t|t j |dddWt|n t|0dS)NTrF)tempfilemkdtempospathjoinbasenamerrshutilcopyr_generate_pickle_namer assertTrueexistsunlinkrmtree)rtmpdir grammar_copy pickle_namerrrtest_load_grammar_from_pickle-s  z.TestPgen2Caching.test_load_grammar_from_pickleNzsys.executable requiredc Cszt}tj|d}zPt|tjtj}tj||}tj||}t tj|t tj|t |}t |}| ||t j|ddd|tj|ttj}d|d<tjtjdd|fg|d|tj|t|d T} t|d (} |j| | d d Wdn1s20YWdn1sR0YWt |n t |0dS) NZsubdirTrrandomZPYTHONHASHSEEDz-cz{ from lib2to3.pgen2 import driver as pgen2_driver pgen2_driver.load_grammar(%r, save=True, force=True) )envrbzGGrammar caches generated using different hash seeds were not identical.)msg)r!r"r#r$r%mkdirr&rrr'r(rr)ZassertNotEqualrr*r+dictenviron subprocess check_callsys executableopenr readr-) rr.Z tmpsubdirZ grammar_baser/Zgrammar_sub_copyr0Zpickle_sub_nameZsub_envZ pickle_f_1Z pickle_f_2rrr!test_load_grammar_from_subprocess?sD      Hz2TestPgen2Caching.test_load_grammar_from_subprocesscsftdGdddGfddd}|tj<|tjtjtd}||j ddS)Nz .load_testc@seZdZddZdS)z=TestPgen2Caching.test_load_packaged_grammar..MyLoadercSstddiS)Nelephant)pickledumps)rwhererrrget_datakszFTestPgen2Caching.test_load_packaged_grammar..MyLoader.get_dataN)rrrrErrrrMyLoaderjsrFcs eZdZdZejZdS)z=TestPgen2Caching.test_load_packaged_grammar..MyModuleZparsertestmoduleN)rrr__file__ importlibutilspec_from_loader__spec__rrFmodnamerrMyModulemsrNz Grammar.txtrA) rr;modulesZ addCleanupoperatordelitemrZload_packaged_grammarr r@)rrNgrrLrtest_load_packaged_grammarhs  z+TestPgen2Caching.test_load_packaged_grammar) rrrr r1unittestZskipIfr;r<r?rSrrrrr)s  (rc@seZdZddZddZdS) GrammarTestcCst|dS)N)rr rcoderrrvalidatewszGrammarTest.validatecCs.z||Wnty Yn 0tddS)Nz Syntax shouldn't have been valid)rXrAssertionErrorrVrrrinvalid_syntaxzs  zGrammarTest.invalid_syntaxN)rrrrXrZrrrrrUvsrUc@seZdZddZdS)TestMatrixMultiplicationcCs|d|ddS)Nza @ bza @= brXrrrr#test_matrix_multiplication_operators zZ!d?d@Z"dAS)BTestUnpackingGeneralizationscCs|ddS)Nzfunc(1, *(2, 3), 4)r\rrrrtest_mid_positional_starsz5TestUnpackingGeneralizations.test_mid_positional_starcCs|ddS)Nz,func(**{'eggs':'scrambled', 'spam':'fried'})r\rrrrtest_double_star_dict_literalsz:TestUnpackingGeneralizations.test_double_star_dict_literalcCs|ddS)Nz*func(spam='fried', **{'eggs':'scrambled'})r\rrrr,test_double_star_dict_literal_after_keywordsszITestUnpackingGeneralizations.test_double_star_dict_literal_after_keywordscCs|d|ddS)Nzfunc(**{'a':2} or {})zfunc(**() or {})r\rrrrtest_double_star_expressions z8TestUnpackingGeneralizations.test_double_star_expressioncCs|ddS)Nzfunc(*[] or [2])r\rrrrtest_star_expression sz1TestUnpackingGeneralizations.test_star_expressioncCs|ddS)Nz[*{2}, 3, *[4]]r\rrrrtest_list_displaysz.TestUnpackingGeneralizations.test_list_displaycCs|ddS)Nz{*{2}, 3, *[4]}r\rrrrtest_set_displaysz-TestUnpackingGeneralizations.test_set_displaycCs|ddS)Nz{**{}}r\rrrrtest_dict_display_1sz0TestUnpackingGeneralizations.test_dict_display_1cCs|ddS)Nz{**{}, 3:4, **{5:6, 7:8}}r\rrrrtest_dict_display_2sz0TestUnpackingGeneralizations.test_dict_display_2cCs|ddS)Nzfunc(* [] or [1])r\rrrrtest_complex_star_expressionsz9TestUnpackingGeneralizations.test_complex_star_expressioncCs|ddS)Nz5func(**{1: 3} if False else {x: x for x in range(3)})r\rrrr#test_complex_double_star_expressionsz@TestUnpackingGeneralizations.test_complex_double_star_expressioncCs|ddS)Nzf(a, *b, *c, d)r\rrrrtest_argument_unpacking_1 sz6TestUnpackingGeneralizations.test_argument_unpacking_1cCs|ddS)Nz f(**a, **b)r\rrrrtest_argument_unpacking_2#sz6TestUnpackingGeneralizations.test_argument_unpacking_2cCs|ddS)Nzf(2, *a, *b, **b, **c, **d)r\rrrrtest_argument_unpacking_3&sz6TestUnpackingGeneralizations.test_argument_unpacking_3cCs|d|ddS)Nzdef f(a, b): call(a, b)zdef f(a, b,): call(a, b,)r\rrrrtest_trailing_commas_1)s z3TestUnpackingGeneralizations.test_trailing_commas_1cCs|d|ddS)Nzdef f(a, *b): call(a, *b)zdef f(a, *b,): call(a, *b,)r\rrrrtest_trailing_commas_2-s z3TestUnpackingGeneralizations.test_trailing_commas_2cCs|d|ddS)Nzdef f(a, b=1): call(a, b=1)zdef f(a, b=1,): call(a, b=1,)r\rrrrtest_trailing_commas_31s z3TestUnpackingGeneralizations.test_trailing_commas_3cCs|d|ddS)Nzdef f(a, **b): call(a, **b)zdef f(a, **b,): call(a, **b,)r\rrrrtest_trailing_commas_45s z3TestUnpackingGeneralizations.test_trailing_commas_4cCs|d|ddS)Nzdef f(*a, b=1): call(*a, b=1)zdef f(*a, b=1,): call(*a, b=1,)r\rrrrtest_trailing_commas_59s z3TestUnpackingGeneralizations.test_trailing_commas_5cCs|d|ddS)Nzdef f(*a, **b): call(*a, **b)zdef f(*a, **b,): call(*a, **b,)r\rrrrtest_trailing_commas_6=s z3TestUnpackingGeneralizations.test_trailing_commas_6cCs|d|ddS)Nzdef f(*, b=1): call(*b)zdef f(*, b=1,): call(*b,)r\rrrrtest_trailing_commas_7As z3TestUnpackingGeneralizations.test_trailing_commas_7cCs|d|ddS)Nzdef f(a=1, b=2): call(a=1, b=2)z!def f(a=1, b=2,): call(a=1, b=2,)r\rrrrtest_trailing_commas_8Es z3TestUnpackingGeneralizations.test_trailing_commas_8cCs|d|ddS)Nzdef f(a=1, **b): call(a=1, **b)z!def f(a=1, **b,): call(a=1, **b,)r\rrrrtest_trailing_commas_9Is z3TestUnpackingGeneralizations.test_trailing_commas_9cCs|d|ddS)Nzf = lambda a, b: call(a, b)zf = lambda a, b,: call(a, b,)r\rrrrtest_trailing_commas_lambda_1Ms z:TestUnpackingGeneralizations.test_trailing_commas_lambda_1cCs|d|ddS)Nzf = lambda a, *b: call(a, *b)zf = lambda a, *b,: call(a, *b,)r\rrrrtest_trailing_commas_lambda_2Qs z:TestUnpackingGeneralizations.test_trailing_commas_lambda_2cCs|d|ddS)Nzf = lambda a, b=1: call(a, b=1)z!f = lambda a, b=1,: call(a, b=1,)r\rrrrtest_trailing_commas_lambda_3Us z:TestUnpackingGeneralizations.test_trailing_commas_lambda_3cCs|d|ddS)Nzf = lambda a, **b: call(a, **b)z!f = lambda a, **b,: call(a, **b,)r\rrrrtest_trailing_commas_lambda_4Ys z:TestUnpackingGeneralizations.test_trailing_commas_lambda_4cCs|d|ddS)Nz!f = lambda *a, b=1: call(*a, b=1)z#f = lambda *a, b=1,: call(*a, b=1,)r\rrrrtest_trailing_commas_lambda_5]s z:TestUnpackingGeneralizations.test_trailing_commas_lambda_5cCs|d|ddS)Nz!f = lambda *a, **b: call(*a, **b)z#f = lambda *a, **b,: call(*a, **b,)r\rrrrtest_trailing_commas_lambda_6as z:TestUnpackingGeneralizations.test_trailing_commas_lambda_6cCs|d|ddS)Nzf = lambda *, b=1: call(*b)zf = lambda *, b=1,: call(*b,)r\rrrrtest_trailing_commas_lambda_7es z:TestUnpackingGeneralizations.test_trailing_commas_lambda_7cCs|d|ddS)Nz#f = lambda a=1, b=2: call(a=1, b=2)z%f = lambda a=1, b=2,: call(a=1, b=2,)r\rrrrtest_trailing_commas_lambda_8is z:TestUnpackingGeneralizations.test_trailing_commas_lambda_8cCs|d|ddS)Nz#f = lambda a=1, **b: call(a=1, **b)z%f = lambda a=1, **b,: call(a=1, **b,)r\rrrrtest_trailing_commas_lambda_9ms z:TestUnpackingGeneralizations.test_trailing_commas_lambda_9N)#rrrrtrurvrwrxryrzr{r|r}r~rrrrrrrrrrrrrrrrrrrrrrrrrrss@rsc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+S),TestFunctionAnnotationscCs|ddS)Nzdef f(x) -> list: passr\rrrrtest_1tszTestFunctionAnnotations.test_1cCs|ddS)Nzdef f(x:int): passr\rrrrtest_2wszTestFunctionAnnotations.test_2cCs|ddS)Nzdef f(*x:str): passr\rrrrtest_3zszTestFunctionAnnotations.test_3cCs|ddS)Nzdef f(**x:float): passr\rrrrtest_4}szTestFunctionAnnotations.test_4cCs|ddS)Nzdef f(x, y:1+2): passr\rrrrtest_5szTestFunctionAnnotations.test_5cCs|ddS)Nzdef f(a, (b:1, c:2, d)): passr\rrrrtest_6szTestFunctionAnnotations.test_6cCs|ddS)Nz/def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): passr\rrrrtest_7szTestFunctionAnnotations.test_7cCsd}||dS)Nzgdef f(a, (b:1, c:2, d), e:3=4, f=5, *g:6, h:7, i=8, j:9=10, **k:11) -> 12: passr\rrrrrtest_8szTestFunctionAnnotations.test_8cCsd}||dS)Nzdef f( a: str, b: int, *, c: bool = False, **kwargs, ) -> None: call(c=c, **kwargs,)r\rrrrtest_9szTestFunctionAnnotations.test_9cCsd}||dS)Nz@def f( a: str, ) -> None: call(a,)r\rrrrtest_10szTestFunctionAnnotations.test_10cCsd}||dS)NzGdef f( a: str = '', ) -> None: call(a=a,)r\rrrrtest_11szTestFunctionAnnotations.test_11cCsd}||dS)NzHdef f( *args: str, ) -> None: call(*args,)r\rrrrtest_12szTestFunctionAnnotations.test_12cCs|d|ddS)Nz)def f(a: str, b: int) -> None: call(a, b)z+def f(a: str, b: int,) -> None: call(a, b,)r\rrrrtest_13s zTestFunctionAnnotations.test_13cCs|d|ddS)Nz+def f(a: str, *b: int) -> None: call(a, *b)z-def f(a: str, *b: int,) -> None: call(a, *b,)r\rrrrtest_14s zTestFunctionAnnotations.test_14cCs|d|ddS)Nz-def f(a: str, b: int=1) -> None: call(a, b=1)z/def f(a: str, b: int=1,) -> None: call(a, b=1,)r\rrrrtest_15s zTestFunctionAnnotations.test_15cCs|d|ddS)Nz-def f(a: str, **b: int) -> None: call(a, **b)z/def f(a: str, **b: int,) -> None: call(a, **b,)r\rrrrtest_16s zTestFunctionAnnotations.test_16cCs|d|ddS)Nz/def f(*a: str, b: int=1) -> None: call(*a, b=1)z1def f(*a: str, b: int=1,) -> None: call(*a, b=1,)r\rrrrtest_17s zTestFunctionAnnotations.test_17cCs|d|ddS)Nz/def f(*a: str, **b: int) -> None: call(*a, **b)z1def f(*a: str, **b: int,) -> None: call(*a, **b,)r\rrrrtest_18s zTestFunctionAnnotations.test_18cCs|d|ddS)Nz$def f(*, b: int=1) -> None: call(*b)z&def f(*, b: int=1,) -> None: call(*b,)r\rrrrtest_19s zTestFunctionAnnotations.test_19cCs|d|ddS)Nz2def f(a: str='', b: int=2) -> None: call(a=a, b=2)z4def f(a: str='', b: int=2,) -> None: call(a=a, b=2,)r\rrrrtest_20s zTestFunctionAnnotations.test_20cCs|d|ddS)Nz2def f(a: str='', **b: int) -> None: call(a=a, **b)z4def f(a: str='', **b: int,) -> None: call(a=a, **b,)r\rrrrtest_21s zTestFunctionAnnotations.test_21N)rrrrrrrrrrrrrrrrrrrrrrrrrrrrrss* rc@s<eZdZddZddZddZddZd d Zd d Zd S)TestVarAnnotationscCs|ddS)Nz var1: int = 5r\rrrrrszTestVarAnnotations.test_1cCs|ddS)Nzvar2: [int, str]r\rrrrrszTestVarAnnotations.test_2cCs|ddS)NzCdef f(): st: str = 'Hello' a.b: int = (1, 2) return st r\rrrrrszTestVarAnnotations.test_3cCs|ddS)Nz$def fbad(): x: int print(x) r\rrrrrszTestVarAnnotations.test_4cCs|ddS)Nzeclass C: x: int s: str = 'attr' z = 2 def __init__(self, x): self.x: int = x r\rrrrrszTestVarAnnotations.test_5cCs|ddS)Nzlst: List[int] = []r\rrrrrszTestVarAnnotations.test_6N) rrrrrrrrrrrrrrs rc@seZdZddZddZdS) TestExceptcCsd}||dS)NzP try: x except E as N: yr\rrrrtest_newszTestExcept.test_newcCsd}||dS)NzN try: x except E, N: yr\rrrrtest_oldszTestExcept.test_oldN)rrrrrrrrrrsrc@seZdZdZddZdS)TestStringLiterals)*'"zr'zr"zR'zR"zu'zu"zU'zU"zb'zb"zB'zB"zf'zf"zF'zF"zur'zur"zUr'zUr"zuR'zuR"zUR'zUR"zbr'zbr"zBr'zBr"zbR'zbR"zBR'zBR"zrb'zrb"zRb'zRb"zrB'zrB"zRB'zRB"cCsH|jD]<}dj||dd}||dj||dd}||qdS)Nz{p}spamspamspam{s})prz{p}{s}{s}eggs{s}{s}{s})prefixesformatrX)rpreZsingleZtriplerrrtest_lits   zTestStringLiterals.test_litN)rrrrrrrrrrs rc@s,eZdZddZddZddZddZd S) TestSetLiteralcCs|ddS)Nz x = {'one'}r\rrrrrszTestSetLiteral.test_1cCs|ddS)Nzx = {'one', 1,}r\rrrrrszTestSetLiteral.test_2cCs|ddS)Nzx = {'one', 'two', 'three'}r\rrrrr!szTestSetLiteral.test_3cCs|ddS)Nzx = {2, 3, 4,}r\rrrrr$szTestSetLiteral.test_4N)rrrrrrrrrrrrsrc@seZdZddZdS)TestIdentifiercCs,|d|d|d|ddS)Nu!Örter = 'places' grün = 'green'u蟒 = a蟒 = 锦蛇 = 1uµ = aµ = µµ = 1uA𝔘𝔫𝔦𝔠𝔬𝔡𝔢 = a_𝔘𝔫𝔦𝔠𝔬𝔡𝔢 = 1r\rrrrtest_non_ascii_identifiers+s   z)TestIdentifier.test_non_ascii_identifiersN)rrrrrrrrr*src@seZdZddZddZdS)TestNumericLiteralscCs|d|ddS)NZ0o7777777777777Z 0o7324528887r_rrrrtest_new_octal_notation3s z+TestNumericLiterals.test_new_octal_notationcCs|d|ddS)NZ0b101010Z 0b0101021r_rrrrtest_new_binary_notation7s z,TestNumericLiterals.test_new_binary_notationN)rrrrrrrrrr2src@seZdZddZdS) TestClassDefcCs6|d|d|d|d|ddS)Nzclass B(t=7): passzclass B(t, *args): passzclass B(t, **kwargs): passz!class B(t, *args, **kwargs): passz'class B(t, y=9, *args, **kwargs,): passr\rrrrtest_new_syntax=s     zTestClassDef.test_new_syntaxN)rrrrrrrrr<src@s eZdZdZddZddZdS)TestParserIdempotencyz,A cut-down version of pytree_idempotency.py.cCs*tD]}t|d }t|jd}Wdn1s>0Y||d|t|d|d}|}Wdn1s0Yzt |}WnXt yzt |}Wn6t y}z| d||fWYd}~n d}~00Yn0t |}||krtt|||| d|qdS)Nr4rzcan't detect encoding for %sr)encodingzParseError on file %s (%s)zIdempotency failed: %s)rZall_project_filesr=rdetect_encodingreadlineZassertIsNotNoner>rr rrZfailstrprint diff_texts)rfilepathfprsourceZtreeerrnewrrrtest_all_project_filesIs& .& .z,TestParserIdempotency.test_all_project_filescCs,tdtdtdtddS)Nz a, *b, c = x z [*a, b] = x z(z, *y, w) = m zfor *z, m in d: pass rr rrrrtest_extended_unpacking]s   z-TestParserIdempotency.test_extended_unpackingN)rrr__doc__rrrrrrrEsrc@s,eZdZddZddZddZddZd S) TestLiteralscCstt|ddS)Nz )rr rdedentrrrrrXfszTestLiterals.validatecCsd}||dS)Nu md5test(b"ª" * 80, (b"Test Using Larger Than Block-Size Key " b"and Larger Than One Block-Size Data"), "6f630fad67cda0ee1fb1f562db3aa53e") r\rrrrtest_multiline_bytes_literalsisz*TestLiterals.test_multiline_bytes_literalscCsd}||dS)Nz b""" """ r\rrrr'test_multiline_bytes_tripquote_literalsrsz4TestLiterals.test_multiline_bytes_tripquote_literalscCsd}||dS)Nu md5test("ª" * 80, ("Test Using Larger Than Block-Size Key " "and Larger Than One Block-Size Data"), "6f630fad67cda0ee1fb1f562db3aa53e") r\rrrrtest_multiline_str_literals{sz(TestLiterals.test_multiline_str_literalsN)rrrrXrrrrrrrrds  rc@s0eZdZdZddZddZddZdd Zd S) TestNamedAssignmentsz"Also known as the walrus operator.cCstddS)Nzif f := x(): pass rrrrrtest_named_assignment_ifsz-TestNamedAssignments.test_named_assignment_ifcCstddS)Nzwhile f := x(): pass rrrrrtest_named_assignment_whilesz0TestNamedAssignments.test_named_assignment_whilecCstddS)Nz0any((lastNum := num) == 1 for num in [1, 2, 3]) rrrrrtest_named_assignment_generatorsz4TestNamedAssignments.test_named_assignment_generatorcCstddS)Nz-[(lastNum := num) == 1 for num in [1, 2, 3]] rrrrrtest_named_assignment_listcompsz3TestNamedAssignments.test_named_assignment_listcompN)rrrrrrrrrrrrrs rc@s4eZdZddZddZddZddZd d Zd S) TestPositionalOnlyArgscCstddS)Nz!def one_pos_only_arg(a, /): pass rrrrrtest_one_pos_only_argsz,TestPositionalOnlyArgs.test_one_pos_only_argcCstddS)Nz4def all_markers(a, b=2, /, c, d=4, *, e=5, f): pass rrrrrtest_all_markerssz'TestPositionalOnlyArgs.test_all_markerscCstddS)Nzdef all_markers_with_args_and_kwargs( aa, b, /, _cc, d, *args, e, f_f, **kwargs, ): pass rrrrrtest_all_with_args_and_kwargssz4TestPositionalOnlyArgs.test_all_with_args_and_kwargscCstddS)Nz,lambda a, b, /, c, d, *args, e, f, **kw: kw rrrrrtest_lambda_soupsz'TestPositionalOnlyArgs.test_lambda_soupcCstddS)Nzdef func(a,b,/,*,g,e=3): pass rrrrrtest_only_positional_or_keywordsz6TestPositionalOnlyArgs.test_only_positional_or_keywordN)rrrrrrrrrrrrrs rc@seZdZddZdS)TestPickleableExceptioncCstdddd}ttjdD]h}ttj||d}||j|j||j|j||j |j ||j |j ||j |j qdS)Nr5r)rcontextr)protocol) rrangerBHIGHEST_PROTOCOLloadsrCr argsr5r valuer)rrprotoZerr2rrrtest_ParseErrorsz'TestPickleableException.test_ParseErrorN)rrrrrrrrrsrc Cs(|}|}tj||||ddddS)Nz (original)z(reserialized))Zlineterm) splitlinesdifflibZ unified_diff)abfilenamerrrrs  r__main__)0rrrrrrrHrPr#rBr'r9r;r!rTZ lib2to3.pgen2rrZ pgen2.parserZlib2to3.pygramr rZTestCaser rrUr[r^rargrsrrrrrrrrrrrrrrrmainrrrrsP      M M va  !