
    daf-8                        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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j,                  e	du d       G d dej"                               Zedk(  r ej2                          yy# e
$ r dZ	Y w xY w)    N)support)threading_helper)assert_python_okc                   l    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y)	ClearTestz"
    Tests for frame.clear().
    c                     ddz   y N   r    )selfxkwargss      */root/Python-3.12.4/Lib/test/test_frame.pyinnerzClearTest.inner   s	    	!    c                 ^    	  | j                   di | S # t        $ r}|}Y d }~|S d }~ww xY w)Nr   )r   ZeroDivisionError)r   r   eexcs       r   outerzClearTest.outer   s;    	DJJ   
 ! 	C
	s    	,',c                 \    |*|j                   j                          |j                  }|)yy)z2
        Clear all frames in a traceback.
        N)tb_framecleartb_next)r   tbs     r   clear_traceback_framesz ClearTest.clear_traceback_frames#   s)     nKKB nr   c                 X    G d d      } |       }t        j                  |      }| j                  |      }~t        j                          | j                  d  |              | j                  |j                         t        j                          | j                  d  |              y )Nc                       e Zd Zy)&ClearTest.test_clear_locals.<locals>.CN__name__
__module____qualname__r   r   r   Cr   ,       r   r$   c)	weakrefrefr   r   
gc_collectassertIsNotr   __traceback__assertIsr   r$   r'   wrr   s        r   test_clear_localszClearTest.test_clear_locals+   s    	 	C[[^jj1jort$##C$5$56dBD!r   c                 x    G d d      } |       }| j                  |      }~|j                  j                  }|j                          | j	                  |j
                  d        | j	                  |j                  d        | j	                  |j                  d        | j	                  |j                  d        y )Nc                       e Zd Zy)7ClearTest.test_clear_does_not_clear_specials.<locals>.CNr    r   r   r   r$   r3   ;   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_specials:   s    	 	Cjj1jo&&		4(T*t,d+r   c                      d fd} |       }t        |        j                         |j                  j                           j	                         y )NFc               3   J   K   	 d  j                          d y # d w xY ww)NT)r   )endlyr   s   r   gz)ClearTest.test_clear_generator.<locals>.gI   s"     

s   # # #)nextassertFalsegi_framer   
assertTrue)r   r=   genr<   s   `  @r   test_clear_generatorzClearTest.test_clear_generatorG   sG    	 cS	r   c                 d   	 ddz   | j                  t              5  j                          d d d        | j                  t              5  j                  j                          d d d        y # t         $ r }|j                  j                  }Y d }~d }~ww xY w# 1 sw Y   nxY w# 1 sw Y   y xY wr	   r   r,   r   assertRaisesRuntimeErrorr   f_back)r   r   r8   s      r   test_clear_executingzClearTest.test_clear_executingW   s    	)aC |, 	GGI	|, 	HHNN	 		 ! 	)((A	)	 		 	s.   A. B
B&.	B7BBB#&B/c                      d fd} |       }t        |      } j                         |j                           j                         y )NFc               3     K   	 ddz   dy # t         $ r} | j                  j                  }j                  t              5  |j                          d d d        n# 1 sw Y   nxY wj                  t              5  |j                  j                          d d d        n# 1 sw Y   nxY w| Y d } ~ dy d } ~ ww xY w# dw xY ww)Nr
   r   TrE   )r   r8   r<   r   s     r   r=   z3ClearTest.test_clear_executing_generator.<locals>.ge   s     
!  % OO,,&&|4 GGI  &&|4 %HHNN$% % % sl   C C	B<+B7A	B7A$	 B7<B 	B7 B)	%B70B? 4C7B<<B? ?CC)r>   r?   r   rA   )r   r=   rB   r8   r<   s   `   @r   test_clear_executing_generatorz(ClearTest.test_clear_executing_generatorb   sB    	 cI		r   c                     fdfd}g d}g  |d       | j                  |       g  |d       | j                  |       y )Nc                      t        j                  d      } j                  | j                  | j                  j
                  z
         y )Nr
   )sys	_getframeappendf_linenor4   co_firstlineno)r8   liness    r   record_linez7ClearTest.test_lineno_with_tracing.<locals>.record_linez   s0    a ALLAHH$;$;;<r   c                 h             | rdt        j                  d      _                          y )NTr   )rO   rP   f_trace)tracerU   s    r   testz0ClearTest.test_lineno_with_tracing.<locals>.test~   s&    M+/a (MMr   )r
         FT)assertEqual)r   rY   expected_linesrT   rU   s      @@r   test_lineno_with_tracingz"ClearTest.test_lineno_with_tracingy   sL    	=	 #U/T
/r   c                 Z   t        j                         5   G d d      } |       }t        j                  |      }| j	                  |      }~| j                  d  |              | j                  |j                         | j                  d  |              d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zy))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T24(''(9(9:MM$%		& 		& 		&s   BB!!B*N)r[   )r!   r"   r#   __doc__r   r   r   r0   r9   rC   rI   rL   r^   r   cpython_onlyrc   r   r   r   r   r      sN    ", 	.0( & &r   r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)FrameAttrsTestc                     d }	  |        S # t         $ rE}|j                  }g }|r*|j                  |j                         |j                  }|r*Y d }~|S d }~ww xY w)Nc                  $    dd} fd} |       S )Nr[      c                      dz   } ddz   d}y 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       AA 7Nr   )r   r,   rQ   r   r   )r   r   r   r   framess        r   make_frameszFrameAttrsTest.make_frames   sa    		 G  ! 	 BFbkk*ZZ  	 s    	A9AAc                    | j                         \  }}}|j                  }| j                  |j                  d      t        j
                         | j                  |ddd       |j                  }| j                  |ddd       y )Nr   r[   rj   )r   rt      )r   rp   )rw   r5   assertIsInstancepoptypesFunctionTyper\   )r   r8   r   r   outer_localsinner_localss         r   test_localszFrameAttrsTest.test_locals   ss    **,5%~~l..w79K9KLQQ'78~~QQ'78r   c                     | j                         \  }}}|j                          |j                          | j                  |j                  i        | j                  |j                  i        y N)rw   r   r\   r5   r   r8   r   r   s       r   r0   z FrameAttrsTest.test_clear_locals   sP    **,5%,,r   c                    | j                         \  }}}|j                   |j                   |j                          |j                          | j                  |j                  i        | j                  |j                  i        y r   )rw   r5   r   r\   r   s       r   test_locals_clear_localsz'FrameAttrsTest.test_locals_clear_locals   s^    **,5%,,r   c                     | j                         \  }}}| j                  t              5  |`d d d        y # 1 sw Y   y xY wr   )rw   rF   AttributeErrorrR   )r   r8   _s      r   test_f_lineno_del_segfaultz)FrameAttrsTest.test_f_lineno_del_segfault   s>    ""$1a~. 	
	 	 	s   6?N)r!   r"   r#   rw   r   r0   r   r   r   r   r   rg   rg      s    &9--r   rg   c                       e Zd ZdZd Zy)ReprTestz 
    Tests for repr(frame).
    c                 *   d }|j                   j                  }	  |        | j                  d       \  }}}t        j                  t        t                    }	| j                  t        |      d|	|dz   fz         | j                  t        |      d|	|dz   fz         | j                  t        |      d|	|dz   fz         y # t        $ rD}|j                  }g }|r*|j                  |j                         |j                  }|r*Y d }~d }~ww xY w)	Nc                  $    dd} fd} |       S )Nr[   rj   c                      dz   } ddz   d}y 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__rS   failr   r,   rQ   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   s   	 ..		,G II*+#) IId8n-	fY%v{34	5 	gU%vz23	4 	gU%vz23	4# ! 	 BFbkk*ZZ 	 s   C 	D9DDN)r!   r"   r#   rd   r   r   r   r   r   r      s     4r   r   c                       e Zd Zd Zej
                  d        Zej
                   ej                         d               Z	d Z
y)TestIncompleteFrameAreInvisiblec                 V    t        j                  dt        d      }t        d|       y )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   s2     $  l #	  $ 	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                  j                         d 	t        j                          t        |       | j                  |j                  	       t        j                  |  |t         j                  d d  t	        j                  |       |rt        j                          y y # t        j                  |  |t         j                  d d  t	        j                  |       |rt        j                          w w xY w)Nc                      y)z^
            Don't actually do anything, just force a frame object to be created.
            Nr   )frameeventargs      r   rX   zGTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.trace  s    r   c                     t        j                         j                  j                  t        j                  j                         y)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)rO   rP   rH   gc	callbacksremove)phaseinfocallbacksneaky_frame_objects     r   r   zJTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.callback  s/     #&--/"8"8"?"?LL)r   c               3      K   	 d  wr   r   r   r   r   r8   zCTestIncompleteFrameAreInvisible.test_sneaky_frame_object.<locals>.f#  s      s   	r
   r   )r   get_thresholdr   	isenabledrO   gettracedisabler>   collectset_thresholdsettracerQ   enabler-   r@   )
r   rX   r8   old_thresholdold_callbacksold_enabled	old_tracer=   r   r   s
           @@r   test_sneaky_frame_objectz8TestIncompleteFrameAreInvisible.test_sneaky_frame_object  s=   	
		*	 ((*QllnLLN	"	JJLAG JJLQ1% LLLL)"&IIKG MM!**&9:m,+BLLOLL#		  m,+BLLOLL#		 s   CE3 3AGc                 N    G fdd       G fddt         j                        }d  |       }|j                          |j                          | j	                         @| j                  j                  |j                  j                         j                  ?y y )Nc                       e Zd Z fdZy)TTestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyDelc                 .    t        j                         y)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rO   rP   )r   r   s    r   __del__z\TestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyDel.__del__T  s     '*mmo#r   N)r!   r"   r#   r   )r   s   r   	SneakyDelr   S  s    	6r   r   c                       e Zd ZdZ fdZy)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                             }y)z.Run SneakyDel.__del__ as this frame is popped.Nr   )r   r)   r   s     r   runz[TestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardown.<locals>.SneakyThread.runf  s
    kr   N)r!   r"   r#   rd   r   )r   s   r   SneakyThreadr   _  s    "r   r   )
	threadingThreadstartjoinassertIsNotNoner+   r4   r   r   rH   )r   r   rq   r   r   s      @@r   !test_sneaky_frame_object_teardownzATestIncompleteFrameAreInvisible.test_sneaky_frame_object_teardownO  s    
	6 
	6		"9++ 		" #N			 	01!-#**L,<,<,E,E #6"<"<	 "-r   c                      G d d      fd}t        j                         5 }t        j                  |      }| j	                  |j
                  j                  t               d d d        | j                                 y # 1 sw Y    xY w)Nc                       e Zd ZdZy)ZTestIncompleteFrameAreInvisible.test_entry_frames_are_invisible_during_teardown.<locals>.CzA weakref'able class.N)r!   r"   r#   rd   r   r   r   r$   r   y  s    'r   r$   c                  F            } t        j                  | t              S )z>Try to find globals and locals as this frame is being cleared.)r(   r)   exec)r)   r$   s    r   r8   zZTestIncompleteFrameAreInvisible.test_entry_frames_are_invisible_during_teardown.<locals>.f|  s    #C
 ;;sD))r   )	r   catch_unraisable_exceptionoperatorcallr-   
unraisableexc_type	TypeErrorassertIsNone)r   r8   catcherweakr$   s       @r   /test_entry_frames_are_invisible_during_teardownzOTestIncompleteFrameAreInvisible.test_entry_frames_are_invisible_during_teardownx  su    	( 	(	* //1 	BW==#DMM',,55yA		B
 	$&!	B 	Bs   A BBN)r!   r"   r#   r   r   re   r   r   requires_working_threadingr   r   r   r   r   r   r      sV    %, < <| 0002%= 3 %=N"r   r   zneed _testcapic                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)TestCAPIc                 *    t        j                         S r   r   r   s    r   getframezTestCAPI.getframe  s    }}r   c                    | j                         }| j                  |j                  t        j                  |             | j                  |j                  t        j                  |             | j                  |j                  t        j                  |             | j                  |j                  t        j                  |             y r   )r   r\   r5   	_testcapiframe_getlocalsr-   r7   frame_getglobalsr6   frame_getbuiltinsf_lastiframe_getlasti)r   r   s     r   test_frame_getterszTestCAPI.test_frame_getters  s    )B)B5)IJeooy'A'A%'HIe&&	(C(CE(JK	(@(@(GHr   c                    t        j                         }d}| j                  t        j                  |d      d       | j                  t        j
                  |d      d       | j                  t              5  t        j                  |d       d d d        | j                  t              5  t        j
                  |d       d d d        | j                  t              5  t        j                  |d       d d d        | j                  t              5  t        j                  |d       d d d        y # 1 sw Y   xY w# 1 sw Y   ~xY w# 1 sw Y   VxY w# 1 sw Y   y xY w)Nr
   r      xrt      y{   )	rO   rP   r\   r   frame_getvarframe_getvarstringrF   	NameErrorr   )r   current_framer   s      r   test_getvarzTestCAPI.test_getvar  s   //sCQG55mTJANy) 	7""=#6	7y) 	>((=	> y) 	8""=$7	8y) 	7""=#6	7 	7	7 	7	> 	>	8 	8	7 	7s0   8D4,E  EE4D= E	EE!c              #   8   K   t        j                          y wr   r   r   s    r   getgenframezTestCAPI.getgenframe  s     mmos   c                     | j                         }t        |      }| j                  |t        j                  |             y r   )r   r>   r-   r   frame_getgenerator)r   rB   r   s      r   test_frame_get_generatorz!TestCAPI.test_frame_get_generator  s2     S	c977>?r   c                     d }t        j                  |j                  t               t	                     }| j                  |j                         y)zzTest that accessing `f_back` does not cause a segmentation fault on
        a frame created with `PyFrame_New` (GH-99110).c                       y r   r   r   r   r   dummyz,TestCAPI.test_frame_fback_api.<locals>.dummy  s    r   N)r   	frame_newr   globalslocalsr   rH   )r   r   r   s      r   test_frame_fback_apizTestCAPI.test_frame_fback_api  s7    	 ##ENNGIvxH%,,'r   N)	r!   r"   r#   r   r   r   r   r   r  r   r   r   r   r     s"    I7 @
(r   r   __main__)r   r   r   rO   r   r   r|   unittestr(   r   ImportErrorrY   r   test.supportr   test.support.script_helperr   TestCaser   rg   r   r   skipIfr   r!   mainr   r   r   <module>r     s    	  	 
       ) 7E&!! E&P2X&& 2j%4x   %4NR"h&7&7 R"h d"$45+(x   +( 6+(Z zHMMO ]  Is   C CC