U
    祡c:
                     @   sZ   d dl mZ d dlmZmZ d dlZd dlZdddgZG dd deZdd Z	d	d Z
dS )
    )ReferenceGraph)truncppNGarbageGraphstart_debug_garbageend_debug_garbagec                   @   s$   e Zd ZdZd	ddZd
ddZdS )r   a  
    The ``GarbageGraph`` is a ``ReferenceGraph`` that illustrates the objects
    building reference cycles. The garbage collector is switched to debug mode
    (all identified garbage is stored in `gc.garbage`) and the garbage
    collector is invoked. The collected objects are then illustrated in a
    directed graph.

    Large graphs can be reduced to the actual cycles by passing ``reduce=True``
    to the constructor.

    It is recommended to disable the garbage collector when using the
    ``GarbageGraph``.

    >>> from pympler.garbagegraph import GarbageGraph, start_debug_garbage
    >>> start_debug_garbage()
    >>> l = []
    >>> l.append(l)
    >>> del l
    >>> gb = GarbageGraph()
    >>> gb.render('garbage.eps')
    True
    FTc                 C   s8   |rt t j n
t d t   t| t j| dS )z
        Initialize the GarbageGraph with the objects identified by the garbage
        collector. If `collectable` is true, every reference cycle is recorded.
        Otherwise only uncollectable objects are reported.
        r   N)gc	set_debugZDEBUG_SAVEALLcollectr   __init__garbage)selfreduceZcollectable r   8/tmp/pip-unpacked-wheel-8ad_c8mj/pympler/garbagegraph.pyr   "   s
    
zGarbageGraph.__init__Nc              
   C   s   |s
t j}| jjdd d |dd  | jD ].}|d|j|jt|jdt|j	df  q0|d	| j
| jt| jf  d
S )z
        Log annotated garbage objects to console or file.

        :param stream: open file, uses sys.stdout if not given
        c                 S   s   | j  S )N)size)xr   r   r   <lambda>8       z*GarbageGraph.print_stats.<locals>.<lambda>)keyz%-10s %8s %-12s %-46s
)idr   typeZrepresentationz0x%08x %8d %-12s %-46s
   .   z4Garbage: %8d collected objects (%s in cycles): %12s
N)sysstdoutmetadatasortwriter   r   r   r   strcountZnum_in_cyclesr   Z
total_size)r   streamgr   r   r   print_stats0   s    



zGarbageGraph.print_stats)FT)N)__name__
__module____qualname____doc__r   r#   r   r   r   r   r      s   
c                   C   s   t   t   dS )zO
    Turn off garbage collector to analyze *collectable* reference cycles.
    N)r   r
   disabler   r   r   r   r   C   s    c                   C   s   t d t   dS )z>
    Turn garbage collection on and disable debug output.
    r   N)r   r	   enabler   r   r   r   r   K   s    
)Zpympler.refgraphr   Zpympler.util.stringutilsr   r   r   r   __all__r   r   r   r   r   r   r   <module>   s   
8