B
    u9ay‡  ã               @   s$  d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
mZ d dlmZmZ d dlZd dlZd dlZd dlZd dlZyd dlmZ W n ek
rœ   dd„ ZY nX G dd„ deƒZG d	d
„ d
eƒZeG dd„ deƒƒZG dd„ de jƒZG dd„ de jƒZG dd„ de jƒZdd„ Ze dkr eƒ  dS )é    N)
ÚverboseÚrefcount_testÚrun_unittestÚstrip_python_stderrÚcpython_onlyÚstart_threadsÚtemp_dirÚrequires_type_collectingÚTESTFNÚunlink)Úassert_python_okÚmake_script)Úwith_tp_delc             C   s   G dd„ dt ƒ}|S )Nc               @   s   e Zd Zdd„ ZdS )zwith_tp_del.<locals>.Cc             _   s   t dƒ‚d S )Nzrequires _testcapi.with_tp_del)Ú	TypeError)ÚclsÚargsÚkwargs© r   ú/usr/lib/python3.7/test_gc.pyÚ__new__   s    zwith_tp_del.<locals>.C.__new__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚC   s   r   )Úobject)r   r   r   r   r   r      s    r   c               @   s   e Zd Zdd„ ZdS )ÚC1055820c             C   s   || _ | | _d S )N)ÚiÚloop)Úselfr   r   r   r   Ú__init__   s    zC1055820.__init__N)r   r   r   r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd„ ZdS )ÚGC_Detectorc                s(   dˆ _ ‡ fdd„}t tdƒ|¡ˆ _d S )NFc                s
   dˆ _ d S )NT)Úgc_happened)Úignored)r   r   r   Úit_happened*   s    z)GC_Detector.__init__.<locals>.it_happenediš  )r!   ÚweakrefÚrefr   Úwr)r   r#   r   )r   r   r   '   s    zGC_Detector.__init__N)r   r   r   r   r   r   r   r   r    #   s   r    c               @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚUncollectablea)  Create a reference cycle with multiple __del__ methods.

    An object in a reference cycle will never have zero references,
    and so must be garbage collected.  If one or more objects in the
    cycle have __del__ methods, the gc refuses to guess an order,
    and leaves the cycle uncollected.Nc             C   s    |d krt | d| _n|| _d S )N)Úpartner)r'   r(   )r   r(   r   r   r   r   9   s    zUncollectable.__init__c             C   s   d S )Nr   )r   r   r   r   Ú
__tp_del__>   s    zUncollectable.__tp_del__)N)r   r   r   Ú__doc__r   r)   r   r   r   r   r'   1   s   
r'   c               @   s<  e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	dd„ ƒZ
dd„ Zedd„ ƒZedd„ ƒZdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zedd „ ƒZed!d"„ ƒZd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Z ed7d8„ ƒZ!e	d9d:„ ƒZ"e	d;d<„ ƒZ#e	d=d>„ ƒZ$d?d@„ Z%dAdB„ Z&dCS )DÚGCTestsc             C   s,   g }|  |¡ t ¡  ~|  t ¡ d¡ d S )Né   )ÚappendÚgcÚcollectÚassertEqual)r   Úlr   r   r   Ú	test_listE   s
    
zGCTests.test_listc             C   s*   i }||d< t  ¡  ~|  t  ¡ d¡ d S )Nr,   )r.   r/   r0   )r   Údr   r   r   Ú	test_dictL   s
    zGCTests.test_dictc             C   s4   g }|f}|  |¡ t ¡  ~~|  t ¡ d¡ d S )Né   )r-   r.   r/   r0   )r   r1   Útr   r   r   Ú
test_tupleS   s    
zGCTests.test_tuplec             C   s2   G dd„ dƒ}||_ t ¡  ~|  t ¡ d¡ d S )Nc               @   s   e Zd ZdS )zGCTests.test_class.<locals>.AN)r   r   r   r   r   r   r   ÚA^   s   r8   r   )Úar.   r/   ÚassertNotEqual)r   r8   r   r   r   Ú
test_class]   s
    zGCTests.test_classc             C   s.   G dd„ dt ƒ}t ¡  ~|  t ¡ d¡ d S )Nc               @   s   e Zd ZdS )z%GCTests.test_newstyleclass.<locals>.AN)r   r   r   r   r   r   r   r8   f   s   r8   r   )r   r.   r/   r:   )r   r8   r   r   r   Útest_newstyleclasse   s    zGCTests.test_newstyleclassc             C   s8   G dd„ dƒ}|ƒ }||_ t ¡  ~|  t ¡ d¡ d S )Nc               @   s   e Zd ZdS )z GCTests.test_instance.<locals>.AN)r   r   r   r   r   r   r   r8   m   s   r8   r   )r9   r.   r/   r:   )r   r8   r9   r   r   r   Útest_instancel   s    zGCTests.test_instancec             C   sÀ   G dd„ dt ƒ}|ƒ }||_t ¡  ~|  t ¡ d¡ G dd„ dtƒ}G dd„ d||ƒ}|ƒ }||_t ¡  ~|  t ¡ d¡ ~~|  t ¡ d¡ |ƒ |_~|  t ¡ d¡ |  t ¡ d¡ d S )Nc               @   s   e Zd ZdS )z#GCTests.test_newinstance.<locals>.AN)r   r   r   r   r   r   r   r8   w   s   r8   r   c               @   s   e Zd ZdS )z#GCTests.test_newinstance.<locals>.BN)r   r   r   r   r   r   r   ÚB~   s   r>   c               @   s   e Zd ZdS )z#GCTests.test_newinstance.<locals>.CN)r   r   r   r   r   r   r   r   €   s   r   )r   r9   r.   r/   r:   Úlistr0   )r   r8   r9   r>   r   r   r   r   Útest_newinstanceu   s&    zGCTests.test_newinstancec             C   s2   G dd„ dƒ}|ƒ }t  ¡  ~|  t  ¡ d¡ d S )Nc               @   s   e Zd Zdd„ ZdS )zGCTests.test_method.<locals>.Ac             S   s   | j | _d S )N)r   Zinit)r   r   r   r   r   ‘   s    z'GCTests.test_method.<locals>.A.__init__N)r   r   r   r   r   r   r   r   r8      s   r8   r   )r.   r/   r:   )r   r8   r9   r   r   r   Útest_methodŽ   s
    zGCTests.test_methodc             C   s˜   t G dd„ dƒƒ}G dd„ dƒ}|ƒ }||_t|ƒ}|ƒ }||_t ¡  ~~|  t ¡ d¡ x*tjD ]}t|ƒ|krd|`P qdW |  d¡ tj 	|¡ d S )Nc               @   s   e Zd Zdd„ ZdS )z(GCTests.test_legacy_finalizer.<locals>.Ac             S   s   d S )Nr   )r   r   r   r   r)   ž   ó    z3GCTests.test_legacy_finalizer.<locals>.A.__tp_del__N)r   r   r   r)   r   r   r   r   r8   œ   s   r8   c               @   s   e Zd ZdS )z(GCTests.test_legacy_finalizer.<locals>.BN)r   r   r   r   r   r   r   r>   Ÿ   s   r>   r   z&didn't find obj in garbage (finalizer))
r   r9   ÚidÚbr.   r/   r:   ÚgarbageÚfailÚremove)r   r8   r>   r9   Úid_arD   Úobjr   r   r   Útest_legacy_finalizer˜   s"    
zGCTests.test_legacy_finalizerc             C   sœ   t G dd„ dtƒƒ}G dd„ dtƒ}|ƒ }||_t|ƒ}|ƒ }||_t ¡  ~~|  t ¡ d¡ x*tjD ]}t|ƒ|krh|`P qhW |  	d¡ tj 
|¡ d S )Nc               @   s   e Zd Zdd„ ZdS )z1GCTests.test_legacy_finalizer_newclass.<locals>.Ac             S   s   d S )Nr   )r   r   r   r   r)   ¸   rB   z<GCTests.test_legacy_finalizer_newclass.<locals>.A.__tp_del__N)r   r   r   r)   r   r   r   r   r8   ¶   s   r8   c               @   s   e Zd ZdS )z1GCTests.test_legacy_finalizer_newclass.<locals>.BN)r   r   r   r   r   r   r   r>   ¹   s   r>   r   z&didn't find obj in garbage (finalizer))r   r   r9   rC   rD   r.   r/   r:   rE   rF   rG   )r   r8   r>   r9   rH   rD   rI   r   r   r   Útest_legacy_finalizer_newclass²   s$    
z&GCTests.test_legacy_finalizer_newclassc             C   s,   i }t d|ƒ t ¡  ~|  t ¡ d¡ d S )Nzdef f(): pass
r5   )Úexecr.   r/   r0   )r   r3   r   r   r   Útest_functionÌ   s
    
zGCTests.test_functionc             C   s*   dd„ }t  ¡  |ƒ  |  t  ¡ d¡ d S )Nc              S   s   t  ¡ } d S )N)ÚsysÚ	_getframe)Úframer   r   r   Úf×   s    zGCTests.test_frame.<locals>.fr,   )r.   r/   r0   )r   rQ   r   r   r   Ú
test_frameÕ   s    zGCTests.test_framec             C   sˆ   t  ¡  |  t jg ¡ g }| |¡ t|ƒ}t  ¡ }t  |t jB ¡ ~t  ¡  t  |¡ |  t	t jƒd¡ t j 
¡ }|  t|ƒ|¡ d S )Nr,   )r.   r/   r0   rE   r-   rC   Ú	get_debugÚ	set_debugZDEBUG_SAVEALLÚlenÚpop)r   ÚLZid_LÚdebugrI   r   r   r   Útest_saveallÝ   s    


zGCTests.test_saveallc             C   sF   t  ¡ }t  ¡  t  d¡ G dd„ dƒ}|ƒ }~t  ¡  t j|Ž  d S )Nr,   c               @   s   e Zd Zdd„ ZdS )zGCTests.test_del.<locals>.Ac             S   s   t | ƒ d S )N)Údir)r   r   r   r   Ú__del__ü   s    z#GCTests.test_del.<locals>.A.__del__N)r   r   r   r[   r   r   r   r   r8   û   s   r8   )r.   Úget_thresholdÚenableÚset_thresholdÚdisable)r   Ú
thresholdsr8   r9   r   r   r   Útest_delõ   s    
zGCTests.test_delc             C   sH   t  ¡ }t  ¡  t  d¡ G dd„ dtƒ}|ƒ }~t  ¡  t j|Ž  d S )Nr,   c               @   s   e Zd Zdd„ ZdS )z$GCTests.test_del_newclass.<locals>.Ac             S   s   t | ƒ d S )N)rZ   )r   r   r   r   r[     s    z,GCTests.test_del_newclass.<locals>.A.__del__N)r   r   r   r[   r   r   r   r   r8   
  s   r8   )r.   r\   r]   r^   r   r_   )r   r`   r8   r9   r   r   r   Útest_del_newclass  s    
zGCTests.test_del_newclassc             C   sd   t  ¡  t  ¡ \}}}g }t  ¡ \}}}|  ||fd¡ |  ||fd¡ |  |d¡ |  ||¡ d S )N)r   r   é   )r.   r/   Ú	get_countr0   Z
assertLessÚassertGreater)r   r9   rD   ÚcÚxr3   ÚerQ   r   r   r   Útest_get_count  s    zGCTests.test_get_countc             C   sˆ   t  ¡  g }t  d¡ t  ¡ \}}}t  d¡ t  ¡ \}}}t  d¡ t  ¡ \}}	}
|  ||fd¡ |  ||fd¡ |  |	|
fd¡ d S )Nr   r,   r5   )r,   r   )r   r,   )r   r   )r.   r/   rd   r0   )r   rg   r9   rD   rf   r3   rh   rQ   ÚgÚhr   r   r   r   Útest_collect_generations'  s    


z GCTests.test_collect_generationsc                sž   G ‡ fdd„dƒ‰ t  ¡  d}xrtdƒD ]f}g }xt|ƒD ]}|ˆ ƒ g}q:W g }xt|ƒD ]}|ˆ ƒ g}qZW i }xt|ƒD ]}|ˆ ƒ dœ}qzW q(W t  ¡  d S )Nc                   s   e Zd ZdZ‡ fdd„ZdS )z#GCTests.test_trashcan.<locals>.Ouchr   c                s&   ˆ j d ˆ _ ˆ j d dkr"t ¡  d S )Nr,   é   r   )Únr.   r/   )r   )ÚOuchr   r   r[   ?  s    z+GCTests.test_trashcan.<locals>.Ouch.__del__N)r   r   r   rn   r[   r   )ro   r   r   ro   =  s   ro   é–   r5   )r,   r5   )r.   r]   Úranger_   )r   ÚNÚcountr6   r   ÚuÚvr   )ro   r   Útest_trashcan<  s    zGCTests.test_trashcanc          	      sâ   d‰d}dd„ ‰G ‡ ‡fdd„dt ƒ‰ ‡ ‡fdd„‰‡‡fd	d
„}t ¡ }t d¡ z\g ‰g }x&t|ƒD ]}tj|d}| |¡ qhW t|‡fdd„ƒ t	 
d¡ W d Q R X W d t |¡ X t ¡  |  tˆ jƒtˆ jƒ¡ d S )Né<   r5   c            	   s   s   z
dV  W dt  d¡ X dS )z<A generator that releases the GIL when closed or dealloc'ed.Ngíµ ÷Æ°>)ÚtimeÚsleepr   r   r   r   Úsleeper_gend  s    
z2GCTests.test_trashcan_threads.<locals>.sleeper_genc                   s.   e Zd Zg Zg Z‡ fdd„Z‡ ‡fdd„ZdS )z(GCTests.test_trashcan_threads.<locals>.Cc                s   || d d …< ˆ j  d ¡ d S )N)Úinitsr-   )r   Úalist)r   r   r   r   o  s    z1GCTests.test_trashcan_threads.<locals>.C.__init__c                s   ˆ j  d ¡ ˆƒ }t|ƒ d S )N)Údelsr-   Únext)r   rj   )r   rz   r   r   r[   r  s    z0GCTests.test_trashcan_threads.<locals>.C.__del__N)r   r   r   r{   r}   r   r[   r   )r   rz   r   r   r   k  s   r   c                 s,   ˆ g ƒ} xt ˆƒD ]}ˆ | gƒg} qW ~ dS )zyCreate a sufficiently nested container object so that the
            trashcan mechanism is invoked when deallocating it.N)rq   )rg   r   )r   ÚNESTINGr   r   Úmake_nested}  s    z2GCTests.test_trashcan_threads.<locals>.make_nestedc                  s   xˆ sˆƒ  qW dS )z!Exercise make_nested() in a loop.Nr   r   )Úexitr€   r   r   Ú
run_thread…  s    z1GCTests.test_trashcan_threads.<locals>.run_threadgñhãˆµøä>)Útargetc                  s
   ˆ   d¡S )Nr,   )r-   r   )r   r   r   Ú<lambda>’  rB   z/GCTests.test_trashcan_threads.<locals>.<lambda>g      ð?)r?   rN   ÚgetswitchintervalÚsetswitchintervalrq   Ú	threadingZThreadr-   r   rx   ry   r.   r/   r0   rU   r{   r}   )r   Z	N_THREADSr‚   Zold_switchintervalZthreadsr   r6   r   )r   r   r   r€   rz   r   Útest_trashcan_threads_  s&    
zGCTests.test_trashcan_threadsc             C   sb   G dd„ dƒ}|ƒ }|ƒ }||_ ||_ t ¡  ttjƒ}~~|  t ¡ d¡ |  ttjƒ|¡ d S )Nc               @   s   e Zd Zdd„ ZdS )zGCTests.test_boom.<locals>.Boomc             S   s   | ` t‚d S )N)ÚattrÚAttributeError)r   Úsomeattributer   r   r   Ú__getattr__›  s    z+GCTests.test_boom.<locals>.Boom.__getattr__N)r   r   r   rŒ   r   r   r   r   ÚBoomš  s   r   é   )r‰   r.   r/   rU   rE   r0   )r   r   r9   rD   Ú
garbagelenr   r   r   Ú	test_boom™  s    
zGCTests.test_boomc             C   sb   G dd„ dƒ}|ƒ }|ƒ }||_ ||_ t ¡  ttjƒ}~~|  t ¡ d¡ |  ttjƒ|¡ d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z!GCTests.test_boom2.<locals>.Boom2c             S   s
   d| _ d S )Nr   )rg   )r   r   r   r   r   ³  s    z*GCTests.test_boom2.<locals>.Boom2.__init__c             S   s$   |  j d7  _ | j dkr| `t‚d S )Nr,   )rg   r‰   rŠ   )r   r‹   r   r   r   rŒ   ¶  s    
z-GCTests.test_boom2.<locals>.Boom2.__getattr__N)r   r   r   r   rŒ   r   r   r   r   ÚBoom2²  s   r‘   rŽ   )r‰   r.   r/   rU   rE   r0   )r   r‘   r9   rD   r   r   r   r   Ú
test_boom2±  s    

zGCTests.test_boom2c             C   sd   G dd„ dt ƒ}|ƒ }|ƒ }||_||_t ¡  ttjƒ}~~|  t ¡ d¡ |  ttjƒ|¡ d S )Nc               @   s   e Zd Zdd„ ZdS )z'GCTests.test_boom_new.<locals>.Boom_Newc             S   s   | ` t‚d S )N)r‰   rŠ   )r   r‹   r   r   r   rŒ   Ñ  s    z3GCTests.test_boom_new.<locals>.Boom_New.__getattr__N)r   r   r   rŒ   r   r   r   r   ÚBoom_NewÐ  s   r“   rŽ   )r   r‰   r.   r/   rU   rE   r0   )r   r“   r9   rD   r   r   r   r   Útest_boom_newÌ  s    
zGCTests.test_boom_newc             C   sd   G dd„ dt ƒ}|ƒ }|ƒ }||_||_t ¡  ttjƒ}~~|  t ¡ d¡ |  ttjƒ|¡ d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z)GCTests.test_boom2_new.<locals>.Boom2_Newc             S   s
   d| _ d S )Nr   )rg   )r   r   r   r   r   â  s    z2GCTests.test_boom2_new.<locals>.Boom2_New.__init__c             S   s$   |  j d7  _ | j dkr| `t‚d S )Nr,   )rg   r‰   rŠ   )r   r‹   r   r   r   rŒ   å  s    
z5GCTests.test_boom2_new.<locals>.Boom2_New.__getattr__N)r   r   r   r   rŒ   r   r   r   r   Ú	Boom2_Newá  s   r•   rŽ   )r   r‰   r.   r/   rU   rE   r0   )r   r•   r9   rD   r   r   r   r   Útest_boom2_newà  s    

zGCTests.test_boom2_newc             C   sÖ   dddg}t  |¡}| ¡  |  ||¡ t|ƒ}t  |¡}| ¡  |  ||¡ dddœ}ddddg}t  |¡}| ¡  |  ||¡ t  ddgddid¡}| ¡  |  |d	d	gttdƒƒ ¡ |  t  dd
d¡g ¡ d S )Nr,   é   rc   é   )r,   rc   r5   rŽ   )r   r   r   r   r9   y              @)r.   Zget_referentsÚsortr0   Útupler?   rq   )r   r|   ZgotZatupleZadictZexpectedr   r   r   Útest_get_referentsö  s"    




zGCTests.test_get_referentsc             C   sÂ  |   t d ¡¡ |   t d¡¡ |   t d¡¡ |   t d¡¡ |   t d¡¡ |   t d¡¡ |   t d¡¡ |   t d¡¡ |   t tdƒ¡¡ |   t t¡¡ |   t t¡¡ |   t t¡¡ |   t tƒ ¡¡ G dd	„ d	ƒ}G d
d„ dtƒ}G dd„ dƒ}G dd„ dtƒ}G dd„ dtƒ}|  t t¡¡ |  t |¡¡ |  t |ƒ ¡¡ |  t |ƒ ¡¡ |  t g ¡¡ |  t t	ƒ ¡¡ |   t |ƒ ¡¡ |   t |ƒ ¡¡ |   t |ƒ ¡¡ d S )Nr,   g      ð?y      ð?      @TFó   ar9   c               @   s   e Zd ZdS )z*GCTests.test_is_tracked.<locals>.UserClassN)r   r   r   r   r   r   r   Ú	UserClass   s   r   c               @   s   e Zd ZdS )z(GCTests.test_is_tracked.<locals>.UserIntN)r   r   r   r   r   r   r   ÚUserInt#  s   rž   c               @   s   e Zd ZdZdS )z/GCTests.test_is_tracked.<locals>.UserClassSlotsr   N)r   r   r   Ú	__slots__r   r   r   r   ÚUserClassSlots'  s   r    c               @   s   e Zd ZdZdS )z/GCTests.test_is_tracked.<locals>.UserFloatSlotsr   N)r   r   r   rŸ   r   r   r   r   ÚUserFloatSlots+  s   r¡   c               @   s   e Zd ZdZdS )z-GCTests.test_is_tracked.<locals>.UserIntSlotsr   N)r   r   r   rŸ   r   r   r   r   ÚUserIntSlots/  s   r¢   )
ZassertFalser.   Z
is_trackedÚ	bytearrayÚtypeÚintr   ÚfloatÚ
assertTrueÚset)r   r   rž   r    r¡   r¢   r   r   r   Útest_is_tracked  s6    zGCTests.test_is_trackedc                sŒ   g ‰‡ ‡fdd„‰dd„ t dƒD ƒ}‡fdd„|D ƒ‰ d }t ¡  |  tˆƒd¡ d }t ¡  |  tˆƒd¡ xˆD ]}|  |d ¡ qtW d S )Nc                s   dd„ ˆ D ƒˆd d …< d S )Nc             S   s   g | ]
}|ƒ ‘qS r   r   )Ú.0r&   r   r   r   ú
<listcomp>A  s    z>GCTests.test_bug1055820b.<locals>.callback.<locals>.<listcomp>r   )r"   )ÚWRsÚouchr   r   Úcallback@  s    z*GCTests.test_bug1055820b.<locals>.callbackc             S   s   g | ]}t |ƒ‘qS r   )r   )rª   r   r   r   r   r«   C  s    z,GCTests.test_bug1055820b.<locals>.<listcomp>r5   c                s   g | ]}t  |ˆ ¡‘qS r   )r$   r%   )rª   rf   )r®   r   r   r«   D  s    r   )rq   r.   r/   r0   rU   )r   ZCsrf   rg   r   )r¬   r®   r­   r   Útest_bug1055820b<  s    
zGCTests.test_bug1055820bc                sD   t  ¡  G dd„ dƒ‰ G dd„ dƒ‰‡ ‡fdd„}|ƒ  t  ¡  d S )Nc               @   s   e Zd ZdS )z GCTests.test_bug21435.<locals>.AN)r   r   r   r   r   r   r   r8   `  s   r8   c               @   s   e Zd Zdd„ Zdd„ ZdS )z GCTests.test_bug21435.<locals>.Bc             S   s
   || _ d S )N)rg   )r   rg   r   r   r   r   d  s    z)GCTests.test_bug21435.<locals>.B.__init__c             S   s
   d | _ d S )N)r‰   )r   r   r   r   r[   g  s    z(GCTests.test_bug21435.<locals>.B.__del__N)r   r   r   r   r[   r   r   r   r   r>   c  s   r>   c                 s    ˆ ƒ } ˆˆ ƒ ƒ}|| _ | |_ d S )N)r‰   )r9   rD   )r8   r>   r   r   Údo_workj  s    
z&GCTests.test_bug21435.<locals>.do_work)r.   r/   )r   r°   r   )r8   r>   r   Útest_bug21435T  s    
zGCTests.test_bug21435c                sŠ   dd l ‰d}‡ ‡fdd„}||d ƒ}ˆ  d|¡ ˆ  d|¡ ||d ƒ}ˆ  d	|¡ ˆ  d
|kphd|k|¡ ||d ƒ}ˆ  d|¡ d S )Nr   aÚ  if 1:
            import gc
            import _testcapi
            @_testcapi.with_tp_del
            class X:
                def __init__(self, name):
                    self.name = name
                def __repr__(self):
                    return "<X %%r>" %% self.name
                def __tp_del__(self):
                    pass

            x = X('first')
            x.x = x
            x.y = X('second')
            del x
            gc.set_debug(%s)
        c                sd   ˆj tjdd| gˆjˆjd}| ¡ \}}|j ¡  |j ¡  ˆ  |j	d¡ ˆ  | 
¡ d¡ t|ƒS )Nz-Wdz-c)ÚstdoutÚstderrr   rB   )ÚPopenrN   Ú
executableÚPIPEZcommunicater²   Úcloser³   r0   Ú
returncodeÚstripr   )ÚcodeÚpr²   r³   )r   Ú
subprocessr   r   Úrun_command‰  s    


z5GCTests.test_garbage_at_shutdown.<locals>.run_commandÚ0s=   ResourceWarning: gc: 2 uncollectable objects at shutdown; uses   <X 'first'>zgc.DEBUG_UNCOLLECTABLEs8   ResourceWarning: gc: 2 uncollectable objects at shutdowns   [<X 'first'>, <X 'second'>]s   [<X 'second'>, <X 'first'>]zgc.DEBUG_SAVEALLs!   uncollectable objects at shutdown)r¼   ZassertInZassertNotInr§   )r   rº   r½   r³   r   )r   r¼   r   Útest_garbage_at_shutdownt  s    z GCTests.test_garbage_at_shutdownc             C   s(   d}t d|ƒ\}}}|  | ¡ d¡ d S )Nz¤if 1:
            class C:
                def __del__(self):
                    print('__del__ called')
            l = [C()]
            l.append(l)
            z-cs   __del__ called)r   r0   r¹   )r   rº   ÚrcÚoutÚerrr   r   r   Útest_gc_main_module_at_shutdown¥  s    
z'GCTests.test_gc_main_module_at_shutdownc          	   C   sP   t ƒ @}d}d|f }t|d|ƒ td|ƒ\}}}|  | ¡ d¡ W d Q R X d S )Nz¼if 1:
                class C:
                    def __del__(self):
                        print('__del__ called')
                l = [C()]
                l.append(l)
                zvif 1:
                import sys
                sys.path.insert(0, %r)
                import gctest
                Zgctestz-cs   __del__ called)r   r   r   r0   r¹   )r   Z
script_dirÚmodulerº   rÀ   rÁ   rÂ   r   r   r   Ú#test_gc_ordinary_module_at_shutdown³  s    
z+GCTests.test_gc_ordinary_module_at_shutdownc          	   C   sR   d}|   tt¡ ttdƒ}| |¡ W d Q R X ttƒ\}}}|  | ¡ d¡ d S )NzÊif 1:
            class ClassWithDel:
                def __del__(self):
                    print('__del__ called')
            a = ClassWithDel()
            a.link = a
            raise SystemExit(0)Úws   __del__ called)Ú
addCleanupr   r
   ÚopenÚwriter   r0   r¹   )r   rº   ZscriptrÀ   rÁ   rÂ   r   r   r   Útest_global_del_SystemExitÇ  s    z"GCTests.test_global_del_SystemExitc             C   sx  t  ¡ }|  t|ƒd¡ x^|D ]V}|  |t¡ |  t|ƒdddh¡ |  |d d¡ |  |d d¡ |  |d d¡ qW t  ¡ r”|  	t j
¡ t  ¡  t  ¡ }t  d¡ t  ¡ }|  |d d |d d d ¡ |  |d d |d d ¡ |  |d d |d d ¡ t  d¡ t  ¡ }|  |d d |d d d ¡ |  |d d |d d ¡ |  |d d |d d d ¡ d S )Nr—   Ú	collectedÚcollectionsÚuncollectabler   r,   r5   )r.   Z	get_statsr0   rU   ÚassertIsInstanceÚdictr¨   ZassertGreaterEqualÚ	isenabledrÇ   r]   r_   r/   )r   ZstatsÚstÚoldÚnewr   r   r   Útest_get_statsÖ  s.    


 
 zGCTests.test_get_statsc             C   s4   t  ¡  |  t  ¡ d¡ t  ¡  |  t  ¡ d¡ d S )Nr   )r.   Zfreezere   Zget_freeze_countZunfreezer0   )r   r   r   r   Útest_freezeð  s    zGCTests.test_freezeN)'r   r   r   r2   r4   r7   r;   r<   r=   r	   r@   rA   r   rJ   rK   rM   r   rR   rY   ra   rb   ri   rl   rv   rˆ   r   r’   r”   r–   r›   r©   r¯   r±   r¿   rÃ   rÅ   rÊ   rÔ   rÕ   r   r   r   r   r+   D   sB   
	
	#:/ 1r+   c               @   sP   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	e
dd„ ƒZdS )ÚGCCallbackTestsc             C   sL   t  ¡ | _t  ¡  t  ¡ | _t  d¡ t j | j	¡ t j | j
¡ g | _d S )Nr   )r.   rÐ   Úenabledr_   rS   rX   rT   Ú	callbacksr-   Úcb1Úcb2Úothergarbage)r   r   r   r   ÚsetUpø  s    


zGCCallbackTests.setUpc             C   s~   | ` tj | j¡ tj | j¡ t | j¡ | jr:t 	¡  t 
¡  xtjD ]}t|tƒrJd |_qJW tjd d …= | `t 
¡  d S )N)Úvisitr.   rØ   rG   rÙ   rÚ   rT   rX   r×   r]   r/   rE   Ú
isinstancer'   r(   rÛ   )r   rI   r   r   r   ÚtearDown  s    

zGCCallbackTests.tearDownc             C   sB   g | _ t ¡  tjd d … g  }tjd d …< | j |¡ g | _ d S )N)rÝ   r.   r/   rE   rÛ   r-   )r   rE   r   r   r   Úpreclean  s
    zGCCallbackTests.precleanc             C   s   | j  d|t|ƒf¡ d S )Nr,   )rÝ   r-   rÏ   )r   ÚphaseÚinfor   r   r   rÙ     s    zGCCallbackTests.cb1c             C   sj   | j  d|t|ƒf¡ |dkrft| dƒrfdd„ tjD ƒ}dd„ tjD ƒtjd d …< x|D ]
}d |_qXW d S )Nr5   ÚstopÚcleanupc             S   s   g | ]}t |tƒr|‘qS r   )rÞ   r'   )rª   rh   r   r   r   r«   $  s    z'GCCallbackTests.cb2.<locals>.<listcomp>c             S   s   g | ]}t |tƒs|‘qS r   )rÞ   r'   )rª   rh   r   r   r   r«   %  s    )rÝ   r-   rÏ   Úhasattrr.   rE   r(   )r   rá   râ   Zucrh   r   r   r   rÚ      s    
zGCCallbackTests.cb2c             C   sö   |   ¡  t ¡  dd„ | jD ƒ}dd„ |D ƒ}dd„ |D ƒ}|  |dgd ¡ |  |dgd ¡ dd„ | jD ƒ}dd„ |D ƒ}d	d„ |D ƒ}|  |d
gd ¡ |  |dgd ¡ x@| jD ]6}|d }|  d|k¡ |  d|k¡ |  d|k¡ q¸W d S )Nc             S   s   g | ]}|d  ‘qS )r   r   )rª   ru   r   r   r   r«   1  s    z0GCCallbackTests.test_collect.<locals>.<listcomp>c             S   s   g | ]}|d kr|‘qS )r,   r   )rª   r   r   r   r   r«   2  s    c             S   s   g | ]}|d kr|‘qS )r5   r   )rª   r   r   r   r   r«   3  s    r,   r5   c             S   s   g | ]}|d  ‘qS )r,   r   )rª   ru   r   r   r   r«   8  s    c             S   s   g | ]}|d kr|‘qS )Ústartr   )rª   r   r   r   r   r«   9  s    c             S   s   g | ]}|d kr|‘qS )rã   r   )rª   r   r   r   r   r«   :  s    ræ   rã   Ú
generationrË   rÍ   )rà   r.   r/   rÝ   r0   r§   )r   rn   Zn1Zn2ru   râ   r   r   r   Útest_collect*  s"    zGCCallbackTests.test_collectc             C   s>   |   ¡  t d¡ x&| jD ]}|d }|  |d d¡ qW d S )Nr5   rç   )rà   r.   r/   rÝ   r0   )r   ru   râ   r   r   r   Útest_collect_generationE  s
    
z'GCCallbackTests.test_collect_generationc             C   s  |   ¡  tƒ  tƒ  tdƒ t ¡  xD| jD ]:}|d dkr>q,|d }|  |d d¡ |  |d d¡ q,W |  ttjƒd¡ xtjD ]}|  	|t¡ q„W d	| _
g | _g tjd d …< t ¡  xD| jD ]:}|d dkrÔqÂ|d }|  |d d
¡ |  |d d¡ qÂW |  ttjƒd
¡ d S )Niš  r,   rã   r5   rË   rÍ   é   rŽ   Tr   )rà   r'   r   r.   r/   rÝ   r0   rU   rE   rÎ   rä   )r   ru   râ   rh   r   r   r   Útest_collect_garbageL  s2    z$GCCallbackTests.test_collect_garbageN)r   r   r   rÜ   rß   rà   rÙ   rÚ   rè   ré   r   rë   r   r   r   r   rÖ   ÷  s   


rÖ   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚGCTogglingTestsc             C   s   t  ¡  d S )N)r.   r]   )r   r   r   r   rÜ   r  s    zGCTogglingTests.setUpc             C   s   t  ¡  d S )N)r.   r_   )r   r   r   r   rß   u  s    zGCTogglingTests.tearDownc       
         sÚ   t dƒ}t ¡  t dƒ}||_|`t dƒ}t |¡‰ g ‰‡ ‡fdd„}t ||¡}d  } }}g }d}tƒ }x>|jsª|d7 }|dkrŽ|  	d¡ |  
tˆƒd¡ | g ¡ qnW |  
tˆƒd¡ xˆD ]}	|  
|	d ¡ qÂW d S )Nr   r,   r5   c                s   ˆ ƒ gˆd d …< d S )Nr   )r"   )Úc2wrr­   r   r   r®   ˆ  s    z2GCTogglingTests.test_bug1055820c.<locals>.callbacki'  z'gc didn't happen after 10000 iterations)r   r.   r/   Zkeep_c0_aliver   r$   r%   r    r!   rF   r0   rU   r-   )
r   Zc0Úc1Úc2r®   Zc0wrÚjunkr   Údetectorrg   r   )rí   r­   r   Útest_bug1055820cx  s.    
 

z GCTogglingTests.test_bug1055820cc       	         sÖ   g ‰G ‡ ‡fdd„dt ƒ}|dƒ}t ¡  t dƒ}||_|`t dƒ}t |¡‰ d  } }}tƒ }g }d}x>|js¦|d7 }|dkrŠ|  	d¡ |  
tˆƒd¡ | g ¡ qjW |  
tˆƒd¡ xˆD ]}|  
|d ¡ q¾W d S )Nc                   s   e Zd Z‡ ‡fdd„ZdS )z+GCTogglingTests.test_bug1055820d.<locals>.Dc                s   ˆ ƒ gˆd d …< d S )Nr   )r   )rí   r­   r   r   r[   Å  s    z3GCTogglingTests.test_bug1055820d.<locals>.D.__del__N)r   r   r   r[   r   )rí   r­   r   r   ÚDÄ  s   ró   r   r,   r5   i'  z'gc didn't happen after 10000 iterations)r   r.   r/   Zkeep_d0_aliver   r$   r%   r    r!   rF   r0   rU   r-   )	r   ró   Zd0rî   rï   rñ   rð   r   rg   r   )rí   r­   r   Útest_bug1055820d¾  s,    


z GCTogglingTests.test_bug1055820dN)r   r   r   rÜ   rß   rò   rô   r   r   r   r   rì   q  s   Frì   c           	   C   st   t  ¡ } t  ¡  t  ¡ }t  |t j @ ¡ zt  ¡  ttt	t
ƒ W d t  |¡ trZtdƒ t  ¡  | snt  ¡  X d S )Nzrestoring automatic collection)r.   rÐ   r_   rS   rT   Z
DEBUG_LEAKr/   r   r+   rì   rÖ   r   Úprintr]   )r×   rX   r   r   r   Ú	test_main  s    
rö   Ú__main__)!ZunittestZtest.supportr   r   r   r   r   r   r   r	   r
   r   Ztest.support.script_helperr   r   rN   rx   r.   r$   r‡   Z	_testcapir   ÚImportErrorr   r   r    r'   ZTestCaser+   rÖ   rì   rö   r   r   r   r   r   Ú<module>   s6   0     8z 
