
=Nc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d d l	 m
 Z
 y d d l m Z Wn e k
 r e Z n Xd   Z d e j f d     YZ d	 e f d
     YZ d e f d     YZ d e e f d     YZ d e j f d     YZ d e j f d     YZ d e j f d     YZ d e j f d     YZ d e j f d     YZ e j d  d d f k rd d l m Z n  d S(   s    
Test cases for failure module.
iN(   t   unittestt   util(   t   failure(   t   raiserc          O   s,   y d d Wn t  j |  |   } n X| 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.
    i   i    (   R   t   Failure(   t   argst   kwargst   f(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   getDivisionFailure   s
    t   FailureTestCasec           B   s2  e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d e _ d Z d   Z d   Z e j d d  g e _ d   Z e j d d  g e _ e j d  d k r d Z e e _ e e _ n  d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s_   y t  d   Wn t j   } n X| j t t  } |  j | t  |  j | j t   d S(   s   Trapping a failure.t   testN(   t   NotImplementedErrorR   R   t   trapt
   SystemExitt   RuntimeErrort   assertEqualt   type(   t   selfR   t   error(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   testFailAndTrap(   s    c         C   s@   y t     Wn t j   } n X|  j t j | j t  d S(   s0   Making sure trap doesn't trap what it shouldn't.N(   t
   ValueErrorR   R   t   assertRaisesR   t   OverflowError(   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_notTrapped3   s
    c         C   s'   |  j  | j |  d | | f  d S(   s@   
        Assert that s starts with a particular prefix.
        s   %r is not the start of %rN(   t
   assertTruet
   startswith(   R   t   st   prefix(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   assertStartsWith<   s    c         C   s   t    } t j   } | j |  |  j | j   d  t j   } | j |  |  j | j   d  t j   } | j |  |  j | j   d  d S(   s>   
        None of the print* methods fail when called.
        s   *--- Failuret	   TracebackN(   R   t   StringIOt   printDetailedTracebackR   t   getvaluet   printBriefTracebackt   printTraceback(   R   R   t   out(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_printingSmokeTestD   s    	c         C   s   d } t  d t  } t j   } | j |  |  j | j   d  |  j d t j	 d | j     t j   } | j
 |  |  j | j   d  t j   } | j |  |  j | j   d  d S(   s   
        None of the print* methods fail when called on a L{Failure} constructed
        with C{captureVars=True}.

        Local variables on the stack can be seen in the detailed traceback.
        t   xyzzyt   captureVarss   *--- Failures   exampleLocalVar.*xyzzyR   N(   R   t   TrueR   R   R   R    t   assertNotEqualt   Nonet   ret   searchR!   R"   (   R   t   exampleLocalVarR   R#   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt"   test_printingCapturedVarsSmokeTestT   s    c         C   s^   d } t  d t  } | j   t j   } | j |  |  j d t j d | j	     d S(   s   
        C{printDetailedTraceback} includes information about local variables on
        the stack after C{cleanFailure} has been called.
        R%   R&   s   exampleLocalVar.*xyzzyN(
   R   R'   t   cleanFailureR   R   R(   R)   R*   R+   R    (   R   R,   R   R#   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt)   test_printingCapturedVarsCleanedSmokeTestj   s    
c         C   s   t  j    } t   } | j |  g  | j   j   D] } | j d  r5 | ^ q5 } |  j g  |  |  j d | j    d S(   s   
        Calling C{Failure()} with no arguments does not capture any locals or
        globals, so L{printDetailedTraceback} cannot show them in its output.
        s     s&   Capture of Locals and Globals disabledN(   R   R   R   R    t
   splitlinesR   R   t   assertSubstring(   R   R#   R   t   linet   linesWithVars(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_printingNoVarsx   s    	c         C   sp   t  j    } t d t  } | j |  g  | j   j   D] } | j d  r; | ^ q; } |  j g  |  d S(   s   
        Calling C{Failure(captureVars=True)} captures the locals and globals
        for its stack frames, so L{printDetailedTraceback} will show them in
        its output.
        R&   s     N(   R   R   R'   R   R    R0   R   R(   (   R   R#   R   R2   R3   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_printingCaptureVars   s    c         C   s<   t    } t j |  } | j t   |  j | j |  d  S(   N(   R   R   R   R   R   t   value(   R   t   eR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   testExplictPass   s    	c         C   sT   y | j    Wn3 t k
 rC t j t j   d  } | d d SXt d   d  S(   Ni   is3   f.raiseException() didn't raise ZeroDivisionError!?(   t   raiseExceptiont   ZeroDivisionErrort	   tracebackt
   extract_tbt   syst   exc_infot	   Exception(   R   R   t   tb(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   _getInnermostFrameLine   s    c         C   s,   t    } |  j |  } |  j | d  d  S(   Ns   1/0(   R   RA   R   (   R   R   t	   innerline(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   testRaiseExceptionWithTB   s    	c         C   s6   t    } | j   |  j |  } |  j | d  d  S(   Ns   1/0(   R   R.   RA   R   (   R   R   RB   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   testLackOfTB   s    	
sQ   the traceback is not preserved, exarkun said he'll try to fix this! god knows hows
   bugger offc         C   s'   y |  j   Wn t j   } n X| S(   N(   t   _stringExceptionR   R   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   _getStringFailure   s
    c         C   sQ   |  j    } y | j   Wn$ |  j t j   d |  j  n Xt d   d  S(   Ni    s   Should have raised(   RF   R9   R   R=   R>   RE   t   AssertionError(   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_raiseStringExceptions   s    !t   messages(   raising a string exception is deprecatedc         C   sU   |  j    } t j   } | j d |  | j   j   } |  j | d |  j  d S(   s   
        L{Failure.printTraceback} should write out stack and exception
        information, even for string exceptions.
        t   fileiN(   RF   R   R"   R    R0   R   RE   (   R   R   t   outputt   lines(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_printStringExceptions   s
    i   i   s>   String exceptions aren't supported anymore starting Python 2.6c         C   s   |  j  t j t j  d S(   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(   R   R   t   NoCurrentExceptionErrorR   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   testConstructionFails   s    c         C   s&   t    } |  j | j   | j  d S(   s   
        If the C{Failure} has not been cleaned, then C{getTracebackObject}
        returns the traceback object that captured in its constructor.
        N(   R   R   t   getTracebackObjectR@   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_getTracebackObject   s    	c         C   s[   y d d Wn/ t  k
 r= t j   } t j d t  } n X|  j | j   | j  d S(   sc   
        C{captureVars=True} has no effect on the result of
        C{getTracebackObject}.
        i   i    R&   N(   R:   R   R   R'   R   RP   R@   (   R   t   noVarsFailuret   varsFailure(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt&   test_getTracebackObjectFromCaptureVars   s    c         C   sa   t    } t j | j    } | j   t j | j    } |  j d |  |  j | |  d S(   s   
        If the Failure has been cleaned, then C{getTracebackObject} returns an
        object that looks the same to L{traceback.extract_tb}.
        N(   R   R;   R<   RP   R.   R(   R)   R   (   R   R   t   expectedt   observed(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt    test_getTracebackObjectFromClean  s    	
c         C   sW   t  d t  } t j | j    } | j   t j | j    } |  j | |  d S(   s   
        If the Failure was created with captureVars, then C{getTracebackObject}
        returns an object that looks the same to L{traceback.extract_tb}.
        R&   N(   R   R'   R;   R<   RP   R.   R   (   R   R   RU   RV   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt.   test_getTracebackObjectFromCaptureVarsAndClean  s
    
c         C   s/   t  j t d   } |  j | j   d  d S(   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(   R   R   R?   R   RP   R)   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt'   test_getTracebackObjectWithoutTraceback  s    (   i   i   (   t   __name__t
   __module__R   R   R   R$   R-   R/   R4   R5   R8   RA   RC   RD   t   todoRE   RF   RH   R   t   suppressRM   R=   t   version_infot   skipMsgt   skipRO   RQ   RT   RW   RX   RY   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR	   &   s:   																
									t	   BrokenStrc           B   s   e  Z d  Z d   Z RS(   sb   
    An exception class the instances of which cannot be presented as strings via
    C{str}.
    c         C   s
   |   d  S(   N(    (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   __str__/  s    (   RZ   R[   t   __doc__Rb   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRa   *  s   t   BrokenExceptionMetaclassc           B   s   e  Z d  Z d   Z RS(   sa   
    A metaclass for an exception type which cannot be presented as a string via
    C{str}.
    c         C   s   t  d   d  S(   Ns#   You cannot make a string out of me.(   R   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRb   :  s    (   RZ   R[   Rc   Rb   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRd   5  s   t   BrokenExceptionTypec           B   s   e  Z d  Z e Z RS(   sa   
    The aforementioned exception type which cnanot be presented as a string via
    C{str}.
    (   RZ   R[   Rc   Rd   t   __metaclass__(    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRe   ?  s   t   GetTracebackTestsc           B   sV   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   s,   
    Tests for L{Failure.getTraceback}.
    c         C   s>   t    } t j |  } | j d |  } |  j | t  d S(   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.
        t   detailN(   Ra   R   R   t   getTracebackt   assertIsInstancet   str(   R   Rh   t   xR   R;   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   _brokenValueTestL  s    	c         C   s   |  j  d  d S(   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.
        t   briefN(   Rm   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenValueBriefDetailX  s    c         C   s   |  j  d  d S(   sY   
        Like test_brokenValueBriefDetail, but for the C{"default"} detail case.
        t   defaultN(   Rm   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenValueDefaultDetaila  s    c         C   s   |  j  d  d S(   sY   
        Like test_brokenValueBriefDetail, but for the C{"default"} detail case.
        t   verboseN(   Rm   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenValueVerboseDetailh  s    c         C   s8   t  j t    } | j d |  } |  j | t  d S(   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.
        Rh   N(   R   R   Re   Ri   Rj   Rk   (   R   Rh   R   R;   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   _brokenTypeTesto  s    c         C   s   |  j  d  d S(   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   N(   Rt   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenTypeBriefDetailz  s    c         C   s   |  j  d  d S(   sX   
        Like test_brokenTypeBriefDetail, but for the C{"default"} detail case.
        Rp   N(   Rt   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenTypeDefaultDetail  s    c         C   s   |  j  d  d S(   sX   
        Like test_brokenTypeBriefDetail, but for the C{"verbose"} detail case.
        Rr   N(   Rt   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_brokenTypeVerboseDetail  s    (   RZ   R[   Rc   Rm   Ro   Rq   Rs   Rt   Ru   Rv   Rw   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRg   H  s   							
	t   FindFailureTestsc           B   sY   e  Z d  Z d   Z d   Z d   Z d   Z d   Z e d k rW d Z
 e
 e _ n  RS(   sE   
    Tests for functionality related to L{Failure._findFailure}.
    c         C   s@   y d d Wn  |  j  t j j   d  n X|  j d  d S(   s   
        Within an exception handler, _findFailure should return
        C{None} in case no Failure is associated with the current
        exception.
        i   i    s   No exception raised from 1/0!?N(   R   R   R   t   _findFailureR)   t   fail(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt$   test_findNoFailureInExceptionHandler  s
    c         C   s7   |  j  t j   d d  |  j  t j j   d  d S(   sS   
        Outside of an exception handler, _findFailure should return None.
        iN(   R   R=   R>   R)   R   R   Ry   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_findNoFailure  s    c         C   sU   t    } | j   y | j   Wn  |  j t j j   |  n X|  j d  d S(   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   R.   R9   R   R   R   Ry   Rz   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_findFailure  s    	
c         C   sd   t    } | j   y | j   Wn/ t j   } |  j | j   | j    n X|  j d  d S(   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   R.   R9   R   R   R   Ri   Rz   (   R   R   t   newF(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt,   test_failureConstructionFindsOriginalFailure  s    	
 c         C   s[   y t  j   Wn9 t  j k
 rI t j   } |  j | j t  j   n X|  j d  d S(   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(   R   R9   t   RaiserExceptionR   R   R   t   checkRz   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt/   test_failureConstructionWithMungedStackSucceeds  s    s   raiser extension not availableN(   RZ   R[   Rc   R{   R|   R}   R   R   R   R)   R_   R`   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyRx     s   					t   TestFormattableTracebackc           B   s    e  Z d  Z d   Z d   Z RS(   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.
    c         C   sA   t  j d d d i  i  g g  } |  j t j |  d g  d S(   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.
        t   methods   filename.pyi{   N(   s   filename.pyi{   s   methodN(   R   t
   _TracebackR   R;   R<   R)   (   R   R@   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_singleFrame  s    !c         C   sV   t  j d d d i  i  g d d d i  i  g g  } |  j t j |  d d g  d S(	   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.
        t   method1s   filename.pyi{   t   method2i   N(   s   filename.pyi{   R   N(   s   filename.pyi   R   N(   R   R   R   R;   R<   R)   (   R   R@   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_manyFrames  s    (   RZ   R[   Rc   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR     s   	t   TestFrameAttributesc           B   s   e  Z d  Z d   Z RS(   sq   
    _Frame objects should possess some basic attributes that qualify them as
    fake python Frame objects.
    c         C   sR   t  j d d  } |  j | j t  |  j | j t  |  j | j t  j  d S(   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(   R   t   _FrameRj   t	   f_globalst   dictt   f_localst   f_codet   _Code(   R   t   frame(    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_fakeFrameAttributes  s    (   RZ   R[   Rc   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR     s   t   TestDebugModec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sF   
    Failure's debug mode should allow jumping into the debugger.
    c            s^   t  j  t j j d      f d   } |  j |  g  |  _ |  j j t  _ t j   d S(   sK   
        Override pdb.post_mortem so we can make sure it's called.
        t   __init__c              s     t  _  t j j d <d  S(   NR   (   t   pdbt   post_mortemR   R   t   __dict__(    (   R   t   origInit(    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   restore*  s    	N(	   R   R   R   R   R   t
   addCleanupt   resultt   appendt   startDebugMode(   R   R   (    (   R   R   s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   setUp#  s    		c         C   sd   y d d Wn( t  j   \ } } } t j   } n X|  j |  j | g  |  j | j t  d S(   s~   
        If startDebugMode() is called, calling Failure() will first call
        pdb.post_mortem with the traceback.
        i   i    N(   R=   R>   R   R   R   R   R&   t   False(   R   t   typt   excR@   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_regularFailure4  s    c         C   sj   y d d Wn. t  j   \ } } } t j d t  } n X|  j |  j | g  |  j | j t  d S(   sk   
        If startDebugMode() is called, passing captureVars to Failure() will
        not blow up.
        i   i    R&   N(   R=   R>   R   R   R'   R   R   R&   (   R   R   R   R@   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   test_captureVarsB  s    (   RZ   R[   Rc   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyR     s   		i   i   (   t   TwoPointFiveFailureTests(    Rc   R*   R=   R   R;   R   t   twisted.trialR    R   t   twisted.pythonR   t   twisted.testR   t   ImportErrorR)   R   t   TestCaseR	   R?   Ra   R   Rd   t   objectRe   Rg   Rx   R   R   R   R^   t$   twisted.test.generator_failure_testsR   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/test/test_failure.pyt   <module>   s0   
	 
	KR&3