
    ;eC-                     0   d dl Z d dlZd dl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	 d dl
mZ d dlmZ  G d dej                  Z G d dej                  Z G d	 d
ej                  Z G d dej                  Zedk    r ej                     dS dS )    N)support)threading_helper)assert_python_okc                   j    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zej        d             ZdS )	ClearTestz"
    Tests for frame.clear().
       c                     ddz   d S N   r    )selfxkwargss      &/usr/lib/python3.11/test/test_frame.pyinnerzClearTest.inner   s    	!    c                 V    	  | j         di | n# t          $ r}|}Y d }~nd }~ww xY w|S )Nr   )r   ZeroDivisionError)r   r   eexcs       r   outerzClearTest.outer   sU    	DJ        	 	 	CCCCCC	
s    
&!&c                 R    |$|j                                          |j        }|"dS dS )z2
        Clear all frames in a traceback.
        N)tb_framecleartb_next)r   tbs     r   clear_traceback_framesz ClearTest.clear_traceback_frames   s3     nKB nnnnnr   c                     G d d          } |            }t          j        |          }|                     |          }~t          j                     |                     d  |                       |                     |j                   t          j                     |                     d  |                       d S )Nc                       e Zd ZdS )&ClearTest.test_clear_locals.<locals>.CN__name__
__module____qualname__r   r   r   Cr    '           Dr   r%   c)	weakrefrefr   r   
gc_collectassertIsNotr   __traceback__assertIsr   r%   r(   wrr   s        r   test_clear_localszClearTest.test_clear_locals&   s    	 	 	 	 	 	 	 	ACC[^^jj1joorrtt$$$##C$5666dBBDD!!!!!r   c                 z    G d d          } |            }|                      |          }~|j        j        }|                                 |                     |j        d            |                     |j        d            |                     |j        d            |                     |j        d            d S )Nc                       e Zd ZdS )7ClearTest.test_clear_does_not_clear_specials.<locals>.CNr!   r   r   r   r%   r4   6   r&   r   r%   r'   )	r   r-   r   r   r,   f_codef_locals
f_builtins	f_globals)r   r%   r(   r   fs        r   "test_clear_does_not_clear_specialsz,ClearTest.test_clear_does_not_clear_specials5   s    	 	 	 	 	 	 	 	ACCjj1joo&				4(((T***t,,,d+++++r   c                      d fd} |            }t          |                                           |j                                                                         d S )NFc               3   P   K   	 d V                                    d d S # d w xY w)NT)r   )endlyr   s   r   gz)ClearTest.test_clear_generator.<locals>.gD   s8      

s   ! %)nextassertFalsegi_framer   
assertTrue)r   r>   genr=   s   `  @r   test_clear_generatorzClearTest.test_clear_generatorB   s    	 	 	 	 	 	 accS			r   c                 ~   	 ddz   n## t           $ r}|j        j        }Y d }~nd }~ww xY w|                     t                    5  |                                 d d d            n# 1 swxY w Y   |                     t                    5  |j                                         d d d            d S # 1 swxY w Y   d S r
   r   r-   r   assertRaisesRuntimeErrorr   f_back)r   r   r9   s      r   test_clear_executingzClearTest.test_clear_executingR   sE   	)aCCC  	) 	) 	)(AAAAAA	)|,, 	 	GGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	|,, 	 	HNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s4    
(#(A&&A*-A*B22B69B6c                      d fd} |            }t          |          }                                |                                                                 d S )NFc               3     K   	 ddz   n# t           $ r} | j        j        }                    t                    5  |                                 d d d            n# 1 swxY w Y                       t                    5  |j                                         d d d            n# 1 swxY w Y   |V  Y d } ~ nd } ~ ww xY wdd S # dw xY w)Nr   r   TrF   )r   r9   r=   r   s     r   r>   z3ClearTest.test_clear_executing_generator.<locals>.g`   s\     
!$   O,&&|44  GGIII              &&|44 % %HNN$$$% % % % % % % % % % % % % % % sr    C 
C &B;AB;A 	 B;#A 	$B;B'B;'B+	+B;.B+	/B;6C ;C  C C)r?   r@   r   rB   )r   r>   rC   r9   r=   s   `   @r   test_clear_executing_generatorz(ClearTest.test_clear_executing_generator]   sx    	 	 	 	 	 	 accII				r   c                     fdfd}g d}g  |d           |                      |           g  |d           |                      |           d S )Nc                  ~    t          j        d          }                     | j        | j        j        z
             d S )Nr   )sys	_getframeappendf_linenor5   co_firstlineno)r9   liness    r   record_linez7ClearTest.test_lineno_with_tracing.<locals>.record_lineu   s6    a  ALLAH$;;<<<<<r   c                 z                  | rdt          j        d          _                                    d S )NTr   )rP   rQ   f_trace)tracerV   s    r   testz0ClearTest.test_lineno_with_tracing.<locals>.testy   sB    KMMM 0+/a  (KMMMKMMMMMr   )r      r   FT)assertEqual)r   rZ   expected_linesrU   rV   s      @@r   test_lineno_with_tracingz"ClearTest.test_lineno_with_tracingt   s    	= 	= 	= 	= 	=	 	 	 	 	 #U///T


/////r   c                    t          j                    5   G d d          } |            }t          j        |          }|                     |          }~|                     d  |                       |                     |j                   |                     d  |                       d d d            d S # 1 swxY w Y   d S )Nc                       e Zd ZdS ))ClearTest.test_clear_refcycles.<locals>.CNr!   r   r   r   r%   ra      s        r   r%   r'   )	r   
disable_gcr)   r*   r   r,   r   r-   r.   r/   s        r   test_clear_refcycleszClearTest.test_clear_refcycles   s    !! 		& 		&       AQB**q*//CT2244(((''(9:::MM$%%%		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		& 		&s   BB::B>B>N)r   )r"   r#   r$   __doc__r   r   r   r1   r:   rD   rJ   rM   r^   r   cpython_onlyrc   r   r   r   r   r      s                " " ", , ,   	 	 	  .0 0 0( & & & & &r   r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )FrameAttrsTestc                     d }	  |             nE# t           $ r8}|j        }g }|r#|                    |j                   |j        }|#Y d }~nd }~ww xY w|S )Nc                  *    dd} fd} |            S )Nr      c                       dz   } ddz   d}d S N   r   r   	   r   ztr   s     r   r   z8FrameAttrsTest.make_frames.<locals>.outer.<locals>.inner       E!r   r   yr   r   s     @r   r   z)FrameAttrsTest.make_frames.<locals>.outer   3    AA     577Nr   )r   r-   rR   r   r   )r   r   r   r   framess        r   make_frameszFrameAttrsTest.make_frames   s    	 	 		 EGGGG  	  	  	 BF  bk***Z   	  s   
 
A.AAc                    |                                  \  }}}|j        }|                     |                    d          t          j                   |                     |ddd           |j        }|                     |ddd           d S )Nr   r   rj   )r   rt      )r   rp   )rw   r6   assertIsInstancepoptypesFunctionTyper\   )r   r9   r   r   outer_localsinner_localss         r   test_localszFrameAttrsTest.test_locals   s    **,,5%~l..w779KLLLQQ'7'7888~QQ'7'788888r   c                     |                                  \  }}}|                                 |                                 |                     |j        i            |                     |j        i            d S N)rw   r   r\   r6   r   r9   r   r   s       r   r1   z FrameAttrsTest.test_clear_locals   si    **,,5%,,,,,,,,r   c                    |                                  \  }}}|j         |j         |                                 |                                 |                     |j        i            |                     |j        i            d S r   )rw   r6   r   r\   r   s       r   test_locals_clear_localsz'FrameAttrsTest.test_locals_clear_locals   sw    **,,5%,,,,,,,,r   c                     |                                  \  }}}|                     t                    5  |`d d d            d S # 1 swxY w Y   d S r   )rw   rG   AttributeErrorrS   )r   r9   _s      r   test_f_lineno_del_segfaultz)FrameAttrsTest.test_f_lineno_del_segfault   s    ""$$1a~.. 	 	
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA
AN)r"   r#   r$   rw   r   r1   r   r   r   r   r   rg   rg      s_          &9 9 9- - -- - -    r   rg   c                       e Zd ZdZd ZdS )ReprTestz 
    Tests for repr(frame).
    c                 J   d }|j         j        }	  |             |                     d           nE# t          $ r8}|j        }g }|r#|                    |j                   |j        }|#Y d }~nd }~ww xY w|\  }}}t          j	        t          t                              }	|                     t          |          d|	|dz   fz             |                     t          |          d|	|dz   fz             |                     t          |          d|	|dz   fz             d S )	Nc                  *    dd} fd} |            S )Nr   rj   c                       dz   } ddz   d}d S rl   r   ro   s     r   r   z0ReprTest.test_repr.<locals>.outer.<locals>.inner   rr   r   r   rs   s     @r   r   z!ReprTest.test_repr.<locals>.outer   ru   r   zshould have raisedz=^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code test_repr>$   z9^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code outer>$ry   z9^<frame at 0x[0-9a-fA-F]+, file %s, line %d, code inner>$r   )__code__rT   failr   r-   rR   r   r   reescaperepr__file__assertRegex)
r   r   offsetr   r   rv   f_thisf_outerf_inner	file_reprs
             r   	test_reprzReprTest.test_repr   sq   	 	 	 .		,EGGG II*++++ ! 	  	  	 BF  bk***Z   	  $* Id8nn--	fY%v{34	5 	5 	5 	gU%vz23	4 	4 	4 	gU%vz23	4 	4 	4 	4 	4s   
1 
A3.A..A3N)r"   r#   r$   rd   r   r   r   r   r   r      s-          4  4  4  4  4r   r   c                   ~    e Zd Zd Zej        d             Zej         ej                    d                         Z	dS )TestIncompleteFrameAreInvisiblec                 `    t          j        dt          d          }t          d|           d S )Nz
            import gc

            gc.set_threshold(1,1,1)
            class GCHello:
                def __del__(self):
                    print("Destroyed from gc")

            def gen():
                yield

            fd = open(zy)
            l = [fd, GCHello()]
            l.append(l)
            del fd
            del l
            gen()
        z-c)textwrapdedentr   r   )r   codes     r   test_issue95818z/TestIncompleteFrameAreInvisible.test_issue95818   sD               $ 	t$$$$$r   c                 |  	 d }	fdd }t          j                    }t           j        d d          }t          j                    }t	          j                    }	 t          j                      |            }t          |           t          j                     t          j	        ddd           t	          j
        |           t           j                                       d 	t          j                     t          |           |                     |j        	           t          j	        |  |t           j        d d <   t	          j
        |           |rt          j                     d S d S # t          j	        |  |t           j        d d <   t	          j
        |           |rt          j                     w w xY w)Nc                     dS )z^
            Don't actually do anything, just force a frame object to be created.
            Nr   )frameeventargs      r   rY   zGTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.trace  s      r   c                 v    t          j                    j        t          j                                       dS )z
            Yo dawg, I heard you like frames, so I'm allocating a frame while
            you're allocating a frame, so you can have a frame while you have a
            frame!
            N)rP   rQ   rI   gc	callbacksremove)phaseinfocallbacksneaky_frame_objects     r   r   zJTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.callback  s0     #&-//"8L)))))r   c               3      K   	 d V  r   r   r   r   r   r9   zCTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.f  s      r   r   r   )r   get_thresholdr   	isenabledrP   gettracedisabler?   collectset_thresholdsettracerR   enabler.   rA   )
r   rY   r9   old_thresholdold_callbacksold_enabled	old_tracer>   r   r   s
           @@r   test_sneaky_frame_objectz8TestIncompleteFrameAreInvisible.test_sneaky_frame_object  s   	 	 	
		* 		* 		* 		* 		* 		*	 	 	 (**QQQlnnLNN	"	JLLLAGGG JLLLQ1%%% LL)))"&IKKKGGG MM!*&9:::m,,+BLOL### 	  m,,+BLOL### 	s   CE0 0AF;c                 R    G fdd           G fddt           j                  }d  |            }|                                 |                                 |                                0|                     j        |j        j                   j	        .d S d S )Nc                       e Zd Z fdZdS )TTestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyDelc                 .    t          j                    dS )a@  
                Stash a reference to the entire stack for walking later.

                It may look crazy, but you'd be surprised how common this is
                when using a test runner (like pytest). The typical recipe is:
                ResourceWarning + -Werror + a custom sys.unraisablehook.
                N)rP   rQ   )r   r   s    r   __del__z\TestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyDel.__del__O  s     '*moo###r   N)r"   r#   r$   r   )r   s   r   	SneakyDelr   N  s.        	6 	6 	6 	6 	6 	6 	6r   r   c                       e Zd ZdZ fdZdS )WTestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyThreadz
            A separate thread isn't needed to make this code crash, but it does
            make crashes more consistent, since it means sneaky_frame_object is
            backed by freed memory after the thread completes!
            c                                  }dS )z.Run SneakyDel.__del__ as this frame is popped.Nr   )r   r*   r   s     r   runz[TestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyThread.runa  s    ikkr   N)r"   r#   r$   rd   r   )r   s   r   SneakyThreadr   Z  s8         " " " " " " "r   r   )
	threadingThreadstartjoinassertIsNotNoner,   r5   r   r   rI   )r   r   rq   r   r   s      @@r   !test_sneaky_frame_object_teardownzATestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardownJ  s    
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6 
	6		" 		" 		" 		" 		" 		" 		"9+ 		" 		" 		" #LNN					 	0111!-#*L,<,E   #6"<	 "-----r   N)
r"   r#   r$   r   r   re   r   r   requires_working_threadingr   r   r   r   r   r      sy        % % %, < < <| 0022%= %= 32 %= %= %=r   r   __main__)r   r   rP   r   r   r|   unittestr)   rZ   r   test.supportr   test.support.script_helperr   TestCaser   rg   r   r   r"   mainr   r   r   <module>r      st   				 				 



               ) ) ) ) ) ) 7 7 7 7 7 7E& E& E& E& E&! E& E& E&P2 2 2 2 2X& 2 2 2j%4 %4 %4 %4 %4x  %4 %4 %4N~= ~= ~= ~= ~=h&7 ~= ~= ~=@ zHMOOOOO r   