o
    Pjo                     @   sV   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 ejs!J G dd deZ	dS )    N   )TestCase) fails_leakcheck_on_py314_or_lessc                   @   s@   e Zd Zdd Zdd Zdd Zedd Zd	d
 Zdd Z	dS )TestGCc                 C   sZ   t ttj }t  | d urtdt	|  | 
|  | tjtj d S )NzO IS NOT NONE.)weakrefrefgreenlet
getcurrentswitchgccollectprintsysgetrefcountassertIsNoneassertFalsegarbage)selfo r   K/home/thesage/.local/lib/python3.10/site-packages/greenlet/tests/test_gc.pytest_dead_circular_ref   s   
zTestGC.test_dead_circular_refc                 C   sP   G dd dt j }| }||_t|}t  | |  | tjtj d S )Nc                   @   s   e Zd ZdZdS )z8TestGC.test_circular_greenlet.<locals>.circular_greenletN)__name__
__module____qualname__r   r   r   r   r   circular_greenlet   s    r   )	r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   test_circular_greenlet   s   
zTestGC.test_circular_greenletc                 C   sJ   G dd dt j }| }t|}t  | |  | tjtj d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z3TestGC.test_inactive_ref.<locals>.inactive_greenletc                 S   s   t j j| | jd d S )N)run)r   __init__r   r   r   r   r   r   #   s   z<TestGC.test_inactive_ref.<locals>.inactive_greenlet.__init__c                 S      d S Nr   r   r   r   r   r   &      z7TestGC.test_inactive_ref.<locals>.inactive_greenlet.runN)r   r   r   r   r   r   r   r   r   inactive_greenlet"   s    r#   )r   r   r   r   r   r   r   r   )r   r#   r   r   r   r   test_inactive_ref!   s   
zTestGC.test_inactive_refc                    sd   G dd dt  g }t  fdd}t|}||_|| |  ~~t  t  d S )Nc                   @   s   e Zd Zdd ZdS )z:TestGC.test_finalizer_crash.<locals>.object_with_finalizerc                 S   r    r!   r   r   r   r   r   __del__B   r"   zBTestGC.test_finalizer_crash.<locals>.object_with_finalizer.__del__N)r   r   r   r%   r   r   r   r   object_with_finalizerA   s    r&   c                      s^     t  _z#z  W n t jy   td Y nw W t  `d S W t  `d S t  `w )NzGot greenlet exit!)r   r	   objectr
   GreenletExitr   r   r&   parentr   r   greenlet_bodyF   s   z2TestGC.test_finalizer_crash.<locals>.greenlet_body)r'   r   r	   arrayappendr
   r   r   )r   r,   r+   gr   r)   r   test_finalizer_crash.   s   

zTestGC.test_finalizer_crashc                 C   6   t jdk r
| d dd l}dd }||t  d S )N      Test is 3.15+ onlyr   c                   S      dS )z
            >>> import gc
            >>> from greenlet import getcurrent, greenlet, GreenletExit
            >>> def outer():
            ...     gc.collect()
            >>> outer_glet = greenlet(outer)
            >>> outer_glet.switch()
            Nr   r   r   r   r   with_doctest[       z:TestGC.test_crashing_deferred_object.<locals>.with_doctestr   version_infoskipTestdoctestrun_docstring_examplesdictr   r;   r6   r   r   r   test_crashing_deferred_objectW   s
   

	z$TestGC.test_crashing_deferred_objectc                 C   r0   )Nr1   r4   r   c                   S   r5   )a  
            >>> import gc
            >>> from greenlet import getcurrent, greenlet
            >>> class Cycle:
            ...     def __del__(self):
            ...         print("(Running finalizer)")
            >>> def collect_it():
            ...     print("Collecting garbage")
            ...     gc.collect()
            >>> def inner():
            ...     cycle1 = Cycle()
            ...     cycle2 = Cycle()
            ...     cycle1.cycle = cycle2
            ...     cycle2.cycle = cycle1
            ...     getcurrent().parent.switch()
            >>> def outer():
            ...     glet = greenlet(inner)
            ...     glet.switch()
            ...     collect_it()

            >>> outer_glet = greenlet(outer)
            >>> outer_glet.switch()
            Collecting garbage
            >>> outer_glet.dead
            True
            >>> collect_it()
            Collecting garbage
            (Running finalizer)
            (Running finalizer)
            Nr   r   r   r   r   r6   j   r7   z:TestGC.test_cycle_in_suspended_frame.<locals>.with_doctestr8   r>   r   r   r   test_cycle_in_suspended_framef   s
   

z$TestGC.test_cycle_in_suspended_frameN)
r   r   r   r   r   r$   r   r/   r?   r@   r   r   r   r   r      s    

(r   )
r   r   r   r    r   	leakcheckr   GREENLET_USE_GCr   r   r   r   r   <module>   s    
