chQc@`sdZddlmZmZddlZddlZddlZddlZddlZddl m Z m Z ddl m Zddl mZddlmZyddlmZWnek reZnXd Zd efd YZd efd YZdefdYZdeefdYZdefdYZdefdYZdefdYZ defdYZ!defdYZ"defdYZ#dS(s6 Test cases for the L{twisted.python.failure} module. i(tdivisiontabsolute_importN(tNativeStringIOt_PY3(t _reflectpy3(tfailure(tSynchronousTestCase(traisercO`s,y ddWntj||}nX|S(s Make a C{Failure} of a divide-by-zero error. @param args: Any C{*args} are passed to Failure's constructor. @param kwargs: Any C{**kwargs} are passed to Failure's constructor. ii(RtFailure(targstkwargstf((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytgetDivisionFailures  tFailureTestCasecB`seZdZdZdZer0de_ndZesKde_ndZdZ dZ d Z d Z e e d Ze d Ze d ZdZdZdZdZdZdZdZdZdZdZdZdZde_er [ndZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&esd$e%_d$e&_nRS(%s' Tests for L{failure.Failure}. cC`s_ytdWntj}nX|jtt}|j|t|j|jtdS(s( Trapping a L{Failure}. ttestN(tNotImplementedErrorRRttrapt SystemExitt RuntimeErrort assertEqualttype(tselfR terror((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_failAndTrap1scC`sXt}y |Wntj}nX|jtj|jt}|j||dS(s If the wrapped C{Exception} is not a subclass of one of the expected types, L{failure.Failure.trap} raises the current L{failure.Failure} ie C{self}. N(t ValueErrorRRt assertRaisesRt OverflowErrortassertIdentical(Rt exceptionR t untrapped((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_trapRaisesCurrentFailure>s  s_In Python3, Failure.trap raises the wrapped Exception instead of the original Failure instance.cC`sUt}y |Wntj}nX|jt|jt}|j||dS(s If the wrapped C{Exception} is not a subclass of one of the expected types, L{failure.Failure.trap} raises the wrapped C{Exception}. N(RRRRRRR(RRR R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_trapRaisesWrappedExceptionSs  s^In Python2, Failure.trap raises the current Failure instance instead of the wrapped Exception.cC`s>t}tj|}tj|}|j|j|dS(s A L{failure.Failure} constructed from another L{failure.Failure} instance, has its C{value} property set to the value of that L{failure.Failure} instance. N(RRRRtvalue(RRtf1tf2((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_failureValueFromFailureis cC`sVt}tj|}y|jtWntj}nX|j|j|dS(s A L{failure.Failure} constructed without a C{exc_value} argument, will search for an "original" C{Failure}, and if found, its value will be used as the value for the new C{Failure}. N(RRRRRRR (RRR!R"((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt!test_failureValueFromFoundFailureus cC`s'|j|j|d||fdS(s Assert that C{s} starts with a particular C{prefix}. @param s: The input string. @type s: C{str} @param prefix: The string that C{s} should start with. @type prefix: C{str} s%r is not the start of %rN(t assertTruet startswith(Rtstprefix((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertStartsWiths cC`s'|j|j|d||fdS(s Assert that C{s} end with a particular C{suffix}. @param s: The input string. @type s: C{str} @param suffix: The string that C{s} should end with. @type suffix: C{str} s%r is not the end of %rN(R%tendswith(RR'tsuffix((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertEndsWiths cC`s$|j|||j||dS(sc Assert that the C{tb} traceback contains a particular C{prefix} and C{suffix}. @param tb: The traceback string. @type tb: C{str} @param prefix: The string that C{tb} should start with. @type prefix: C{str} @param suffix: The string that C{tb} should end with. @type suffix: C{str} N(R)R,(RttbR(R+((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertTracebackFormats c C`sF|rd}ntd|}t}|r:|jn|j||j}d|j|jrkdpndf}dtj|j tj |j |jf}|j |||g|j D]} | jdr| ^q} |r"|jg| |r d} nd } |j| | n |jg| |jd |d S( s Assert that L{printDetailedTraceback} produces and prints a detailed traceback. The detailed traceback consists of a header:: *--- Failure #20 --- The body contains the stacktrace:: /twisted/trial/_synctest.py:1180: _run(...) /twisted/python/util.py:1076: runWithWarningsSuppressed(...) --- --- /twisted/test/test_failure.py:39: getDivisionFailure(...) If C{captureVars} is enabled the body also includes a list of globals and locals:: [ Locals ] exampleLocalVar : 'xyz' ... ( Globals ) ... Or when C{captureVars} is disabled:: [Capture of Locals and Globals disabled (use captureVars=True)] When C{cleanFailure} is enabled references to other objects are removed and replaced with strings. And finally the footer with the L{Failure}'s value:: exceptions.ZeroDivisionError: float division *--- End of Failure #20 --- @param captureVars: Enables L{Failure.captureVars}. @type captureVars: C{bool} @param cleanFailure: Enables L{Failure.cleanFailure}. @type cleanFailure: C{bool} txyzt captureVarss*--- Failure #%d%s--- s (pickled) t s#%s: %s *--- End of Failure #%s --- s s exampleLocalVar : "'xyz'"s exampleLocalVar : 'xyz'sA [Capture of Locals and Globals disabled (use captureVars=True)] N(R Rt cleanFailuretprintDetailedTracebacktgetvaluetcounttpickledtreflecttqualRtsafe_strR R.t splitlinesR&tassertNotEqualtassertInR( RR0R2texampleLocalVarR toutR-tstarttendtlinet linesWithVars((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertDetailedTracebacks0*        c C`s|rd}nt}t}|j||j}d}x7|jD],\}}} } } |d|| |f7}qJWtrd} nd} |j|d| fdtj|f|r|j d t j d|nd S( s` Assert that L{printBriefTraceback} produces and prints a brief traceback. The brief traceback consists of a header:: Traceback: : float division The body with the stacktrace:: /twisted/trial/_synctest.py:1180:_run /twisted/python/util.py:1076:runWithWarningsSuppressed And the footer:: --- --- /twisted/test/test_failure.py:39:getDivisionFailure @param captureVars: Enables L{Failure.captureVars}. @type captureVars: C{bool} tabcdets %s:%s:%s sclass 'ZeroDivisionError's#type 'exceptions.ZeroDivisionError'sTraceback: <%s>: s%s %ssexampleLocalVar.*abcdeN( R RtprintBriefTracebackR4tframesRR.RtEXCEPTION_CAUGHT_HERERtNonetretsearch( RR0R=R R>R-tstacktmethodtfilenametlinenot localVarst globalVarstzde((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertBriefTracebacks"        c C`s |rd}ntd|}t}|j||j}d}xZ|jD]O\}}} } } |d|| |f7}|dtj|| jf7}qPW|j|ddt j |t j |j t j|jf|r|jd tjd|nd S( s Assert that L{printTraceback} produces and prints a default traceback. The default traceback consists of a header:: Traceback (most recent call last): The body with traceback:: File "/twisted/trial/_synctest.py", line 1180, in _run runWithWarningsSuppressed(suppress, method) And the footer:: --- --- File "twisted/test/test_failure.py", line 39, in getDivisionFailure 1/0 exceptions.ZeroDivisionError: float division @param captureVars: Enables L{Failure.captureVars}. @type captureVars: C{bool} txyzzyR0REs File "%s", line %s, in %s s %s s"Traceback (most recent call last):s %s %s%s: %s sexampleLocalVar.*xyzzyN(R RtprintTracebackR4RGt linecachetgetlinetstripR.RRHR7R8RR9R RRIRJRK( RR0R=R R>R-RLRMRNRORPRQ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pytassertDefaultTraceback)s$         &cC`s|jdS(sr L{printDetailedTraceback} returns a detailed traceback including the L{Failure}'s count. N(RC(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_printDetailedTracebackWscC`s|jdS(sC L{printBriefTraceback} returns a brief traceback. N(RS(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_printBriefTraceback_scC`s|jdS(s8 L{printTraceback} returns a traceback. N(RY(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_printTracebackfscC`s|jdtdS(s L{printDetailedTraceback} captures the locals and globals for its stack frames and adds them to the traceback, when called on a L{Failure} constructed with C{captureVars=True}. R0N(RCtTrue(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt'test_printDetailedTracebackCapturedVarsmscC`s|jdtdS(s L{printBriefTraceback} returns a brief traceback when called on a L{Failure} constructed with C{captureVars=True}. Local variables on the stack can not be seen in the resulting traceback. R0N(RSR](R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt$test_printBriefTracebackCapturedVarsvscC`s|jdtdS(s L{printTraceback} returns a traceback when called on a L{Failure} constructed with C{captureVars=True}. Local variables on the stack can not be seen in the resulting traceback. R0N(RYR](R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_printTracebackCapturedVarsscC`s|jdtdtdS(s C{printDetailedTraceback} includes information about local variables on the stack after C{cleanFailure} has been called. R0R2N(RCR](R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt.test_printDetailedTracebackCapturedVarsCleanedscC`s#|jttjdddddS(su L{failure.format_frames} raises a L{ValueError} if the supplied C{detail} level is unknown. tdetailtnoisiaN(RRRt format_framesRI(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_invalidFormatFramesDetailscC`s<t}tj|}|jt|j|j|dS(N(RRRRRR (RteR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttestExplictPasss  cC`sTy|jWn3tk rCtjtjd}|ddSXtddS(Niis3f.raiseException() didn't raise ZeroDivisionError!?(traiseExceptiontZeroDivisionErrort tracebackt extract_tbtsystexc_infot Exception(RR R-((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt_getInnermostFrameLines  cC`s,t}|j|}|j|ddS(Ns1/0(R RoR(RR t innerline((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttestRaiseExceptionWithTBs cC`s6t}|j|j|}|j|ddS(Ns1/0(R R2RoR(RR Rp((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt testLackOfTBs  sQthe traceback is not preserved, exarkun said he'll try to fix this! god knows howcC`s2|jttjd}|jdt|dS(s Constructing a C{Failure} with a string as its exception value raises a C{TypeError}, as this is no longer supported as of Python 2.6. sono!s$Strings are not supported by FailureN(Rt TypeErrorRRR<tstr(Rtexc((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt test_stringExceptionConstructionscC`s|jtjtjdS(s Creating a Failure with no arguments causes it to try to discover the current interpreter exception state. If no such state exists, creating the Failure should raise a synchronous exception. N(RRtNoCurrentExceptionErrorR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttestConstructionFailsscC`s&t}|j|j|jdS(s If the C{Failure} has not been cleaned, then C{getTracebackObject} returns the traceback object that captured in its constructor. N(R RtgetTracebackObjectR-(RR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_getTracebackObjects cC`s[y ddWn/tk r=tj}tjdt}nX|j|j|jdS(sc C{captureVars=True} has no effect on the result of C{getTracebackObject}. iiR0N(RiRRR]RRyR-(Rt noVarsFailuret varsFailure((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt&test_getTracebackObjectFromCaptureVarss    cC`sat}tj|j}|jtj|j}|jd||j||dS(s If the Failure has been cleaned, then C{getTracebackObject} returns an object that looks the same to L{traceback.extract_tb}. N(R RjRkRyR2R;RIR(RR texpectedtobserved((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt test_getTracebackObjectFromCleans   cC`sWtdt}tj|j}|jtj|j}|j||dS(s If the Failure was created with captureVars, then C{getTracebackObject} returns an object that looks the same to L{traceback.extract_tb}. R0N(R R]RjRkRyR2R(RR R~R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt.test_getTracebackObjectFromCaptureVarsAndCleans  cC`s/tjtd}|j|jddS(s  L{failure.Failure}s need not be constructed with traceback objects. If a C{Failure} has no traceback information at all, C{getTracebackObject} just returns None. None is a good value, because traceback.extract_tb(None) -> []. s some errorN(RRRnRRyRI(RR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt'test_getTracebackObjectWithoutTracebackscC`sQy ddWntj\}}}nXtj|}|j|j|dS(s If a L{failure.Failure} is constructed with an exception but no traceback in Python 3, the traceback will be extracted from the exception's C{__traceback__} attribute. iiN(RlRmRRRR-(RtklassRR-R ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt$test_tracebackFromExceptionInPython3 s  cC`sYt}|j|jd|j|jj|j|j|j|jjddS(s L{failure.Failure.cleanFailure} sets the C{__traceback__} attribute of the exception to C{None} in Python 3. N(R R;R-RIRR t __traceback__R2(RR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt*test_cleanFailureRemovesTracebackInPython3s   sPython 3 only.('t__name__t __module__t__doc__RRRtskipRR#R$R)R,R.tFalseRCRSRYRZR[R\R^R_R`RaReRgRoRqRrttodoRvRxRzR}RRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR ,sV      K . .           t BrokenStrcB`seZdZdZRS(sb An exception class the instances of which cannot be presented as strings via C{str}. cC`s |dS(N((R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt__str__1s(RRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR,stBrokenExceptionMetaclasscB`seZdZdZRS(sa A metaclass for an exception type which cannot be presented as a string via C{str}. cC`stddS(Ns#You cannot make a string out of me.(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR<s(RRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR7stBrokenExceptionTypecB`seZdZeZRS(sa The aforementioned exception type which cnanot be presented as a string via C{str}. (RRRRt __metaclass__(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRAstGetTracebackTestscB`sVeZdZdZdZdZdZdZdZdZ dZ RS( s, Tests for L{Failure.getTraceback}. cC`s>t}tj|}|jd|}|j|tdS(s Construct a L{Failure} with an exception that raises an exception from its C{__str__} method and then call C{getTraceback} with the specified detail and verify that it returns a string. RbN(RRRt getTracebacktassertIsInstanceRt(RRbtxR Rj((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt_brokenValueTestNs cC`s|jddS(s A L{Failure} might wrap an exception with a C{__str__} method which raises an exception. In this case, calling C{getTraceback} on the failure with the C{"brief"} detail does not raise an exception. tbriefN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenValueBriefDetailZscC`s|jddS(sY Like test_brokenValueBriefDetail, but for the C{"default"} detail case. tdefaultN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenValueDefaultDetailcscC`s|jddS(sY Like test_brokenValueBriefDetail, but for the C{"default"} detail case. tverboseN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenValueVerboseDetailjscC`s8tjt}|jd|}|j|tdS(s Construct a L{Failure} with an exception type that raises an exception from its C{__str__} method and then call C{getTraceback} with the specified detail and verify that it returns a string. RbN(RRRRRRt(RRbR Rj((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt_brokenTypeTestqscC`s|jddS(s A L{Failure} might wrap an exception the type object of which has a C{__str__} method which raises an exception. In this case, calling C{getTraceback} on the failure with the C{"brief"} detail does not raise an exception. RN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenTypeBriefDetail|scC`s|jddS(sX Like test_brokenTypeBriefDetail, but for the C{"default"} detail case. RN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenTypeDefaultDetailscC`s|jddS(sX Like test_brokenTypeBriefDetail, but for the C{"verbose"} detail case. RN(R(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_brokenTypeVerboseDetails( RRRRRRRRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRJs   tFindFailureTestscB`sYeZdZdZdZdZdZdZedkrWdZ e e_ nRS(sE Tests for functionality related to L{Failure._findFailure}. cC`s@y ddWn |jtjjdnX|jddS(s Within an exception handler, _findFailure should return C{None} in case no Failure is associated with the current exception. iisNo exception raised from 1/0!?N(RRRt _findFailureRItfail(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt$test_findNoFailureInExceptionHandlers  cC`s7|jtjdd|jtjjddS(sS Outside of an exception handler, _findFailure should return None. iN(RRlRmRIRRR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_findNoFailurescC`sUt}|jy|jWn |jtjj|nX|jddS(s Within an exception handler, it should be possible to find the original Failure that caused the current exception (if it was caused by raiseException). s)No exception raised from raiseException!?N(R R2RhRRRRR(RR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_findFailures  cC`sdt}|jy|jWn/tj}|j|j|jnX|jddS(s When a Failure is constructed in the context of an exception handler that is handling an exception raised by raiseException, the new Failure should be chained to that original Failure. s)No exception raised from raiseException!?N(R R2RhRRRRR(RR tnewF((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt,test_failureConstructionFindsOriginalFailures    cC`s[ytjWn9tjk rItj}|j|jtjnX|jddS(s Pyrex and Cython are known to insert fake stack frames so as to give more Python-like tracebacks. These stack frames with empty code objects should not break extraction of the exception. s$No exception raised from extension?!N(RRhtRaiserExceptionRRR%tcheckR(RR ((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt/test_failureConstructionWithMungedStackSucceedss  sraiser extension not availableN( RRRRRRRRRRItskipMsgR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRs      tTestFormattableTracebackcB`s eZdZdZdZRS(s Whitebox tests that show that L{failure._Traceback} constructs objects that can be used by L{traceback.extract_tb}. If the objects can be used by L{traceback.extract_tb}, then they can be formatted using L{traceback.format_tb} and friends. cC`sAtjdddiigg}|jtj|dgdS(s A C{_Traceback} object constructed with a single frame should be able to be passed to L{traceback.extract_tb}, and we should get a singleton list containing a (filename, lineno, methodname, line) tuple. RMs filename.pyi{N(s filename.pyi{smethodN(Rt _TracebackRRjRkRI(RR-((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_singleFrames!cC`sVtjdddiigdddiigg}|jtj|ddgdS( s A C{_Traceback} object constructed with multiple frames should be able to be passed to L{traceback.extract_tb}, and we should get a list containing a tuple for each frame. tmethod1s filename.pyi{tmethod2iN(s filename.pyi{RN(s filename.pyiRN(RRRRjRkRI(RR-((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_manyFramess (RRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRs tTestFrameAttributescB`seZdZdZRS(sq _Frame objects should possess some basic attributes that qualify them as fake python Frame objects. cC`sRtjdd}|j|jt|j|jt|j|jtjdS(s L{_Frame} instances have the C{f_globals} and C{f_locals} attributes bound to C{dict} instance. They also have the C{f_code} attribute bound to something like a code object. t dummynamet dummyfilenameN(Rt_FrameRt f_globalstdicttf_localstf_codet_Code(Rtframe((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyttest_fakeFrameAttributess(RRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR st TestDebugModecB`s)eZdZdZdZdZRS(sF Failure's debug mode should allow jumping into the debugger. c`sstjtrtjjntjjdfd}|j|g|_|jj t_tj dS(sK Override pdb.post_mortem so we can make sure it's called. t__init__c`s2t_trtj_ntjjds6           KR&9