o
    >h:                     @  s  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mZm	Z	m
Z
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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$d%Z$ee"e$ G d&d' d'Z%G d(d) d)Z&d;d+d,Z'G d-d. d.eZ(G d/d0 d0eZ)G d1d2 d2Z*G d3d4 d4Z+e
ed5e,f  Z-G d6d7 d7eZ.G d8d9 d9eZ/eee(gZ0dS )<    )annotationsN)AnyCallableListTuple)NoReturn)aotcrefutilstyles)unittest)TestCasec                   @  s4   e Zd ZdddZdddZdddZdd	d
ZdS )VersionTestsreturnNonec                 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dZdS )NullVersionedr   r   c                 S  
   d| _ d S )Nr   okself r   {/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/test/test_persisted.py__init__      
zNullVersioned.__init__Nr   r   __name__
__module____qualname__r   r   r   r   r   r          r   c                   @     e Zd ZdZdddZdS )r      r   r   c                 S  r   Nr"   r   r   r   r   r   upgradeToVersion1"   r   zNullVersioned.upgradeToVersion1Nr   )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                   @  s<   e Zd ZdZdgZdZdZdddZddd	Zdd
dZ	dS )MyVersioned   garbagedatar   r   r   c                 S  s   d| _ dd | _d S )Nxxxc                 S  s   dS )Nzcant persistr   )qr   r   r   <lambda>5   s    z&MyVersioned.__init__.<locals>.<lambda>)somedatar0   r   r   r   r   r   3   s   zMyVersioned.__init__c                 S     |  j d7  _ d S r#   )v3r   r   r   r   upgradeToVersion37      zMyVersioned.upgradeToVersion3c                 S  r5   r#   )v4r   r   r   r   upgradeToVersion4:   r8   zMyVersioned.upgradeToVersion4Nr   )
r   r   r   r%   persistenceForgetsr6   r9   r   r7   r:   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.   r6   r9   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                   @  s    e Zd ZdddZdd	d
ZdS )ClassWithCustomHashuniquestrhashintr   r   c                 S  s   || _ || _d S N)rB   rD   )r   rB   rD   r   r   r   r   O   s   
zClassWithCustomHash.__init__c                 S     | j S rF   )rD   r   r   r   r   __hash__S      zClassWithCustomHash.__hash__N)rB   rC   rD   rE   r   r   r   rE   )r   r   r   r   rH   r   r   r   r   rA   N   s    
rA   v1r   v2r"   c                 S  s   t | ddS NupgradedTsetattrr   r   r   r   r3   \   s    z3VersionTests.test_nonIdentityHash.<locals>.<lambda>)r
   r(   rA   r&   r'   r%   r$   r)   r*   assertEqualrB   
assertTruerN   )r   rK   rL   pklr   r   r   test_nonIdentityHashK   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_d fd
d}|t_dt_ddd}|t_t|}t 	  | 
|jj d S )Nc                   @     e Zd ZdS )	ToyClassANr   r   r   r   r   r   r   rV   i       rV   c                   @  rU   )	ToyClassBNrW   r   r   r   r   rY   l   rX   rY   r"   r   r   r   r   c                   s   t  | _t  d S rF   )r&   r)   yr
   r*   r   pklBr   r   r$   u   s   zWVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.upgradeToVersion1objectc                 S  s   t | dd d S rM   rO   r   r   r   r   setUpgraded|   s   zQVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.setUpgraded)r   r   r   r   )r   r]   r   r   )r
   r(   rV   rY   r&   r'   r%   r$   r)   r*   rR   rZ   rN   )r   xrZ   pklAr$   r^   r   r[   r   /test_upgradeDeserializesObjectsRequiringUpgradef   s   

z<VersionTests.test_upgradeDeserializesObjectsRequiringUpgradeNr   )r   r   r   r-   r@   rT   ra   r   r   r   r   r      s
    


"r   c                   @  rU   )VersionedSubClassNrW   r   r   r   r   rb      rX   rb   c                   @  rU   )SecondVersionedSubClassNrW   r   r   r   r   rc      rX   rc   c                   @  rU   )VersionedSubSubClassNrW   r   r   r   r   rd      rX   rd   c                   @  rU   )VersionedDiamondSubClassNrW   r   r   r   r   re      rX   re   c                   @  s8   e Zd ZdZdddZdddZddd	Zd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.
    r   r   c                 C  s   |  ttjg  dS )zM
        L{styles._aybabtu} of L{Versioned} itself is an empty list.
        N)rQ   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)rQ   r
   rg   rb   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)rQ   r
   rg   rd   rb   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)rQ   r
   rg   re   rd   rb   rc   r   r   r   r   test_aybabtuStrict   s   zAybabtuTests.test_aybabtuStrictNr   )r   r   r   __doc__rh   ri   rj   rk   r   r   r   r   rf      s    


rf   c                   @  s   e Zd ZdddZdS )	MyEphemeralr_   rE   r   r   c                 C  
   || _ d S rF   r_   r   r_   r   r   r   r      r   zMyEphemeral.__init__Nr_   rE   r   r   r   r   r   r   r   rm      r    rm   c                   @  r   )EphemeralTestsr   r   c                 C  s\   t d}| |jt  | |jd t|}t|}| |jtj | 	t
|d d S )N   r_   )rm   rQ   	__class__r_   r&   r'   r)   r
   	EphemeralassertFalsehasattr)r   or>   r   r   r   test_ephemeral   s   

zEphemeralTests.test_ephemeralNr   )r   r   r   ry   r   r   r   r   rr      r    rr   c                   @  s    e Zd Zd
ddZdddZd	S )
Pickleabler_   rE   r   r   c                 C  rn   rF   ro   rp   r   r   r   r      r   zPickleable.__init__c                 C  rG   rF   ro   r   r   r   r   getX   rI   zPickleable.getXNrq   rJ   )r   r   r   r   r{   r   r   r   r   rz      s    
rz   c                   @  r!   )NotPickleablez)
    A class that is not pickleable.
    r   r   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   r   rl   r~   r   r   r   r   r|      s    r|   c                   @  r!   )CopyRegisteredzb
    A class that is pickleable only because it is registered with the
    C{copyreg} module.
    r   r   c                 C  s   t  | _dS )zE
        Ensure that this object is normally not pickleable.
        N)r|   notPickleabler   r   r   r   r      s   zCopyRegistered.__init__Nr   )r   r   r   rl   r   r   r   r   r   r          r   c                   @  s   e Zd ZdZdS )CopyRegisteredLoadedz2
    L{CopyRegistered} after unserialization.
    N)r   r   r   rl   r   r   r   r   r      s    r   crr]   r   ,tuple[type[CopyRegisteredLoaded], tuple[()]]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   )r   )r   r   r   r   reduceCopyRegistered   s   	r   c                   @  $   e Zd ZU dZded< d	ddZdS )
A
    dummy class
    zCallable[[], None]bmethodr   r   c                 C     d S rF   r   r   r   r   r   amethod
     z	A.amethodNr   )r   r   r   rl   __annotations__r   r   r   r   r   r        
 r   c                   @  r   )
Br   r   ar   r   c                 C  r   rF   r   r   r   r   r   r     r   z	B.bmethodNr   )r   r   r   rl   r   r   r   r   r   r   r     r   r   r   c                   C  r   rF   r   r   r   r   r   funktion  r   r   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
PicklingTestsz$Test pickling of extra object types.r   r   c                 C  s$   t t}t |}| |t d S rF   )r&   r'   r
   r)   rQ   )r   r>   rx   r   r   r   test_module   s   

zPicklingTests.test_modulec                 C  sF   t d}t|j}t|}| | d | t|t|j d S )Nr<   )rz   r&   r'   r{   r)   rQ   type)r   r?   r>   rx   r   r   r   test_instanceMethod%  s
   
z!PicklingTests.test_instanceMethodNr   )r   r   r   rl   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.
    r   r   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StringIOrQ   getvalue)r   pickledStringIWithTextloadedr   r   r   test_unpickleBytesIO3  s
   
z,StringIOTransitionTests.test_unpickleBytesIONr   )r   r   r   rl   r   r   r   r   r   r   -  r   r   c                   @  s0   e Zd ZU d ed< d ed< ded< ddd	Zd
S )EvilSourcerorr   br]   cr_   r   r   c                 C  s   | | _ | | j _|| j j_d S rF   )r   r   r   rp   r   r   r   r   F  s   zEvilSourceror.__init__N)r_   r]   r   r   )r   r   r   r   r   r   r   r   r   r   A  s
   
 r   c                   @  s*   e Zd ZU ded< d
ddZdddZd	S )NonDictStaterC   stater   c                 C  rG   rF   r   r   r   r   r   __getstate__O  rI   zNonDictState.__getstate__r   c                 C  rn   rF   r   )r   r   r   r   r   __setstate__R  r   zNonDictState.__setstate__N)r   rC   )r   rC   r   r   )r   r   r   r   r   r   r   r   r   r   r   L  s   
 
r   _CircularTupleTypec                   @  sp   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdddZ	dddZ
dddZdddZdS )AOTTestsr   r   c              
   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/   rs   helloworld)slicesysmaxsizeEllipsisr   unjellyFromSourcejellyToSourcerQ   )r   r?   rtObjr   r   r   test_simpleTypesZ  s   
zAOTTests.test_simpleTypesc                 C  sL   t  }t }|j|_||_tt|jj}| t|j	t
| d S rF   )r   r   r   r   r   r   r   rQ   _selfOfMethodrt   _classOfMethod)r   r   r   im_r   r   r   test_methodSelfIdentityj  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)	r   r   r   r   assertRaisesr}   r   r   r   )r   r   r   savedbmethodr   r   r   test_methodNotSelfIdentityr  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dZedddZdS )	z2AOTTests.test_unsupportedType.<locals>.UnknownTyper   r   c                 S     t  rF   AttributeErrorr   r   r   r   __dict__     z;AOTTests.test_unsupportedType.<locals>.UnknownType.__dict__c                 S  r   rF   r   r   r   r   r   r     r   z?AOTTests.test_unsupportedType.<locals>.UnknownType.__getstate__Nr   )r   r   r   propertyr   r   r   r   r   r   UnknownType  s
    r   N)r   r}   r   r   )r   r   r   r   r   test_unsupportedType  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/   rs   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   r   r   r   )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 rF   )r   r   
jellyToAOTunjellyFromAOTr   r   r   )r   rx   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   assertIsrQ   )r   r   r   r   r   r   r   r   test_circularTuple  s   


zAOTTests.test_circularTuplec                 C  s$   |  tddg iitd dS )z:
        The generated serialization is indented.
        r   r   z|                app={
                  'hello':{
                    'world':[],
                    },
                  }N)rQ   r   r   textwrapdedentr   r   r   r   testIndentify  s   zAOTTests.testIndentifyNr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Y  s    








	r   c                   @  sB   e Zd ZdZdddZdddZddd	Zdd
dZdddZdS )CrefUtilTestsz 
    Tests for L{crefutil}.
    r   r   c                 C  s    t i }| t|jdd dS )zO
        L{crefutil._DictKeyAndValue} only support keys C{0} and C{1}.
        r/   rs   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/   rs   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_dontCallInstanceMethodsThatArentReadyNr   )	r   r   r   rl   r   r   r   r   r   r   r   r   r   r     s    



	r   )r   r]   r   r   r   )1
__future__r   copyregr   r&   r   r   typingr   r   r   r   typing_extensionsr   twisted.persistedr   r	   r
   twisted.trialr   twisted.trial.unittestr   r   r(   rb   rc   rd   re   rf   ru   rm   rr   rz   r|   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   	testCasesr   r   r   r   <module>   sJ   q%


 4