3 7_c@sddlZddlZddlZddlZddlZddlZddlmZddlm Z Gdddej Z Gdddej Z Gdd d ej Z Gd d d ej ZGd d d ej ZGdddej ZedkrejdS)N)support)assert_python_failurec@s eZdZddZddZddZddZd 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"dAS)BRoundtripLegalSyntaxTestCasecCsl||}|j}ytj|}Wn6tjk rT}z|jd||fWYdd}~XnX|j||jddS)Nzcould not roundtrip %r: %sz!could not re-generate syntax tree)totupleparser sequence2st ParserErrorfail assertEqual)selffsst1tst2Zwhyr(/usr/lib64/python3.6/test/test_parser.py roundtrips$ z&RoundtripLegalSyntaxTestCase.roundtripcC|jtj|dSN)rrexprr r rrr check_exprz'RoundtripLegalSyntaxTestCase.check_exprcCs6tjd}|j}i}t|i||j|dtdS)Nz/from __future__ import unicode_literals; x = ''x)rsuitecompileexecZassertIsInstancestr)r rcodeZscoperrrtest_flags_passed s   z.RoundtripLegalSyntaxTestCase.test_flags_passedcCrr)rrrrrrr check_suite)rz(RoundtripLegalSyntaxTestCase.check_suitecC|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jd |jd |jd |jd|jd|jd|jddS)Nzdef f(): yield 1zdef f(): yieldzdef f(): x += yieldzdef f(): x = yield 1zdef f(): x = y = yield 1zdef f(): x = yieldzdef f(): x = y = yieldzdef f(): 1 + (yield)*2zdef f(): (yield 1)*2zdef f(): return; yield 1zdef f(): yield 1; returnzdef f(): yield from 1zdef f(): x = yield from 1zdef f(): f((yield from 1))zdef f(): yield 1; return 1z1def f(): for x in range(30): yield x z)def f(): if (yield): yield x r r rrrtest_yield_statement,s"                z1RoundtripLegalSyntaxTestCase.test_yield_statementcC@|jd|jd|jd|jd|jd|jddS)Nzasync def f(): await smth()z"async def f(): foo = await smth()z'async def f(): foo, bar = await smth()zasync def f(): (await smth())z#async def f(): foo((await smth()))z&async def f(): await foo(); return 42r"r#rrrtest_await_statementCs      z1RoundtripLegalSyntaxTestCase.test_await_statementcC|jd|jddS)Nz"async def f(): async with 1: passz/async def f(): async with a as b, c as d: passr"r#rrrtest_async_with_statementK z6RoundtripLegalSyntaxTestCase.test_async_with_statementcCr')Nz'async def f(): async for i in (): passz*async def f(): async for i, b in (): passr"r#rrrtest_async_for_statementOr)z5RoundtripLegalSyntaxTestCase.test_async_for_statementcCr')Nz3def f(): x = 0 def g(): nonlocal x z:def f(): x = y = 0 def g(): nonlocal x, y r"r#rrrtest_nonlocal_statementSs z4RoundtripLegalSyntaxTestCase.test_nonlocal_statementcCs|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jd |jd |jd |jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd!|jd"|jd#|jd$|jd%|jd&|jd'|jd(dS))Nzfoo(1)z [1, 2, 3]z[x**3 for x in range(20)]z"[x**3 for x in range(20) if x % 3]z+[x**3 for x in range(20) if x % 2 if x % 3]list(x**3 for x in range(20))z&list(x**3 for x in range(20) if x % 3)z/list(x**3 for x in range(20) if x % 2 if x % 3)z foo(*args)zfoo(*args, **kw)z foo(**kw)zfoo(key=value)zfoo(key=value, *args)zfoo(key=value, *args, **kw)zfoo(key=value, **kw)zfoo(a, b, c, *args)zfoo(a, b, c, *args, **kw)zfoo(a, b, c, **kw)zfoo(a, *args, keyword=23)z foo + barz foo - barz foo * barz foo / barz foo // barz lambda: 0z lambda x: 0z lambda *y: 0zlambda *y, **z: 0z lambda **z: 0zlambda x, y: 0zlambda foo=bar: 0z"lambda foo=bar, spaz=nifty+spit: 0zlambda foo=bar, **z: 0z#lambda foo=bar, blaz=blat+2, **z: 0z'lambda foo=bar, blaz=blat+2, *y, **z: 0zlambda x, *y, **z: 0z(x for x in range(10))zfoo(x for x in range(10))z...za[...]rr#rrrtest_expressions]sP                                       z-RoundtripLegalSyntaxTestCase.test_expressionscC|jddS)Nar"r#rrrtest_simple_expressionz3RoundtripLegalSyntaxTestCase.test_simple_expressioncCr')Nza = bza = b = c = d = er"r#rrrtest_simple_assignmentsr)z4RoundtripLegalSyntaxTestCase.test_simple_assignmentscJCs|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jttd iiWdQRX|jttd iiWdQRX|jttd iiWdQRX|jttdiiWdQRX|jttdiiWdQRX|jttdiiWdQRX|jttdiiWdQRX|jttdiiWdQRX|jttdiiWdQRX|jttdiiWdQRXdS)Nz x: int = 5z"y: List[T] = []; z: [list] = fun()zx: tuple = (1, 2)zd[f()]: int = 42zf(d[x]): str = 'abc'zx.y.z.w: complex = 42jzx: intz#def f(): x: str y: int = 5 z#class C: x: str y: int = 5 zIclass C: def __init__(self, x: int) -> None: self.x: int = x z2+2: intz []: int = 5zx, *y, z: int = range(5)zt: tuple = 1, 2z u = v: intz False: intz x.False: intz x.y,: intz[0]: intzf(): int)r assertRaises SyntaxErrorrr#rrrtest_var_annots<                    z+RoundtripLegalSyntaxTestCase.test_var_annotcCs||jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jd |jd dS) Nza += bza -= bza *= bza /= bza //= bza %= bza &= bza |= bza ^= bza <<= bza >>= bza **= br"r#rrr!test_simple_augmented_assignmentss           z>RoundtripLegalSyntaxTestCase.test_simple_augmented_assignmentscCsl|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jd |jd |jd |jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd!|jd"|jd#|jd$dS)%Nz def f(): passzdef f(*args): passzdef f(*args, **kw): passzdef f(**kw): passzdef f(foo=bar): passzdef f(foo=bar, *args): passz!def f(foo=bar, *args, **kw): passzdef f(foo=bar, **kw): passzdef f(a, b): passzdef f(a, b, *args): passzdef f(a, b, *args, **kw): passzdef f(a, b, **kw): passzdef f(a, b, foo=bar): passz!def f(a, b, foo=bar, *args): passz'def f(a, b, foo=bar, *args, **kw): passz def f(a, b, foo=bar, **kw): passz@staticmethod def f(): passz,@staticmethod @funcattrs(x, y) def f(): passz@funcattrs() def f(): passzdef f(*, a): passzdef f(*, a = 5): passzdef f(*, a = 5, b): passzdef f(*, a, b = 5): passz def f(*, a, b = 5, **kwds): passzdef f(*args, a): passzdef f(*args, a = 5): passzdef f(*args, a = 5, b): passzdef f(*args, a, b = 5): passz$def f(*args, a, b = 5, **kwds): passzdef f(a: int): passzdef f(a: int = 5): passzdef f(*args: list): passzdef f(**kwds: dict): passzdef f(*, a: int): passzdef f(*, a: int = 5): passzdef f() -> int: passr"r#rrrtest_function_defssH                                   z/RoundtripLegalSyntaxTestCase.test_function_defscC6|jd|jd|jd|jd|jddS)Nzclass foo():passzclass foo(object):passz!@class_decorator class foo():passz&@class_decorator(arg) class foo():passz(@decorator1 @decorator2 class foo():passr"r#rrrtest_class_defss     z,RoundtripLegalSyntaxTestCase.test_class_defscCr!)Nzfrom sys.path import *zfrom sys.path import dirnamezfrom sys.path import (dirname)zfrom sys.path import (dirname,)z*from sys.path import dirname as my_dirnamez,from sys.path import (dirname as my_dirname)z-from sys.path import (dirname as my_dirname,)z&from sys.path import dirname, basenamez(from sys.path import (dirname, basename)z)from sys.path import (dirname, basename,)z4from sys.path import dirname as my_dirname, basenamez6from sys.path import (dirname as my_dirname, basename)z7from sys.path import (dirname as my_dirname, basename,)z5from sys.path import dirname, basename as my_basenamez7from sys.path import (dirname, basename as my_basename)z8from sys.path import (dirname, basename as my_basename,)zfrom .bogus import xr"r#rrrtest_import_from_statements.          z7RoundtripLegalSyntaxTestCase.test_import_from_statementcCr9)Nz import syszimport sys as systemzimport sys, mathzimport sys as system, mathzimport sys, math as my_mathr"r#rrrtest_basic_import_statement     z8RoundtripLegalSyntaxTestCase.test_basic_import_statementcCsT|jd|jd|jd|jd|jd|jd|jd|jddS) Nzfrom . import namezfrom .. import namezfrom ... import namezfrom .... import namezfrom .pkg import namezfrom ..pkg import namezfrom ...pkg import namezfrom ....pkg import namer"r#rrrtest_relative_importss       z2RoundtripLegalSyntaxTestCase.test_relative_importscCr/)Nz"# -*- coding: iso-8859-1 -*- pass r"r#rrr test_pep263+rz(RoundtripLegalSyntaxTestCase.test_pep263cCr/)Nzassert alo < ahi and blo < bhi r"r#rrr test_assert/rz(RoundtripLegalSyntaxTestCase.test_assertcC"|jd|jd|jddS)Nzwith open('x'): pass zwith open('x') as f: pass z*with open('x') as f, open('y') as g: pass r"r#rrr test_with2  z&RoundtripLegalSyntaxTestCase.test_withcCr%)Nztry: pass except: pass ztry: pass finally: pass z'try: pass except A: pass finally: pass z4try: pass except A: pass except: pass finally: pass z"try: pass except: pass else: pass z0try: pass except: pass else: pass finally: pass r"r#rrr test_try_stmt7s      z*RoundtripLegalSyntaxTestCase.test_try_stmtcs@d}tj|}fddd&d'd(d)d*d+d,d.d/d0d1d2d3d5d7d9g}|jt|jddd||jt|jdd|D|jt|jdd d!d|D|jt|jdd"d#d|D|jt|jdddd$d|D|jttj|ddd||jttj|dddd%d|DdS):Nzdef f(x): return x + 1c3sX|d}|d}t|ttfrNx4|ddD]}x|D] }|Vq:Wq,Wn|VdS)Nr) isinstanceZtuplelist)treeZ node_typeZnextZeltrwalkrrrJGsz8RoundtripLegalSyntaxTestCase.test_position..walkrEdefrr (r) : return+ 1T) line_infocol_infocSsg|]\}}}}||fqSrr.0rnZlcrrr gz>RoundtripLegalSyntaxTestCase.test_position..)r_cSsg|]\}}}}|||fqSrrrarrrreirf)r`cSsg|]\}}}}|||fqSrrrarrrrekrfcSg|] }t|qSrrGrbrrrrremrfcSrgrrhrirrrresrf)rErKrEr)rEr rErL)rMrNrErO)rErrErP)rQrRrErM)rSrTrErQ)rLrUrErV)rOrUrWrj)rErXrWrL)rErrWrS)rYrZrWr[)rWr\rWr])rLrUrWr^rj)rPrUrWrjrj)rLrUrWrjrj)rrUrWrj)rrr rGrZtolistZst2tupleZst2list)r rstZexpectedrrIr test_positionAsF    z*RoundtripLegalSyntaxTestCase.test_positioncCs,|jd|jd|jd|jddS)Nz*a = yz x, *b, = mz [*a, *b] = yzfor [*x, b] in x: passr"r#rrrtest_extended_unpackingus   z4RoundtripLegalSyntaxTestCase.test_extended_unpackingcCrA)Nzraise zraise e zBtry: suite except Exception as e: raise ValueError from e r"r#rrrtest_raise_statement{rCz1RoundtripLegalSyntaxTestCase.test_raise_statementcCr')Nz[]z[*{2}, 3, *[4]]r-r#rrrtest_list_displaysr)z/RoundtripLegalSyntaxTestCase.test_list_displayscCr9)Nz{*{2}, 3, *[4]}z{2}z{2,}z{2, 3}z{2, 3,}r-r#rrrtest_set_displaysr=z.RoundtripLegalSyntaxTestCase.test_set_displayscCsJ|jd|jd|jd|jd|jd|jd|jddS)Nz{}z{a:b}z{a:b,}z {a:b, c:d}z {a:b, c:d,}z{**{}}z{**{}, 3:4, **{5:6, 7:8}}r-r#rrrtest_dict_displayss      z/RoundtripLegalSyntaxTestCase.test_dict_displayscCr9)Nz f(*a, **b)zf(a, *b, *c, *d)z f(**a, **b)zf(2, *a, *b, **b, **c, **d)z0f(*b, *() or () and (), **{} and {}, **() or {})r-r#rrrtest_argument_unpackingr=z4RoundtripLegalSyntaxTestCase.test_argument_unpackingcCrA)Nz{x for x in seq}z{f(x) for x in seq}z#{f(x) for x in seq if condition(x)}r-r#rrrtest_set_comprehensionsrCz4RoundtripLegalSyntaxTestCase.test_set_comprehensionscCrA)Nz{x:x for x in seq}z({x**2:x[3] for x in seq if condition(x)}z4{x:x for x in seq1 for y in seq2 if condition(x, y)}r-r#rrrtest_dict_comprehensionsrCz5RoundtripLegalSyntaxTestCase.test_dict_comprehensionsN)#__name__ __module__ __qualname__rrrr r$r&r(r*r+r.r1r3r6r7r8r:r;r<r>r?r@rBrDrlrmrnrorprqrrrsrtrrrrrs@   *'0   4 rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)IllegalSyntaxTestCasec Cs8ytj|Wntjk r$YnX|jd|dS)Nz"did not detect invalid tree for %r)rrrr)r rHZlabelrrrcheck_bad_trees z$IllegalSyntaxTestCase.check_bad_treecCs|jdddS)NrErWz)rErWrzryr#rrr test_junkr2zIllegalSyntaxTestCase.test_junkc CsDd}|j|d d"}|j|d d+}|j|dd4}|j|ddS)5N rErLrUrz too small items in terminal nodepassz'non-string second item in terminal nodepass0z'non-integer third item in terminal nodeztoo many items in terminal noderErrrrrLrUrrrr~rrrrU)r}rrrrErrrrrrrrrr~rrr)r}rrrrErrrrrrrrrrrr~rrr)r}rrrrErrrrrrrrrrrr~rrr)r}rrrr{r rHrrrtest_illegal_terminalsP   z+IllegalSyntaxTestCase.test_illegal_terminalc$Csdg}|j|d,dS)hNr}rErKr rMrNrQrRrSrT#rLrUrO  rX9$%&')*+,-./01rWr\yieldrPrdef f(): return 1 yield 1rErKrEr rMrNrQrRrrrrSrTrrOrUrErXrWr\rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrErrrrrrrrrrrrrrrrrrrrrrrrrrrrrrPrUrrrrrrrrrrrrrrrrrrrrrrrr)r}rr{rrrrtest_illegal_yield_1sfz*IllegalSyntaxTestCase.test_illegal_yield_1c%Csdy}|j|d3dS)zNr}rrrrEfrom __future__import generatorsrLrUrrrKr rrMrNrQrRrSrTrrOrrrXrrrrrrrrrrrrrrrWr\rrrPrrrErrErrrrErrErrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr)r}rrr{rrrrtest_illegal_yield_2!sxz*IllegalSyntaxTestCase.test_illegal_yield_2cCsd:}|j|ddS);N7"rrrr(rrrrrrrrEr0 ,rdrLrUrza,,crEr0rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrErdrrrrrrrr rr rr rr rr rrrrrrrrrrrrrrrrr)rrrrr{rrrrtest_a_comma_comma_cas.z*IllegalSyntaxTestCase.test_a_comma_comma_ccCsdD}|j|ddS)ENr}rrr 8rrrrrrrrrrrrrrEr0 %$=brLrUrza $= brrrrrrrrrrrrr rr!rr"rr#rr$rr%rr&rr'rr(rrrr*rErrr,rr-rr.rr/rr0rr1rr2rr3rr4rr5rr6rr7rr8rr9rr)r+r:rr;rrr<rrr=r)r}r>rr{rrrrtest_illegal_operator|s:z+IllegalSyntaxTestCase.test_illegal_operatorcCsd}|j|d dS)Nr}rrrrEfoorLrUrzmalformed global astrErAr@rBrrCrrrDrrrErr)r}rFrrr{rrrrtest_malformed_globalsz+IllegalSyntaxTestCase.test_malformed_globalc Csd}|j|ddS)Nr}rr~rr@rErrrfredrLrUrzfrom import fredrrrErJrrKrIrLrHrrrMr@rNrrOrr~rPrrrQrr)r}rRrrr{rrrrtest_missing_import_sourcesz0IllegalSyntaxTestCase.test_missing_import_sourcec CsHd }|j|dd}|j|dd}|jttj|WdQRXdS)NSr}rrUzmissed encoding iso-8859-1znon-string encodingrr}r)rTrWrrW)rTrWrUrrW)rTrWrV)ryr4ZUnicodeEncodeErrorrrrrrrtest_illegal_encodings   z+IllegalSyntaxTestCase.test_illegal_encodingN) rurvrwryr|rrrrr?rGrSrXrrrrrxs.7@! rxc@s<eZdZddZddZddZddZd d Zd d Zd S)CompileTestCasecCs(tjd}tj|}|jt|ddS)N2 + 3rOrr compilestr Zeval)r rkrrrrtest_compile_exprrCz!CompileTestCase.test_compile_exprcCs6tjd}tj|}i}t|||j|dddS)Nx = 2; y = x + 3ZyrO)rrr\rr )r rkrZglobsrrrtest_compile_suites    z"CompileTestCase.test_compile_suitecCstjd}|jttj|dS)Nz 1 = 3 + 4rrr4r5r\r rkrrrtest_compile_errorr)z"CompileTestCase.test_compile_errorcCs8tjd}|jttj|tjd}|jttj|dS)Nza = "\U12345678"z a = "\u1"r`rarrrtest_compile_badunicodes  z'CompileTestCase.test_compile_badunicodecCsBtjd}tj|}|jt|dtj|}|jt|ddS)Nz-3rzrdr[)r rkZcode1Zcode2rrrtest_issue_9011s    zCompileTestCase.test_issue_9011cCstjd}tj|}|j|jd|j}|j|jdx>dD]6}tj||}|j|jd|j|}|j|jdq>WxvtdtdfD]b}|jt tj||}WdQRX|j|jd|jt |j|}WdQRX|j|jdqW|j t tj|t d|j t |jt ddS)Nza + 5z file.pyfile.py)rfrg) rrr\r Z co_filenamerZ bytearrayZ memoryviewZ assertWarnsZDeprecationWarningr4 TypeErrorrG)r rkrZfilenamerrrtest_compile_filenames&       z%CompileTestCase.test_compile_filenameN) rurvrwr]r_rbrcrerirrrrrYs  rYc@s$eZdZddZddZddZdS)ParserStackLimitTestCasecCsd|d|S)N[]r)r Zlevelrrr_nested_expressionrz+ParserStackLimitTestCase._nested_expressioncCs |jd}tj|}|jdS)Ni])rmrrr)r erkrrrtest_deeply_nested_list s  z0ParserStackLimitTestCase.test_deeply_nested_listcCs6|jd}td|\}}}|jd||jd|dS)Nidz-css_push: parser stack overflows MemoryError)rmrZassertIn)r rnZrcZoutZerrrrrtest_trigger_memory_errors  z2ParserStackLimitTestCase.test_trigger_memory_errorN)rurvrwrmrorprrrrrjsrjc@s0eZdZddZddZejZejddZdS)STObjectTestCasec Cstjd}tjd}tjd}tjd}tjd}tjd}|j||kd|j||kd|j||kd|j|||j|||j|||j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||kd|j||k||k |j||k||k |j||k||k |j||k||k|j||k||k|j||k||k|j||k||k|j||k||k|j||k||kt|||}t|||}t|||gd} |j|| k|j||k|j| |k|j|| k|j||k|j| |k|j||k|j| | k|j||k|j|dkd|jd|kd|jt t j |d|jt t j d||jt t j |d |jt t jd |dS) NrZr^r,TFrEghҘ@zspanish armadaiswaterloo)rrrr ZminZmaxZsortedZ assertTruer4rhoperatorZgeZleZltZgt) r r rZst3Zst1_copyZst2_copyZst3_copyZbottomZtopZmidrrrtest_comparisonssf           z!STObjectTestCase.test_comparisonscCstjdtjdtjdg}x|D]~}tj|}|j|j|jtj|}|j|j|jxS)NrEirzi)Z bit_length)rcrrr XXXROUNDUPks  z0STObjectTestCase.test_sizeof..XXXROUNDUPZPiiZhP3iP0hcs|dkr dSd}t|dko(t|dt}|rB|t|dd7}|rR|ddn |dd}|r|t|7}x|D]}||7}q|W|S)NrrErjrjrj)ZlenrFr)ZnodeZresZhasstrZchildrenZchild)rwnodesizesizeofchildrenrrryts z4STObjectTestCase.test_sizeof..sizeofchildrencs j||jdSr) check_sizeofr)rk)basesizerxr ryrrcheck_st_sizeofs z5STObjectTestCase.test_sizeof..check_st_sizeofrZz 2 + 3 + 4z x = 2 + 3rUz# -*- coding: utf-8 -*-rkz2,irl)rZ calcobjsizestructZcalcsizerrr)r r|r)rwr{rxr ryr test_sizeofis  zSTObjectTestCase.test_sizeofN) rurvrwrsrvrrzZ cpython_onlyr~rrrrrqs=rqc@seZdZddZdS)OtherParserCasec Cs&|jttjddWdQRXdS)Nr0r)r4rhrrr#rrrtest_two_args_to_exprs z%OtherParserCase.test_two_args_to_exprN)rurvrwrrrrrrsrZ__main__)rtrruZunittestrrr}ZtestrZtest.support.script_helperrZTestCaserrxrYrjrqrruZmainrrrrZs(  !:x