U
    n5{iÃŠ  ã                   @  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œdddœd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&d#d$d#d%d&d'œd(d)„Z'G d*d+„ d+eƒZ(G d,d-„ d-eƒZ)G d.d/„ d/eƒZ*G d0d1„ d1eƒZ+dS )2z6
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œdd„ZdS )ÚComparableExceptionz+An exception that can be compared by value.ÚobjectÚbool)ÚotherÚreturnc                 C  s   | j |j ko| jtt|ƒjkS ©N)Ú	__class__Úargsr
   r   )Úselfr   © r   ú=/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/test/test_failure.pyÚ__eq__   s    ÿzComparableException.__eq__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   F©ÚcaptureVarsr   úfailure.Failure©r!   r   c                 C  s>   | rd}| zdd  W n  t k
r8   tj| d�}Y nX |S )z6
    Make a C{Failure} of a divide-by-zero error.
    Zxyzé   r   r    )ÚBaseExceptionr   ÚFailure)r!   ÚexampleLocalVarÚfr   r   r   ÚgetDivisionFailure%   s    r)   c                   @  sD  e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddddœdd„Zddddœdd„Z	dddddœdd„Z
d]ddddœdd„Zd^dddœdd„Zd_dddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zddœd(d)„Zddœd*d+„Zddœd,d-„Zddœd.d/„Zddœd0d1„Zd2d3d4œd5d6„Zddœd7d8„Zddœd9d:„Zddœd;d<„Zddœd=d>„Zddœd?d@„ZddœdAdB„ZddœdCdD„ZddœdEdF„ZddœdGdH„Z ddœdIdJ„Z!ddœdKdL„Z"ddœdMdN„Z#ddœdOdP„Z$ddœdQdR„Z%d2ddSœdTdU„Z&ddœdVdW„Z'ddœdXdY„Z(ddœdZd[„Z)d\S )`ÚFailureTestsz'
    Tests for L{failure.Failure}.
    ÚNone©r   c                 C  sT   zt dƒ‚W n tk
r(   t ¡ }Y nX | 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  sL   t ƒ }z|‚W n tk
r*   t ¡ }Y nX |  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(   Z	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)r6   r   r&   r9   Úvalue©r   r:   Úf1Úf2r   r   r   Útest_failureValueFromFailureV   s    

z)FailureTests.test_failureValueFromFailurec                 C  sN   t ƒ }t |¡}z| t¡ W n tk
r:   t ¡ }Y nX |  |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)r6   r   r&   r/   r8   r%   r9   r<   r=   r   r   r   Ú!test_failureValueFromFoundFailurea   s    
z.FailureTests.test_failureValueFromFoundFailureÚstr)ÚsÚprefixr   c                 C  s    |   | |¡|›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   rC   rD   r   r   r   ÚassertStartsWithq   s    	zFailureTests.assertStartsWith)rC   Úsuffixr   c                 C  s    |   | |¡|›d|›�¡ dS )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)rE   Úendswith)r   rC   rH   r   r   r   ÚassertEndsWith|   s    	zFailureTests.assertEndsWith)ÚtbrD   rH   r   c                 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)rG   rJ   )r   rK   rD   rH   r   r   r   ÚassertTracebackFormat‡   s    z"FailureTests.assertTracebackFormatFr   )r!   ÚcleanFailurer   c           
      C  sÎ   t |d�}tƒ }|r| ¡  | |¡ | ¡ }d|j|jr>dp@df }d t 	|j
¡t |j¡|j¡}|  |||¡ dd„ | ¡ D ƒ}|r²|  g |¡ |r d}	nd	}	|  |	|¡ n|  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  )rF   )Ú.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   rM   ZprintDetailedTracebackÚgetvalueÚcountÚpickledÚformatr   Úqualr3   Úsafe_strr<   rL   Ú
splitlinesÚassertNotEqualÚassertInr2   )
r   r!   rM   r(   ÚoutrK   ÚstartÚendZlinesWithVarsrP   r   r   r   ÚassertDetailedTraceback–   s8    )

þ

ýþz$FailureTests.assertDetailedTracebackr#   c                 C  s˜   |rd}| t ƒ }tƒ }| |¡ | ¡ }d}|jD ](\}}}	}
}||› d|	› d|› d�7 }q4ttƒ}|  |d|› d�|› ¡ |r”|  t	 
d|¡¡ 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}
        ZabcdeÚ ú:Ú
zTraceback: z: zexampleLocalVar.*abcdeN)r)   r   ZprintBriefTracebackrR   ÚframesÚreprÚZeroDivisionErrorrL   ÚassertIsNoneÚreÚsearch)r   r!   r'   r(   r[   rK   ÚstackÚmethodÚfilenameÚlinenoÚ	localVarsÚ
globalVarsZzder   r   r   ÚassertBriefTracebackâ   s$    

ýz!FailureTests.assertBriefTracebackc              
   C  sÂ   |rd}| t |d�}tƒ }| |¡ | ¡ }d}|jD ]F\}}}	}
}|d|› d|	› d|› d�7 }|dt ||	¡ ¡ › d�7 }q8|  |d	d
|t	 
|j¡t	 |j¡f ¡ |r¾|  t d|¡¡ 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}
        Zxyzzyr    r_   z  File "z", line z, in ra   z    z"Traceback (most recent call last):z	%s%s: %s
zexampleLocalVar.*xyzzyN)r)   r   ZprintTracebackrR   rb   Ú	linecacheÚgetlineÚstriprL   r   rV   r3   rW   r<   re   rf   rg   )r   r!   r'   r(   r[   rK   rh   ri   rj   rk   rl   rm   r   r   r   ÚassertDefaultTraceback
  s.    



ýÿýz#FailureTests.assertDefaultTracebackc                 C  s   |   ¡  dS )zr
        L{printDetailedTraceback} returns a detailed traceback including the
        L{Failure}'s count.
        N©r^   ©r   r   r   r   Útest_printDetailedTraceback8  s    z(FailureTests.test_printDetailedTracebackc                 C  s   |   ¡  dS )zC
        L{printBriefTraceback} returns a brief traceback.
        N©rn   rt   r   r   r   Útest_printBriefTraceback?  s    z%FailureTests.test_printBriefTracebackc                 C  s   |   ¡  dS )z8
        L{printTraceback} returns a traceback.
        N©rr   rt   r   r   r   Útest_printTracebackE  s    z FailureTests.test_printTracebackc                 C  s   | 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    Nrs   rt   r   r   r   Ú'test_printDetailedTracebackCapturedVarsK  s    z4FailureTests.test_printDetailedTracebackCapturedVarsc                 C  s   | j dd� dS )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    Nrv   rt   r   r   r   Ú$test_printBriefTracebackCapturedVarsS  s    z1FailureTests.test_printBriefTracebackCapturedVarsc                 C  s   | j dd� dS )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    Nrx   rt   r   r   r   Útest_printTracebackCapturedVars]  s    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!   rM   Nrs   rt   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.
        NZnoisia©Údetail)r7   r6   r   Zformat_framesrt   r   r   r   Útest_invalidFormatFramesDetailn  s        ÿz+FailureTests.test_invalidFormatFramesDetailc                 C  s,   t ƒ }t |¡}| t ¡ |  |j|¡ d S r   )r1   r   r&   r/   r2   r<   )r   Úer(   r   r   r   Útest_ExplictPassw  s    

zFailureTests.test_ExplictPassr"   z
str | None)r(   r   c                 C  sJ   z|  ¡  W n0 tk
r<   t t ¡ d ¡}|d j Y S X tdƒ‚d S )Né   éÿÿÿÿz3f.raiseException() didn't raise ZeroDivisionError!?)ÚraiseExceptionrd   Ú	tracebackÚ
extract_tbÚsysÚexc_inforP   Ú	Exception)r   r(   rK   r   r   r   Ú_getInnermostFrameLine}  s    z#FailureTests._getInnermostFrameLinec                 C  s    t ƒ }|  |¡}|  |d¡ d S )Nú1 / 0)r)   r‹   r2   )r   r(   Z	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)r7   r   ZNoCurrentExceptionErrorr&   rt   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)   r2   ÚgetTracebackObjectrK   ©r   r(   r   r   r   Útest_getTracebackObject“  s    z$FailureTests.test_getTracebackObjectc                 C  sL   zdd  W n( t k
r4   t ¡ }tjdd�}Y nX |  | ¡ |j¡ dS )zc
        C{captureVars=True} has no effect on the result of
        C{getTracebackObject}.
        r$   r   Tr    N)rd   r   r&   r2   r�   rK   )r   ZnoVarsFailureZ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�   rM   ÚassertIsNotNoner2   ©r   r(   ÚexpectedZ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�   rM   r2   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Š   re   r�   r�   r   r   r   Ú'test_getTracebackObjectWithoutTraceback¾  s    z4FailureTests.test_getTracebackObjectWithoutTracebackc                 C  sL   zdd  W n" t k
r.   t ¡ \}}}Y nX 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&   r9   rK   )r   Úklassr:   rK   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“   rK   r9   r<   Ú__traceback__rM   re   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�   rZ   rR   )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   ZfullyQualifiedNamerd   r2   rc   )r   r(   ZtypeNamer   r   r   Ú	test_reprî  s    
þzFailureTests.test_reprc                 C  sN   t ƒ }|j|_|  ¡ }|  t|ƒdk¡ |dd… D ]}|  d|d ¡ q4d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)   rh   ZflushWarningsrE   Úlenr2   )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)}.
        ÚhellorS   r$   N)	rd   r   r&   Ú__dict__ÚcopyÚ_withoutTracebackÚassertIsInstancer2   Úpop)r   ÚexcZdict1Úfailure2Zdict2r   r   r   Útest_failureWithoutTraceback  s    
z)FailureTests.test_failureWithoutTracebackc                 C  sj   t dƒ}t |¡}|  |¡ tj |¡}|  |¡ zt dƒ‚W n tk
rZ   t ¡ }Y nX |  |¡ dS )zm
        C{Failure(exc)} and C{Failure._withoutTraceback(exc)} can be pickled
        and unpickled.
        r§   ZbooN)r   r   r&   ÚassertPicklingRoundtripsrª   r%   )r   r­   Zfailure1r®   Zfailure3r   r   r   Útest_failurePickling  s    


z!FailureTests.test_failurePickling)Úoriginal_failurer   c                 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$   rT   NrK   )ÚpickleÚloadsÚdumpsr¨   r©   r2   rb   )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   r2   Ú__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)   rb   r³   r´   rµ   r2   ©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)FF)F)F)*r   r   r   r   r5   r;   r@   rA   rG   rJ   rL   r^   rn   rr   ru   rw   ry   rz   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                   @  s   e Zd ZdZddœdd„ZdS )Ú	BrokenStrzb
    An exception class the instances of which cannot be presented as strings
    via L{str}.
    rB   r,   c                 C  s   | ‚d S r   r   rt   r   r   r   Ú__str___  s    zBrokenStr.__str__N©r   r   r   r   r¾   r   r   r   r   r½   Y  s   r½   c                   @  s   e Zd ZdZddœdd„ZdS )ÚBrokenExceptionMetaclassza
    A metaclass for an exception type which cannot be presented as a string via
    L{str}.
    rB   r,   c                 C  s   t dƒ‚d S )Nz#You cannot make a string out of me.)r6   rt   r   r   r   r¾   j  s    z BrokenExceptionMetaclass.__str__Nr¿   r   r   r   r   rÀ   d  s   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œdd„Zddœdd	„Zddœd
d„Zddœdd„Zdddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„ZdS )ÚGetTracebackTestsz,
    Tests for L{Failure.getTraceback}.
    rB   r+   )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«   rB   )r   r   Úxr(   r†   r   r   r   Ú_brokenValueTest{  s    
z"GetTracebackTests._brokenValueTestr,   c                 C  s   |   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Æ   rt   r   r   r   Útest_brokenValueBriefDetail†  s    z-GetTracebackTests.test_brokenValueBriefDetailc                 C  s   |   d¡ dS )úY
        Like test_brokenValueBriefDetail, but for the C{"default"} detail case.
        ÚdefaultNrÈ   rt   r   r   r   Útest_brokenValueDefaultDetailŽ  s    z/GetTracebackTests.test_brokenValueDefaultDetailc                 C  s   |   d¡ dS )rÊ   ÚverboseNrÈ   rt   r   r   r   Útest_brokenValueVerboseDetail”  s    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«   rB   )r   r   r(   r†   r   r   r   Ú_brokenTypeTestš  s    z!GetTracebackTests._brokenTypeTestc                 C  s   |   d¡ dS )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Ï   rt   r   r   r   Útest_brokenTypeBriefDetail¤  s    	z,GetTracebackTests.test_brokenTypeBriefDetailc                 C  s   |   d¡ dS )zX
        Like test_brokenTypeBriefDetail, but for the C{"default"} detail case.
        rË   NrÐ   rt   r   r   r   Útest_brokenTypeDefaultDetail¯  s    z.GetTracebackTests.test_brokenTypeDefaultDetailc                 C  s   |   d¡ dS )zX
        Like test_brokenTypeBriefDetail, but for the C{"verbose"} detail case.
        rÍ   NrÐ   rt   r   r   r   Útest_brokenTypeVerboseDetailµ  s    z.GetTracebackTests.test_brokenTypeVerboseDetailN)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kdƒ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 n0 t jk
r<   t ¡ }|  | t j¡¡ Y nX |  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…   ZRaiserExceptionr   r&   rE   ÚcheckÚfailr�   r   r   r   Ú/test_failureConstructionWithMungedStackSucceedsÁ  s    z@FindFailureTests.test_failureConstructionWithMungedStackSucceeds)r   r   r   r   r   r   r×   r   r   r   r   rÔ   ¼  s   rÔ   rB   Úintr+   r   )Úfnrk   ÚnameÚtextr   c                 C  s   t | ||ƒS r   r   )rÙ   rk   rÚ   rÛ   r   r   r   Ú_tbÕ  s    rÜ   c                   @  s,   e Zd ZdZddœdd„Zd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.
        ri   úfilename.pyé{   N)r   Ú
_Tracebackr2   r†   r‡   rÜ   ©r   rK   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.
        Zmethod1rÞ   rß   Zmethod2éë   N)r   rà   r2   r†   r‡   rÜ   Úextract_stackÚtb_framerá   r   r   r   Útest_manyFramesð  s     þÿþþ	
ÿþz)FormattableTracebackTests.test_manyFramesN)r   r   r   r   râ   ræ   r   r   r   r   rÝ   Ù  s   rÝ   c                   @  s:   e Zd ZdZddœdd„Zddœdd„Zd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.
        )ZdummyparentZdummyparentfileéo   NNNZ	local_varé*   Z
global_varéd   Ú	dummynameÚdummyfilename)r   Ú_Framer2   Ú	f_globalsÚf_localsr«   Úf_codeÚ_CodeÚf_backÚ
f_builtinsÚdictÚf_lastirØ   Úf_linenoÚf_tracer3   )r   Z
back_frameZfake_localsZ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ñ   r2   Ú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ÚlistZ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í   Z_TracebackFramer2   rå   Ú	tb_linenor«   Útb_lastirØ   rE   Úhasattr)r   rø   Ztraceback_framer   r   r   Útest_fakeTracebackFrameR  s    þ
z+FakeAttributesTests.test_fakeTracebackFrameN)r   r   r   r   rù   r  r  r   r   r   r   rç     s   %rç   c                   @  s:   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zd
S )ÚDebugModeTestszF
    Failure's debug mode should allow jumping into the debugger.
    r+   r,   c                   sZ   t j‰tjj‰ ddœ‡ ‡fdd„}ˆ |¡ g ˆ_d
d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   )ÚorigInitr  r   r   Úrestoren  s    z%DebugModeTests.setUp.<locals>.restoreNzTracebackType | None)Útr   c                   s   ˆ j  | ¡ d S r   )r¶   Úappend)r  rt   r   r   Úlogging_post_mortemv  s    z1DebugModeTests.setUp.<locals>.logging_post_mortem)N)r  r  r   r&   r  Z
addCleanupr¶   ZstartDebugMode)r   r  r  r   )r  r  r   r   ÚsetUpf  s    
zDebugModeTests.setUpc                 C  sX   zdd  W n* t k
r6   t ¡ \}}}t ¡ }Y nX |  | 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&   r2   r¶   ZassertFalser!   ©r   Útypr­   rK   r(   r   r   r   Útest_regularFailure|  s    z"DebugModeTests.test_regularFailurec                 C  s\   zdd  W n. t k
r:   t ¡ \}}}tjdd�}Y nX |  | 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&   r2   r¶   rE   r!   r  r   r   r   Útest_captureVars‰  s    zDebugModeTests.test_captureVarsN)r   r   r   r   r  r!  r"  r   r   r   r   r  a  s   r  c                   @  sZ   e Zd ZdZddddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zddœdd„ZdS )ÚExtendedGeneratorTestszU
    Tests C{failure.Failure} support for generator features added in Python 2.5
    r"   zGenerator[Any, Any, Any]r+   )r(   Úgr   c                 C  s2   z|  |¡ W n tk
r"   Y nX |  d¡ d S )Nz<throwExceptionIntoGenerator should have raised StopIteration)ZthrowExceptionIntoGeneratorÚStopIterationrÖ   )r   r(   r$  r   r   r   Ú_throwIntoGeneratorœ  s
    z*ExtendedGeneratorTests._throwIntoGeneratorr,   c                   s†   g ‰d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.
        úGenerator[(None, None, None)]r,   c                   3  s<   z
d V  W n" t k
r,   ˆ t ¡ ¡ Y nX ˆ  d¡ d S )Nz$Yield should have yielded exception.)r%   r  rˆ   r‰   rÖ   r   ©r   Ústuffr   r   Ú	generator­  s
    
zJExtendedGeneratorTests.test_throwExceptionIntoGenerator.<locals>.generatorr   r$   rƒ   r„   rŒ   N)r)   Únextr&  r2   rd   r«   r†   r‡   ©r   r*  r$  r(   r   r(  r   Ú test_throwExceptionIntoGenerator¦  s    z7ExtendedGeneratorTests.test_throwExceptionIntoGeneratorc                   s¨   t ƒ }| ¡  | ¡ }g ‰ d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 k
r,   ˆ  t ¡ ¡ Y nX ˆ d¡ d S )NzNo exception sent to generator)r%   r  r   r&   rÖ   r   ©ZnewFailuresr   r   r   r*  Ì  s
    
zVExtendedGeneratorTests.test_failureConstructionFindsOriginalFailure.<locals>.generatorr$   r   r*  N)
r)   rM   rÄ   r+  r&  r2   r£   rY   rZ   ZassertNotIn)r   r(   Zoriginal_failure_strr*  r$  r   r.  r   Ú,test_failureConstructionFindsOriginalFailure¿  s    zCExtendedGeneratorTests.test_failureConstructionFindsOriginalFailurec                   s6   d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 n t k
r(   g d  Y nX W n& t k
rR   ˆ  t ¡ jt¡ Y nX d S ©Nr$   )r%   r«   r   r&   r<   Ú
IndexErrorr   rt   r   r   r*  ê  s    
zJExtendedGeneratorTests.test_ambiguousFailureInGenerator.<locals>.generatorN)r+  r)   r&  r,  r   rt   r   Ú test_ambiguousFailureInGeneratorã  s
    	z7ExtendedGeneratorTests.test_ambiguousFailureInGeneratorc                 C  s^   ddœdd„}|ƒ }t |ƒ tƒ }z|  ||¡ W n& tk
rX   |  t ¡ jt¡ Y nX dS )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 n t k
r&   g d  Y nX d S r0  )r%   r   r   r   r   r*  ÿ  s    
zLExtendedGeneratorTests.test_ambiguousFailureFromGenerator.<locals>.generatorN)	r+  r)   r&  r%   r«   r   r&   r<   r1  r,  r   r   r   Ú"test_ambiguousFailureFromGeneratorø  s    z9ExtendedGeneratorTests.test_ambiguousFailureFromGeneratorN)	r   r   r   r   r&  r-  r/  r2  r3  r   r   r   r   r#  —  s   
$r#  ),r   Ú
__future__r   ro   r  r³   rf   rˆ   r†   Údisr   Úior   r   Útypesr   Útypingr   r	   r
   Zunittestr   Zcython_test_exception_raiserr   Ztwisted.pythonr   r   Ztwisted.trial.unittestr   rŠ   r   r)   r*   r½   r3   rÀ   rÁ   rÃ   rÔ   rÜ   rÝ   rç   r  r#  r   r   r   r   Ú<module>   sB   	    '
F4T6