B
    u9a                  @   s   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 yd dlm	Z	 W n$ e
k
rl   d dlZdd Z	Y nX dd Zd	d
 Zdd Zdd ZdS )    N)
isabstract)support)	_get_dumpc             C   s&   t dd | jD }|| j| j| jfS )Nc             s   s   | ]}t |V  qd S )N)weakrefref).0obj r	   /usr/lib/python3.7/refleak.py	<genexpr>   s    z_get_dump.<locals>.<genexpr>)set_abc_registry
_abc_cache_abc_negative_cache_abc_negative_cache_version)clsZregistry_weakrefsr	   r	   r
   r      s    r   c       '   
      s  dd l }dd l ttds"tdt  tjd d  }|j	 }tj
	 }ydd l}W n tk
rn   d }Y nX |j	 }i }	xP fdd jjD D ]6}
t|
sqx&|
 |
g D ]}t|d |	|< qW qW dd tdd	D fd
d}| j\}}}tjtj|}|| }tt|}dg| }dg| }dg| }tj}tj}tj}d } }}| jstd|dtj d td|d d  d | tj dd t!|||||	 x|D ]}|  t!|||||	 | }| }| }| jstddtj dd ||| ||< ||| ||< ||| ||< |}|}|}qW | jsLttj d dd }dd } d}!x|d|f|d|f|d| fgD ]t\}"}#}$|"|d  }"|$|"r~d||"|#t"|"f }%t|%tj dd t#|d }&t|%|&d |&$  W d Q R X d}!q~W |!S )!Nr   gettotalrefcountz9Tracking reference leaks requires a debug build of Pythonc                s   g | ]}t  j|qS r	   )getattrabc)r   a)collectionsr	   r
   
<listcomp>1   s    zdash_R.<locals>.<listcomp>c             S   s   i | ]
}||qS r	   r	   )r   valuer	   r	   r
   
<dictcomp>;   s    zdash_R.<locals>.<dictcomp>ii  c                s     | | S )N)
setdefault)r   )int_poolr	   r
   get_pooled_int<   s    zdash_R.<locals>.get_pooled_intZ	beginningZrepetitions)fileZ
1234567890
      T)r   flush. )endr   r    c             S   s   t dd | D S )Nc             s   s   | ]}|d kV  qdS )r   Nr	   )r   Zdeltar	   r	   r
   r   |   s    z2dash_R.<locals>.check_rc_deltas.<locals>.<genexpr>)all)deltasr	   r	   r
   check_rc_deltaso   s    zdash_R.<locals>.check_rc_deltasc             S   s   t | S )N)any)r%   r	   r	   r
   check_fd_deltas~   s    zdash_R.<locals>.check_fd_deltasFZ
referenceszmemory blockszfile descriptorsz%s leaked %s %s, sum=%sr   )%copyregcollections.abchasattrsys	Exceptionwarm_cacheswarningsfiltersdispatch_tablecopypath_importer_cache	zipimportImportError_zip_directory_cacher   __all__r   __subclasses__r   rangeZ
huntrleaksospathjoinr   ZSAVEDCWDlistgetallocatedblocksr   fd_countquietprintstderrdash_R_cleanupsumopenr    )'nsZ	test_nameZ	test_funcr)   fspspicr4   zdcabcsr   r   r   ZnwarmupZntrackedZfnameZrepcountZ	rep_rangeZ	rc_deltasZalloc_deltasZ	fd_deltasr>   r   r?   Z	rc_beforeZalloc_beforeZ	fd_beforeiZalloc_afterZrc_afterZfd_afterr&   r(   Zfailedr%   Z	item_nameZcheckermsgZrefrepr	   )r   r   r
   dash_R   s    











rN   c                s  dd l }dd l | tjd d < |j  |j| tj  tj| ydd l	}W n t
k
rj   Y nX |j  |j| t   fdd jjD }tt|}xZ|D ]R}xL| |g D ]:}	x,||	t D ]}
|
 d k	r|	|
  qW |	  qW qW t  d S )Nr   c                s   g | ]}t  j|qS r	   )r   r   )r   r   )r   r	   r
   r      s    z"dash_R_cleanup.<locals>.<listcomp>)r)   r*   r/   r0   r1   clearupdater,   r3   r4   r5   r6   _clear_type_cacher   r7   filterr   r8   getr   register_abc_caches_clearclear_caches)rG   rH   rI   rJ   rK   r)   r4   Zabs_classesr   r   r   r	   )r   r
   rC      s.    





rC   c              C   sl  x t j D ]} t| dr| `qW x,t jt jt jt jfD ]}|d k	r8|	  q8W yt jd }W n t
k
rr   Y nX |j  t  yt jd }W n t
k
r   Y nX |j  yt jd }W n t
k
r   Y n
X |  yt jd }W n t
k
r   Y n
X |  yt jd }W n t
k
r2   Y n
X |  yt jd }W n t
k
r`   Y n
X |  yt jd }W n t
k
r   Y nX |j  yt jd	 }	W n t
k
r   Y n
X |	  yt jd
 }
W n t
k
r   Y nX d |
_yt jd }W n t
k
r   Y n
X |  yt jd }W n t
k
rF   Y nX x|jD ]}|  qPW t  d S )N__warningregistry__zdistutils.dir_util	_strptimezurllib.parsezurllib.request	linecache	mimetypesfilecmpstructdoctestctypestyping)r,   modulesvaluesr+   rW   stdoutrB   
__stdout__
__stderr__r    KeyErrorZ_path_createdrO   repurgeZ_regex_cacheZclear_cacheZ
urlcleanup
clearcacheZ_default_mime_types_cache_clearcacheZmasterZ_reset_cacheZ	_cleanupsr   Z
gc_collect)modstreamZdistutils_dir_utilrX   Zurllib_parseZurllib_requestrY   rZ   r[   r\   r]   r^   r_   fr	   r	   r
   rV      s    



rV   c              C   sR   t td} x tdD ]}| ||d   qW dd tdD  ttdd d S )N   r   c             S   s   g | ]}t |qS r	   )chr)r   rL   r	   r	   r
   r     s    zwarm_caches.<locals>.<listcomp>i  )bytesr9   r=   )srL   r	   r	   r
   r.     s
    r.   )r:   rf   r,   r/   inspectr   Ztestr   _abcr   r5   r   rN   rC   rV   r.   r	   r	   r	   r
   <module>   s    "`