
    VhÊ                       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e      Zddd(dZ G d de      Z  G d de      Z! G d de"      Z# G d dee#      Z$ G d de      Z% G d de      Z&d)dZ' G d  d!e      Z( G d" d#e      Z) G d$ d%e      Z* G d& d'e      Z+y)*z6
Test cases for the L{twisted.python.failure} module.
    )annotationsN)distb)StringIOFrameSummary)TracebackType)Any	Generatorcast)skipIf)raiser)failurereflect)SynchronousTestCasec                      e Zd ZdZddZy)ComparableExceptionz+An exception that can be compared by value.c                    | j                   |j                   k(  xr' | j                  t        t        |      j                  k(  S N)	__class__argsr   r   )selfothers     I/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_failure.py__eq__zComparableException.__eq__   s6    %//1 
II159>>>	
    N)r   objectreturnbool)__name__
__module____qualname____doc__r    r   r   r   r      s
    5
r   r   FcaptureVarsc                j    | rd}| 	 ddz   S # t         $ r t        j                  |       }Y |S w xY w)z6
    Make a C{Failure} of a divide-by-zero error.
    xyz   r   r$   )BaseExceptionr   Failure)r%   exampleLocalVarfs      r   getDivisionFailurer-   %   sH      	5	A H  5OO4H5s    22c                  R   e Zd ZdZd)dZd)dZd)dZd)dZd*dZd+dZ	d,dZ
	 d-	 	 	 	 	 d.d	Zd/d0d
Zd/d0dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZd1dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZd)dZ d)dZ!d)d Z"d)d!Z#d)d"Z$d)d#Z%d2d$Z&d)d%Z'd)d&Z(d)d'Z)y()3FailureTestsz'
    Tests for L{failure.Failure}.
    c                   	 t        d      # t        $ r t        j                         }Y nw xY w|j	                  t
        t              }| j                  |t               | j                  |j                  t                y)z(
        Trapping a L{Failure}.
        testN)	NotImplementedErrorr)   r   r*   trap
SystemExitRuntimeErrorassertEqualtype)r   r,   errors      r   test_failAndTrapzFailureTests.test_failAndTrap;   sb    	"%f-- 	"!A	"z<0-!45s    --c                    t               }	 |# t        $ r t        j                         }Y nw xY w| j	                  t         |j
                  t              }| j                  ||       y)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*   assertRaisesr3   OverflowErrorassertIs)r   	exceptionr,   	untrappeds       r   test_trapRaisesWrappedExceptionz,FailureTests.test_trapRaisesWrappedExceptionG   sX     L		"O 	"!A	" %%j!&&-H	i+s    ..c                    t               }t        j                  |      }t        j                  |      }| j                  |j                  |       y)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)r;   r   r*   r>   valuer   r?   f1f2s       r   test_failureValueFromFailurez)FailureTests.test_failureValueFromFailureV   s9     L	__Y'__R bhh	*r   c                    t               }t        j                  |      }	 |j                  t               | j                  j                  |       y# t
        $ r t        j                         }Y <w xY w)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)r;   r   r*   r3   r=   r)   r>   rC   rD   s       r   !test_failureValueFromFoundFailurez.FailureTests.test_failureValueFromFoundFailurea   s[     L	__Y'	#GGM" 	bhh	*  	#"B	#s   A A32A3c                P    | j                  |j                  |      |d|       y)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   sprefixs      r   assertStartsWithzFailureTests.assertStartsWithq   s(     	V,
:OPQu.UVr   c                P    | j                  |j                  |      |d|       y)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)rK   endswith)r   rM   suffixs      r   assertEndsWithzFailureTests.assertEndsWith|   s'     	

6*vj8KA5,QRr   c                L    | j                  ||       | j                  ||       y)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)rO   rS   )r   tbrN   rR   s       r   assertTracebackFormatz"FailureTests.assertTracebackFormat   s$     	b&)B'r   c                   t        |      }t               }|r|j                          |j                  |       |j	                         }d|j
                  |j                  xr dxs dfz  }dj                  t        j                  |j                        t        j                  |j                        |j
                        }| j                  |||       |j                         D cg c]  }|j                  d      s| }	}|r,| j!                  g |	       |rd}nd}| j#                  ||	       y
| j%                  g |	       | j#                  d	|       y
c c}w )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 #{} ---
z  z  exampleLocalVar : "'xyz'"z  exampleLocalVar : 'xyz'zA [Capture of Locals and Globals disabled (use captureVars=True)]
N)r-   r   cleanFailureprintDetailedTracebackgetvaluecountpickledformatr   qualr7   safe_strrC   rV   
splitlinesrL   assertNotEqualassertInr6   )
r   r%   rY   r,   outrU   startendlinelinesWithVarss
             r   assertDetailedTracebackz$FailureTests.assertDetailedTraceback   s.   R ;7jNN	  %\\^)GGYY(=0S-
 
 6<<LL QWW%GG

 	""2uc2 +---/S$T__T=RSSM262MM$.R/MMW Ts   )E Ec                f   |rd}| t               }t               }|j                  |       |j                         }d}|j                  D ]  \  }}}	}
}|| d|	 d| dz  } t        t              }| j                  |d| d|        |r&| j                  t        j                  d|             yy)	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exampleLocalVar.*abcdeN)r-   r   printBriefTracebackr[   framesreprZeroDivisionErrorrV   assertIsNoneresearch)r   r%   r+   r,   rd   rU   stackmethodfilenamelineno	localVars
globalVarszdes                r   assertBriefTracebackz!FailureTests.assertBriefTraceback   s      %O  j	c"\\^?@xx 	7;FHfiz6(!F8266E	7 $%""#b!g	
 bii(@"EF r   c           
        |rd}| t        |      }t               }|j                  |       |j                         }d}|j                  D ]B  \  }}}	}
}|d| d|	 d| dz  }|dt        j                  ||	      j                          dz  }D | j                  |d	|t        j                  |j                        d
t        j                  |j                        d       |r&| j                  t        j                   d|             yy)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$   rl   z  File "z", line z, in rn   z    z"Traceback (most recent call last):ro   zexampleLocalVar.*xyzzyN)r-   r   printTracebackr[   rq   	linecachegetlinestriprV   r   r_   r7   r`   rC   rt   ru   rv   )r   r%   r+   r,   rd   rU   rw   rx   ry   rz   r{   r|   s               r   assertDefaultTracebackz#FailureTests.assertDefaultTraceback
  s   " %O ;7j	\\^?@xx 	L;FHfixz&vhbIIEtI--h?EEGHKKE	L 	""0 QVV$  )			
 bii(@"EF r   c                $    | j                          y)zr
        L{printDetailedTraceback} returns a detailed traceback including the
        L{Failure}'s count.
        Nri   r   s    r   test_printDetailedTracebackz(FailureTests.test_printDetailedTraceback8  s    
 	$$&r   c                $    | j                          y)zC
        L{printBriefTraceback} returns a brief traceback.
        Nr~   r   s    r   test_printBriefTracebackz%FailureTests.test_printBriefTraceback?  s     	!!#r   c                $    | j                          y)z8
        L{printTraceback} returns a traceback.
        Nr   r   s    r   test_printTracebackz FailureTests.test_printTracebackE  s     	##%r   c                (    | j                  d       y)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   s    r   'test_printDetailedTracebackCapturedVarsz4FailureTests.test_printDetailedTracebackCapturedVarsK  s     	$$$6r   c                (    | j                  d       y)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   s    r   $test_printBriefTracebackCapturedVarsz1FailureTests.test_printBriefTracebackCapturedVarsS  s     	!!d!3r   c                (    | j                  d       y)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   s    r   test_printTracebackCapturedVarsz,FailureTests.test_printTracebackCapturedVars]  s     	###5r   c                *    | j                  dd       y)z
        C{printDetailedTraceback} includes information about local variables on
        the stack after C{cleanFailure} has been called.
        T)r%   rY   Nr   r   s    r   .test_printDetailedTracebackCapturedVarsCleanedz;FailureTests.test_printDetailedTracebackCapturedVarsCleanedg  s    
 	$$D$Ir   c                T    | j                  t        t        j                  ddd       y)zu
        L{failure.format_frames} raises a L{ValueError} if the supplied
        C{detail} level is unknown.
        Nnoisiadetail)r<   r;   r   format_framesr   s    r   test_invalidFormatFramesDetailz+FailureTests.test_invalidFormatFramesDetailn  s(    
 	--tT( 	 	
r   c                    t               }t        j                  |      }|j                  t                | j	                  |j
                  |       y r   )r5   r   r*   r3   r6   rC   )r   er,   s      r   test_ExplictPasszFailureTests.test_ExplictPassw  s7    NOOA	|!$r   c                    	 |j                          t        d      # t        $ r< t        j                  t        j                         d         }|d   j                  cY S w xY w)Nz3f.raiseException() didn't raise ZeroDivisionError!?   )raiseException	Exceptionrs   	traceback
extract_tbsysexc_inforg   )r   r,   rU   s      r   _getInnermostFrameLinez#FailureTests._getInnermostFrameLine}  s\    	S
 QRR	 ! 	%%cllnQ&78Bb6;;	s    AA"!A"c                ^    t               }| j                  |      }| j                  |d       y )N1 / 0)r-   r   r6   )r   r,   	innerlines      r   test_RaiseExceptionWithTBz&FailureTests.test_RaiseExceptionWithTB  s*     //2	G,r   c                `    | j                  t        j                  t        j                         y)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)r<   r   NoCurrentExceptionErrorr*   r   s    r   test_ConstructionFailsz#FailureTests.test_ConstructionFails  s     	'997??Kr   c                l    t               }| j                  |j                         |j                         y)z
        If the C{Failure} has not been cleaned, then C{getTracebackObject}
        returns the traceback object that captured in its constructor.
        N)r-   r6   getTracebackObjectrU   r   r,   s     r   test_getTracebackObjectz$FailureTests.test_getTracebackObject  s)    
  --/6r   c                    	 ddz   | j                  j	                         j
                         y# t         $ r- t        j                         }t        j                  d      }Y `w xY w)zc
        C{captureVars=True} has no effect on the result of
        C{getTracebackObject}.
        r(   r   Tr$   N)rs   r   r*   r6   r   rU   )r   noVarsFailurevarsFailures      r   &test_getTracebackObjectFromCaptureVarsz3FailureTests.test_getTracebackObjectFromCaptureVars  sX    
	<E 	99;[^^L ! 	<#OO-M!//d;K	<s   2 3A('A(c                
   t               }t        j                  |j                               }|j	                          t        j                  |j                               }| j                  |       | j                  ||       y)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   rY   assertIsNotNoner6   r   r,   expectedobserveds       r    test_getTracebackObjectFromCleanz-FailureTests.test_getTracebackObjectFromClean  sf    
  ''(<(<(>?	''(<(<(>?X&8,r   c                    t        d      }t        j                  |j                               }|j	                          t        j                  |j                               }| j                  ||       y)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   rY   r6   r   s       r   .test_getTracebackObjectFromCaptureVarsAndCleanz;FailureTests.test_getTracebackObjectFromCaptureVarsAndClean  sZ    
 40''(<(<(>?	''(<(<(>?8,r   c                ~    t        j                  t        d            }| j                  |j	                                y)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   rt   r   r   s     r   'test_getTracebackObjectWithoutTracebackz4FailureTests.test_getTracebackObjectWithoutTraceback  s.     OOIl34!..01r   c                    	 ddz   t        j                        }| j                  |j                         y# t         $ r t        j                         \  }}}Y Uw xY w)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*   r>   rU   )r   klassr?   rU   r,   s        r   $test_tracebackFromExceptionInPython3z1FailureTests.test_tracebackFromExceptionInPython3  sS    	2E OOI&addB  	2#&<<> E9b	2s   9 !AAc                   t               }| j                  |j                         | j                  |j                  j
                  |j                         |j                          | j                  |j                  j
                         y)z
        L{failure.Failure.cleanFailure} sets the C{__traceback__} attribute of
        the exception to L{None} in Python 3.
        N)r-   r   rU   r>   rC   __traceback__rY   rt   r   s     r   *test_cleanFailureRemovesTracebackInPython3z7FailureTests.test_cleanFailureRemovesTracebackInPython3  s\    
  QTT"agg++QTT2	!''//0r   c                    t               }t               }t        |j                         |       | j	                  d|j                                y)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   rc   r[   )r   r,   bufs      r   
test_distbzFailureTests.test_distb  s<      ja""$3/ 	gs||~.r   c                    t               }t        j                  t              }| j	                  t        |      d|d       y)z
        The C{repr} of a L{failure.Failure} shows the type and string
        representation of the underlying exception.
        z <twisted.python.failure.Failure z: division by zero>N)r-   r   fullyQualifiedNamers   r6   rr   )r   r,   typeNames      r   	test_reprzFailureTests.test_repr  s8    
  --.?@GJRT	
r   c                    t               }|j                  |_        | j                         }| j                  t	        |      dk\         |dd D ]  }| j                  d|d           y)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-   rw   flushWarningsrK   lenr6   )r   r,   warningsws       r   test_stackDeprecationz"FailureTests.test_stackDeprecation  sg      ''%%'H*+"# 	AX)	r   c                   t        d      }t        j                  |      j                  j	                         }t        j                  j                  |      }| j                  |t        j                         |j                  j	                         }| j                  |j                  d      dz   |j                  d             | j                  ||       y)ze
        C{Failure._withoutTraceback(exc)} gives the same result as
        C{Failure(exc)}.
        hellor\   r(   N)	rs   r   r*   __dict__copy_withoutTracebackassertIsInstancer6   pop)r   excdict1failure2dict2s        r   test_failureWithoutTracebackz)FailureTests.test_failureWithoutTraceback  s    
  ($--224??44S9h8!!&&( 	7+a/71CD 	&r   c                F   t        d      }t        j                  |      }| j                  |       t        j                  j	                  |      }| j                  |       	 t        d      # t
        $ r t        j                         }Y nw xY w| j                  |       y)zm
        C{Failure(exc)} and C{Failure._withoutTraceback(exc)} can be pickled
        and unpickled.
        r   booN)r   r   r*   assertPicklingRoundtripsr   r)   )r   r   failure1r   failure3s        r   test_failurePicklingz!FailureTests.test_failurePickling  s    
 "'*??3'%%h/
 ??44S9%%h/	)%e,, 	)(H	)%%h/s   #A. .BBc                @   t        j                  t        j                  |            }|j                  j	                         }d|d<   d|d<   |j                  j	                         }| j                  ||       | j                  |j                  |j                         y)z{
        The failure can be pickled and unpickled, and the C{parents} attribute
        is included in the pickle.
        r(   r]   NrU   )pickleloadsdumpsr   r   r6   rq   )r   original_failurer   r   results        r   r   z%FailureTests.assertPicklingRoundtrips/  s    
 <<-= >?#,,113""'')6**:*A*ABr   c                    t        j                  t        d            }| j                  |j	                         d   |j
                         y)z?
        C{Failure.parents} is included in the pickle.
        r   parentsN)r   r*   r   r6   __getstate__r   r   s     r   #test_failurePicklingIncludesParentsz0FailureTests.test_failurePicklingIncludesParents<  s8     OO/89))4aii@r   c                0   t        j                  t                     }|j                  dd |_        t	        j
                  t	        j                  |            }|j                  dd |_        | j                  |j                  |j                  dd        y)zO
        C{Failure.frames} can be set, both before and after pickling.
        Nr   )r   r*   r-   rq   r   r   r   r6   r   r   r   s      r   test_settableFramesz FailureTests.test_settableFramesC  sw     #??+=+?@"2"9"9!"<<<-= >?"//#2.*:*A*A#2*FGr   c                    t        j                  t        d            }|j                  dd |_        t	        j
                  t	        j                  |            }|j                  dd |_        y)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   s      r   test_settableParentsz!FailureTests.test_settableParentsM  sX     #??+>w+GH#3#;#;A#> <<-= >?#++A.r   Nr   None)rM   strrN   r   r   r   )rM   r   rR   r   r   r   )rU   r   rN   r   rR   r   r   r   )FF)r%   r   rY   r   r   r   )F)r%   r   r   r   )r,   failure.Failurer   z
str | None)r   r   r   r   )*r   r    r!   r"   r9   rA   rG   rI   rO   rS   rV   ri   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/   r/   6   s    
6,	++ 	W	S(  ?DJJ7;J	JX&GP,G\'$&746J
%S-
L7
M
-	-	2 	1/

'"0,CAH	/r   r/   c                      e Zd ZdZddZy)	BrokenStrzb
    An exception class the instances of which cannot be presented as strings
    via L{str}.
    c                    | r   r#   r   s    r   __str__zBrokenStr.__str___  s    
r   Nr   r   r   r    r!   r"   r  r#   r   r   r   r   Y  s    
r   r   c                      e Zd ZdZddZy)BrokenExceptionMetaclassza
    A metaclass for an exception type which cannot be presented as a string via
    L{str}.
    c                    t        d      )Nz#You cannot make a string out of me.)r;   r   s    r   r  z BrokenExceptionMetaclass.__str__j  s    >??r   Nr  r  r#   r   r   r  r  d  s    
@r   r  c                      e Zd ZdZy)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   r	  )	metaclassc                  P    e Zd ZdZddZddZddZddZddZddZ	ddZ
dd	Zy
)GetTracebackTestsz,
    Tests for L{Failure.getTraceback}.
    c                    t               }t        j                  |      }|j                  |      }| j	                  |t
               y)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   r   )r   r   xr,   r   s        r   _brokenValueTestz"GetTracebackTests._brokenValueTest{  s9     KOOANN&N1	i-r   c                &    | j                  d       y)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   s    r   test_brokenValueBriefDetailz-GetTracebackTests.test_brokenValueBriefDetail  s     	g&r   c                &    | j                  d       y)Y
        Like test_brokenValueBriefDetail, but for the C{"default"} detail case.
        defaultNr  r   s    r   test_brokenValueDefaultDetailz/GetTracebackTests.test_brokenValueDefaultDetail       	i(r   c                &    | j                  d       y)r  verboseNr  r   s    r   test_brokenValueVerboseDetailz/GetTracebackTests.test_brokenValueVerboseDetail  r  r   c                    t        j                  t                     }|j                  |      }| j	                  |t
               y)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   r   )r   r   r,   r   s       r   _brokenTypeTestz!GetTracebackTests._brokenTypeTest  s6     OO/12NN&N1	i-r   c                &    | j                  d       y)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   s    r   test_brokenTypeBriefDetailz,GetTracebackTests.test_brokenTypeBriefDetail  s     	W%r   c                &    | j                  d       y)zX
        Like test_brokenTypeBriefDetail, but for the C{"default"} detail case.
        r  Nr   r   s    r   test_brokenTypeDefaultDetailz.GetTracebackTests.test_brokenTypeDefaultDetail       	Y'r   c                &    | j                  d       y)zX
        Like test_brokenTypeBriefDetail, but for the C{"verbose"} detail case.
        r  Nr   r   s    r   test_brokenTypeVerboseDetailz.GetTracebackTests.test_brokenTypeVerboseDetail  r$  r   N)r   r   r   r   r   )r   r    r!   r"   r  r  r  r  r  r!  r#  r&  r#   r   r   r  r  v  s/    	.')).	&((r   r  c                  4    e Zd ZdZ eedu d      dd       Zy)FindFailureTestszH
    Tests for functionality related to identifying the C{Failure}.
    Nzraiser extension not availablec                   	 t        j                          | j                  d       y# t         j                  $ rE t	        j
                         }| j                  |j                  t         j                               Y yw xY w)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   failRaiserExceptionr   r*   rK   checkr   s     r   /test_failureConstructionWithMungedStackSucceedsz@FindFailureTests.test_failureConstructionWithMungedStackSucceeds  s`    	>!!#
 II<=	 %% 	=!AOOAGGF$:$:;<	=s   ( AB ?B r   )r   r    r!   r"   r   r   r-  r#   r   r   r(  r(    s'     FdN<=> >>r   r(  c                    t        | ||      S r   r   )fnrz   nametexts       r   _tbr2    s    FD))r   c                       e Zd ZdZddZddZy)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.
    c           	         t        j                  dddi i gg      }| j                  t        j                  |      t        dddd      g       y)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.
        rx   filename.py{   N)r   
_Tracebackr6   r   r   r2  r   rU   s     r   test_singleFramez*FormattableTracebackTests.test_singleFrame  sP     (M3B!G HI 	  $s=#x'N&O	
r   c           
     <   t        j                  dddi i gdddi i gg      }| j                  t        j                  |      t        dddd      t        dddd      g       | j                  t        j                  |j                        t        dddd      g       y)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.
        method1r6  r7  method2   N)r   r8  r6   r   r   r2  extract_stacktb_framer9  s     r   test_manyFramesz)FormattableTracebackTests.test_manyFrames  s     M3B7M3B7
 	  $M3	48M3	48	
 	##BKK0M3	48	
r   Nr   )r   r    r!   r"   r:  rA  r#   r   r   r4  r4    s    

r   r4  c                  (    e Zd ZdZddZddZddZy)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).
    c                t   t        j                  dd      }ddi}ddi}t        j                  ddd||f|      }| j                  |j                  |       | j                  |j                  |       | j                  |j                  t         j                         | j                  |j                  |       | j                  |j                  t               | j                  |j                  t               | j                  |j                  d       | j                  |j                  t        d             y)	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   _Framer6   	f_globalsf_localsr   f_code_Codef_back
f_builtinsdictf_lastiintf_linenof_tracer7   )r   
back_framefake_localsfake_globalsframes        r   test_fakeFrameAttributesz,FakeAttributesTests.test_fakeFrameAttributes  s    ^^ 	

 #B'$c* 	
 	,75ellGMM:z2e..5emmS1,emmT$Z8r   c                h   t        j                  dd      }| j                  |j                  d       | j                  |j                  d       | j                  |j                  t               | j                  |j                  t               | j                  |j                  t               | j                  |j                  t               | j                  |j                  t               | j                  |j                  t               | j                  |j                  t               | j                  |j                   t               | j                  |j"                  t               | j                  |j$                  t               | j                  |j&                  t               | j                  |j(                  t               | j                  |j*                  t               | j                  |j,                  t.               | j                  |j1                         t               y)N
        See L{FakeAttributesTests} for more details about this test.
        rL  rM  N)r   rR  r6   co_nameco_filenamer   co_argcountrW  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   codes     r   test_fakeCodeAttributesz+FakeAttributesTests.test_fakeCodeAttributes;  s    }}[/:{3))?;d..4dllE2d..6dnne4d1137dmmS1dnne4d..6d55s;d44c:dmmU3doos3d//5d..5d//159r   c                J   t        j                  dddi i fd      }t        j                  |      }| j                  |j                  |       | j                  |j
                  d       | j                  |j                  t               | j                  t        |d             y)r`  rL  rM  rI  Ntb_next)r   rN  _TracebackFramer6   r@  	tb_linenor   tb_lastirW  rK   hasattr)r   r]  traceback_frames      r   test_fakeTracebackFramez+FakeAttributesTests.test_fakeTracebackFrameR  s     /2r26
 "11%8115922B7o66<;<r   Nr   )r   r    r!   r"   r^  rv  r~  r#   r   r   rC  rC    s    #9J:.=r   rC  c                  (    e Zd ZdZddZddZddZy)DebugModeTestszF
    Failure's debug mode should allow jumping into the debugger.
    c                     t         j                  t        j                  j                  dfd} j                  |       g  _        dd fd}|t         _        t        j                          y)zK
        Override pdb.post_mortem so we can make sure it's called.
        c                 F    t         _         t        j                  _        y r   )pdbpost_mortemr   r*   __init__)origInitr  s   r   restorez%DebugModeTests.setUp.<locals>.restoren  s    )CO'/GOO$r   Nc                <    j                   j                  |        y r   )r   append)tr   s    r   logging_post_mortemz1DebugModeTests.setUp.<locals>.logging_post_mortemv  s    KKq!r   r   r   )r  zTracebackType | Noner   r   )r  r  r   r*   r  
addCleanupr   startDebugMode)r   r  r  r  r  s   `  @@r   setUpzDebugModeTests.setUpf  sQ    
 oo??++	0 	 24	" . r   c                    	 ddz   | j                  | j                  g       | j                  j                         y# t         $ r/ t        j                         \  }}}t        j                         }Y pw xY w)z~
        If startDebugMode() is called, calling Failure() will first call
        pdb.post_mortem with the traceback.
        r(   r   N)	r)   r   r   r   r*   r6   r   assertFalser%   r   typr   rU   r,   s        r   test_regularFailurez"DebugModeTests.test_regularFailure|  sf    
	"E 	rd+'	  	"<<>LCb!A	"s   A   5A87A8c                    	 ddz   | j                  | j                  g       | j                  j                         y# t         $ r1 t        j                         \  }}}t        j                  d      }Y rw xY w)zk
        If startDebugMode() is called, passing captureVars to Failure() will
        not blow up.
        r(   r   Tr$   N)	r)   r   r   r   r*   r6   r   rK   r%   r  s        r   test_captureVarszDebugModeTests.test_captureVars  sf    
	2E 	rd+&	  	2<<>LCbD1A	2s   A   7A:9A:Nr   )r   r    r!   r"   r  r  r  r#   r   r   r  r  a  s    !,('r   r  c                  D    e Zd ZdZ	 	 	 	 	 	 ddZd	dZd	dZd	dZd	dZy)
ExtendedGeneratorTestszU
    Tests C{failure.Failure} support for generator features added in Python 2.5
    c                h    	 |j                  |       | j                  d       y # t        $ r Y y w xY w)Nz<throwExceptionIntoGenerator should have raised StopIteration)throwExceptionIntoGeneratorr*  StopIteration)r   r,   gs      r   _throwIntoGeneratorz*ExtendedGeneratorTests._throwIntoGenerator  s9    	Y))!, IIWX  		s   % 	11c                H    g d fd} |       }t               }t        |        j                  ||        j                  d   d   t                j                  d   d   t                j                  t        j                  d   d         d   d   d       y)	zr
        It should be possible to throw the exception that a Failure
        represents into a generator.
        c               3     K   	 d   j                  d       y # t        $ r& j                  t        j                                Y y w xY ww)Nz$Yield should have yielded exception.)r*  r)   r  r   r   )r   stuffs   r   	generatorzJExtendedGeneratorTests.test_throwExceptionIntoGenerator.<locals>.generator  sA     B 		@A ! -S\\^,-%   A A,A
A	A

Ar   r(   r   r   r   Nr   zGenerator[None, None, None])r-   nextr  r6   rs   r   r   r   )r   r  r  r,   r  s   `   @r    test_throwExceptionIntoGeneratorz7ExtendedGeneratorTests.test_throwExceptionIntoGenerator  s    
 	B K Q  A&q!&78eAhqk+<=--eAhqk:2>rBGLr   c                    t               }|j                          |j                         }g d fd} |       }t        |        j	                  ||        j                  t              d        j                  ||j                                 j                  d   j                         |j                                 j                  dd   j                                 j                  d|j                                y)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.
        c               3     K   	 d  j                  d       y # t        $ r&  j                  t        j                                Y y w xY ww)NzNo exception sent to generator)r*  r)   r  r   r*   )newFailuresr   s   r   r  zVExtendedGeneratorTests.test_failureConstructionFindsOriginalFailure.<locals>.generator  sC     < 		:; ! 6""7??#456r  r(   r   r  Nr  )
r-   rY   r  r  r  r6   r   rb   rc   assertNotIn)r   r,   original_failure_strr  r  r  s   `    @r   ,test_failureConstructionFindsOriginalFailurezCExtendedGeneratorTests.test_failureConstructionFindsOriginalFailure  s      	 ~~/	< KQ  A&[)1- 	-q~~/?@ 	KN7791>>;KLk;q>#>#>#@Aann&67r   c                n     d fd} |       }t        |       t               } j                  ||       y)z
        When a generator reraises a different exception, creating a L{Failure}
        inside the generator should find the reraised exception rather than
        original one.
        c               3     K   	 	 d  y # t         $ r g d    Y y w xY w# t         $ r5  j                  t        j                         j                  t
               Y y w xY wwNr(   )r)   r   r   r*   rC   
IndexErrorr   s   r   r  zJExtendedGeneratorTests.test_ambiguousFailureInGenerator.<locals>.generator  sW     K$ qE  K%%goo&7&=&=zJKs:   A  A  A  ;AA AA Nr  )r  r-   r  r   r  r  r,   s   `   r    test_ambiguousFailureInGeneratorz7ExtendedGeneratorTests.test_ambiguousFailureInGenerator  s1    	K KQ   A&r   c                    dd} |       }t        |       t               }	 | j                  ||       y# t        $ r5 | j	                  t        j                         j                  t               Y yw xY 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.
        c               3  >   K   	 d  y # t         $ r g d    Y y w xY wwr  )r)   r#   r   r   r  zLExtendedGeneratorTests.test_ambiguousFailureFromGenerator.<locals>.generator  s#       1s   	 Nr  )	r  r-   r  r)   r   r   r*   rC   r  r  s       r   "test_ambiguousFailureFromGeneratorz9ExtendedGeneratorTests.test_ambiguousFailureFromGenerator  sc    	 KQ 	G$$Q* 	G!!'//"3"9"9:F	Gs   5 ;A32A3N)r,   r   r  zGenerator[Any, Any, Any]r   r   r   )	r   r    r!   r"   r  r  r  r  r  r#   r   r   r  r    s?    Y Y%=Y	YM2"8H'*Gr   r  )r%   r   r   r   )
r/  r   rz   rW  r0  r   r1  r   r   r   ),r"   
__future__r   r   r  r   ru   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   r7   r  r	  r  r(  r2  r4  rC  r  r  r#   r   r   <module>r     s    #  
  	 
    "  ' '  / + 6
) 
 /4 "`/& `/F	 @t @)/G C(+ C(L>* >2*1
 3 1
hQ=- Q=h3'( 3'ltG0 tGr   