o
    Óó·iú:  ã                   @  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   úaC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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   ZpkclZ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 )NZxxxc                 S  s   dS )Nzcant persistr   )Úqr   r   r   Ú<lambda>5   s    z&MyVersioned.__init__.<locals>.<lambda>)Zsomedatar.   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  r1   r#   )Úv4r   r   r   r   ÚupgradeToVersion4:   r4   zMyVersioned.upgradeToVersion4Nr   )
r   r   r   r%   ZpersistenceForgetsr2   r5   r   r3   r6   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,   r2   r5   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)r=   r?   )r   r=   r?   r   r   r   r   O   s   
zClassWithCustomHash.__init__c                 S  ó   | j S rA   )r?   r   r   r   r   Ú__hash__S   ó   zClassWithCustomHash.__hash__N)r=   r>   r?   r@   r   r   ©r   r@   )r   r   r   r   rC   r   r   r   r   r<   N   s    
r<   Úv1r   Úv2r"   c                 S  s   t | ddƒS ©NÚupgradedT©Úsetattrr   r   r   r   r0   \   s    ÿz3VersionTests.test_nonIdentityHash.<locals>.<lambda>)r
   r(   r<   r&   r'   r%   r$   r)   r*   ÚassertEqualr=   Ú
assertTruerI   )r   rF   rG   Z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   rP   i   ó    rP   c                   @  rO   )Ú	ToyClassBNrQ   r   r   r   r   rS   l   rR   rS   r"   r   r   r   r   c                   s   t  ˆ ¡| _t ¡  d S rA   )r&   r)   Úyr
   r*   r   ©ZpklBr   r   r$   u   s   zWVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.upgradeToVersion1Úobjectc                 S  s   t | ddƒ d S rH   rJ   r   r   r   r   ÚsetUpgraded|   s   zQVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.setUpgraded)r   r   r   r   )r   rV   r   r   )r
   r(   rP   rS   r&   r'   r%   r$   r)   r*   rM   rT   rI   )r   ÚxrT   ZpklAr$   rW   r   rU   r   Ú/test_upgradeDeserializesObjectsRequiringUpgradef   s   

z<VersionTests.test_upgradeDeserializesObjectsRequiringUpgradeNr   )r   r   r   r+   r;   rN   rY   r   r   r   r   r      s
    


"r   c                   @  rO   )ÚVersionedSubClassNrQ   r   r   r   r   rZ   †   rR   rZ   c                   @  rO   )ÚSecondVersionedSubClassNrQ   r   r   r   r   r[   Š   rR   r[   c                   @  rO   )ÚVersionedSubSubClassNrQ   r   r   r   r   r\   Ž   rR   r\   c                   @  rO   )ÚVersionedDiamondSubClassNrQ   r   r   r   r   r]   ’   rR   r]   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)rL   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)rL   r
   r_   rZ   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)rL   r
   r_   r\   rZ   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)rL   r
   r_   r]   r\   rZ   r[   r   r   r   r   Útest_aybabtuStrict°   s   þzAybabtuTests.test_aybabtuStrictNr   )r   r   r   Ú__doc__r`   ra   rb   rc   r   r   r   r   r^   –   s    


r^   c                   @  s   e Zd Zddd„ZdS )	ÚMyEphemeralrX   r@   r   r   c                 C  ó
   || _ d S rA   ©rX   ©r   rX   r   r   r   r   ¼   r   zMyEphemeral.__init__N©rX   r@   r   r   r   r   r   r   r   re   »   r    re   c                   @  r   )ÚEphemeralTestsr   r   c                 C  s\   t dƒ}|  |jt ¡ |  |jd¡ t |¡}t |¡}|  |jtj¡ |  	t
|dƒ¡ d S )Né   rX   )re   rL   Ú	__class__rX   r&   r'   r)   r
   Ú	EphemeralZassertFalseÚhasattr)r   Úor9   r   r   r   Útest_ephemeralÁ   s   

zEphemeralTests.test_ephemeralNr   )r   r   r   rp   r   r   r   r   rj   À   r    rj   c                   @  s    e Zd Zd
dd„Zddd„Zd	S )Ú
PickleablerX   r@   r   r   c                 C  rf   rA   rg   rh   r   r   r   r   Î   r   zPickleable.__init__c                 C  rB   rA   rg   r   r   r   r   ÚgetXÑ   rD   zPickleable.getXNri   rE   )r   r   r   r   rr   r   r   r   r   rq   Í   s    
rq   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.N)Ú	TypeErrorr   r   r   r   Ú
__reduce__Ú   s   zNotPickleable.__reduce__N©r   r   )r   r   r   rd   ru   r   r   r   r   rs   Õ   s    rs   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)rs   ZnotPickleabler   r   r   r   r   ç   s   zCopyRegistered.__init__Nr   )r   r   r   rd   r   r   r   r   r   rw   á   ó    rw   c                   @  s   e Zd ZdZdS )ÚCopyRegisteredLoadedz2
    L{CopyRegistered} after unserialization.
    N)r   r   r   rd   r   r   r   r   ry   î   s    ry   ÚcrrV   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   N)ry   )rz   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 rA   r   r   r   r   r   Úamethod
  ó   z	A.amethodNr   )r   r   r   rd   Ú__annotations__r‚   r   r   r   r   r~     ó   
 r~   c                   @  r}   )
ÚBr   r~   Úar   r   c                 C  r�   rA   r   r   r   r   r   r€     rƒ   z	B.bmethodNr   )r   r   r   rd   r„   r€   r   r   r   r   r†     r…   r†   r   c                   C  r�   rA   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 rA   )r&   r'   r
   r)   rL   )r   r9   ro   r   r   r   Útest_module   s   

zPicklingTests.test_modulec                 C  sF   t dƒ}t |j¡}t |¡}|  |ƒ d¡ |  t|ƒt|jƒ¡ d S )Nr7   )rq   r&   r'   rr   r)   rL   Útype)r   r:   r9   ro   r   r   r   Útest_instanceMethod%  s
   
z!PicklingTests.test_instanceMethodNr   )r   r   r   rd   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ÚStringIOrL   Úgetvalue)r   ZpickledStringIWithTextZloadedr   r   r   Útest_unpickleBytesIO3  s
   ÿ
z,StringIOTransitionTests.test_unpickleBytesIONr   )r   r   r   rd   r“   r   r   r   r   r�   -  rx   r�   c                   @  s0   e Zd ZU d ed< d ed< ded< ddd	„Zd
S )ÚEvilSourcerorr‡   ÚbrV   ÚcrX   r   r   c                 C  s   | | _ | | j _|| j j_d S rA   )r‡   r•   r–   rh   r   r   r   r   F  s   zEvilSourceror.__init__N)rX   rV   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 )ÚNonDictStater>   Ústater   c                 C  rB   rA   ©r˜   r   r   r   r   Ú__getstate__O  rD   zNonDictState.__getstate__r   c                 C  rf   rA   r™   )r   r˜   r   r   r   Ú__setstate__R  r   zNonDictState.__setstate__N)r   r>   )r˜   r>   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-   rk   ÚhelloÚworld)ÚsliceÚsysÚmaxsizeÚEllipsisr   ÚunjellyFromSourceÚjellyToSourcerL   )r   r:   ZrtObjr   r   r   Útest_simpleTypesZ  s   
özAOTTests.test_simpleTypesc                 C  sL   t ƒ }tƒ }|j|_||_t t |¡¡jj}|  t |¡j	t 
|¡¡ d S rA   )r~   r†   r€   r‡   r   r¤   r¥   rL   Z_selfOfMethodrl   Z_classOfMethod)r   r‡   r•   Z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‡   ÚassertRaisesrt   r   r¤   r¥   )r   r‡   r•   Z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 ƒ ‚rA   ©ÚAttributeErrorr   r   r   r   Ú__dict__‹  ó   z;AOTTests.test_unsupportedType.<locals>.UnknownType.__dict__c                 S  rª   rA   r«   r   r   r   r   rš   �  r®   z?AOTTests.test_unsupportedType.<locals>.UnknownType.__getstate__Nrv   )r   r   r   Úpropertyr­   rš   r   r   r   r   ÚUnknownTypeŠ  s
    r°   N)r¨   rt   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-   rk   zhe	llo

"x world!u   goodbye 
	á€� world!g      ð?éd   r   é   )	r   Z
AOTJellierZ	jellyToAOr   rˆ   ÚtupleÚappendr¤   r¥   )r   Z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¥   rw   r�   ry   )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 rA   )r”   r   Ú
jellyToAOTÚunjellyFromAOTr‡   r•   r–   )r   ro   Ú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µ   ZassertIsrL   )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)rL   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-   rk   N)r	   Z_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	   Z_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-   rk   z	ignore-meN)r	   Z
_ContainerÚlistr¨   ÚAssertionErrorZ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.
        Zno_nameN)r¨   rÎ   r	   Z_InstanceMethodrÓ   r‹   r   r   r   r   Ú*test_dontCallInstanceMethodsThatArentReady  s   þz8CrefUtilTests.test_dontCallInstanceMethodsThatArentReadyNr   )	r   r   r   rd   rË   rÌ   rÐ   rÔ   rÕ   r   r   r   r   rÇ   ä  s    



	rÇ   )rz   rV   r   r{   r   )1Ú
__future__r   Úcopyregr�   r&   r¡   rÄ   Útypingr   r   r   r   Ztyping_extensionsr   Ztwisted.persistedr   r	   r
   Ztwisted.trialr   Ztwisted.trial.unittestr   r   r(   rZ   r[   r\   r]   r^   rm   re   rj   rq   rs   rw   ry   r|   r~   r†   rˆ   r‰   r�   r”   r—   r@   rœ   r�   rÇ   Z	testCasesr   r   r   r   Ú<module>   sJ   q%


 4