ó
[³XMc           @   sq   d  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 „  Z	 d e f d „  ƒ  YZ
 d S(	   s=   
Python 2.5+ test cases for failures thrown into generators.
iÿÿÿÿN(   t   TestCase(   t   Failure(   t   deferc          C   s#   y d d Wn t  ƒ  }  n X|  S(   Ni   i    (   R   (   t   f(    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   getDivisionFailure   s
    t   TwoPointFiveFailureTestsc           B   sG   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c            s«   t  ƒ  } t j ƒ  } y | j ƒ  Wn | j ƒ  n Xg  ‰  ‡  f d †  } d „  } t j | ƒ } | | ƒ j | ƒ ˆ  \ } |  j t j	 | j
 ƒ  ƒ d d d ƒ d S(   sx   
        inlineCallbacks that re-raise tracebacks into their deferred
        should not lose their tracebacsk.
        c            s   ˆ  j  |  ƒ d  S(   N(   t   append(   t   result(   t   failures(    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   collect_error*   s    c         s   s	   |  Vd  S(   N(    (   t   d(    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   ic-   s    iÿÿÿÿs   1/0N(   R   R   t   Deferredt   raiseExceptiont   errbackt   inlineCallbackst
   addErrbackt   assertEqualst	   tracebackt
   extract_tbt   getTracebackObject(   t   selfR   R
   R	   R   t
   newFailure(    (   R   sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   test_inlineCallbacksTracebacks   s    			c         C   s6   y | j  | ƒ Wn t k
 r$ n X|  j d ƒ d  S(   Ns<   throwExceptionIntoGenerator should have raised StopIteration(   t   throwExceptionIntoGeneratort   StopIterationt   fail(   R   R   t   g(    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   _throwIntoGenerator9   s
    c            s§   g  ‰ ‡  ‡ f d †  } | ƒ  } t  ƒ  } | j ƒ  ˆ  j | | ƒ ˆ  j ˆ d d t ƒ ˆ  j t ˆ d d t ƒ ƒ ˆ  j t j ˆ d d ƒ d d d ƒ d S(   sr   
        It should be possible to throw the exception that a Failure
        represents into a generator.
        c           3   s7   y	 d  VWn ˆ j  t j ƒ  ƒ n Xˆ  j d ƒ d  S(   Ns$   Yield should have yielded exception.(   R   t   syst   exc_infoR   (    (   R   t   stuff(    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt	   generatorH   s
    	i    i   i   iÿÿÿÿs   1/0N(	   R   t   nextR   R   t   ZeroDivisionErrort
   assertTruet
   isinstanceR   R   (   R   R    R   R   (    (   R   R   sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt    test_throwExceptionIntoGeneratorB   s    		
c            se   t  ƒ  } | j ƒ  g  ‰ ‡ ‡  f d †  } | ƒ  } | j ƒ  ˆ  j | | ƒ ˆ  j ˆ | g ƒ d S(   sÆ   
        Within an exception handler, it should be possible to find the
        original Failure that caused the current exception (if it was
        caused by throwExceptionIntoGenerator).
        c           3   s7   y	 d  VWn ˆ  j  t j ƒ  ƒ n Xˆ j d ƒ d  S(   Ns   No exception sent to generator(   R   R   t   _findFailureR   (    (   t   foundFailuresR   (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyR    d   s
    	N(   R   t   cleanFailureR!   R   t   assertEqual(   R   R   R    R   (    (   R   R'   sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   test_findFailureInGeneratorZ   s    	
	
c            sˆ   t  ƒ  } | j ƒ  g  ‰ ‡  ‡ f d †  } | ƒ  } | j ƒ  ˆ  j | | ƒ ˆ  j t ˆ ƒ d ƒ ˆ  j ˆ d j ƒ  | j ƒ  ƒ d S(   sï   
        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   s4   y	 d  VWn ˆ j  t ƒ  ƒ n Xˆ  j d ƒ d  S(   Ns   No exception sent to generator(   R   R   R   (    (   R   t   newFailures(    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyR       s
    	i   i    N(   R   R(   R!   R   R)   t   lent   getTraceback(   R   R   R    R   (    (   R   R+   sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt,   test_failureConstructionFindsOriginalFailures   s    	
	
c            s?   ‡  f d †  } | ƒ  } | j  ƒ  t ƒ  } ˆ  j | | ƒ d S(   s¼   
        When a generator reraises a different exception,
        L{Failure._findFailure} inside the generator should find the reraised
        exception rather than original one.
        c           3   sC   y y	 d  VWn g  d n XWn ˆ  j  t ƒ  j t ƒ n Xd  S(   Ni   (   t   assertIsInstanceR   t   valuet
   IndexError(    (   R   (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyR    “   s    	N(   R!   R   R   (   R   R    R   R   (    (   R   sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt    test_ambiguousFailureInGenerator   s
    	
	c         C   s]   d „  } | ƒ  } | j  ƒ  t ƒ  } y |  j | | ƒ Wn |  j t ƒ  j t ƒ n Xd S(   s»   
        When a generator reraises a different exception,
        L{Failure._findFailure} above the generator should find the reraised
        exception rather than original one.
        c           s   s   y	 d  VWn g  d n Xd  S(   Ni   (    (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyR    ¦   s    	N(   R!   R   R   R/   R   R0   R1   (   R   R    R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt"   test_ambiguousFailureFromGenerator    s    		
	(	   t   __name__t
   __module__R   R   R%   R*   R.   R2   R3   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyR      s   							(   t   __doc__R   R   t   twisted.trial.unittestR    t   twisted.python.failureR   t   twisted.internetR   R   R   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/test/generator_failure_tests.pyt   <module>   s   		