3 7_@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.roundtripcCs|jtj|dS)N)rrexpr)r r rrr check_exprsz'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_passedcCs|jtj|dS)N)rrr)r r rrr check_suite)sz(RoundtripLegalSyntaxTestCase.check_suitecCs|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_statementcCs@|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 42)r)r rrrtest_await_statementCs      z1RoundtripLegalSyntaxTestCase.test_await_statementcCs|jd|jddS)Nz"async def f(): async with 1: passz/async def f(): async with a as b, c as d: pass)r)r rrrtest_async_with_statementKs z6RoundtripLegalSyntaxTestCase.test_async_with_statementcCs|jd|jddS)Nz'async def f(): async for i in (): passz*async def f(): async for i, b in (): pass)r)r rrrtest_async_for_statementOs z5RoundtripLegalSyntaxTestCase.test_async_for_statementcCs|jd|jddS)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]zlist(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[...])r)r rrrtest_expressions]sP                                       z-RoundtripLegalSyntaxTestCase.test_expressionscCs|jddS)Na)r)r rrrtest_simple_expressionsz3RoundtripLegalSyntaxTestCase.test_simple_expressioncCs|jd|jddS)Nza = bza = b = c = d = e)r)r rrrtest_simple_assignmentss 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 SyntaxErrorr)r 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 **= b)r)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: pass)r)r rrrtest_function_defssH                                   z/RoundtripLegalSyntaxTestCase.test_function_defscCs6|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():pass)r)r rrrtest_class_defss     z,RoundtripLegalSyntaxTestCase.test_class_defscCs|jd|jd|jd|jd|jd|jd|jd|jd|jd |jd |jd |jd |jd |jd|jd|jd|jddS)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 x)r)r rrrtest_import_from_statements.          z7RoundtripLegalSyntaxTestCase.test_import_from_statementcCs6|jd|jd|jd|jd|jddS)Nz import syszimport sys as systemzimport sys, mathzimport sys as system, mathzimport sys, math as my_math)r)r rrrtest_basic_import_statements     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 name)r)r rrrtest_relative_importss       z2RoundtripLegalSyntaxTestCase.test_relative_importscCs|jddS)Nz"# -*- coding: iso-8859-1 -*- pass )r)r rrr test_pep263+sz(RoundtripLegalSyntaxTestCase.test_pep263cCs|jddS)Nzassert alo < ahi and blo < bhi )r)r rrr test_assert/sz(RoundtripLegalSyntaxTestCase.test_assertcCs"|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_with2s  z&RoundtripLegalSyntaxTestCase.test_withcCs@|jd|jd|jd|jd|jd|jddS)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) isinstancetuplelist)treeZ node_typenextZeltr)walkrrr:Gsz8RoundtripLegalSyntaxTestCase.test_position..walkr4defrr (r) : return+ 1T) line_infocol_infocSsg|]\}}}}||fqSrr).0rnlcrrr gsz>RoundtripLegalSyntaxTestCase.test_position..)rOcSsg|]\}}}}|||fqSrr)rQrrRrSrTrrrrUis)rPcSsg|]\}}}}|||fqSrr)rQrrRrSrTrrrrUkscSsg|] }t|qSr)r7)rQrrrrrUmscSsg|] }t|qSr)r7)rQrrrrrUss)r4r;r4r)r4r r4r<)r=r>r4r?)r4rr4r@)rArBr4r=)rCrDr4rA)r<rEr4rF)r?rErGrV)r4rHrGr<)r4rrGrC)rIrJrGrK)rGrLrGrM)r<rErGrNrV)r@rErGrVrV)r<rErGrVrV)rrErGrV)rrr r7rtolistZst2tupleZst2list)r rstZexpectedr)r:r test_positionAsF    z*RoundtripLegalSyntaxTestCase.test_positioncCs,|jd|jd|jd|jddS)Nz*a = yz x, *b, = mz [*a, *b] = yzfor [*x, b] in x: pass)r)r rrrtest_extended_unpackingus   z4RoundtripLegalSyntaxTestCase.test_extended_unpackingcCs"|jd|jd|jddS)Nzraise zraise e zBtry: suite except Exception as e: raise ValueError from e )r)r rrrtest_raise_statement{s  z1RoundtripLegalSyntaxTestCase.test_raise_statementcCs|jd|jddS)Nz[]z[*{2}, 3, *[4]])r)r rrrtest_list_displayss z/RoundtripLegalSyntaxTestCase.test_list_displayscCs6|jd|jd|jd|jd|jddS)Nz{*{2}, 3, *[4]}z{2}z{2,}z{2, 3}z{2, 3,})r)r rrrtest_set_displayss     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_displayscCs6|jd|jd|jd|jd|jddS)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_unpackings     z4RoundtripLegalSyntaxTestCase.test_argument_unpackingcCs"|jd|jd|jddS)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_comprehensionss  z4RoundtripLegalSyntaxTestCase.test_set_comprehensionscCs"|jd|jd|jddS)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_comprehensionss  z5RoundtripLegalSyntaxTestCase.test_dict_comprehensionsN)#__name__ __module__ __qualname__rrrrrrr r!r"r#r%r&r)r*r+r,r-r.r/r0r1r2r3rYrZr[r\r]r^r_r`rarrrrrs@   *'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 r8Zlabelrrrcheck_bad_trees z$IllegalSyntaxTestCase.check_bad_treecCs|jdddS)Nr4rGz)r4rGrg)rf)r rrr test_junkszIllegalSyntaxTestCase.test_junkc CsDd}|j|d d"}|j|d d+}|j|dd4}|j|ddS)5N r4r<rErz too small items in terminal nodepassz'non-string second item in terminal nodepass0z'non-integer third item in terminal nodeztoo many items in terminal noder4rmrqrlrrr<rErkrsrtrjrur<rErrE)rirvrwrxr4rnrmryrlrzr<rErkr{r|rjr}r<rErrE)rir~rrr4rorprrmrrlrr<rErkrrrjrr<rErrE)rirrrr4rorrrmrrlrr<rErkrrrjrr<rErrE)rirrr)rf)r r8rrrtest_illegal_terminalsP   z+IllegalSyntaxTestCase.test_illegal_terminalc$Csdg}|j|d,dS)hNr4r;r r=r>rArBrCrD#r<rEr?  rH9$%&')*+,-./01rGrLyieldr@rzdef f(): return 1 yield 1r4r;r4r r=r>rArBrrrrCrDr<rEr?rEr4rHrGrLrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr4rrGrLrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr<rErrrrrr@rErrrrrrrrrrrrrrrrr<rErrErrrrrr)rr)rf)r r8rrrtest_illegal_yield_1sfz*IllegalSyntaxTestCase.test_illegal_yield_1c%Csdy}|j|d3dS)zN  r4from __future__import generatorsr<rEr;r r=r>rArBrCrD#r?rH9$%&')*+,-./01rGrLrr@rzdef f(): return 1 yield 1r4rr4rrrr4rr4rrr rrr r r rr r<rErrrrrr4r;r4r r=r>rArBrrrrCrDr<rEr?rEr4rHrGrLrrrrrrrrrrrr rr!rr"rr#rr$rr%rr&rr'rr(rrr)rr*rr+r4rrGrLrr.rr/rr0rr1rr2rr3rr4rr5rr6rr7rr8rr9rr:rr;rr-r<rr=rr>r<rErr?r@rrAr@rErr,rBrCrrDrrrrErrrrrrFr<rErrErrGrHrIrrJ)rrrK)rf)r r8rrrtest_illegal_yield_2!sxz*IllegalSyntaxTestCase.test_illegal_yield_2cCsd:}|j|ddS);N7"#$%'()*+,-./r4r$ ,rTr<rErza,,cr4r$r[r^rZr_rYr`rXrarWrbrVrcrUrdrTrerSrfrRrgrQrhrPrirOrjr\r]r\r]r4rTr[rnrZrorYrprXrqrWrrrVrsrUrtrTrurSrvrRrwrQrxrPryrOrzrNrkrlrmr{r<rErrE)rMr|r}r~)rf)r r8rrrtest_a_comma_comma_cas.z*IllegalSyntaxTestCase.test_a_comma_comma_ccCsdD}|j|ddS)EN   8#$%&()*+,-./0r4r$ %$=br<rErza $= br4r$rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr4rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr<rErrrrrrrE)rrr)rf)r r8rrrtest_illegal_operator|s:z+IllegalSyntaxTestCase.test_illegal_operatorcCsd}|j|d dS)N  r4foor<rErzmalformed global astr4rrrrrr<rErrrrrr<rErrE)rrrr)rf)r r8rrrtest_malformed_globalsz+IllegalSyntaxTestCase.test_malformed_globalc Csd}|j|ddS)N  r4rrfredr<rErzfrom import fredr4rr4rr4rrrrrrrrrrrrrr<rErrrrrr<rErrE)rrrr)rf)r r8rrrtest_missing_import_sourcesz0IllegalSyntaxTestCase.test_missing_import_sourcec CsHd }|j|dd}|j|dd}|jttj|WdQRXdS)NSrrEzmissed encoding iso-8859-1znon-string encodingrrErr)rrrrErr)rrrrrErr)rrr)rfr'UnicodeEncodeErrorrr)r r8rrrtest_illegal_encodings   z+IllegalSyntaxTestCase.test_illegal_encodingN) rbrcrdrfrhrrrLrrrrrrrrrres.7@! rec@s<eZdZddZddZddZddZd d Zd d Zd S)CompileTestCasecCs(tjd}tj|}|jt|ddS)Nz2 + 3r?)rr compilestr eval)r rXrrrrtest_compile_exprs  z!CompileTestCase.test_compile_exprcCs6tjd}tj|}i}t|||j|dddS)Nzx = 2; y = x + 3yr?)rrrrr )r rXrZglobsrrrtest_compile_suites    z"CompileTestCase.test_compile_suitecCstjd}|jttj|dS)Nz 1 = 3 + 4)rrr'r(r)r rXrrrtest_compile_errors z"CompileTestCase.test_compile_errorcCs8tjd}|jttj|tjd}|jttj|dS)Nza = "\U12345678"z a = "\u1")rrr'r(r)r rXrrrtest_compile_badunicodes  z'CompileTestCase.test_compile_badunicodecCsBtjd}tj|}|jt|dtj|}|jt|ddS)Nz-3rgr)rrrr r)r rXcode1code2rrrtest_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)rr) rrrr co_filenamer bytearray memoryviewZ assertWarnsDeprecationWarningr' TypeErrorr7)r rXrfilenamerrrtest_compile_filenames&       z%CompileTestCase.test_compile_filenameN) rbrcrdrrrrrrrrrrrs  rc@s$eZdZddZddZddZdS)ParserStackLimitTestCasecCsd|d|S)N[]r)r levelrrr_nested_expressionsz+ParserStackLimitTestCase._nested_expressioncCs |jd}tj|}|jdS)N])r rrr)r erXrrrtest_deeply_nested_list s  z0ParserStackLimitTestCase.test_deeply_nested_listcCs6|jd}td|\}}}|jd||jd|dS)Ndz-css_push: parser stack overflows MemoryError)r rZassertIn)r r Zrcouterrrrrtest_trigger_memory_errors  z2ParserStackLimitTestCase.test_trigger_memory_errorN)rbrcrdr r rrrrrrsrc@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) Nz2 + 3zx = 2; y = x + 3zlist(x**3 for x in range(20))TFr4ghҘ@zspanish armadaiswaterloo)rrrr minmaxsortedZ assertTruer'roperatorgeleltgt) r rrZst3Zst1_copyZst2_copyZst3_copyZbottomtopZmidrrrtest_comparisonssf           z!STObjectTestCase.test_comparisonscCstjdtjdtjdg}x|D]~}tj|}|j|j|jtj|}|j|j|jxS)Nr4rg) bit_length)rRrrr 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)Nrr4rVrVrV)lenr5r)ZnodereshasstrZchildrenZchild)r(nodesizesizeofchildrenrrr-ts z4STObjectTestCase.test_sizeof..sizeofchildrencs j||jdS)N) check_sizeofr)rX)basesizer,r r-rrcheck_st_sizeofs z5STObjectTestCase.test_sizeof..check_st_sizeofz2 + 3z 2 + 3 + 4z x = 2 + 3rEz# -*- coding: utf-8 -*-rz2,ir)rZ calcobjsizestructZcalcsizerrr)r r0r)r(r/r,r r-r test_sizeofis  zSTObjectTestCase.test_sizeofN) rbrcrdrr$rr.Z cpython_onlyr2rrrrrs=rc@seZdZddZdS)OtherParserCasec Cs&|jttjddWdQRXdS)Nr$r)r'rrr)r rrrtest_two_args_to_exprs z%OtherParserCase.test_two_args_to_exprN)rbrcrdr4rrrrr3sr3__main__)rrrZunittestrr1ZtestrZtest.support.script_helperrZTestCaserrerrrr3rbmainrrrrs(  !:x