o
    b_4                     @   s  d dl Z d dlZd dlZd dl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jZG d	d
 d
ejZG dd deZG dd deeZG dd deZG dd dejZG dd deZG dd dZG dd dZG dd dZG dd dZdd Ze ee G dd  d ZG d!d" d"Zd#d$ ZG d%d& d&eZG d'd( d(eZG d)d* d*Z G d+d, d,Z!G d-d. d.eZ"G d/d0 d0eZ#eeegZ$dS )1    N)aotcrefutilstyles)unittest)TestCasec                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
VersionTestsc                 C   sP   G dd da tt  }G dd dtja t|}t  |js&J dd S )Nc                   @      e Zd Zdd ZdS )NullVersionedc                 S   
   d| _ d S )Nr   okself r   =/usr/lib/python3/dist-packages/twisted/test/test_persisted.py__init__      
zNullVersioned.__init__N__name__
__module____qualname__r   r   r   r   r   r	          r	   c                   @      e Zd ZdZdd ZdS )r	      c                 S   r
   Nr   r   r   r   r   r   upgradeToVersion1   r   zNullVersioned.upgradeToVersion1N)r   r   r   persistenceVersionr   r   r   r   r   r	      s    zinitial upgrade not run!)r	   pickledumpsr   	Versionedloads	doUpgrader   )r   pkclmnvr   r   r   test_nullVersionUpgrade   s   
z$VersionTests.test_nullVersionUpgradec                 C   s   G dd dt jat }|js|jrJ dt|}dt_t|}t 	  |js.J d|js5J dt|}t|}t 	  |jdksLJ d|jdksUJ dd S )	Nc                   @   s6   e Zd ZdZdgZdZdZdd Zdd Zdd	 Z	d
S )MyVersioned   garbagedatar   c                 S   s   d| _ dd | _d S )Nxxxc                 S   s   dS )Nzcant persistr   )qr   r   r   <lambda>0   s    z&MyVersioned.__init__.<locals>.<lambda>)somedatar'   r   r   r   r   r   .   s   zMyVersioned.__init__c                 S      |  j d7  _ d S r   )v3r   r   r   r   upgradeToVersion32      zMyVersioned.upgradeToVersion3c                 S   r,   r   )v4r   r   r   r   upgradeToVersion45   r/   zMyVersioned.upgradeToVersion4N)
r   r   r   r   persistenceForgetsr-   r0   r   r.   r1   r   r   r   r   r%   (   s    r%   zhasn't been upgraded yet   zdidn't do version 3 upgradezdidn't do version 4 upgrader   zupgraded unnecessarily)
r   r   r%   r-   r0   r   r   r   r    r!   )r   mvpicklobjr   r   r   test_versionUpgrade%   s   



z VersionTests.test_versionUpgradec                 C   s   G dd dt jatdd}tdd}t||f}~~dt_dd t_t|\}}t   | 	|j
d | 	|j
d | |j | |j d S )	Nc                   @      e Zd Zdd Zdd ZdS )ClassWithCustomHashc                 S   s   || _ || _d S N)uniquehash)r   r;   r<   r   r   r   r   J   s   
zClassWithCustomHash.__init__c                 S      | j S r:   )r<   r   r   r   r   __hash__N      zClassWithCustomHash.__hash__N)r   r   r   r   r>   r   r   r   r   r9   I   s    r9   v1r   v2r   c                 S   s   t | ddS NupgradedTsetattrr   r   r   r   r*   W   s    z3VersionTests.test_nonIdentityHash.<locals>.<lambda>)r   r   r9   r   r   r   r   r    r!   assertEqualr;   
assertTruerC   )r   r@   rA   pklr   r   r   test_nonIdentityHashF   s   


z!VersionTests.test_nonIdentityHashc                    s   G dd dt jaG dd dt jat }t }t|t|} ~~dt_ fdd}|t_dt_dd	 }|t_t|}t 	  | 
|jj d S )
Nc                   @      e Zd ZdS )	ToyClassANr   r   r   r   r   r   r   rK   d       rK   c                   @   rJ   )	ToyClassBNrL   r   r   r   r   rN   g   rM   rN   r   c                    s   t  | _t  d S r:   )r   r    yr   r!   r   pklBr   r   r   p   s   zWVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.upgradeToVersion1c                 S   s   t | dd d S rB   rD   r   r   r   r   setUpgradedw   s   zQVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.setUpgraded)r   r   rK   rN   r   r   r   r   r    r!   rG   rO   rC   )r   xrO   pklAr   rR   r   rP   r   /test_upgradeDeserializesObjectsRequiringUpgradea   s   
z<VersionTests.test_upgradeDeserializesObjectsRequiringUpgradeN)r   r   r   r$   r7   rI   rU   r   r   r   r   r      s
    !r   c                   @   rJ   )VersionedSubClassNrL   r   r   r   r   rV      rM   rV   c                   @   rJ   )SecondVersionedSubClassNrL   r   r   r   r   rW      rM   rW   c                   @   rJ   )VersionedSubSubClassNrL   r   r   r   r   rX      rM   rX   c                   @   rJ   )VersionedDiamondSubClassNrL   r   r   r   r   rY      rM   rY   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )AybabtuTestsz
    L{styles._aybabtu} gets all of classes in the inheritance hierarchy of its
    argument that are strictly between L{Versioned} and the class itself.
    c                 C   s   |  ttjg  dS )zM
        L{styles._aybabtu} of L{Versioned} itself is an empty list.
        N)rF   r   _aybabtur   r   r   r   r   test_aybabtuStrictEmpty   s   z$AybabtuTests.test_aybabtuStrictEmptyc                 C   s   |  ttg  dS )z
        There are no classes I{between} L{VersionedSubClass} and L{Versioned},
        so L{styles._aybabtu} returns an empty list.
        N)rF   r   r[   rV   r   r   r   r   test_aybabtuStrictSubclass   s   z'AybabtuTests.test_aybabtuStrictSubclassc                 C   s   |  tttg dS )z
        With a sub-sub-class of L{Versioned}, L{styles._aybabtu} returns a list
        containing the intervening subclass.
        N)rF   r   r[   rX   rV   r   r   r   r   test_aybabtuSubsubclass   s   z$AybabtuTests.test_aybabtuSubsubclassc                 C   s   |  tttttg dS )z
        For a diamond-shaped inheritance graph, L{styles._aybabtu} returns a
        list containing I{both} intermediate subclasses.
        N)rF   r   r[   rY   rX   rV   rW   r   r   r   r   test_aybabtuStrict   s   zAybabtuTests.test_aybabtuStrictN)r   r   r   __doc__r\   r]   r^   r_   r   r   r   r   rZ      s    rZ   c                   @   r   )MyEphemeralc                 C   
   || _ d S r:   rS   r   rS   r   r   r   r      r   zMyEphemeral.__init__Nr   r   r   r   r   ra      r   ra   c                   @   r   )EphemeralTestsc                 C   s\   t d}| |jt  | |jd t|}t|}| |jtj | 	t
|d d S )N   rS   )ra   rF   	__class__rS   r   r   r    r   	EphemeralassertFalsehasattr)r   or5   r   r   r   test_ephemeral   s   

zEphemeralTests.test_ephemeralN)r   r   r   rl   r   r   r   r   re      r   re   c                   @   r8   )
Pickleablec                 C   rb   r:   rc   rd   r   r   r   r      r   zPickleable.__init__c                 C   r=   r:   rc   r   r   r   r   getX   r?   zPickleable.getXN)r   r   r   r   rn   r   r   r   r   rm          rm   c                   @   r   )NotPickleablez)
    A class that is not pickleable.
    c                 C   s   t d)z9
        Raise an exception instead of pickling.
        zNot serializable.)	TypeErrorr   r   r   r   
__reduce__   s   zNotPickleable.__reduce__N)r   r   r   r`   rr   r   r   r   r   rp          rp   c                   @   r   )CopyRegisteredzb
    A class that is pickleable only because it is registered with the
    C{copyreg} module.
    c                 C   s   t  | _dS )zE
        Ensure that this object is normally not pickleable.
        N)rp   notPickleabler   r   r   r   r      s   zCopyRegistered.__init__N)r   r   r   r`   r   r   r   r   r   rt          rt   c                   @   s   e Zd ZdZdS )CopyRegisteredLoadedz2
    L{CopyRegistered} after unserialization.
    N)r   r   r   r`   r   r   r   r   rw      s    rw   c                 C   s   t dfS )z
    Externally implement C{__reduce__} for L{CopyRegistered}.

    @param cr: The L{CopyRegistered} instance.

    @return: a 2-tuple of callable and argument list, in this case
        L{CopyRegisteredLoaded} and no arguments.
    r   )rw   )crr   r   r   reduceCopyRegistered   s   	ry   c                   @   r   )A
    dummy class
    c                 C      d S r:   r   r   r   r   r   amethod     z	A.amethodN)r   r   r   r`   r}   r   r   r   r   rz      rs   rz   c                   @   r   )Br{   c                 C   r|   r:   r   r   r   r   r   bmethod  r~   z	B.bmethodN)r   r   r   r`   r   r   r   r   r   r     rs   r   c                   C   r|   r:   r   r   r   r   r   funktion  r~   r   c                   @   s    e Zd ZdZdd Zdd ZdS )PicklingTestsz$Test pickling of extra object types.c                 C   s$   t t}t |}| |t d S r:   )r   r   r   r    rF   )r   r5   rk   r   r   r   test_module  s   

zPicklingTests.test_modulec                 C   sF   t d}t|j}t|}| | d | t|t|j d S )Nr3   )rm   r   r   rn   r    rF   type)r   r6   r5   rk   r   r   r   test_instanceMethod  s
   
z!PicklingTests.test_instanceMethodN)r   r   r   r`   r   r   r   r   r   r   r     s    r   c                   @   r   )StringIOTransitionTestsz
    When pickling a cStringIO in Python 2, it should unpickle as a BytesIO or a
    StringIO in Python 3, depending on the type of its contents.
    c                 C   s0   d}t |}| |tj | | d dS )zg
        A cStringIO pickled with bytes in it will yield an L{io.BytesIO} on
        python 3.
        sE   ctwisted.persisted.styles
unpickleStringI
p0
(S'test'
p1
I0
tp2
Rp3
.testN)r   r    assertIsInstanceioStringIOrF   getvalue)r   pickledStringIWithTextloadedr   r   r   test_unpickleBytesIO*  s
   
z,StringIOTransitionTests.test_unpickleBytesION)r   r   r   r`   r   r   r   r   r   r   $  rv   r   c                   @   r   )EvilSourcerorc                 C   s   | | _ | | j _|| j j_d S r:   )abcrd   r   r   r   r   9  s   zEvilSourceror.__init__Nr   r   r   r   r   r   8  r   r   c                   @   r8   )NonDictStatec                 C   r=   r:   stater   r   r   r   __getstate__@  r?   zNonDictState.__getstate__c                 C   rb   r:   r   )r   r   r   r   r   __setstate__C  r   zNonDictState.__setstate__N)r   r   r   r   r   r   r   r   r   r   ?  ro   r   c                   @   sT   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S )AOTTestsc              
   C   sF   ddddt dddddtjd d tf
}tt|}| || d S )	Nr   g       @y              @Tr&   rf   helloworld)slicesysmaxsizeEllipsisr   unjellyFromSourcejellyToSourcerF   )r   r6   rtObjr   r   r   test_simpleTypesH  s   
zAOTTests.test_simpleTypesc                 C   sL   t  }t }|j|_||_tt|jj}| t|j	t
| d S r:   )rz   r   r   r   r   r   r   rF   _selfOfMethodrg   _classOfMethod)r   r   r   im_r   r   r   test_methodSelfIdentityX  s   z AOTTests.test_methodSelfIdentityc                 C   sP   t  }t }|j|_||_tj}t`z| ttjt| W |t_dS |t_w )z
        If a class change after an instance has been created,
        L{aot.unjellyFromSource} shoud raise a C{TypeError} when trying to
        unjelly the instance.
        N)	rz   r   r   r   assertRaisesrq   r   r   r   )r   r   r   savedbmethodr   r   r   test_methodNotSelfIdentity`  s   z#AOTTests.test_methodNotSelfIdentityc                 C   s$   G dd d}|  ttj|  dS )z
        L{aot.jellyToSource} should raise a C{TypeError} when trying to jelly
        an unknown type without a C{__dict__} property or C{__getstate__}
        method.
        c                   @   s   e Zd Zedd ZdS )z2AOTTests.test_unsupportedType.<locals>.UnknownTypec                 S   s   t  r:   )AttributeErrorr   r   r   r   __dict__y  s   z;AOTTests.test_unsupportedType.<locals>.UnknownType.__dict__N)r   r   r   propertyr   r   r   r   r   UnknownTypex  s    r   N)r   rq   r   r   )r   r   r   r   r   test_unsupportedTypeq  s   zAOTTests.test_unsupportedTypec                 C   s   t  j}d|d}dddddddd	d	 tt j|tg}t|}|| || || t t ||g}|d
 |d u sBJ |d d
d |d
d ksRJ d S )Nr   )r   methodr   r&   rf   zhe	llo

"x world!u   goodbye 
	တ world!g      ?d   r      )	r   
AOTJellier	jellyToAOr   r   tupleappendr   r   )r   ajdltujr   r   r   test_basicIdentity  s,   




$zAOTTests.test_basicIdentityc                 C   s,   t  }d|_tt|j|jksJ d S )Nz	meringue!)r   r   r   r   r   )r   r   r   r   r   test_nonDictState  s    zAOTTests.test_nonDictStatec                 C   s"   t t t }| |t dS )z
        L{aot.jellyToSource} and L{aot.unjellyFromSource} honor functions
        registered in the pickle copy registry.
        N)r   r   r   rt   r   rw   )r   r   r   r   r   test_copyReg  s   zAOTTests.test_copyRegc                 C   sV   t t g }t|}t|}|j|u sJ |jj|ju s J |j|jjus)J d S r:   )r   r   
jellyToAOTunjellyFromAOTr   r   r   )r   rk   j1ojr   r   r   test_funkyReferences  s   

zAOTTests.test_funkyReferencesc                 C   sf   g }|df}| | t|}t|}| |d t | |d d | | |d d d dS )zS
        L{aot.jellyToAOT} can persist circular references through tuples.
        i  r   r   N)r   r   r   r   r   r   assertIsrF   )r   r   r   r   r   r   r   r   test_circularTuple  s   


zAOTTests.test_circularTupleN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   G  s    	r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )CrefUtilTestsz 
    Tests for L{crefutil}.
    c                 C   s    t i }| t|jdd dS )zO
        L{crefutil._DictKeyAndValue} only support keys C{0} and C{1}.
        r&   rf   N)r   _DictKeyAndValuer   RuntimeError__setitem__r   r   r   r   r   test_dictUnknownKey  s   
z!CrefUtilTests.test_dictUnknownKeyc                 C   s&   t  }d|d< | t|jdd dS )zI
        L{crefutil._Defer} can be assigned a key only one time.
        r   r   N)r   _Deferr   r   r   r   r   r   r   test_deferSetMultipleTimes  s   z(CrefUtilTests.test_deferSetMultipleTimesc                 C   s&   t g dt}| t|ji d dS )z
        A L{crefutil._Container} where all of its elements are known at
        construction time is nonsensical and will result in errors in any call
        to addDependant.
        )r   r&   rf   z	ignore-meN)r   
_Containerlistr   AssertionErroraddDependant)r   	containerr   r   r   &test_containerWhereAllElementsAreKnown  s   z4CrefUtilTests.test_containerWhereAllElementsAreKnownc                 C   s   |  tt jt d dS )z
        If a dictionary key contains a circular reference (which is probably a
        bad practice anyway) it will be resolved by a
        L{crefutil._DictKeyAndValue}, not by placing a L{crefutil.NotKnown}
        into a dictionary key.
        valueN)r   r   dictr   r   NotKnownr   r   r   r   .test_dontPutCircularReferencesInDictionaryKeys  s   z<CrefUtilTests.test_dontPutCircularReferencesInDictionaryKeysc                 C   s   |  ttdt t dS )z
        L{crefutil._InstanceMethod} raises L{AssertionError} to indicate it
        should not be called.  This should not be possible with any of its API
        clients, but is provided for helping to debug.
        no_nameN)r   r   r   _InstanceMethodr   r   r   r   r   r   *test_dontCallInstanceMethodsThatArentReady  s   z8CrefUtilTests.test_dontCallInstanceMethodsThatArentReadyN)	r   r   r   r`   r   r   r   r   r   r   r   r   r   r     s    	r   )%copyregr   r   r   twisted.persistedr   r   r   twisted.trialr   twisted.trial.unittestr   r   r   rV   rW   rX   rY   rZ   rh   ra   re   rm   rp   rt   rw   ry   rz   r   r   r   r   r   r   r   r   	testCasesr   r   r   r   <module>   s>   p%		w4