
    \d_4                        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e          Z G d dej                  Z G d	 d
ej                  Z G d de          Z G d dee          Z G d de          Z G d dej                  Z G d de          Z G d d          Z G d d          Z G d d          Z G d d          Zd Z e j        ee            G d d          Z G d  d!          Zd" Z G d# d$e          Z G d% d&e          Z G d' d(          Z  G d) d*          Z! G d+ d,e          Z" G d- d.e          Z#eeegZ$dS )/    N)aotcrefutilstyles)unittest)TestCasec                   &    e Zd Zd Zd Zd Zd ZdS )VersionTestsc                      G d d          a t          j        t                                } G d dt          j                  a t          j        |          }t          j                     |j        s
J d            d S )Nc                       e Zd Zd ZdS )NullVersionedc                     d| _         d S )Nr   okselfs    ;lib/python3.11/site-packages/twisted/test/test_persisted.py__init__zNullVersioned.__init__           N__name__
__module____qualname__r    r   r   r   r      s#            r   r   c                       e Zd ZdZd ZdS )r      c                     d| _         d S Nr   r   r   s    r   upgradeToVersion1zNullVersioned.upgradeToVersion1   r   r   N)r   r   r   persistenceVersionr   r   r   r   r   r      s)        !"    r   zinitial upgrade not run!)r   pickledumpsr   	Versionedloads	doUpgrader   )r   pkclmnvs      r   test_nullVersionUpgradez$VersionTests.test_nullVersionUpgrade   s    	 	 	 	 	 	 	 	 |MOO,,	 	 	 	 	F, 	 	 	 l4  v11111111r   c                     G d dt           j                  at                      }|j        s|j        r
J d            t          j        |          }dt          _        t          j        |          }t          j	                     |j        s
J d            |j        s
J d            t          j        |          }t          j        |          }t          j	                     |j        dk    s
J d            |j        dk    s
J d            d S )	Nc                   2    e Zd ZdZdgZdZdZd Zd Zd Z	dS )MyVersioned   garbagedatar   c                 $    d| _         d | _        d S )Nxxxc                     dS )Nzcant persistr   )qs    r   <lambda>z&MyVersioned.__init__.<locals>.<lambda>0   s    ^ r   )somedatar-   r   s    r   r   zMyVersioned.__init__.   s     %#;#;   r   c                 &    | xj         dz  c_         d S r   )v3r   s    r   upgradeToVersion3zMyVersioned.upgradeToVersion32       1r   c                 &    | xj         dz  c_         d S r   )v4r   s    r   upgradeToVersion4zMyVersioned.upgradeToVersion45   r7   r   N)
r   r   r   r    persistenceForgetsr5   r9   r   r6   r:   r   r   r   r+   r+   (   sY        !""/BB< < <      r   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+   r5   r9   r!   r"   r    r$   r%   )r   mvpicklobjs       r   test_versionUpgradez VersionTests.test_versionUpgrade%   s   	 	 	 	 	&* 	 	 	  ]]E?RU??%????R  )*&l5!!v444444v444444S!!l5!!v{{{4{{{v{{{4{{{{{r   c                      G d dt           j                  at          dd          }t          dd          }t          j        ||f          }~~dt          _        d t          _        t          j        |          \  }}t          j                     | 	                    |j
        d           | 	                    |j
        d           |                     |j                   |                     |j                   d S )Nc                       e Zd Zd Zd ZdS )ClassWithCustomHashc                 "    || _         || _        d S N)uniquehash)r   rF   rG   s      r   r   zClassWithCustomHash.__init__J   s    $ 			r   c                     | j         S rE   )rG   r   s    r   __hash__zClassWithCustomHash.__hash__N   s
    y r   N)r   r   r   r   rI   r   r   r   rC   rC   I   s2        ! ! !! ! ! ! !r   rC   v1r   v2r   c                 $    t          | dd          S NupgradedTsetattrr   s    r   r2   z3VersionTests.test_nonIdentityHash.<locals>.<lambda>W   s    W*d>
 >
 r   )r   r#   rC   r!   r"   r    r   r$   r%   assertEqualrF   
assertTruerN   )r   rJ   rK   pkls       r   test_nonIdentityHashz!VersionTests.test_nonIdentityHashF   s   	! 	! 	! 	! 	!&"2 	! 	! 	! !q)) q))lB8$$12.1
 1
- c""BD)))D)))$$$$$$$$r   c                     G d dt           j                  a G d dt           j                  at                      }t                      }t	          j        |          t	          j        |          c}~~dt          _        fd}|t          _        dt          _        d }|t          _        t	          j        |          }t          j	                     | 
                    |j        j                   d S )Nc                       e Zd ZdS )	ToyClassANr   r   r   r   r   r   rW   rW   d           Dr   rW   c                       e Zd ZdS )	ToyClassBNrX   r   r   r   r[   r[   g   rY   r   r[   r   c                 `    t          j                  | _        t          j                     d S rE   )r!   r$   yr   r%   )r   pklBs    r   r   zWVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.upgradeToVersion1p   s)    \$''DFr   c                 (    t          | dd           d S rM   rO   r   s    r   setUpgradedzQVersionTests.test_upgradeDeserializesObjectsRequiringUpgrade.<locals>.setUpgradedw   s    D*d+++++r   )r   r#   rW   r[   r!   r"   r    r   r$   r%   rR   r]   rN   )r   xr]   pklAr   r`   r^   s         @r   /test_upgradeDeserializesObjectsRequiringUpgradez<VersionTests.test_upgradeDeserializesObjectsRequiringUpgradea   s   	 	 	 	 	( 	 	 		 	 	 	 	( 	 	 	 KKKK\!__fl1oo
dq'(	$	 	 	 	 	 '8	#'(	$	, 	, 	, '2	#L%%%%%r   N)r   r   r   r(   r@   rT   rc   r   r   r   r	   r	      sQ        2 2 2&5 5 5B% % %6& & & & &r   r	   c                       e Zd ZdS )VersionedSubClassNrX   r   r   r   re   re              Dr   re   c                       e Zd ZdS )SecondVersionedSubClassNrX   r   r   r   rh   rh      rf   r   rh   c                       e Zd ZdS )VersionedSubSubClassNrX   r   r   r   rj   rj      rf   r   rj   c                       e Zd ZdS )VersionedDiamondSubClassNrX   r   r   r   rl   rl      rf   r   rl   c                   *    e Zd ZdZd Zd Zd Z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                 j    |                      t          j        t          j                  g            dS )zM
        L{styles._aybabtu} of L{Versioned} itself is an empty list.
        N)rQ   r   _aybabtur#   r   s    r   test_aybabtuStrictEmptyz$AybabtuTests.test_aybabtuStrictEmpty   s-     	)9::B?????r   c                 `    |                      t          j        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   rp   re   r   s    r   test_aybabtuStrictSubclassz'AybabtuTests.test_aybabtuStrictSubclass   s+    
 	):;;R@@@@@r   c                 l    |                      t          j        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   rp   rj   re   r   s    r   test_aybabtuSubsubclassz$AybabtuTests.test_aybabtuSubsubclass   s/    
 	)=>>AR@STTTTTr   c                     |                      t          j        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   rp   rl   rj   re   rh   r   s    r   test_aybabtuStrictzAybabtuTests.test_aybabtuStrict   s@    
 	O455!#46MN	
 	
 	
 	
 	
r   N)r   r   r   __doc__rq   rs   ru   rw   r   r   r   rn   rn      sc         
@ @ @A A AU U U
 
 
 
 
r   rn   c                       e Zd Zd ZdS )MyEphemeralc                     || _         d S rE   ra   r   ra   s     r   r   zMyEphemeral.__init__       r   Nr   r   r   r   rz   rz      s#            r   rz   c                       e Zd Zd ZdS )EphemeralTestsc                 z   t          d          }|                     |j        t                      |                     |j        d           t	          j        |          }t	          j        |          }|                     |j        t          j                   | 	                    t          |d                     d S )N   ra   )rz   rQ   	__class__ra   r!   r"   r$   r   	EphemeralassertFalsehasattr)r   or>   s      r   test_ephemeralzEphemeralTests.test_ephemeral   s    NNk222a   QLf&6777C)))))r   N)r   r   r   r   r   r   r   r   r      s#        	* 	* 	* 	* 	*r   r   c                       e Zd Zd Zd ZdS )
Pickleablec                     || _         d S rE   r|   r}   s     r   r   zPickleable.__init__   r~   r   c                     | j         S rE   r|   r   s    r   getXzPickleable.getX   s	    vr   N)r   r   r   r   r   r   r   r   r   r      s2              r   r   c                       e Zd ZdZd ZdS )NotPickleablez)
    A class that is not pickleable.
    c                      t          d          )z9
        Raise an exception instead of pickling.
        zNot serializable.)	TypeErrorr   s    r   
__reduce__zNotPickleable.__reduce__   s     +,,,r   N)r   r   r   rx   r   r   r   r   r   r      s-         - - - - -r   r   c                       e Zd ZdZd ZdS )CopyRegisteredzb
    A class that is pickleable only because it is registered with the
    C{copyreg} module.
    c                 ,    t                      | _        dS )zE
        Ensure that this object is normally not pickleable.
        N)r   notPickleabler   s    r   r   zCopyRegistered.__init__   s     +__r   N)r   r   r   rx   r   r   r   r   r   r      s-         
- - - - -r   r   c                       e Zd ZdZdS )CopyRegisteredLoadedz2
    L{CopyRegistered} after unserialization.
    N)r   r   r   rx   r   r   r   r   r      s           r   r   c                     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   )crs    r   reduceCopyRegisteredr      s      ##r   c                       e Zd ZdZd ZdS )A
    dummy class
    c                     d S rE   r   r   s    r   amethodz	A.amethod      r   N)r   r   r   rx   r   r   r   r   r   r      -             r   r   c                       e Zd ZdZd ZdS )Br   c                     d S rE   r   r   s    r   bmethodz	B.bmethod  r   r   N)r   r   r   rx   r   r   r   r   r   r     r   r   r   c                      d S rE   r   r   r   r   funktionr     s    Dr   c                       e Zd ZdZd Zd ZdS )PicklingTestsz$Test pickling of extra object types.c                     t          j        t                    }t          j        |          }|                     |t                     d S rE   )r!   r"   r   r$   rQ   )r   r>   r   s      r   test_modulezPicklingTests.test_module  s<    V$$LF#####r   c                 $   t          d          }t          j        |j                  }t          j        |          }|                      |            d           |                     t          |          t          |j                             d S )Nr<   )r   r!   r"   r   r$   rQ   type)r   r?   r>   r   s       r   test_instanceMethodz!PicklingTests.test_instanceMethod  ss    mmSX&&La   a$sx..11111r   N)r   r   r   rx   r   r   r   r   r   r   r     s8        ..$ $ $
2 2 2 2 2r   r   c                       e Zd ZdZd ZdS )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                     d}t          j        |          }|                     |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loadeds      r   test_unpickleBytesIOz,StringIOTransitionTests.test_unpickleBytesIO*  s\    - 	 455fbk222**F33333r   N)r   r   r   rx   r   r   r   r   r   r   $  s-         
4 4 4 4 4r   r   c                       e Zd Zd ZdS )EvilSourcerorc                 N    | | _         | | j         _        || j         j        _        d S rE   )abcr}   s     r   r   zEvilSourceror.__init__9  s!    


r   Nr   r   r   r   r   r   8  s#            r   r   c                       e Zd Zd Zd ZdS )NonDictStatec                     | j         S rE   stater   s    r   __getstate__zNonDictState.__getstate__@  s
    zr   c                     || _         d S rE   r   )r   r   s     r   __setstate__zNonDictState.__setstate__C  s    


r   N)r   r   r   r   r   r   r   r   r   r   ?  s2              r   r   c                   D    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )AOTTestsc           
          ddddt          ddd          ddt          j        dz   d t          f
}t	          j        t	          j        |                    }|                     ||           d S )	Nr   g       @y              @Tr,   r   helloworld)slicesysmaxsizeEllipsisr   unjellyFromSourcejellyToSourcerQ   )r   r?   rtObjs      r   test_simpleTypeszAOTTests.test_simpleTypesH  sp    !QNNK!O
 %c&7&<&<==e$$$$$r   c                 B   t                      }t                      }|j        |_        ||_        t	          j        t	          j        |                    j        j        }|                     t	          j        |          j	        t	          j
        |                     d S rE   )r   r   r   r   r   r   r   rQ   _selfOfMethodr   _classOfMethod)r   r   r   im_s       r   test_methodSelfIdentityz AOTTests.test_methodSelfIdentityX  sz    CCCCI	#C$5a$8$899;C*3//93;Mc;R;RSSSSSr   c                 6   t                      }t                      }|j        |_        ||_        t          j        }t          `	 |                     t
          t          j        t          j        |                     |t          _        dS # |t          _        w xY 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   savedbmethods       r   test_methodNotSelfIdentityz#AOTTests.test_methodNotSelfIdentity`  s|     CCCCI	yI	%i)>@QRS@T@TUUU$AIIIAI$$$$s   8B
 
Bc                 |     G 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                   $    e Zd Zed             ZdS )2AOTTests.test_unsupportedType.<locals>.UnknownTypec                     t                      rE   )AttributeErrorr   s    r   __dict__z;AOTTests.test_unsupportedType.<locals>.UnknownType.__dict__y  s    $&&&r   N)r   r   r   propertyr   r   r   r   UnknownTyper   x  s-        ' ' X' ' 'r   r   N)r   r   r   r   )r   r   s     r   test_unsupportedTypezAOTTests.test_unsupportedTypeq  sS    	' 	' 	' 	' 	' 	' 	' 	'
 	)S%6FFFFFr   c                    t          j                    j        }d|d}dddddddd	d	z  t          t           j        |t          g}t          |          }|                    |           |                    |           |                    |           t          j        t          j        ||g                    }|d
         |d         u sJ |d         d
d         |d
d         k    sJ d S )Nr   )r   methodr   r,   r   zhe	llo

"x world!u   goodbye 
	တ world!g      ?d   r      )	r   
AOTJellier	jellyToAOr   r   tupleappendr   r   )r   ajdltujs         r   test_basicIdentityzAOTTests.test_basicIdentity  s     ^',,"'3JN
 !HH			"3#4aV#<#<==!u1~~~~!uQqSzQqsV######r   c                     t                      }d|_        t          j        t          j        |                    j        |j        k    sJ d S )Nz	meringue!)r   r   r   r   r   )r   r   s     r   test_nonDictStatezAOTTests.test_nonDictState  sD    NN$S%6q%9%9::@AGKKKKKKr   c                     t          j        t          j        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   s     r   test_copyRegzAOTTests.test_copyReg  sB    
 "3#4^5E5E#F#FGGb"677777r   c                     t          t          g                     }t          j        |          }t          j        |          }|j        |u sJ |j        j        |j        u sJ |j        |j        j        usJ d S rE   )r   r   
jellyToAOTunjellyFromAOTr   r   r   )r   r   j1ojs       r   test_funkyReferenceszAOTTests.test_funkyReferences  su    -++,,^A##trzzzztv~~~~t246!!!!!!r   c                 V   g }|df}|                     |           t          j        |          }t          j        |          }|                     |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   s        r   test_circularTuplezAOTTests.test_circularTuple  s     I	^A##beU+++beAh###Aq4(((((r   N)r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   G  s        % % % T T T% % %"G G G$ $ $6L L L
8 8 8" " ") ) ) ) )r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )CrefUtilTestsz 
    Tests for L{crefutil}.
    c                 r    t          j        i           }|                     t          |j        dd           dS )zO
        L{crefutil._DictKeyAndValue} only support keys C{0} and C{1}.
        r,   r   N)r   _DictKeyAndValuer   RuntimeError__setitem__r   r   s     r   test_dictUnknownKeyz!CrefUtilTests.test_dictUnknownKey  s6     %b)),q!<<<<<r   c                 z    t          j                    }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	  s     r   test_deferSetMultipleTimesz(CrefUtilTests.test_deferSetMultipleTimes  s<     O!,q!<<<<<r   c                     t          j        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,   r   z	ignore-meN)r   
_Containerlistr   AssertionErroraddDependant)r   	containers     r   &test_containerWhereAllElementsAreKnownz4CrefUtilTests.test_containerWhereAllElementsAreKnown  s=     '			488	.)*@"kRRRRRr   c                     |                      t          t                      j        t	          j                    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   s    r   .test_dontPutCircularReferencesInDictionaryKeysz<CrefUtilTests.test_dontPutCircularReferencesInDictionaryKeys  s?     	DFF.0A0C0CW	
 	
 	
 	
 	
r   c                     |                      t          t          j        dt          j                    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   s    r   *test_dontCallInstanceMethodsThatArentReadyz8CrefUtilTests.test_dontCallInstanceMethodsThatArentReady  sD     	$Y0A0C0CTJJ	
 	
 	
 	
 	
r   N)	r   r   r   rx   r
  r  r  r  r  r   r   r   r  r    sl         = = == = =S S S	
 	
 	
	
 	
 	
 	
 	
r   r  )%copyregr   r!   r   twisted.persistedr   r   r   twisted.trialr   twisted.trial.unittestr   r	   r#   re   rh   rj   rl   rn   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  	testCasesr   r   r   <module>r#     s    				  



 4 3 3 3 3 3 3 3 3 3 " " " " " " + + + + + +m& m& m& m& m&8 m& m& m&`	 	 	 	 	( 	 	 		 	 	 	 	f. 	 	 		 	 	 	 	, 	 	 		 	 	 	 	35L 	 	 	"
 "
 "
 "
 "
8 "
 "
 "
J    &"   

* 
* 
* 
* 
*X 
* 
* 
*       	- 	- 	- 	- 	- 	- 	- 	-
- 
- 
- 
- 
- 
- 
- 
-       	$ 	$ 	$ ~3 4 4 4              	 	 	2 2 2 2 2H 2 2 2 4 4 4 4 4h 4 4 4(              t) t) t) t) t)x t) t) t)n1
 1
 1
 1
 1
H 1
 1
 1
h >=9			r   