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dd„Zdd„ ZdS )ÚUncollectableNc             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   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.   r/   )r   Údr   r   r   Ú	test_dictL   s
    zGCTests.test_dictc             C   s4   g }|f}|  |¡ t ¡  ~~|  t ¡ d¡ d S )Né   )r,   r-   r.   r/   )r   r0   Ú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   r7   r   )Úar-   r.   ÚassertNotEqual)r   r7   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   r7   f   s   r7   r   )r   r-   r.   r9   )r   r7   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   r7   m   s   r7   r   )r8   r-   r.   r9   )r   r7   r8   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   r7   w   s   r7   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   r8   r-   r.   r9   Úlistr/   )r   r7   r8   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   r7      s   r7   r   )r-   r.   r9   )r   r7   r8   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   r7   œ   s   r7   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   r8   ÚidÚbr-   r.   r9   ÚgarbageÚfailÚremove)r   r7   r=   r8   Úid_arC   Ú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)   ¸   rA   z<GCTests.test_legacy_finalizer_newclass.<locals>.A.__tp_del__N)r   r   r   r)   r   r   r   r   r7   ¶   s   r7   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   r8   rB   rC   r-   r.   r9   rD   rE   rF   )r   r7   r=   r8   rG   rC   rH   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
r4   )Úexecr-   r.   r/   )r   r2   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.   r/   )r   rP   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.   r/   rD   r,   rB   Ú	get_debugÚ	set_debugZDEBUG_SAVEALLÚlenÚpop)r   ÚLZid_LÚdebugrH   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   rZ   r   r   r   r   r7   û   s   r7   )r-   Úget_thresholdÚenableÚset_thresholdÚdisable)r   Ú
thresholdsr7   r8   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)rY   )r   r   r   r   rZ     s    z,GCTests.test_del_newclass.<locals>.A.__del__N)r   r   r   rZ   r   r   r   r   r7   
  s   r7   )r-   r[   r\   r]   r   r^   )r   r_   r7   r8   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_countr/   Z
assertLessÚassertGreater)r   r8   rC   ÚcÚxr2   ÚerP   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+   r4   )r+   r   )r   r+   )r   r   )r-   r.   rc   r/   )r   rf   r8   rC   re   r2   rg   rP   Ú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   rZ   ?  s    z+GCTests.test_trashcan.<locals>.Ouch.__del__N)r   r   r   rm   rZ   r   )rn   r   r   rn   =  s   rn   é–   r4   )r+   r4   )r-   r\   Úranger^   )r   ÚNÚcountr5   r   ÚuÚvr   )rn   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é<   r4   c            	   s   s   z
d V  W d t  d¡ X d S )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   ri   )r   ry   r   r   rZ   r  s    z0GCTests.test_trashcan_threads.<locals>.C.__del__N)r   r   r   rz   r|   r   rZ   r   )r   ry   r   r   r   k  s   r   c                 s,   ˆ g ƒ} xt ˆƒD ]}ˆ | gƒg} qW ~ d S )N)rp   )rf   r   )r   ÚNESTINGr   r   Úmake_nested}  s    z2GCTests.test_trashcan_threads.<locals>.make_nestedc                  s   xˆ sˆƒ  qW d S )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>’  rA   z/GCTests.test_trashcan_threads.<locals>.<lambda>g      ð?)r>   rM   ÚgetswitchintervalÚsetswitchintervalrp   Ú	threadingZThreadr,   r   rw   rx   r-   r.   r/   rT   rz   r|   )r   Z	N_THREADSr   Zold_switchintervalZthreadsr   r5   r   )r   r~   r€   r   ry   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.   rT   rD   r/   )r   rŒ   r8   rC   Ú
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   )rf   )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+   )rf   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.   rT   rD   r/   )r   r   r8   rC   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.   rT   rD   r/   )r   r’   r8   rC   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   )rf   )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+   )rf   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.   rT   rD   r/   )r   r”   r8   rC   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+   é   rb   é   )r+   rb   r4   r   )r   r   r   r   r8   y              @)r-   Zget_referentsÚsortr/   Útupler>   rp   )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ó   ar8   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>r4   c                s   g | ]}t  |ˆ ¡‘qS r   )r$   r%   )r©   re   )r­   r   r   rª   D  s    r   )rp   r-   r.   r/   rT   )r   ZCsre   rf   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   r7   `  s   r7   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)rf   )r   rf   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   rZ   g  s    z(GCTests.test_bug21435.<locals>.B.__del__N)r   r   r   r   rZ   r   r   r   r   r=   c  s   r=   c                 s    ˆ ƒ } ˆˆ ƒ ƒ}|| _ | |_ d S )N)rˆ   )r8   rC   )r7   r=   r   r   Údo_workj  s    
z&GCTests.test_bug21435.<locals>.do_work)r-   r.   )r   r¯   r   )r7   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   rA   )ÚPopenrM   Ú
executableÚPIPEZcommunicater±   Úcloser²   r/   Ú
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   r/   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   r/   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   r/   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+   r4   )r-   Z	get_statsr/   rT   Ú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freezerd   Zget_freeze_countZunfreezer/   )r   r   r   r   Útest_freezeð  s    zGCTests.test_freezeN)'r   r   r   r1   r3   r6   r:   r;   r<   r	   r?   r@   r   rI   rJ   rL   r   rQ   rX   r`   ra   rh   rk   ru   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^   rR   rW   rS   Ú	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×   rF   rØ   rÙ   rS   rW   rÖ   r\   r.   rD   Ú
isinstancer'   r(   rÚ   )r   rH   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.   rD   rÚ   r,   )r   rD   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 )Nr4   ÚstopÚcleanupc             S   s   g | ]}t |tƒr|‘qS r   )rÝ   r'   )r©   rg   r   r   r   rª   $  s    z'GCCallbackTests.cb2.<locals>.<listcomp>c             S   s   g | ]}t |tƒs|‘qS r   )rÝ   r'   )r©   rg   r   r   r   rª   %  s    )rÜ   r,   rÎ   Úhasattrr-   rD   r(   )r   rà   rá   Zucrg   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©   rt   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 )r4   r   )r©   r   r   r   r   rª   3  s    r+   r4   c             S   s   g | ]}|d  ‘qS )r+   r   )r©   rt   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Ü   r/   r¦   )r   rm   Zn1Zn2rt   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 )Nr4   ræ   )rß   r-   r.   rÜ   r/   )r   rt   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â   r4   rÊ   rÌ   é   r   Tr   )rß   r'   r   r-   r.   rÜ   r/   rT   rD   rÍ   rã   )r   rt   rá   rg   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+   r4   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!   rE   r/   rT   r,   )
r   Zc0Úc1Úc2r­   Zc0wrÚjunkr   Údetectorrf   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   rZ   Å  s    z3GCTogglingTests.test_bug1055820d.<locals>.D.__del__N)r   r   r   rZ   r   )rì   r¬   r   r   ÚDÄ  s   rò   r   r+   r4   i'  z'gc didn't happen after 10000 iterations)r   r-   r.   Zkeep_d0_aliver   r$   r%   r    r!   rE   r/   rT   r,   )	r   rò   Zd0rí   rî   rð   rï   r   rf   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^   rR   rS   Z
DEBUG_LEAKr.   r   r*   rë   rÕ   r   Úprintr\   )rÖ   rW   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   rM   rw   r-   r$   r†   Z	_testcapir   ÚImportErrorr   r   r    r'   ZTestCaser*   rÕ   rë   rõ   r   r   r   r   r   Ú<module>   s6   0     8z 
