o
    >h9                     @   sr  d 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 G d	d
 d
eZG dd deZG dd dejZG dd dejZG dd de
jejejZG dd dejejZeee eee eje G dd dejZG dd dej Z!G dd dej"Z#G dd de#Z$G dd de$Z%G dd  d Z&G d!d" d"ej"Z'd#S )$z!
Tests for error handling in PB.
    )StringIO)defer)log)qual)flavorsjellypb)connectedServerAndClient)unittestc                   @      e Zd ZdZdS )AsynchronousExceptionz
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are not L{pb.Error} subclasses.
    N__name__
__module____qualname____doc__ r   r   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/spread/test/test_pbfailure.pyr          r   c                   @   r   )SynchronousExceptionzm
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r      r   r   c                   @   r   )AsynchronousErrorz
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are L{pb.Error} subclasses.
    Nr   r   r   r   r   r   "   r   r   c                   @   r   )SynchronousErrorzi
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r   )   r   r   c                   @      e Zd ZdS )
JellyErrorNr   r   r   r   r   r   r   r   0       r   c                   @   r   )SecurityErrorNr   r   r   r   r   r   4   r   r   c                   @   sh   e 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
dd ZdddZdddZdS )
SimpleRootc                 C      t tdS )zD
        Fail asynchronously with a non-pb.Error exception.
        zremote asynchronous exception)r   failr   selfr   r   r   remote_asynchronousException?      z'SimpleRoot.remote_asynchronousExceptionc                 C      t d)zC
        Fail synchronously with a non-pb.Error exception.
        zremote synchronous exception)r   r    r   r   r   remote_synchronousExceptionE      z&SimpleRoot.remote_synchronousExceptionc                 C   r   )z@
        Fail asynchronously with a pb.Error exception.
        zremote asynchronous error)r   r   r   r    r   r   r   remote_asynchronousErrorK   r#   z#SimpleRoot.remote_asynchronousErrorc                 C   r$   )z?
        Fail synchronously with a pb.Error exception.
        zremote synchronous error)r   r    r   r   r   remote_synchronousErrorQ   r&   z"SimpleRoot.remote_synchronousErrorc                 C   s   G dd dt j}|d)z>
        Fail with error that is not known to client.
        c                   @   r   )z4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   r   UnknownError\   r   r)   zI'm not known to client!)r   Error)r!   r)   r   r   r   remote_unknownErrorW   s   zSimpleRoot.remote_unknownErrorc                 C      |    d S N)
raiseJellyr    r   r   r   remote_jellya      zSimpleRoot.remote_jellyc                 C   r,   r-   )raiseSecurityr    r   r   r   remote_securityd   r0   zSimpleRoot.remote_securityc                 C   "   t  }|| j |d  |S r-   )r   DeferredaddCallbackr.   callbackr!   dr   r   r   remote_deferredJellyg      
zSimpleRoot.remote_deferredJellyc                 C   r3   r-   )r   r4   r5   r1   r6   r7   r   r   r   remote_deferredSecuritym   r:   z"SimpleRoot.remote_deferredSecurityNc                 C   r$   )NzI'm jellyable!)r   r!   resultsr   r   r   r.   s      zSimpleRoot.raiseJellyc                 C   r$   )NzI'm secure!)r   r<   r   r   r   r1   v   r>   zSimpleRoot.raiseSecurityr-   )r   r   r   r"   r%   r'   r(   r+   r/   r2   r9   r;   r.   r1   r   r   r   r   r   >   s    

r   c                   @   s   e Zd ZdZdZdd ZdS )SaveProtocolServerFactoryzd
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    Nc                 C   s
   || _ dS )z3
        Keep track of the given protocol.
        N)protocolInstance)r!   protocolr   r   r   clientConnectionMade   s   
z.SaveProtocolServerFactory.clientConnectionMade)r   r   r   r   r@   rB   r   r   r   r   r?   z   s    r?   c                   @   s   e Zd ZdZdd ZdS )PBConnTestCaser   c                    sJ   t t  _ j j_t  _t fdd fdd\ _ _	 _
d S )Nc                          j d S r-   )serverFactorybuildProtocolr   r    r   r   <lambda>       z&PBConnTestCase.setUp.<locals>.<lambda>c                      rD   r-   )clientFactoryrF   r   r    r   r   rG      rH   )r?   r   rE   unsafeTracebacksr   PBClientFactoryrI   r	   connectedServerconnectedClientpumpr    r   r    r   setUp   s   



zPBConnTestCase.setUpN)r   r   r   rJ   rO   r   r   r   r   rC      s    rC   c                   @   s   e Zd Zejj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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S )%PBFailureTestsc                    s@   fdd j  } fdd}|| j  d S )Nc                    sF   |    | jd r }t|d | j| j| jfS )NTraceback unavailable
   )trapcompare	tracebackflushLoggedErrorsassertEquallentypevalue)errerrs)exceptionTypeflushr!   r   r   eb   s   

z)PBFailureTests._exceptionTest.<locals>.ebc                    s   |  }|  |S r-   )
callRemote
addErrback)rootr8   )r_   methodr   r   gotRootObject   s   

z4PBFailureTests._exceptionTest.<locals>.gotRootObjectrI   getRootObjectr5   rN   r^   )r!   rc   r]   r^   r8   rd   r   )r_   r]   r^   rc   r!   r   _exceptionTest   s
   

zPBFailureTests._exceptionTestc                 C      |  dtdS )z
        Test that a Deferred returned by a remote method which already has a
        Failure correctly has that error passed back to the calling side.
        asynchronousExceptionT)rg   r   r    r   r   r   test_asynchronousException      z)PBFailureTests.test_asynchronousExceptionc                 C   rh   )zw
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        synchronousExceptionT)rg   r   r    r   r   r   test_synchronousException   rk   z(PBFailureTests.test_synchronousExceptionc                 C   rh   )z
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        asynchronousErrorF)rg   r   r    r   r   r   test_asynchronousError   rk   z%PBFailureTests.test_asynchronousErrorc                 C   rh   )z}
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        synchronousErrorF)rg   r   r    r   r   r   test_synchronousError   rk   z$PBFailureTests.test_synchronousErrorc                 C   s   |  || |S r-   )rW   )r!   resultexpectedResultr   r   r   _success   s   zPBFailureTests._successc                 C   s   |j | j||fd |S )N)callbackArgs)addCallbacksrt   )r!   
remoteCallrs   r_   r   r   r   _addFailingCallbacks   s   z#PBFailureTests._addFailingCallbacksNc                    s6   j  } fdd}|| j  dS )z
        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                    s8    |  }d urfdd}|| |S )Nc                    s    t d | S )NrR   )rW   rX   rV   )r[   )excr!   r   r   
gotFailure   s   z@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure)rx   r`   addBoth)objfailureDeferredrz   r_   ry   expectedrc   r!   r   r   
gotRootObj   s   
z,PBFailureTests._testImpl.<locals>.gotRootObjNre   )r!   rc   r   r_   ry   rootDeferredr   r   r~   r   	_testImpl   s   

zPBFailureTests._testImplc                        fdd}  dd|S )z
        Test that an exception which is a subclass of L{pb.Error} has more
        information passed across the network to the calling side.
        c                    ,   |  t  | jt  | j| j dS )N+   rS   r   assertNotIsInstancerY   strassertIsInstancerZ   r   r    r   r   failureJelly      
z6PBFailureTests.test_jellyFailure.<locals>.failureJellyr   r   r   )r!   r   r   r    r   test_jellyFailure      z PBFailureTests.test_jellyFailurec                    r   )z
        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                    r   )N  r   r   r    r   r   failureDeferredJelly   r   zFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJellydeferredJellyr   r   )r!   r   r   r    r   test_deferredJellyFailure   r   z(PBFailureTests.test_deferredJellyFailurec                    r   )z
        A 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                         | jd dS )Ns3   twisted.spread.test.test_pbfailure.SynchronousError  rW   rY   r   r    r   r   failureUnjellyable     zBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyablerp   r   r   )r!   r   r   r    r   test_unjellyableFailure     z&PBFailureTests.test_unjellyableFailurec                    r   )z
        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                    r   )Ns/   twisted.spread.test.test_pbfailure.UnknownError  r   r   r    r   r   failureUnknown  r   z:PBFailureTests.test_unknownFailure.<locals>.failureUnknownunknownErrorr   r   )r!   r   r   r    r   test_unknownFailure  r   z"PBFailureTests.test_unknownFailurec                    r   )z
        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                    r   )N  rS   r   r   rY   r   r   rZ   r   r    r   r   failureSecurity,  r   z<PBFailureTests.test_securityFailure.<locals>.failureSecuritysecurityr   r   )r!   r   r   r    r   test_securityFailure%  r   z#PBFailureTests.test_securityFailurec                    r   )z
        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                    r   )N  r   r   r    r   r   failureDeferredSecurity;  r   zEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecuritydeferredSecurityr   r   )r!   r   r   r    r   test_deferredSecurity4  r   z$PBFailureTests.test_deferredSecurityc                    s    fdd}  dd|tS )z
        Test that attempting to call a method which is not defined correctly
        results in an AttributeError on the calling side.
        c                    s   |  tj  | jd dS )NrQ     )rS   r   NoSuchMethodrT   rU   r   r    r   r   failureNoSuchI  s   z>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchnosuchr   )r   AttributeError)r!   r   r   r    r   test_noSuchMethodFailureC  s   z'PBFailureTests.test_noSuchMethodFailurec                    s@    j  }dd }||  fdd}||  j  dS )z
        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
   |  dS )Nrl   )r`   )rootObjr   r   r   	connectedZ  s   
z;PBFailureTests.test_copiedFailureLogging.<locals>.connectedc                    s(   t |   t} t|d d S )N   )r   r[   rV   r   rW   rX   )failurer\   r    r   r   	exception_  s   

z;PBFailureTests.test_copiedFailureLogging.<locals>.exceptionN)rI   rf   r5   ra   rN   r^   )r!   r8   r   r   r   r    r   test_copiedFailureLoggingP  s   


z(PBFailureTests.test_copiedFailureLoggingc                    s   t td}ttj|t d}g   fdd}| }|d t|j	| 
t d  d }
|jttd 
|jd	 
|jd
 dS )z
        L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a
        L{RemoteError} into the given paused generator at the point where it
        last yielded.
        fooinvokerc               
   3   sN    zd V  W n t jy }  z |  W Y d } ~ d S d } ~ ww d d S )NzRemoteError not raised)r   RemoteErrorappendr   )ry   r   r!   r   r   generatorFuncq  s   
zFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncNrR   r   ascii)r   rQ   )r   CopyableFailurer   r   unjellyDummyInvokersendassertRaisesStopIterationthrowExceptionIntoGeneratorrW   rX   
remoteTyper   encodeargsremoteTraceback)r!   originalcopyr   genry   r   r   r    test_throwExceptionIntoGeneratorg  s   
z/PBFailureTests.test_throwExceptionIntoGeneratorr-   )r   r   r   r
   TestCaserW   rT   rg   rj   rm   ro   rq   rt   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP      s&    
rP   c                   @   s   e Zd ZejjZdZdS )PBFailureUnsafeTestsrR   N)r   r   r   r
   r   failIfEqualsrT   rJ   r   r   r   r   r     s    r   c                   @   s   e Zd ZdZdZdS )r   zZ
    A behaviorless object to be used as the invoker parameter to
    L{jelly.jelly}.
    N)r   r   r   r   serializingPerspectiver   r   r   r   r     s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	FailureJellyingTestsz:
    Tests for the interaction of jelly and failures.
    c                 C   sn   t t }| |tt | |tt ttj|t d}| |tt | |tt dS )z
        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   ZeroDivisionErrorassertIscheckArithmeticErrorr   r   r   )r!   r   copiedr   r   r   test_unjelliedFailureCheck  s   z/FailureJellyingTests.test_unjelliedFailureCheckc                 C   s   t t }| |tt | |tt ttj|t d}t |}ttj|t d}| |tt | |tt dS )aX  
        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   Nr   )r!   r   
copiedOnce
derivativecopiedTwicer   r   r   test_twiceUnjelliedFailureCheck  s   
z4FailureJellyingTests.test_twiceUnjelliedFailureCheckc                 C   sZ   t td}ttj|t d}t }|| tt}d	|}| 
||  dS )a  
        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.
        zsome reasonr   z.Traceback from remote host -- {}: some reason
N)r   r   	Exceptionr   r   r   r   printTracebackr   formatrW   getvalue)r!   r   r   outputr   expectedOutputr   r   r    test_printTracebackIncludesValue  s   
z5FailureJellyingTests.test_printTracebackIncludesValueN)r   r   r   r   r   r   r   r   r   r   r   r     s
    r   N)(r   ior   twisted.internetr   twisted.pythonr   twisted.python.reflectr   twisted.spreadr   r   r   twisted.test.iosimr	   twisted.trialr
   r   r   r   r*   r   r   	Jellyable
RemoteCopyr   r   setUnjellyableForClassglobalSecurityallowInstancesOfRootr   PBServerFactoryr?   r   rC   rP   r   r   r   r   r   r   r   <module>   s2   < j	