m *Ec@sdkZdkZdkZdkZdklZlZdklZdk l Z l Z dk l Z lZyeWn,ej o eiieidZnXeiieZeiieZdZdZdZd Zd efd YZgZd fd YZdZe e!dZ"dZ#dZ$dfdYZ%gZ&dZ'hZ(dZ)dei*fdYZ+edZ,e-djoei.ddndS(N(scompile_restrictedsPrintCollector(sRestrictionCapableEval(srestricted_modulesverify(sRModules RFunctionicCsRd}xE|D]=}|djo|d}q |djo|d}q Pq W|S(s0Returns the indentation level of the given line.it is iN(tindenttlinetc(RRR((tF/data/zmath/zope/lib/python/RestrictedPython/tests/testRestrictions.pyt _getindents  cCst|d}x#t|iD]}|i}qWt |}d}x:|o2||}|i}t ||joPqJqJW|i ||fS(sGiven a func_code object, this function tries to find and return the python source code of the function. Originally written by Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)trtN( topentfntftrangetfunctco_firstlinenotitreadlineRRtindtmsgtclose(R R R RRRR((Rt find_source"s     cCs\|id}|ido|d }nt||id}|ip td|S(s#Less silly interface to find_sourcet__file__s.pyciisSource should not be empty!N( R t func_globalstfiletendswithRt func_codetsourcetstriptAssertionError(R RR((Rt get_source5s c Bseiied}e|d}|i}|i e ||de ||d}hdhde <dd<dd<s  *t AccessDeniedcBstZRS(N(R!t __module__(((RR9QstRestrictedObjectcBsktZeZdZdZdZdZdZdZ d Z dZ d Z d Z d Zd ZeZRS(NiiiiisAnother day, another test...t writeablecCs]|djo tnC|djp |djodSn!|djotSn |i|SdS(Nt protecteditsafei(tidxR9tDisallowedObjecttselfts(RAR?((Rt __getitem___s   cCs|i||!S(N(RARBtlothi(RARDRE((Rt __getslice__iscCs t|iS(N(R)RARB(RA((Rt__len__lscCs$|djo ||_ntdS(NR>(R?tvRAR>R9(RAR?RH((Rt __setitem__os  cCs tdS(N(R9(RARDREtvalue((Rt __setslice__us(s writeable(R!R:R@t disallowedtallowedt_t__t _some_attrt__some_other_attr__RBt__writeable_attrs__RCRFRGRIRKtwrite(((RR;Us    cCs*t||}|tjo tn|S(N(R5tobR7RHR@R9(RTR7RH((Rtguarded_getattr{s  icCst|tjoe|idjoU|i}|i}|djo d}n|djo||}q|||!}n ||}|t jo t n|S(Ni( ttypetindext SliceTypetstepR tstarttstopRTRHR@R9(RTRWR[RZRH((Rtguarded_getitems#        cCs'|djo tdndk}|S(Nt __future__sOnly future imports are allowed(R7t ValueErrorR](R7t_globalst_localstnamesR]((Rtminimal_imports   t TestGuardcBs5tZdZedZdZdZdZRS(s A guard classcCs||idx7tdD])}|id|}|i|dq WdS(Nisprint%ss Hello, world!(R RRARtrest assertEqual(RARR((Rt checkPrints cCs=y|id}Wntj onX|id|dS(Nt printToNonei(RARRtAttributeErrortfail(RAR((RtcheckPrintToNones cCs#|id}|i|ddS(Nt printStuffsa b c(RARRR(RAR((RtcheckPrintStuffscCs#|id}|i|ddS(Nt printLiness0 1 2 3 4 5 6 7 8 (RARRR(RAR((RtcheckPrintLinesscCs#|id}|i|ddS(Ntprimess[2, 3, 5, 7, 11, 13, 17, 19](RARRR(RAR((Rt checkPrimesscCs#|id}|i|ddS(Ntallowed_simplet abcabcabc(RARRR(RAR((RtcheckAllowedSimplescCs|idtdS(Nt allowed_read(RARR;(RA((RtcheckAllowedReadscCs|idtdS(Nt allowed_write(RARR;(RA((RtcheckAllowedWritescCs|idtdS(Ntallowed_default_args(RARR;(RA((RtcheckAllowedArgsscCs#|id}|i|ddS(Nttry_maps [2, 3, 4](RARRR(RAR((Rt checkTryMapscCs:t2|id}|itdg|i|ddS(Nt try_applyRjt321(RkRARRR(RAR((Rt checkApplyscCs1ti|id}|itdddS(Nt try_inplaces+=ii(ii(RrtclearRARRR(RAR((Rt checkInplaces cCsnxgtiD]Y}|d djoBy|i|tWntj oqfX|id|q q WdS(Nitdenieds%s() did not trip security(R4tkeystkRARR;R9R(RAR((Rt checkDenied s c Bseiied}e|d}|i}|i e ||d}hdhde <<}||Ux|iD]\}}e|doce||i\}}d|}ye||d}Wnej oqX|id|qyqyWdS( Nssecurity_in_syntax.pyRRRRRs#from __future__ import generators s%s should not have compiled(R,R-R.R/R RR R0RRR1R3RbtmtitemsRRHthasattrRRtfilenameR2t SyntaxErrorRAR( RAR3R RRRRHRR ((RtcheckSyntaxSecuritys"    cCs#|id}|i|ddS(Ntorder_of_operationsi(RARRR(RAR((RtcheckOrderOfOperations,scCs&|idd}|i|ddS(Ntrot13s Zope is k00ls Mbcr vf x00y(RARRR(RAR((Rt checkRot130scCs#|id}|i|ddS(Ntnested_scopes_1i(RARRR(RAR((RtcheckNestedScopes14scCstd}ddg}|}|i|ihd|<}|i||ddg}|d|}|i||dS(Ns{'a':[m.pop()]}['a'] + [m[0]]i i"R( tRestrictionCapableEvaltexprRHtexpecttreversetevalRRAR(RARRHRR((RtcheckUnrestrictedEval8s    cCsxtiD]u\}}|id oXt|doH|ii}t t |ii}|i ||jd|||fq q WdS(NRNRsNThe stack size estimate for %s() should have been at least %d, but was only %d(R4RRtrfunct startswithRRt co_stacksizetrssR5R"tssRAt failUnless(RARRRR((RtcheckStackSizeCs  ! cCsKdkl}dkl} dkl} tid}g} | i D]!}|i do | |qDqD~ }x|D]}t| |}t|} ti|d| } || d} | i}t| |d d }t|}ti|d|}| |}|it|t|| iti| iquWdS( N(sRModule(sbefore_and_after(sparses$def ([_A-Za-z0-9]+)_(after|before)\(t_beforesdef \1(Ritafter(tRestrictedPython.RCompiletRModuletRestrictedPython.teststbefore_and_aftertcompilertparsetreR1tdefret_[1]R#R7RtbeforelR5tbeforeRt before_srctsubtrmt _get_treet tree_beforeRt after_srct tree_afterRARtstrR~tgetCode(RARRRR7RRRRRRRRRR((RtcheckBeforeAndAfterNs&   8     iicCsKdkl}dkl} dkl}tid}g} | i D]!}|i do | |qDqD~ }x|D]}t| |}t|} ti|d| } || d} | i}t| |d d }t|}ti|d|}||} |it|t| | iti| iquWdS( N(sRModule(sbefore_and_after24(sparses$def ([_A-Za-z0-9]+)_(after|before)\(Rsdef \1(RiR(RRRtbefore_and_after24RRRR1RRR#R7RRR5RRRRRRRRRRRARRR~R(RARRR7RRRRRRRRRRR((RtcheckBeforeAndAfter24js&   8     cCs]tiit|}t|d}|i}|i t ||d}t i ||S(NRR( R,R-R.R/R7RR R0RRR2tcoR~(RAR7RR RR-((Rt _compile_files   cs|id}gd}hd|<de<}||hUd}ddgddfddgddd gd gdd gddd fd fdd fddd gdd d fddfd d ffddfddfd d ffd d f|d dgddgdd gd dggddgdd gd dgddgggd d gggfddgggddggddgd d gggd d ggd d gg}|i |}|i e |e |||<|i|dS(Ns unpack.pycsi|t|S(N(tcallsRltseqR(R(R(Rtgetiters R{R}sa TypeError instanceiit12iitatbi*iii(RARRRRRutglobalst ineffabletexpectedRWRtassert_t isinstancet TypeErrorR(RARRRRRRR((RRtcheckUnpackSequences  Zcsgeddd}ei|gd}hd|<}||hU|idgd gdS( Ns[x for x, y in [(1, 2)]]sRcsi|t|S(N(RRlRBR(RB(R(RRs R{ii(ii(ii(R2RR~RRRRAR(RARRRR((RRtcheckUnpackSequenceExpressions   csaeddd}ei|gd}hd|<}||hU|idgdS( Ns x, y = 1, 2stsinglecsi|t|S(N(RRlRBR(RB(R(RRs R{ii(ii(R2RR~RRRRAR(RARRRR((RRtcheckUnpackSequenceSingles   c sggd}d}|id}hd|<d|<}||hU|iddddddg|id d gdS( Ncsi|t||S(N(t getattr_callsRltattrR5tobj(RR(R(Rt test_getattrs csi|ii|S(N(t setattr_callsRlRt __class__R!(R(R(Rt test_setattrssclass.pyRwRytsettgettstatetMyClass( RRRRRARRRR(RARRRRRR((RRRt checkClasss    cBs|id}|hhUdS(Ns lambda.py(RARR(RAR((Rt checkLambdascCsHtddddh}|itddddh}|idS(NRtissue945sempty.pys# still empty # by(t RFunctiontrfRtrf2(RARR((Rt checkEmptys cCs#d}|itt|dddS(NsVdef f(x, y): if x, y < 2 + 1: return x + y else: return x - y sR(terrRAt assertRaisesRR2(RAR((RtcheckSyntaxErrors c CsKdkl}|dddddddd d f}d |_|idS( N(s RFunctiontpRtbodys+# testing print "testing" return printed R7ttestRsRR(RRtgentmodeR(RARR((RtcheckLineEndingsRFunctions    cCs3dkl}|dd}d|_|idS(N(sRestrictedCompileModes+# testing print "testing" return printed s R(RtRestrictedCompileModeRRR(RARR((Rt%checkLineEndingsRestrictedCompileModes    (ii($R!R:RRRRRRRRRRRRRRRRRRRRRtsyst version_infoRRRRRRRRRRR(((RRvsB                   &    cCstitdS(Ntcheck(tunittestt makeSuiteRv(((Rt test_suitest__main__t defaultTestR (/R,RRR tRestrictedPythonR2RtRestrictedPython.EvalRRR"R~RRRRt NameErrorR-tabspathtargvt _FILEPATHtdirnameR/RRRR8t ExceptionR9R@R;RURVtsliceRXR\RbRcRkRoRrRutTestCaseRvR R!tmain( R RURbRR9R;R8RuRR~RRR/RoRRXRkRR2RRR"RRvR@RcRRRrR\R R,((Rt?sF       &     U