o
    >hÊ                     @  s  d Z ddlmZ ddl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
 ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ G dd deZddd6ddZG dd deZ G dd deZ!G dd de"Z#G dd dee#dZ$G d d! d!eZ%G d"d# d#eZ&d7d,d-Z'G d.d/ d/eZ(G d0d1 d1eZ)G d2d3 d3eZ*G d4d5 d5eZ+dS )8z6
Test cases for the L{twisted.python.failure} module.
    )annotationsN)distb)StringIOFrameSummary)TracebackType)Any	Generatorcast)skipIf)raiser)failurereflect)SynchronousTestCasec                   @  s   e Zd ZdZd	ddZdS )
ComparableExceptionz+An exception that can be compared by value.otherobjectreturnboolc                 C  s   | j |j ko| jtt|jkS N)	__class__argsr
   r   )selfr    r   y/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/test/test_failure.py__eq__   s   zComparableException.__eq__N)r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   FcaptureVarsr!   r   r   failure.Failurec                 C  s<   | rd}| zdd  W |S  t y   tj| d}Y |S w )z6
    Make a C{Failure} of a divide-by-zero error.
    xyz   r   r    )BaseExceptionr   Failure)r!   exampleLocalVarfr   r   r   getDivisionFailure%   s   
r)   c                   @  s  e Zd ZdZd^ddZd^ddZd^dd	Zd^d
dZd_ddZd`ddZ	daddZ
	dbdcddZdddeddZdddedd Zd^d!d"Zd^d#d$Zd^d%d&Zd^d'd(Zd^d)d*Zd^d+d,Zd^d-d.Zd^d/d0Zd^d1d2Zdfd6d7Zd^d8d9Zd^d:d;Zd^d<d=Zd^d>d?Zd^d@dAZd^dBdCZd^dDdEZd^dFdGZd^dHdIZ d^dJdKZ!d^dLdMZ"d^dNdOZ#d^dPdQZ$d^dRdSZ%dgdUdVZ&d^dWdXZ'd^dYdZZ(d^d[d\Z)d]S )hFailureTestsz'
    Tests for L{failure.Failure}.
    r   Nonec                 C  sN   zt d ty   t }Y nw |tt}| |t | |jt  dS )z(
        Trapping a L{Failure}.
        testN)	NotImplementedErrorr%   r   r&   trap
SystemExitRuntimeErrorassertEqualtype)r   r(   errorr   r   r   test_failAndTrap;   s   zFailureTests.test_failAndTrapc                 C  sF   t  }z| ty   t }Y nw | t |jt}| || dS )z
        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)
ValueErrorr%   r   r&   assertRaisesr.   OverflowErrorassertIs)r   	exceptionr(   	untrappedr   r   r   test_trapRaisesWrappedExceptionG   s   z,FailureTests.test_trapRaisesWrappedExceptionc                 C  s,   t  }t|}t|}| |j| dS )z
        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)r5   r   r&   r8   valuer   r9   f1f2r   r   r   test_failureValueFromFailureV   s   

z)FailureTests.test_failureValueFromFailurec                 C  sL   t  }t|}z|t W n ty   t }Y nw | |j| dS )z
        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)r5   r   r&   r.   r7   r%   r8   r<   r=   r   r   r   !test_failureValueFromFoundFailurea   s   
z.FailureTests.test_failureValueFromFoundFailuresstrprefixc                 C      |  |||d| dS )z
        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}
        z is not the start of N)
assertTrue
startswith)r   rB   rD   r   r   r   assertStartsWithq       	zFailureTests.assertStartsWithsuffixc                 C  rE   )z
        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}
        z is not the end of N)rF   endswith)r   rB   rJ   r   r   r   assertEndsWith|   rI   zFailureTests.assertEndsWithtbc                 C  s   |  || | || dS )ac  
        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)rH   rL   )r   rM   rD   rJ   r   r   r   assertTracebackFormat   s   z"FailureTests.assertTracebackFormatFr!   r   cleanFailurec           
      C  s   t |d}t }|r|  || | }d|j|jrdp df }dt	|j
t|j|j}| ||| dd | D }|rZ| g | |rPd}	nd	}	| |	| dS | g | | d
| dS )aV  
        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/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}
        r    z*--- Failure #%d%s---
z (pickled)  z#{}: {}
*--- End of Failure #{} ---
c                 S  s   g | ]	}| d r|qS )z  )rG   ).0liner   r   r   
<listcomp>   s    z8FailureTests.assertDetailedTraceback.<locals>.<listcomp>z  exampleLocalVar : "'xyz'"z  exampleLocalVar : 'xyz'zA [Capture of Locals and Globals disabled (use captureVars=True)]
N)r)   r   rO   printDetailedTracebackgetvaluecountpickledformatr   qualr2   safe_strr<   rN   
splitlinesassertNotEqualassertInr1   )
r   r!   rO   r(   outrM   startendlinesWithVarsrR   r   r   r   assertDetailedTraceback   s8   
)


z$FailureTests.assertDetailedTracebackc                 C  s   |rd}| t  }t }|| | }d}|jD ]\}}}	}
}|| d|	 d| d7 }qtt}| |d| d|  |rL| t	
d| dS dS )	a  
        Assert that L{printBriefTraceback} produces and prints a brief
        traceback.

        The brief traceback consists of a header::

          Traceback: <type 'exceptions.ZeroDivisionError'>: float division

        And the footer::

          /twisted/test/test_failure.py:39:getDivisionFailure

        @param captureVars: Enables L{Failure.captureVars}.
        @type captureVars: C{bool}
        abcde :
zTraceback: z: zexampleLocalVar.*abcdeN)r)   r   printBriefTracebackrU   framesreprZeroDivisionErrorrN   assertIsNoneresearch)r   r!   r'   r(   r^   rM   stackmethodfilenamelineno	localVars
globalVarszder   r   r   assertBriefTraceback   s&   

z!FailureTests.assertBriefTracebackc              
   C  s   |rd}| t |d}t }|| | }d}|jD ]#\}}}	}
}|d| d|	 d| d7 }|dt||	  d7 }q| |d	d
|t	
|jt	|jf  |ra| td| dS dS )a  
        Assert that L{printTraceback} produces and prints a default traceback.

        The default traceback consists of a header::

          Traceback (most recent call last):

        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}
        xyzzyr    rd   z  File "z", line z, in rf   z    z"Traceback (most recent call last):z	%s%s: %s
zexampleLocalVar.*xyzzyN)r)   r   printTracebackrU   rh   	linecachegetlinestriprN   r   rY   r2   rZ   r<   rk   rl   rm   )r   r!   r'   r(   r^   rM   rn   ro   rp   rq   rr   rs   r   r   r   assertDefaultTraceback
  s0   



z#FailureTests.assertDefaultTracebackc                 C     |    dS )zr
        L{printDetailedTraceback} returns a detailed traceback including the
        L{Failure}'s count.
        Nrb   r   r   r   r   test_printDetailedTraceback8  s   z(FailureTests.test_printDetailedTracebackc                 C  r|   )zC
        L{printBriefTraceback} returns a brief traceback.
        Nru   r~   r   r   r   test_printBriefTraceback?     z%FailureTests.test_printBriefTracebackc                 C  r|   )z8
        L{printTraceback} returns a traceback.
        Nr{   r~   r   r   r   test_printTracebackE  r   z FailureTests.test_printTracebackc                 C     | j dd dS )z
        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}.
        Tr    Nr}   r~   r   r   r   'test_printDetailedTracebackCapturedVarsK  s   z4FailureTests.test_printDetailedTracebackCapturedVarsc                 C  r   )z
        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.
        Tr    Nr   r~   r   r   r   $test_printBriefTracebackCapturedVarsS     z1FailureTests.test_printBriefTracebackCapturedVarsc                 C  r   )z
        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.
        Tr    Nr   r~   r   r   r   test_printTracebackCapturedVars]  r   z,FailureTests.test_printTracebackCapturedVarsc                 C  s   | j ddd dS )z
        C{printDetailedTraceback} includes information about local variables on
        the stack after C{cleanFailure} has been called.
        T)r!   rO   Nr}   r~   r   r   r   .test_printDetailedTracebackCapturedVarsCleanedg  s   z;FailureTests.test_printDetailedTracebackCapturedVarsCleanedc                 C  s   | j ttjdddd dS )zu
        L{failure.format_frames} raises a L{ValueError} if the supplied
        C{detail} level is unknown.
        Nnoisiadetail)r6   r5   r   format_framesr~   r   r   r   test_invalidFormatFramesDetailn  s   
z+FailureTests.test_invalidFormatFramesDetailc                 C  s,   t  }t|}|t  | |j| d S r   )r0   r   r&   r.   r1   r<   )r   er(   r   r   r   test_ExplictPassw  s   

zFailureTests.test_ExplictPassr(   r"   
str | Nonec                 C  sB   z	|   W td ty    tt d }|d j Y S w )N   z3f.raiseException() didn't raise ZeroDivisionError!?)raiseExceptionrj   	traceback
extract_tbsysexc_inforR   	Exception)r   r(   rM   r   r   r   _getInnermostFrameLine}  s   
z#FailureTests._getInnermostFrameLinec                 C  s    t  }| |}| |d d S )N1 / 0)r)   r   r1   )r   r(   	innerliner   r   r   test_RaiseExceptionWithTB  s   
z&FailureTests.test_RaiseExceptionWithTBc                 C  s   |  tjtj dS )z
        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)r6   r   NoCurrentExceptionErrorr&   r~   r   r   r   test_ConstructionFails  s   z#FailureTests.test_ConstructionFailsc                 C  s   t  }| | |j dS )z
        If the C{Failure} has not been cleaned, then C{getTracebackObject}
        returns the traceback object that captured in its constructor.
        N)r)   r1   getTracebackObjectrM   r   r(   r   r   r   test_getTracebackObject  s   z$FailureTests.test_getTracebackObjectc                 C  sJ   zdd  W n t y   t }tjdd}Y nw | | |j dS )zc
        C{captureVars=True} has no effect on the result of
        C{getTracebackObject}.
        r$   r   Tr    N)rj   r   r&   r1   r   rM   )r   noVarsFailurevarsFailurer   r   r   &test_getTracebackObjectFromCaptureVars  s   z3FailureTests.test_getTracebackObjectFromCaptureVarsc                 C  sD   t  }t| }|  t| }| | | || dS )z
        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   r   rO   assertIsNotNoner1   r   r(   expectedobservedr   r   r    test_getTracebackObjectFromClean  s   
z-FailureTests.test_getTracebackObjectFromCleanc                 C  s>   t dd}t| }|  t| }| || dS )z
        If the Failure was created with captureVars, then C{getTracebackObject}
        returns an object that looks the same to L{traceback.extract_tb}.
        Tr    N)r)   r   r   r   rO   r1   r   r   r   r   .test_getTracebackObjectFromCaptureVarsAndClean  s
   
z;FailureTests.test_getTracebackObjectFromCaptureVarsAndCleanc                 C  s    t td}| |  dS )a  
        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) -> [].
        z
some errorN)r   r&   r   rk   r   r   r   r   r   'test_getTracebackObjectWithoutTraceback  s   z4FailureTests.test_getTracebackObjectWithoutTracebackc                 C  sJ   zdd  W n t y   t \}}}Y nw t|}| |j| dS )z
        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.
        r$   r   N)r%   r   r   r   r&   r8   rM   )r   klassr9   rM   r(   r   r   r   $test_tracebackFromExceptionInPython3  s   
z1FailureTests.test_tracebackFromExceptionInPython3c                 C  s>   t  }| |j | |jj|j |  | |jj dS )z
        L{failure.Failure.cleanFailure} sets the C{__traceback__} attribute of
        the exception to L{None} in Python 3.
        N)r)   r   rM   r8   r<   __traceback__rO   rk   r   r   r   r   *test_cleanFailureRemovesTracebackInPython3  s
   z7FailureTests.test_cleanFailureRemovesTracebackInPython3c                 C  s0   t  }t }t| |d | d|  dS )z
        The traceback captured by a L{Failure} is compatible with the stdlib
        L{dis.distb} function as used in post-mortem debuggers. Specifically,
        it doesn't cause that function to raise an exception.
        )filez --> N)r)   r   r   r   r]   rU   )r   r(   bufr   r   r   
test_distb  s   zFailureTests.test_distbc                 C  s*   t  }tt}| t|d|f  dS )z
        The C{repr} of a L{failure.Failure} shows the type and string
        representation of the underlying exception.
        z5<twisted.python.failure.Failure %s: division by zero>N)r)   r   fullyQualifiedNamerj   r1   ri   )r   r(   typeNamer   r   r   	test_repr  s   
zFailureTests.test_reprc                 C  sN   t  }|j|_|  }| t|dk |dd D ]
}| d|d  qdS )zL
        C{Failure.stack} is gettable and settable, but depreacted.
        r$   NzFtwisted.python.failure.Failure.stack was deprecated in Twisted 24.10.0message)r)   rn   flushWarningsrF   lenr1   )r   r(   warningswr   r   r   test_stackDeprecation  s   z"FailureTests.test_stackDeprecationc                 C  sh   t d}t|j }tj|}| |tj |j }| |dd |d | || dS )ze
        C{Failure._withoutTraceback(exc)} gives the same result as
        C{Failure(exc)}.
        hellorV   r$   N)	rj   r   r&   __dict__copy_withoutTracebackassertIsInstancer1   pop)r   excdict1failure2dict2r   r   r   test_failureWithoutTraceback  s   
z)FailureTests.test_failureWithoutTracebackc                 C  sd   t d}t|}| | tj|}| | zt d ty*   t }Y nw | | dS )zm
        C{Failure(exc)} and C{Failure._withoutTraceback(exc)} can be pickled
        and unpickled.
        r   booN)r   r   r&   assertPicklingRoundtripsr   r%   )r   r   failure1r   failure3r   r   r   test_failurePickling  s   


z!FailureTests.test_failurePicklingoriginal_failurec                 C  sT   t t |}|j }d|d< d|d< |j }| || | |j|j dS )z{
        The failure can be pickled and unpickled, and the C{parents} attribute
        is included in the pickle.
        r$   rW   NrM   )pickleloadsdumpsr   r   r1   rh   )r   r   r   r   resultr   r   r   r   /  s   

z%FailureTests.assertPicklingRoundtripsc                 C  s(   t td}| | d |j dS )z?
        C{Failure.parents} is included in the pickle.
        r   parentsN)r   r&   r   r1   __getstate__r   r   r   r   r   #test_failurePicklingIncludesParents<  s   z0FailureTests.test_failurePicklingIncludesParentsc                 C  sX   t t }|jdd |_tt|}|jdd |_| |j|jdd  dS )zO
        C{Failure.frames} can be set, both before and after pickling.
        Nr   )r   r&   r)   rh   r   r   r   r1   r   r   r   r   r   r   test_settableFramesC  s
   z FailureTests.test_settableFramesc                 C  sB   t td}|jdd |_tt|}|jdd |_dS )zs
        C{Failure.parents} can be set, both before and after pickling.

        This is used by Foolscap.
        r   N)r   r&   r   r   r   r   r   r   r   r   r   test_settableParentsM  s   z!FailureTests.test_settableParentsNr   r+   )rB   rC   rD   rC   r   r+   )rB   rC   rJ   rC   r   r+   )rM   rC   rD   rC   rJ   rC   r   r+   )FF)r!   r   rO   r   r   r+   )F)r!   r   r   r+   )r(   r"   r   r   )r   r"   r   r+   )*r   r   r   r   r4   r;   r@   rA   rH   rL   rN   rb   ru   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   6   sR    






L(
.









	

	
















r*   c                   @     e Zd ZdZdddZdS )	BrokenStrzb
    An exception class the instances of which cannot be presented as strings
    via L{str}.
    r   rC   c                 C  s   | r   r   r~   r   r   r   __str___  s   zBrokenStr.__str__Nr   rC   r   r   r   r   r   r   r   r   r   r   Y      r   c                   @  r   )BrokenExceptionMetaclassza
    A metaclass for an exception type which cannot be presented as a string via
    L{str}.
    r   rC   c                 C  s   t d)Nz#You cannot make a string out of me.)r5   r~   r   r   r   r   j  s   z BrokenExceptionMetaclass.__str__Nr   r   r   r   r   r   r   d  r   r   c                   @  s   e Zd ZdZdS )BrokenExceptionTypeza
    The aforementioned exception type which cannot be presented as a string via
    L{str}.
    N)r   r   r   r   r   r   r   r   r   n  s    r   )	metaclassc                   @  s`   e Zd ZdZdddZddd	Zdd
dZdddZdddZdddZ	dddZ
dddZdS )GetTracebackTestsz,
    Tests for L{Failure.getTraceback}.
    r   rC   r   r+   c                 C  s,   t  }t|}|j|d}| |t dS )z
        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.
        r   N)r   r   r&   getTracebackr   rC   )r   r   xr(   r   r   r   r   _brokenValueTest{  s   
z"GetTracebackTests._brokenValueTestc                 C     |  d dS )z
        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.
        briefNr   r~   r   r   r   test_brokenValueBriefDetail  s   z-GetTracebackTests.test_brokenValueBriefDetailc                 C  r   )Y
        Like test_brokenValueBriefDetail, but for the C{"default"} detail case.
        defaultNr   r~   r   r   r   test_brokenValueDefaultDetail     z/GetTracebackTests.test_brokenValueDefaultDetailc                 C  r   )r   verboseNr   r~   r   r   r   test_brokenValueVerboseDetail  r   z/GetTracebackTests.test_brokenValueVerboseDetailc                 C  s(   t t }|j|d}| |t dS )z
        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.
        r   N)r   r&   r   r   r   rC   )r   r   r(   r   r   r   r   _brokenTypeTest  s   z!GetTracebackTests._brokenTypeTestc                 C  r   )aQ  
                A L{Failure} might wrap an
                newPublisher(evt)
        xception 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.
        r   Nr   r~   r   r   r   test_brokenTypeBriefDetail  s   	z,GetTracebackTests.test_brokenTypeBriefDetailc                 C  r   )zX
        Like test_brokenTypeBriefDetail, but for the C{"default"} detail case.
        r   Nr   r~   r   r   r   test_brokenTypeDefaultDetail  r   z.GetTracebackTests.test_brokenTypeDefaultDetailc                 C  r   )zX
        Like test_brokenTypeBriefDetail, but for the C{"verbose"} detail case.
        r   Nr   r~   r   r   r   test_brokenTypeVerboseDetail  r   z.GetTracebackTests.test_brokenTypeVerboseDetailN)r   rC   r   r+   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   v  s    







r   c                   @  s(   e Zd ZdZeedu ddddZdS )	FindFailureTestszH
    Tests for functionality related to identifying the C{Failure}.
    Nzraiser extension not availabler   r+   c                 C  sL   zt   W n t jy   t }| |t j Y dS w | d dS )z
        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.
        z$No exception raised from extension?!N)r   r   RaiserExceptionr   r&   rF   checkfailr   r   r   r   /test_failureConstructionWithMungedStackSucceeds  s   z@FindFailureTests.test_failureConstructionWithMungedStackSucceedsr   )r   r   r   r   r   r   r   r   r   r   r   r     s    r   fnrC   rq   intnametextr+   r   c                 C  s   t | ||S r   r   )r   rq   r   r   r   r   r   _tb  s   r   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
FormattableTracebackTestsa  
    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.
    r   r+   c                 C  s8   t dddi i gg}| t|tddddg dS )z
        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.
        ro   filename.py{   N)r   
_Tracebackr1   r   r   r   r   rM   r   r   r   test_singleFrame  s   z*FormattableTracebackTests.test_singleFramec              	   C  sp   t dddi i gdddi i gg}| t|tddddtddddg | t|jtddddg dS )z
        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.
        method1r  r  method2   N)r   r  r1   r   r   r   extract_stacktb_framer  r   r   r   test_manyFrames  s    	
z)FormattableTracebackTests.test_manyFramesNr   )r   r   r   r   r  r  r   r   r   r   r    s    
r  c                   @  .   e Zd ZdZdddZdddZddd	Zd
S )FakeAttributesTestsaR  
    _Frame, _Code and _TracebackFrame objects should possess some basic
    attributes that qualify them as fake python objects, allowing the return of
    _Traceback to be used as a fake traceback. The attributes that have zero or
    empty values are there so that things expecting them find them (e.g. post
    mortem debuggers).
    r   r+   c                 C  s   t dd}ddi}ddi}t ddd||f|}| |j| | |j| | |jt j | |j| | |j	t
 | |jt | |jd | |jtd dS )	z
        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.
        )dummyparentdummyparentfileo   NNN	local_var*   
global_vard   	dummynamedummyfilename)r   _Framer1   	f_globalsf_localsr   f_code_Codef_back
f_builtinsdictf_lastir   f_linenof_tracer2   )r   
back_framefake_localsfake_globalsframer   r   r   test_fakeFrameAttributes  s.   

z,FakeAttributesTests.test_fakeFrameAttributesc                 C  s   t dd}| |jd | |jd | |jt | |jt	 | |j
t | |jt | |jt | |jt | |jt	 | |jt | |jt | |jt | |jt | |jt | |jt | |jt | | t dS )N
        See L{FakeAttributesTests} for more details about this test.
        r  r  N)r   r  r1   co_nameco_filenamer   co_argcountr   co_codebytesco_cellvarstuple	co_constsco_firstlinenoco_flags	co_lnotabco_freevarsco_posonlyargcountco_kwonlyargcountco_names
co_nlocalsco_stacksizeco_varnameslistco_positions)r   coder   r   r   test_fakeCodeAttributes;  s$   z+FakeAttributesTests.test_fakeCodeAttributesc                 C  s^   t dddi i fd}t |}| |j| | |jd | |jt | 	t
|d dS )r(  r  r  r  Ntb_next)r   r  _TracebackFramer1   r  	tb_linenor   tb_lastir   rF   hasattr)r   r&  traceback_framer   r   r   test_fakeTracebackFrameR  s   
z+FakeAttributesTests.test_fakeTracebackFrameNr   )r   r   r   r   r'  r>  rE  r   r   r   r   r    s
    

%r  c                   @  r  )DebugModeTestszF
    Failure's debug mode should allow jumping into the debugger.
    r   r+   c                   sP   t jtjj d
 fdd}| g _ddfdd	}|t _t  dS )zK
        Override pdb.post_mortem so we can make sure it's called.
        r   r+   c                     s   t _ tj_d S r   )pdbpost_mortemr   r&   __init__r   )origInitrH  r   r   restoren  s   z%DebugModeTests.setUp.<locals>.restoreNtTracebackType | Nonec                   s    j |  d S r   )r   append)rL  r~   r   r   logging_post_mortemv  s   z1DebugModeTests.setUp.<locals>.logging_post_mortemr   r   )rL  rM  r   r+   )rG  rH  r   r&   rI  
addCleanupr   startDebugMode)r   rK  rO  r   )rJ  rH  r   r   setUpf  s   
zDebugModeTests.setUpc                 C  sV   zdd  W n t y   t \}}}t }Y nw | | j|g | |j dS )z~
        If startDebugMode() is called, calling Failure() will first call
        pdb.post_mortem with the traceback.
        r$   r   N)	r%   r   r   r   r&   r1   r   assertFalser!   r   typr   rM   r(   r   r   r   test_regularFailure|  s   z"DebugModeTests.test_regularFailurec                 C  sZ   zdd  W n t y   t \}}}tjdd}Y nw | | j|g | |j dS )zk
        If startDebugMode() is called, passing captureVars to Failure() will
        not blow up.
        r$   r   Tr    N)	r%   r   r   r   r&   r1   r   rF   r!   rT  r   r   r   test_captureVars  s   zDebugModeTests.test_captureVarsNr   )r   r   r   r   rR  rV  rW  r   r   r   r   rF  a  s
    

rF  c                   @  sB   e Zd ZdZddd	Zdd
dZdddZdddZdddZdS )ExtendedGeneratorTestszU
    Tests C{failure.Failure} support for generator features added in Python 2.5
    r(   r"   gGenerator[Any, Any, Any]r   r+   c                 C  s2   z| | W n
 ty   Y d S w | d d S )Nz<throwExceptionIntoGenerator should have raised StopIteration)throwExceptionIntoGeneratorStopIterationr   )r   r(   rY  r   r   r   _throwIntoGenerator  s   z*ExtendedGeneratorTests._throwIntoGeneratorc                   s   g d fdd}| }t  }t|  ||  d d t  d d t  td d d d d	 d
S )zr
        It should be possible to throw the exception that a Failure
        represents into a generator.
        r   Generator[None, None, None]c                   3  s>    zd V  W n t y   t  Y d S w  d d S )Nz$Yield should have yielded exception.)r%   rN  r   r   r   r   r   stuffr   r   	generator     
zJExtendedGeneratorTests.test_throwExceptionIntoGenerator.<locals>.generatorr   r$   r   r   r   Nr   r^  )r)   nextr]  r1   rj   r   r   r   r   ra  rY  r(   r   r_  r    test_throwExceptionIntoGenerator  s   &z7ExtendedGeneratorTests.test_throwExceptionIntoGeneratorc                   s   t  }|  | }g  d	 fdd}| }t| || t d ||   d  |  d d   	d|  dS )
z
        When a Failure is constructed in the context of an exception
        handler that is handling an exception raised by
        throwExceptionIntoGenerator, the new Failure should be chained to that
        original Failure.
        r   r^  c                   3  s>    zd V  W n t y    t  Y d S w d d S )NzNo exception sent to generator)r%   rN  r   r&   r   r   newFailuresr   r   r   ra    rb  zVExtendedGeneratorTests.test_failureConstructionFindsOriginalFailure.<locals>.generatorr$   r   ra  Nrc  )
r)   rO   r   rd  r]  r1   r   r\   r]   assertNotIn)r   r(   original_failure_strra  rY  r   rg  r   ,test_failureConstructionFindsOriginalFailure  s   zCExtendedGeneratorTests.test_failureConstructionFindsOriginalFailurec                   s2   d fdd}| }t | t } || dS )z
        When a generator reraises a different exception, creating a L{Failure}
        inside the generator should find the reraised exception rather than
        original one.
        r   r^  c                   3  sX    zzd V  W W d S  t y   g d  Y W d S w  t y+    t jt Y d S w Nr$   )r%   r   r   r&   r<   
IndexErrorr   r~   r   r   ra    s   zJExtendedGeneratorTests.test_ambiguousFailureInGenerator.<locals>.generatorNrc  )rd  r)   r]  re  r   r~   r    test_ambiguousFailureInGenerator  s
   	z7ExtendedGeneratorTests.test_ambiguousFailureInGeneratorc                 C  sX   ddd}| }t | t }z	| || W dS  ty+   | t jt Y dS w )z
        When a generator reraises a different exception, creating a L{Failure}
        above the generator should find the reraised exception rather than
        original one.
        r   r^  c                   s  s,    zd V  W d S  t y   g d  Y d S w rl  )r%   r   r   r   r   ra    s   zLExtendedGeneratorTests.test_ambiguousFailureFromGenerator.<locals>.generatorNrc  )	rd  r)   r]  r%   r   r   r&   r<   rm  re  r   r   r   "test_ambiguousFailureFromGenerator  s   
z9ExtendedGeneratorTests.test_ambiguousFailureFromGeneratorN)r(   r"   rY  rZ  r   r+   r   )	r   r   r   r   r]  rf  rk  rn  ro  r   r   r   r   rX    s    




$rX  )r!   r   r   r"   )
r   rC   rq   r   r   rC   r   r+   r   r   ),r   
__future__r   rx   rG  r   rl   r   r   disr   ior   r   typesr   typingr   r	   r
   unittestr   cython_test_exception_raiserr   twisted.pythonr   r   twisted.trial.unittestr   r   r   r)   r*   r   r2   r   r   r   r   r   r  r  rF  rX  r   r   r   r   <module>   sD   	    '
F
4T6