
    daf:                     b   d Z ddlZddlZddlZddlZ	 ddlmZ 	 ddlmZ ddl	m
Z
  G d d	      Z G d
 de      Ze G d de             Ze G d de             Z G d de      Z G d de      Z G d d      Z G d deej$                        Z G d d      Z G d dee      Z G d dee      Z G d dee      Z G d  d!eej$                        Z G d" d#      Z G d$ d%ee      Z G d& d'ee      Z G d( d)ee      Z G d* d+eej$                        Z G d, d-e      Ze G d. d/e             Ze G d0 d1e             Z e G d2 d3ee             Z!e
jD                   G d4 d5eej$                               Z#e$d6k(  r ejJ                          yy# e$ r d ZY w xY w# e$ r d ZY w xY w)7zB
Tests for object finalization semantics, as outlined in PEP 442.
    N)with_tp_delc                 $     G d dt               }|S )Nc                       e Zd Zd Zy)with_tp_del.<locals>.Cc                     t        d      )Nzrequires _testcapi.with_tp_del	TypeErrorclsargskwargss      1/root/Python-3.12.4/Lib/test/test_finalization.py__new__zwith_tp_del.<locals>.C.__new__   s     @AA    N__name__
__module____qualname__r    r   r   Cr      s    Br   r   )objectr   r   s     r   r   r      s    	B 	B r   )
without_gcc                      G d d      }|S )Nc                       e Zd Zd Zy)without_gc.<locals>.Cc                     t        d      )Nzrequires _testcapi.without_gcr   r
   s      r   r   zwithout_gc.<locals>.C.__new__   s     ?@@r   Nr   r   r   r   r   r      s    Ar   r   r   r   s     r   r   r      s    	A 	A r   )supportc                   x    e Zd ZdZg Zg Zg Zg ZdZdZ	e
d        Ze
ej                  d               Zd Zd Zd Zy	)
NonGCSimpleBasezd
    The base class for all the objects under test, equipped with various
    testing features.
    Fr   c                 8   | j                   j                          | j                  j                          t        j                  j                          t        j
                          | j                  j                          | j                  j                          y N)	survivorsclearerrorsgcgarbagecollect	del_callstp_del_callsr   s    r   _cleanupzNonGCSimpleBase._cleanup.   s]    







 r   c              #     K   t        j                         5  | j                  j                          | j                  j                          dt
        _        	 d | j                  r| j                  d   	 dt
        _        | j                          	 ddd       y# dt
        _        | j                          w xY w# 1 sw Y   yxY ww)zI
        A context manager to use around all finalization tests.
        FNr   T)	r   
disable_gcr)   r$   r*   r    	_cleaningr%   r,   r+   s    r   testzNonGCSimpleBase.test7   s      ! 
	MM!""$(-O%::**Q-'  -1)
	 
	 -1)
	 
	s5   C	A B=B8B=	C	B::B==CC	c                      y)z8
        Check the object is sane (non-broken).
        Nr   selfs    r   check_sanityzNonGCSimpleBase.check_sanityI       r   c                 
   	 | j                   sE| j                  j                  t        |              | j	                          | j                          yy# t        $ r%}| j                  j                  |       Y d}~yd}~ww xY w)z
        PEP 442 finalizer.  Record that this was called, check the
        object is in a sane state, and invoke a side effect.
        N)r/   r)   appendidr4   side_effect	Exceptionr%   r3   es     r   __del__zNonGCSimpleBase.__del__N   sh    
	">>%%bh/!!#  " "  	"KKq!!	"   AA 	BA==Bc                      y)z6
        A side effect called on destruction.
        Nr   r2   s    r   r9   zNonGCSimpleBase.side_effect[   r5   r   N)r   r   r   __doc__r#   r)   r*   r%   r/   	__slots__classmethodr,   
contextlibcontextmanagerr0   r4   r=   r9   r   r   r   r    r       sj    
 IILFII! !    
"r   r    c                       e Zd Zd Zd Zy)
SimpleBasec                 $    t        |       | _        y r"   )r8   id_r2   s    r   __init__zSimpleBase.__init__c   s    d8r   c                 8    | j                   t        |       k(  sJ y r"   )rH   r8   r2   s    r   r4   zSimpleBase.check_sanityf   s    xx2d8###r   N)r   r   r   rI   r4   r   r   r   rF   rF   a   s    $r   rF   c                       e Zd ZdZy)NonGCr   N)r   r   r   rA   r   r   r   rL   rL   j   s    Ir   rL   c                       e Zd ZdZd Zy)NonGCResurrectorr   c                 :    | j                   j                  |        yzF
        Resurrect self by storing self in a class-wide list.
        Nr#   r7   r2   s    r   r9   zNonGCResurrector.side_effectr        	d#r   N)r   r   r   rA   r9   r   r   r   rN   rN   n   s    I$r   rN   c                       e Zd Zy)SimpleNr   r   r   r   r   r   rT   rT   x       r   rT   c                       e Zd Zd Zy)SimpleResurrectorc                 :    | j                   j                  |        yrP   rQ   r2   s    r   r9   zSimpleResurrector.side_effect~   rR   r   Nr   r   r   r9   r   r   r   rX   rX   |   s    $r   rX   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	TestBasec                 Z    t         j                  d d  | _        g t         j                  d d  y r"   )r&   r'   old_garbager2   s    r   setUpzTestBase.setUp   s    ::a=

1r   c                     	 | j                  t        j                  g        | `t        j                          y # | `t        j                          w xY wr"   )assertEqualr&   r'   r^   r(   r2   s    r   tearDownzTestBase.tearDown   s=    	RZZ, JJL  JJLs	    9 Ac                 h    | j                  t        t        j                        t        |             y r"   )ra   sortedrF   r)   r3   idss     r   assert_del_callszTestBase.assert_del_calls   s!    
 4 45vc{Cr   c                 h    | j                  t        t        j                        t        |             y r"   )ra   rd   rF   r*   re   s     r   assert_tp_del_callszTestBase.assert_tp_del_calls   s!    
 7 78&+Fr   c                 v    | j                  t        d t        j                  D              t        |             y )Nc              3   2   K   | ]  }t        |        y wr"   r8   .0xs     r   	<genexpr>z,TestBase.assert_survivors.<locals>.<genexpr>   s     D!1D   )ra   rd   rF   r#   re   s     r   assert_survivorszTestBase.assert_survivors   s'    Dz/C/CDDfSkRr   c                 v    | j                  t        d t        j                  D              t        |             y )Nc              3   2   K   | ]  }t        |        y wr"   rl   rm   s     r   rp   z*TestBase.assert_garbage.<locals>.<genexpr>   s     :!1:rq   )ra   rd   r&   r'   re   s     r   assert_garbagezTestBase.assert_garbage   s%    :rzz::F3KHr   c                 @    t         j                  j                          y r"   )rF   r#   r$   r2   s    r   clear_survivorszTestBase.clear_survivors   s    ""$r   N)
r   r   r   r_   rb   rg   ri   rr   ru   rw   r   r   r   r\   r\      s)    DGSI%r   r\   c                   d    e Zd ZdZd Zd Zej                  d        Zej                  d        Z	y)SimpleFinalizationTestz.
    Test finalization without refcycles.
    c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  g        | j                   |       d        t        j                          | j                  |       | j                  g        d d d        y # 1 sw Y   y xY wr"   )rF   r0   rT   r8   weakrefrefr&   r(   rg   rr   assertIsr3   srf   wrs       r   test_simplez"SimpleFinalizationTest.test_simple   s    __ 	&Aa5'CQBJJL!!#&!!"%MM"$%JJL!!#&!!"%	& 	& 	&   B0CCc                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  |       | j                   |       d        | j                          t        j                          | j                  |       | j                  g        d d d        | j                          d        y # 1 sw Y   !xY wr"   )rF   r0   rX   r8   r{   r|   r&   r(   rg   rr   assertIsNotrw   r}   r~   s       r   test_simple_resurrectz,SimpleFinalizationTest.test_simple_resurrect   s    __ 	&!#Aa5'CQBJJL!!#&!!#&RT4(  "JJL!!#&!!"%	& 	bdD!	& 	&s   C C55C>c                    t         j                         5  t               }| j                  t	        j
                  |             t        |      g}~t	        j                          | j                  |       | j                  g        t	        j                          | j                  |       | j                  g        d d d        y # 1 sw Y   y xY wr"   )
rF   r0   rL   assertFalser&   
is_trackedr8   r(   rg   rr   r3   r   rf   s      r   test_non_gcz"SimpleFinalizationTest.test_non_gc   s    __ 
	&AR]]1-.a5'CJJL!!#&!!"%JJL!!#&!!"%
	& 
	& 
	&s   B(CCc                    t         j                         5  t               }| j                  t	        j
                  |             t        |      g}~t	        j                          | j                  |       | j                  |       | j                          t	        j                          | j                  |dz         | j                  |       d d d        y # 1 sw Y   y xY wN   )rF   r0   rN   r   r&   r   r8   r(   rg   rr   rw   r   s      r   test_non_gc_resurrectz,SimpleFinalizationTest.test_non_gc_resurrect   s    __ 	' "AR]]1-.a5'CJJL!!#&!!#&  "JJL!!#'*!!#&	' 	' 	's   B;CC"N)
r   r   r   r@   r   r   r   cpython_onlyr   r   r   r   r   ry   ry      sE    &"  & & ' 'r   ry   c                   (     e Zd Z fdZ fdZ xZS )SelfCycleBasec                 0    t         |           | | _        y r"   )superrI   r|   r3   	__class__s    r   rI   zSelfCycleBase.__init__   s    r   c                 B    t         |           | j                  | u sJ y r"   )r   r4   r|   r   s    r   r4   zSelfCycleBase.check_sanity   s     xx4r   )r   r   r   rI   r4   __classcell__r   s   @r   r   r      s       r   r   c                       e Zd Zy)SimpleSelfCycleNrU   r   r   r   r   r      rV   r   r   c                       e Zd Zy)SelfCycleResurrectorNrU   r   r   r   r   r      rV   r   r   c                       e Zd Zd Zy)SuicidalSelfCyclec                     d| _         y)7
        Explicitly break the reference cycle.
        N)r|   r2   s    r   r9   zSuicidalSelfCycle.side_effect   s     r   NrZ   r   r   r   r   r      s    r   r   c                   "    e Zd ZdZd Zd Zd Zy)SelfCycleFinalizationTestzX
    Test finalization of an object having a single cyclic reference to
    itself.
    c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  g        | j                   |       d        t        j                          | j                  |       | j                  g        d d d        y # 1 sw Y   y xY wr"   )rF   r0   r   r8   r{   r|   r&   r(   rg   rr   r}   r~   s       r   r   z%SelfCycleFinalizationTest.test_simple  s    __ 	&!Aa5'CQBJJL!!#&!!"%MM"$%JJL!!#&!!"%	& 	& 	&r   c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  |       | j                   |       d        | j                          t        j                          | j                  |       | j                  g        | j                   |       d        d d d        y # 1 sw Y   y xY wr"   )rF   r0   r   r8   r{   r|   r&   r(   rg   rr   r}   rw   r~   s       r   r   z/SelfCycleFinalizationTest.test_simple_resurrect  s    __ 	&$&Aa5'CQBJJL!!#&!!#&MM"$%   "JJL!!#&!!"%MM"$%!	& 	& 	&s   CC55C>c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  g        | j                   |       d        t        j                          | j                  |       | j                  g        | j                   |       d        d d d        y # 1 sw Y   y xY wr"   )rF   r0   r   r8   r{   r|   r&   r(   rg   rr   r}   r~   s       r   test_simple_suicidez-SelfCycleFinalizationTest.test_simple_suicide%  s     __ 	&!#Aa5'CQBJJL!!#&!!"%MM"$%JJL!!#&!!"%MM"$%	& 	& 	&s   CC%%C.N)r   r   r   r@   r   r   r   r   r   r   r   r      s    
&&(&r   r   c                   $     e Zd Zd Z fdZ xZS )ChainedBasec                 .    d| _         || _        | |_        y )NFsuicidedleftright)r3   r   s     r   chainzChainedBase.chain9  s    	
r   c                 N   t         |           | j                  r| j                  J | j                  J y | j                  }|j                  r|j                  J |j                  | u sJ | j                  }|j                  r|j                  J y |j                  | u sJ y r"   )r   r4   r   r   r   )r3   r   r   r   s      r   r4   zChainedBase.check_sanity>  s    ==99$$$::%%%99D}}zz)))zzT)))JJE~~zz)))zzT)))r   )r   r   r   r   r4   r   r   s   @r   r   r   7  s    
* *r   r   c                       e Zd Zy)SimpleChainedNrU   r   r   r   r   r   O  rV   r   r   c                       e Zd Zy)ChainedResurrectorNrU   r   r   r   r   r   R  rV   r   r   c                       e Zd Zd Zy)SuicidalChainedc                 .    d| _         d| _        d| _        y)r   TNr   r2   s    r   r9   zSuicidalChained.side_effectW  s     	
r   NrZ   r   r   r   r   r   U  s    r   r   c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)CycleChainFinalizationTestz
    Test finalization of a cyclic chain.  These tests are similar in
    spirit to the self-cycle tests above, but the collectable object
    graph isn't trivial anymore.
    c                     |D cg c]	  } |        }}t        t        |            D ]  }||   j                  ||dz
             |S c c}w )N   )rangelenr   )r3   classesr   nodesis        r   build_chainz&CycleChainFinalizationTest.build_chaing  sQ    ")*3**s5z" 	'A!HNN51:&	' +s   A
c                 *   t        |      }t        j                         5  | j                  |      }|D cg c]  }t	        |       }}|D cg c]  }t        j                  |       }}~t        j                          | j                  |       | j                  g        | j                  |D cg c]	  } |        c}d g|z         t        j                          | j                  |       d d d        y c c}w c c}w c c}w # 1 sw Y   y xY wr"   )r   rF   r0   r   r8   r{   r|   r&   r(   rg   rr   ra   )r3   r   Nr   r   rf   wrsr   s           r   check_non_resurrecting_chainz7CycleChainFinalizationTest.check_non_resurrecting_chainm  s    L__ 
	'$$W-E"'(Q2a5(C(+01a7;;q>1C1JJL!!#&!!"%S1rbd1D6A:>JJL!!#&
	' 
	'(1
 2
	' 
	's5   D	C:D	C?*AD	2D
 1D	:D		Dc                    t        |      }t        j                         5  | j                  |      }t        |      }|D cg c]  }t	        |       }}|D cg c]  }t        |t              st	        |        }}|D cg c]  }t        j                  |       }}~t        j                          | j                  |       | j                  |       | j                  |D cg c]	  } |        c}d g|z         | j                          t        j                          | j                  |       | j                  g        d d d        y c c}w c c}w c c}w c c}w # 1 sw Y   y xY wr"   )r   rF   r0   r   r8   
isinstancerX   r{   r|   r&   r(   rg   rr   ra   rw   )	r3   r   r   r   r   rf   survivor_idsr   r   s	            r   check_resurrecting_chainz3CycleChainFinalizationTest.check_resurrecting_chain{  s'   L__ 	&$$W-EE
A"'(Q2a5(C(+0UaJqBS4TBqEULU+01a7;;q>1C1JJL!!#&!!,/S1rbd1D6A:>  "JJL!!#&!!"%	& 	& )U1 2	& 	&sI   !E#EE#E/E<E#EAE#&E
4AE#E##E,c                 6    | j                  t        gdz         y N   )r   r   r2   s    r   test_homogenousz*CycleChainFinalizationTest.test_homogenous  s    ))=/A*=>r   c                 6    | j                  t        gdz         y r   )r   r   r2   s    r   test_homogenous_resurrectz4CycleChainFinalizationTest.test_homogenous_resurrect  s    %%'9&:Q&>?r   c                 6    | j                  t        gdz         y r   )r   r   r2   s    r   test_homogenous_suicidalz3CycleChainFinalizationTest.test_homogenous_suicidal  s    ))?*;a*?@r   c                 @    | j                  t        t        gdz         y r   r   r   r   r2   s    r   test_heterogenous_suicidal_onez9CycleChainFinalizationTest.test_heterogenous_suicidal_one  s    ))?M*JQ*NOr   c                 L    | j                  t        gdz  t        gdz  z          y r   r   r2   s    r   test_heterogenous_suicidal_twoz9CycleChainFinalizationTest.test_heterogenous_suicidal_two  s'    ))!]Oa$77	9r   c                 @    | j                  t        t        gdz         y r   )r   r   r   r2   s    r   test_heterogenous_resurrect_onez:CycleChainFinalizationTest.test_heterogenous_resurrect_one  s    %%'9=&IA&MNr   c                 J    | j                  t        t        t        gdz         y r   r   r   r   r   r2   s    r   test_heterogenous_resurrect_twoz:CycleChainFinalizationTest.test_heterogenous_resurrect_two  s     %%@1D	Fr   c                 b    | j                  t        gdz  t        gdz  z   t        gdz  z          y r   r   r2   s    r   !test_heterogenous_resurrect_threez<CycleChainFinalizationTest.test_heterogenous_resurrect_three  s6    %% 1$'::o=NQR=RR	Tr   N)r   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   `  sD    '&&?@AP9OFTr   r   c                       e Zd Zd Zd Zy)
LegacyBasec                     	 | j                   s5| j                  j                  t        |              | j	                          y y # t
        $ r%}| j                  j                  |       Y d }~y d }~ww xY wr"   )r/   r)   r7   r8   r4   r:   r%   r;   s     r   r=   zLegacyBase.__del__  s^    	" >>%%bh/!!# "  	"KKq!!	"s   A A 	A2A--A2c                 
   	 | j                   sE| j                  j                  t        |              | j	                          | j                          yy# t        $ r%}| j                  j                  |       Y d}~yd}~ww xY w)zJ
        Legacy (pre-PEP 442) finalizer, mapped to a tp_del slot.
        N)r/   r*   r7   r8   r4   r9   r:   r%   r;   s     r   
__tp_del__zLegacyBase.__tp_del__  sj    	">>!!((D2!!#  " "  	"KKq!!	"r>   N)r   r   r   r=   r   r   r   r   r   r     s    "
"r   r   c                       e Zd Zy)LegacyNrU   r   r   r   r   r         r   r   c                       e Zd Zd Zy)LegacyResurrectorc                 :    | j                   j                  |        yrP   rQ   r2   s    r   r9   zLegacyResurrector.side_effect  rR   r   NrZ   r   r   r   r   r     s    $r   r   c                       e Zd Zy)LegacySelfCycleNrU   r   r   r   r   r     r   r   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )LegacyFinalizationTestz5
    Test finalization of objects with a tp_del.
    c                     t         j                  j                          t        j                          t        |           y r"   )r&   r'   r$   r(   r   rb   r   s    r   rb   zLegacyFinalizationTest.tearDown  s)     	




r   c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  |       | j                  g        | j                   |       d        t        j                          | j                  |       | j                  |       d d d        y # 1 sw Y   y xY wr"   )rF   r0   r   r8   r{   r|   r&   r(   rg   ri   rr   r}   r~   s       r   test_legacyz"LegacyFinalizationTest.test_legacy  s    __ 	*Aa5'CQBJJL!!#&$$S)!!"%MM"$%JJL!!#&$$S)	* 	* 	*s   CCC(c                 L   t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  |       | j                  |       | j                  |       | j                   |       d        | j                          t        j                          | j                  |       | j                  |dz         | j                  |       d d d        | j                          d        y # 1 sw Y   !xY wr   )rF   r0   r   r8   r{   r|   r&   r(   rg   ri   rr   r}   rw   r~   s       r   test_legacy_resurrectz,LegacyFinalizationTest.test_legacy_resurrect  s    __ 	'!#Aa5'CQBJJL!!#&$$S)!!#&MM"$%  "JJL!!#&$$S1W-!!#&	'  	bdD!!	' 	's   C%DD#c                    t         j                         5  t               }t        |      g}t	        j
                  |      }~t        j                          | j                  g        | j                  g        | j                  g        | j                  |       | j                   |       d        d t        j                  d   _        d d d        | j                  g        | j                          d        y # 1 sw Y   2xY w)Nr   )rF   r0   r   r8   r{   r|   r&   r(   rg   ri   rr   ru   r   r'   r}   r~   s       r   test_legacy_self_cyclez-LegacyFinalizationTest.test_legacy_self_cycle  s    __ 	%!Aa5'CQBJJL!!"%$$R(!!"%$RT4( $BJJqM	% 	BbdD!	% 	%s   B4C::D)	r   r   r   r@   rb   r   r   r   r   r   s   @r   r   r     s    *"&"r   r   __main__)&r@   rC   r&   unittestr{   	_testcapir   ImportErrorr   r0   r   r    rF   rL   rN   rT   rX   r\   TestCasery   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mainr   r   r   <module>r      s    	  %$ ? ?D$ $ O   $ $ $	Z 	$
 $% %<>'Xx'8'8 >'B   	mV 		=*; 	v 7&(*;*; 7&t* *0	K 		&7 	k6 GT8+<+< GTZ" "0 	Z 	 	 $
 $ $ 	mZ 	 	 	>"Xx'8'8 >" >"B zHMMO [    s"   F F" FF"F.-F.