
*Nc           @   s  d  Z  d d l 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
 m Z d d l m Z d e f d	     YZ d
 e f d     YZ d e j f d     YZ d e j f d     YZ d e j e j e j f d     YZ d e j e j f d     YZ e j e e  e j e e  e j j e  d e j f d     YZ d e j f d     YZ d e j f d     YZ  d e  f d     YZ! d e! f d     YZ" d e# f d     YZ$ d  e j f d!     YZ% d S("   s!   
Tests for error handling in PB.
iN(   t   StringIO(   t   unittest(   t   pbt   flavorst   jelly(   t   reactort   defer(   t   logt   AsynchronousExceptionc           B   s   e  Z d  Z RS(   s   
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are not L{pb.Error} subclasses.
    (   t   __name__t
   __module__t   __doc__(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR      s   t   SynchronousExceptionc           B   s   e  Z d  Z RS(   sm   
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    (   R	   R
   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR      s   t   AsynchronousErrorc           B   s   e  Z d  Z RS(   s   
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are L{pb.Error} subclasses.
    (   R	   R
   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   "   s   t   SynchronousErrorc           B   s   e  Z d  Z RS(   si   
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    (   R	   R
   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   )   s   t
   JellyErrorc           B   s   e  Z RS(    (   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   1   s   t   SecurityErrorc           B   s   e  Z RS(    (   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   5   s   t
   SimpleRootc           B   sq   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d d	  Z d d
  Z RS(   c         C   s   t  j t d   S(   sD   
        Fail asynchronously with a non-pb.Error exception.
        s   remote asynchronous exception(   R   t   failR   (   t   self(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_asynchronousExceptionA   s    c         C   s   t  d   d S(   sC   
        Fail synchronously with a non-pb.Error exception.
        s   remote synchronous exceptionN(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_synchronousExceptionG   s    c         C   s   t  j t d   S(   s@   
        Fail asynchronously with a pb.Error exception.
        s   remote asynchronous error(   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_asynchronousErrorM   s    c         C   s   t  d   d S(   s?   
        Fail synchronously with a pb.Error exception.
        s   remote synchronous errorN(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_synchronousErrorS   s    c         C   s)   d t  j f d     Y} | d   d S(   s>   
        Fail with error that is not known to client.
        t   UnknownErrorc           B   s   e  Z RS(    (   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   ]   s   s   I'm not known to client!N(   R   t   Error(   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_unknownErrorY   s    c         C   s   |  j    d  S(   N(   t
   raiseJelly(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_jellya   s    c         C   s   |  j    d  S(   N(   t   raiseSecurity(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_securityd   s    c         C   s-   t  j   } | j |  j  | j d   | S(   N(   R   t   Deferredt   addCallbackR   t   callbackt   None(   R   t   d(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_deferredJellyg   s    c         C   s-   t  j   } | j |  j  | j d   | S(   N(   R   R   R    R   R!   R"   (   R   R#   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_deferredSecuritym   s    c         C   s   t  d   d  S(   Ns   I'm jellyable!(   R   (   R   t   results(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   s   s    c         C   s   t  d   d  S(   Ns   I'm secure!(   R   (   R   R&   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   v   s    N(   R	   R
   R   R   R   R   R   R   R   R$   R%   R"   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   @   s   									t   SaveProtocolServerFactoryc           B   s   e  Z d  Z d Z d   Z RS(   sd   
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    c         C   s   | |  _  d S(   s3   
        Keep track of the given protocol.
        N(   t   protocolInstance(   R   t   protocol(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   clientConnectionMade   s    N(   R	   R
   R   R"   R(   R*   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR'   {   s   t   PBConnTestCasec           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   i    c         C   s   |  j    |  j   d  S(   N(   t   _setUpServert   _setUpClient(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   setUp   s    
c         C   sC   t  t    |  _ |  j |  j _ t j d |  j d d |  _ d  S(   Ni    t	   interfaces	   127.0.0.1(   R'   R   t   serverFactoryt   unsafeTracebacksR   t	   listenTCPt
   serverPort(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR,      s    c         C   s@   |  j  j   j } t j   |  _ t j d | |  j  |  _ d  S(   Ns	   127.0.0.1(	   R3   t   getHostt   portR   t   PBClientFactoryt   clientFactoryR   t
   connectTCPt   clientConnector(   R   t   portNo(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR-      s    c         C   sG   |  j  j d  k	 r( |  j  j j j   n  t j |  j   |  j   g  S(   N(	   R0   R(   R"   t	   transportt   loseConnectionR   t   gatherResultst   _tearDownServert   _tearDownClient(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   tearDown   s
    	c         C   s   t  j |  j j  S(   N(   R   t   maybeDeferredR3   t   stopListening(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR>      s    c         C   s   |  j  j   t j d   S(   N(   R9   t
   disconnectR   t   succeedR"   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR?      s    (	   R	   R
   R1   R.   R,   R-   R@   R>   R?   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR+      s   					t   PBFailureTestc           B   s   e  Z e j j Z d    Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d d  Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z e j d  d k  r d e _ n  RS(   c            sG       f d      j  j   }   f d   } | j |  | S(   Nc            sd   |  j      j |  j d   rN   j   }   j t |  d  n  |  j |  j |  j f S(   Ns   Traceback unavailable
i   (   t   trapt   comparet	   tracebackt   flushLoggedErrorst   assertEqualt   lent   typet   value(   t   errt   errs(   R   t   exceptionTypet   flush(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   eb   s    c            s    |  j    } | j    | S(   N(   t
   callRemotet
   addErrback(   t   rootR#   (   RR   t   method(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   gotRootObject   s    (   R7   t   getRootObjectR    (   R   RV   RP   RQ   R#   RW   (    (   R   RP   RR   RQ   RV   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _exceptionTest   s
    c         C   s   |  j  d t t  S(   s   
        Test that a Deferred returned by a remote method which already has a
        Failure correctly has that error passed back to the calling side.
        t   asynchronousException(   RY   R   t   True(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_asynchronousException   s    c         C   s   |  j  d t t  S(   sw   
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        t   synchronousException(   RY   R   R[   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_synchronousException   s    c         C   s   |  j  d t t  S(   s   
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        t   asynchronousError(   RY   R   t   False(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_asynchronousError   s    c         C   s   |  j  d t t  S(   s}   
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        t   synchronousError(   RY   R   R`   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_synchronousError   s    c         C   s   |  j  | |  | S(   N(   RJ   (   R   t   resultt   expectedResult(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _success   s    c         C   s    | j  |  j | d | f | S(   Nt   callbackArgs(   t   addCallbacksRf   (   R   t
   remoteCallRe   RR   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _addFailingCallbacks   s    
c            s;    j  j   }       f d   } | j |  | S(   s   
        Call the given remote method and attach the given errback to the
        resulting Deferred.  If C{exc} is not None, also assert that one
        exception of that type was logged.
        c            sP    j  |  j      }   d  k	 rL    f d   } | j |  n  | S(   Nc            s#     j  t   j    d  |  S(   Ni   (   RJ   RK   RI   (   RN   (   R   t   exc(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt
   gotFailure   s    (   Rj   RS   R"   t   addBoth(   t   objt   failureDeferredRl   (   Rk   R   RR   t   expectedRV   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt
   gotRootObj   s
    (   R7   RX   R    (   R   RV   Rp   RR   Rk   t   rootDeferredRq   (    (   Rk   R   RV   Rp   RR   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   _testImpl   s    c            s"     f d   }   j  d d |  S(   s   
        Test that an exception which is a subclass of L{pb.Error} has more
        information passed across the network to the calling side.
        c            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni+   (   RF   R   t   failIft
   isinstanceRL   t   strt
   failUnlessRM   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureJelly  s    R   i+   (   Rs   (   R   Rx   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_jellyFailure  s    c            s"     f d   }   j  d d |  S(   s   
        Test that a Deferred which fails with a L{pb.Error} is treated in
        the same way as a synchronously raised L{pb.Error}.
        c            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RF   R   Rt   Ru   RL   Rv   Rw   RM   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureDeferredJelly  s    t   deferredJellyi  (   Rs   (   R   Rz   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_deferredJellyFailure  s    c            s"     f d   }   j  d d |  S(   s   
        An non-jellyable L{pb.Error} subclass raised by a remote method is
        turned into a Failure with a type set to the FQPN of the exception
        type.
        c            s     j  |  j d  d S(   Ns,   twisted.test.test_pbfailure.SynchronousErrori  (   RJ   RL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureUnjellyable"  s    Rb   i  (   Rs   (   R   R}   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unjellyableFailure  s    c            s"     f d   }   j  d d |  S(   s   
        Test that an exception which is a subclass of L{pb.Error} but not
        known on the client side has its type set properly.
        c            s     j  |  j d  d S(   Ns(   twisted.test.test_pbfailure.UnknownErrori  (   RJ   RL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureUnknown.  s    t   unknownErrori  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unknownFailure)  s    c            s"     f d   }   j  d d |  S(   s   
        Test that even if an exception is not explicitly jellyable (by being
        a L{pb.Jellyable} subclass), as long as it is an L{pb.Error}
        subclass it receives the same special treatment.
        c            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RF   R   Rt   Ru   RL   Rv   Rw   RM   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureSecurity;  s    t   securityi  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_securityFailure5  s    c            s"     f d   }   j  d d |  S(   s   
        Test that a Deferred which fails with a L{pb.Error} which is not
        also a L{pb.Jellyable} is treated in the same way as a synchronously
        raised exception of the same type.
        c            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RF   R   Rt   Ru   RL   Rv   Rw   RM   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureDeferredSecurityI  s    t   deferredSecurityi  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_deferredSecurityC  s    c            s%     f d   }   j  d d | t  S(   s   
        Test that attempting to call a method which is not defined correctly
        results in an AttributeError on the calling side.
        c            s'   |  j  t j    j |  j d  d S(   Ns   Traceback unavailable
i  (   RF   R   t   NoSuchMethodRG   RH   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureNoSuchV  s    t   nosuchi  (   Rs   t   AttributeError(   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_noSuchMethodFailureQ  s    c            sE     j  j   } d   } | j |    f d   } | j |  | S(   s   
        Test that a copied failure received from a PB call can be logged
        locally.

        Note: this test needs some serious help: all it really tests is that
        log.err(copiedFailure) doesn't raise an exception.
        c         S   s   |  j  d  S(   NR]   (   RS   (   t   rootObj(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   connectedg  s    c            s6   t  j |     j t  }   j t |  d  d  S(   Ni   (   R   RN   RI   R   RJ   RK   (   t   failureRO   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   exceptionk  s    (   R7   RX   R    RT   (   R   R#   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_copiedFailureLogging]  s    	c            s   t  j t d   } t j t j | d t    } g      f d   } |   } | j d    j t	 | j
 |    j t   d   d }   j | j d    j | j d	    j | j d  d S(
   s   
        L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a
        L{RemoteError} into the given paused generator at the point where it
        last yielded.
        t   foot   invokerc          3   s@   y	 d  VWn# t j k
 r. }    j |   n X j d  d  S(   Ns   RemoteError not raised(   R"   R   t   RemoteErrort   appendR   (   Rk   (   R   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   generatorFunc}  s
    	i   i    s   exceptions.AttributeErrors   Traceback unavailable
N(   R   (   R   t   CopyableFailureR   R   t   unjellyt   DummyInvokert   sendR"   t   assertRaisest   StopIterationt   throwExceptionIntoGeneratorRJ   RK   t
   remoteTypet   argst   remoteTraceback(   R   t   originalt   copyR   t   genRk   (    (   R   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt    test_throwExceptionIntoGeneratort  s    !	
i   i   s<   throwExceptionIntoGenerator is not supported in Python < 2.5N(   i   i   (   R	   R
   R   t   TestCaseRJ   RG   RY   R\   R^   Ra   Rc   Rf   Rj   R"   Rs   Ry   R|   R~   R   R   R   R   R   R   t   syst   version_infot   skip(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyRE      s(   																				t   PBFailureTestUnsafec           B   s   e  Z e j j Z d  Z RS(   i   (   R	   R
   R   R   t   failIfEqualsRG   R1   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR     s   R   c           B   s   e  Z d  Z d Z RS(   sZ   
    A behaviorless object to be used as the invoker parameter to
    L{jelly.jelly}.
    N(   R	   R
   R   R"   t   serializingPerspective(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR     s   t   FailureJellyingTestsc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s:   
    Tests for the interaction of jelly and failures.
    c         C   s   t  j t    } |  j | j t  t  |  j | j t  t  t j t j | d t    } |  j | j t  t  |  j | j t  t  d S(   s   
        An unjellied L{CopyableFailure} has a check method which behaves the
        same way as the original L{CopyableFailure}'s check method.
        R   N(	   R   R   t   ZeroDivisionErrort   assertIdenticalt   checkt   ArithmeticErrorR   R   R   (   R   R   t   copied(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unjelliedFailureCheck  s    !c         C   s   t  j t    } |  j | j t  t  |  j | j t  t  t j t j | d t    } t  j |  } t j t j | d t    } |  j | j t  t  |  j | j t  t  d S(   sX  
        The object which results from jellying a L{CopyableFailure}, unjellying
        the result, creating a new L{CopyableFailure} from the result of that,
        jellying it, and finally unjellying the result of that has a check
        method which behaves the same way as the original L{CopyableFailure}'s
        check method.
        R   N(	   R   R   R   R   R   R   R   R   R   (   R   R   t
   copiedOncet
   derivativet   copiedTwice(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_twiceUnjelliedFailureCheck  s    c         C   sf   t  j t d   } t j t j | d t    } t   } | j |  |  j d | j	    d S(   s  
        When L{CopiedFailure.printTraceback} is used to print a copied failure
        which was unjellied from a L{CopyableFailure} with C{unsafeTracebacks}
        set to C{False}, the string representation of the exception value is
        included in the output.
        s   some reasonR   sV   Traceback from remote host -- Traceback unavailable
exceptions.Exception: some reason
N(
   R   R   t	   ExceptionR   R   R   R    t   printTracebackRJ   t   getvalue(   R   R   R   t   output(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt    test_printTracebackIncludesValue  s    !	(   R	   R
   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR     s   		(&   R   R   R    t   twisted.trialR   t   twisted.spreadR   R   R   t   twisted.internetR   R   t   twisted.pythonR   R   R   R   R   R   R   t	   Jellyablet
   RemoteCopyR   R   t   setUnjellyableForClasst   globalSecurityt   allowInstancesOft   RootR   t   PBServerFactoryR'   R   R+   RE   R   t   objectR   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   <module>   s,   %;!	