
    g                         d 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
Z
ddl
mZmZmZ ddlmZ d Zej"                  d        Ze G d de	j&                               Zy)	ao  Test script to find circular references.

Circular references are not leaks per se, because they will eventually
be GC'd. However, on CPython, they prevent the reference-counting fast
path from being used and instead rely on the slower full GC. This
increases memory footprint and CPU overhead, so we try to eliminate
circular references created by normal operation.
    N)webgen
httpclient)skipNotCPythonc                     fdg g t               t        t        t        |             t                |        S )a  Find circular references in a list of objects.

    The garbage list contains objects that participate in a cycle,
    but also the larger set of objects kept alive by that cycle.
    This function finds subsets of those objects that make up
    the cycle(s).
    c                    | D ]  }t        |      }|vr|	v r|v r7j                  |      d  }|j                  |       j                  |       Sj                  |       j                  |        t	        j
                  |             j                          j                  |       	j                  |        y N)idindexappendaddgcget_referentspopremove)
levelitemitem_id	candidatefoundgarbage_idsinnerstack	stack_idsvisited_idss
       Q/var/www/openai/venv/lib/python3.12/site-packages/tornado/test/circlerefs_test.pyr   z'find_circular_references.<locals>.inner"   s    DhGk)+%)#!%++d"3"56	  &Y'LLMM'""""4()IIKW%OOG$#     )setmapr
   )garbager   r   r   r   r   r   s    @@@@@@r   find_circular_referencesr!      sD    % %( "$EEIc"g&'K%K	'NLr   c               #   F  K   t        j                          t        j                          t        j                  t         j                  t         j
                  z         d 	 t        j                         } t        j                  }| t        _	        	 t        j                          |t        _	        t         j                  dd }g t         j                  dd t        |      dk(  r+	 t        j                  d       t        j                          yt        |      D ]  }| j                  d       |D ]  }| j                  dt        |              ! |D ]m  }t!        |t"        j$                        s| j                  d|j&                          | j                  dt        |              t)        j*                  |       o  ~t-        | j/                               # |t        _	        w xY w# t        j                  d       t        j                          w xY ww)zERaise AssertionError if the wrapped code creates garbage with cycles.Nr   z!
==========
 Circular 
==========z
    z	
Locals: z
Traceback: )r   disablecollect	set_debugDEBUG_STATSDEBUG_SAVEALLioStringIOsysstderrr    lenenabler!   writerepr
isinstancetypes	FrameTypef_locals	tracebackprint_stackAssertionErrorgetvalue)f
old_stderrr    circularr   s        r   assert_no_cycle_garbager;   @   sz     JJLJJLLL""2"223	 KKMZZ

	$JJL#CJ**Q- 

1w<1 	Q
		 19HGG:; &d-. ! dEOO4GGj89GGmDJ<89))$/	 !	 : QZZ\**# $CJ& 	Q
		sD   A!H!$/G3 G# (?G3 (*H!A"G3 5A.G3 #G00G3 3+HH!c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	CircleRefsTestc                 N    G fddt               | j                  t              5 }t               5   d      } d      } d      }||_        ||_        ||_        ||_        ~~d d d        d d d        | j                  dt        j                               | j                  dt        |j                               | j                  dt        |j                               | j                  d	t        |j                               y # 1 sw Y   xY w# 1 sw Y   xY w)
Nc                       e Zd Z fdZd Zy))CircleRefsTest.test_known_leak.<locals>.Cc                 >    || _         d | _        d | _        d | _        y r	   )nameabc)selfrB   Cs     r   __init__z2CircleRefsTest.test_known_leak.<locals>.C.__init__m   s     	-1-1-1r   c                      d| j                    S )Nzname=)rB   rF   s    r   __repr__z2CircleRefsTest.test_known_leak.<locals>.C.__repr__s   s    tyyk**r   N)__name__
__module____qualname__rH   rK   )rG   s   r   rG   r@   l   s    2+r   rG   rC   rD   rE   Circularz
    name=az
    name=bz
    name=c)objectassertRaisesr6   r;   rD   rE   rC   assertInstr	exceptionassertNotIn)rF   cmrC   rD   rE   rG   s        @r   test_known_leakzCircleRefsTest.test_known_leakj   s    	+ 	+ ~."(* cFcFcFq + / 	j#bll"34 	lC$56lC$56s2<<'89' +* /.s"   D7D*DD	DD$c                   K   t        j                  d|fg      }t        j                  j	                         \  }}t        j
                  j                  |      }|j                  |       t        j                         }t               5  |j                  d| d       d {    d d d        |j                          |j                          |j                          y 7 =# 1 sw Y   <xY ww)N/zhttp://127.0.0.1:)r   Applicationtornadotestingbind_unused_port
httpserver
HTTPServer
add_socketr   AsyncHTTPClientr;   fetchclosestop)rF   handler_classappsocketportserverclients          r   run_handlerzCircleRefsTest.run_handler   s     oo}%

 779##..s3&!++-$& ,,!24&:;;; ' 	 < '&s0   BC/
C##C!$C#(9C/!C##C,(C/c                 ~     G d dt         j                        }t        j                  | j	                  |             y )Nc                       e Zd Zd Zy)1CircleRefsTest.test_sync_handler.<locals>.Handlerc                 &    | j                  d       y Nok
)r.   rJ   s    r   getz5CircleRefsTest.test_sync_handler.<locals>.Handler.get   s    

6"r   NrL   rM   rN   rr    r   r   Handlerrn      s    #r   ru   r   RequestHandlerasynciorunrk   rF   ru   s     r   test_sync_handlerz CircleRefsTest.test_sync_handler   s-    	#c(( 	# 	D$$W-.r   c                 ~     G d dt         j                        }t        j                  | j	                  |             y )Nc                       e Zd Zd Zy)=CircleRefsTest.test_finish_exception_handler.<locals>.Handlerc                 ,    t        j                  d      rp   )r   FinishrJ   s    r   rr   zACircleRefsTest.test_finish_exception_handler.<locals>.Handler.get   s    jj((r   Nrs   rt   r   r   ru   r~      s    )r   ru   rv   rz   s     r   test_finish_exception_handlerz,CircleRefsTest.test_finish_exception_handler   s-    	)c(( 	) 	D$$W-.r   c                 ~     G d dt         j                        }t        j                  | j	                  |             y )Nc                   0    e Zd Zej                  d        Zy)1CircleRefsTest.test_coro_handler.<locals>.Handlerc              3   \   K   t        j                  d       | j                  d       y wNg{Gz?rq   rx   sleepr.   rJ   s    r   rr   z5CircleRefsTest.test_coro_handler.<locals>.Handler.get   s!     mmD))

6"s   *,N)rL   rM   rN   r   	coroutinerr   rt   r   r   ru   r      s    ]]# #r   ru   rv   rz   s     r   test_coro_handlerz CircleRefsTest.test_coro_handler   s-    	#c(( 	# 	D$$W-.r   c                 ~     G d dt         j                        }t        j                  | j	                  |             y )Nc                       e Zd Zd Zy)2CircleRefsTest.test_async_handler.<locals>.Handlerc                 l   K   t        j                  d       d {    | j                  d       y 7 wr   r   rJ   s    r   rr   z6CircleRefsTest.test_async_handler.<locals>.Handler.get   s)     mmD)))

6" *s   424Nrs   rt   r   r   ru   r      s    #r   ru   rv   rz   s     r   test_async_handlerz!CircleRefsTest.test_async_handler   s-    	#c(( 	#
 	D$$W-.r   c                    dd l }|j                  j                  d      5  G fddt              } |       fd}t	               5  t        j                   |              d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr      c                   L    e Zd ZW  Zej
                  j                  d        Zy)4CircleRefsTest.test_run_on_executor.<locals>.Factoryc                      y r	   rt   rJ   s    r   ry   z8CircleRefsTest.test_run_on_executor.<locals>.Factory.run   s    r   N)rL   rM   rN   executorr[   
concurrentrun_on_executorry   )thread_pools   r   Factoryr      s&    &##33  4 r   r   c                  b   K   t        d      D ]  } j                          d {     y 7 w)N   )rangery   )ifactorys    r   mainz1CircleRefsTest.test_run_on_executor.<locals>.main   s'     qA!++-'' "'s   "/-/)concurrent.futuresfuturesThreadPoolExecutorrP   r;   rx   ry   )rF   r   r   r   r   r   s       @@r   test_run_on_executorz#CircleRefsTest.test_run_on_executor   sm    
 	"2215 &   iG(
 )*DF# +! 65  +*! 65s#   (B
A6%B6A?	;BBN)
rL   rM   rN   rW   rk   r{   r   r   r   r   rt   r   r   r=   r=   h   s&     :D,////$r   r=   )__doc__rx   
contextlibr   r(   r*   r4   r1   typingunittestr[   r   r   r   tornado.test.utilr   r!   contextmanagerr;   TestCaser=   rt   r   r   <module>r      sx      	 	 
      ( ( ,$N # #N p$X&& p$ p$r   