
[[\#=                 @   s`  d  d l  m Z m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Td  d l
 m Z m Z d d   Z Gd d	   d	  Z Gd
 d   d  Z d d   Z e Gd d   d   Z e Gd d   d e   Z d d   Z Gd d   d  Z Gd d   d e  Z d d   Z Gd d   d  Z Gd d   d  Z d d   Z d S)     )SequenceIterable)total_orderingN)*)_get_object_traceback_get_tracesc             C   s   x d D] } t  |   d k  rP | d k rP | r? d |  | f Sd |  | f Sn  t  |   d k  sn | d k r | r d |  | f Sd |  | f Sn  |  d
 }  q Wd  S)NBKiBMiBGiBTiBd   z%+.1f %sz%.1f %s
   i   z%+.0f %sz%.0f %s)r   r	   r
   r   r   i (  )abs)sizeZsignZunit r   !/usr/lib/python3.4/tracemalloc.py_format_size   s    r   c               @   sd   e  Z d  Z d Z d Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d S)	StatisticzS
    Statistic difference on memory allocations between two Snapshot instance.
    	tracebackr   countc             C   s   | |  _  | |  _ | |  _ d  S)N)r   r   r   )selfr   r   r   r   r   r   __init__%   s    		zStatistic.__init__c             C   s   t  |  j |  j |  j f  S)N)hashr   r   r   )r   r   r   r   __hash__*   s    zStatistic.__hash__c             C   s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S)N)r   r   r   )r   otherr   r   r   __eq__-   s    zStatistic.__eq__c             C   s\   d |  j  t |  j d  |  j f } |  j rX |  j |  j } | d t | d  7} n  | S)Nz%s: size=%s, count=%iFz, average=%s)r   r   r   r   )r   textaverager   r   r   __str__2   s    	zStatistic.__str__c             C   s   d |  j  |  j |  j f S)Nz)<Statistic traceback=%r size=%i count=%i>)r   r   r   )r   r   r   r   __repr__<   s    zStatistic.__repr__c             C   s   |  j  |  j |  j f S)N)r   r   r   )r   r   r   r   	_sort_key@   s    zStatistic._sort_keyN)z	tracebackzsizezcount)__name__
__module____qualname____doc__	__slots__r   r   r   r   r    r!   r   r   r   r   r      s   
r   c               @   sd   e  Z d  Z d Z d Z d d   Z d	 d
   Z d d   Z d d   Z d d   Z	 d d   Z
 d S)StatisticDiffzd
    Statistic difference on memory allocations between an old and a new
    Snapshot instance.
    r   r   	size_diffr   
count_diffc             C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S)N)r   r   r(   r   r)   )r   r   r   r(   r   r)   r   r   r   r   K   s
    				zStatisticDiff.__init__c             C   s(   t  |  j |  j |  j |  j |  j f  S)N)r   r   r   r(   r   r)   )r   r   r   r   r   R   s    zStatisticDiff.__hash__c             C   sX   |  j  | j  k oW |  j | j k oW |  j | j k oW |  j | j k oW |  j | j k S)N)r   r   r(   r   r)   )r   r   r   r   r   r   V   s
    zStatisticDiff.__eq__c             C   sq   d |  j  t |  j d  t |  j d  |  j |  j f } |  j rm |  j |  j } | d t | d  7} n  | S)Nz %s: size=%s (%s), count=%i (%+i)FTz, average=%s)r   r   r   r(   r   r)   )r   r   r   r   r   r   r   ]   s    	zStatisticDiff.__str__c             C   s&   d |  j  |  j |  j |  j |  j f S)Nz9<StatisticDiff traceback=%r size=%i (%+i) count=%i (%+i)>)r   r   r(   r   r)   )r   r   r   r   r    i   s    zStatisticDiff.__repr__c             C   s.   t  |  j  |  j t  |  j  |  j |  j f S)N)r   r(   r   r)   r   r   )r   r   r   r   r!   n   s    zStatisticDiff._sort_keyN)z	tracebackzsizez	size_diffzcountz
count_diff)r"   r#   r$   r%   r&   r   r   r   r   r    r!   r   r   r   r   r'   D   s   r'   c             C   s   g  } x | j    D] \ } } |  j | d   } | d  k	 rr t | | j | j | j | j | j | j  } n$ t | | j | j | j | j  } | j |  q WxG |  j    D]9 \ } } t | d | j d | j  } | j |  q W| S)Nr   )itemspopr'   r   r   append)	old_group	new_group
statisticsr   statZpreviousr   r   r   _compare_grouped_statst   s     r1   c               @   s   e  Z d  Z d Z 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 S)Framez
    Frame of a traceback.
    _framec             C   s   | |  _  d  S)N)r3   )r   framer   r   r   r      s    zFrame.__init__c             C   s   |  j  d S)Nr   )r3   )r   r   r   r   filename   s    zFrame.filenamec             C   s   |  j  d S)N   )r3   )r   r   r   r   lineno   s    zFrame.linenoc             C   s   |  j  | j  k S)N)r3   )r   r   r   r   r   r      s    zFrame.__eq__c             C   s   |  j  | j  k  S)N)r3   )r   r   r   r   r   __lt__   s    zFrame.__lt__c             C   s   t  |  j  S)N)r   r3   )r   r   r   r   r      s    zFrame.__hash__c             C   s   d |  j  |  j f S)Nz%s:%s)r5   r7   )r   r   r   r   r      s    zFrame.__str__c             C   s   d |  j  |  j f S)Nz<Frame filename=%r lineno=%r>)r5   r7   )r   r   r   r   r       s    zFrame.__repr__N)z_frame)r"   r#   r$   r%   r&   r   propertyr5   r7   r   r8   r   r   r    r   r   r   r   r2      s   r2   c               @   s   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d d  Z d S)	Tracebackz`
    Sequence of Frame instances sorted from the most recent frame
    to the oldest frame.
    _framesc             C   s   t  j |   | |  _ d  S)N)r   r   r;   )r   framesr   r   r   r      s    zTraceback.__init__c             C   s   t  |  j  S)N)lenr;   )r   r   r   r   __len__   s    zTraceback.__len__c             C   sB   t  | t  r- t d d   |  j | D  St |  j |  Sd  S)Nc             s   s   |  ] } t  |  Vq d  S)N)r2   ).0tracer   r   r   	<genexpr>   s    z(Traceback.__getitem__.<locals>.<genexpr>)
isinstanceslicetupler;   r2   )r   indexr   r   r   __getitem__   s    zTraceback.__getitem__c             C   s   | j  |  j k S)N)r3   r;   )r   r4   r   r   r   __contains__   s    zTraceback.__contains__c             C   s   t  |  j  S)N)r   r;   )r   r   r   r   r      s    zTraceback.__hash__c             C   s   |  j  | j  k S)N)r;   )r   r   r   r   r   r      s    zTraceback.__eq__c             C   s   |  j  | j  k  S)N)r;   )r   r   r   r   r   r8      s    zTraceback.__lt__c             C   s   t  |  d  S)Nr   )str)r   r   r   r   r      s    zTraceback.__str__c             C   s   d t  |   f S)Nz<Traceback %r>)rD   )r   r   r   r   r       s    zTraceback.__repr__Nc             C   s   g  } | d  k	 r" | d k  r" | Sxm |  d  |  D][ } | j  d | j | j f  t j | j | j  j   } | r3 | j  d |  q3 q3 W| S)Nr   z  File "%s", line %sz    %s)r,   r5   r7   	linecachegetlinestrip)r   limitlinesr4   liner   r   r   format   s    	zTraceback.format)z_frames)r"   r#   r$   r%   r&   r   r>   rF   rG   r   r   r8   r   r    rO   r   r   r   r   r:      s   r:   c             C   s*   t  |   } | d k	 r" t |  Sd Sd S)z
    Get the traceback where the Python object *obj* was allocated.
    Return a Traceback instance.

    Return None if the tracemalloc module is not tracing memory allocations or
    did not trace the allocation of the object.
    N)r   r:   )objr<   r   r   r   get_object_traceback   s    
rQ   c               @   s|   e  Z d  Z d Z 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 S)Tracez"
    Trace of a memory block.
    _tracec             C   s   | |  _  d  S)N)rS   )r   r@   r   r   r   r      s    zTrace.__init__c             C   s   |  j  d S)Nr   )rS   )r   r   r   r   r      s    z
Trace.sizec             C   s   t  |  j d  S)Nr6   )r:   rS   )r   r   r   r   r      s    zTrace.tracebackc             C   s   |  j  | j  k S)N)rS   )r   r   r   r   r   r     s    zTrace.__eq__c             C   s   t  |  j  S)N)r   rS   )r   r   r   r   r     s    zTrace.__hash__c             C   s   d |  j  t |  j d  f S)Nz%s: %sF)r   r   r   )r   r   r   r   r   	  s    zTrace.__str__c             C   s   d t  |  j d  |  j f S)Nz<Trace size=%s, traceback=%r>F)r   r   r   )r   r   r   r   r      s    zTrace.__repr__N)z_trace)r"   r#   r$   r%   r&   r   r9   r   r   r   r   r   r    r   r   r   r   rR      s   rR   c               @   sX   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d S)_Tracesc             C   s   t  j |   | |  _ d  S)N)r   r   _traces)r   tracesr   r   r   r     s    z_Traces.__init__c             C   s   t  |  j  S)N)r=   rU   )r   r   r   r   r>     s    z_Traces.__len__c             C   sB   t  | t  r- t d d   |  j | D  St |  j |  Sd  S)Nc             s   s   |  ] } t  |  Vq d  S)N)rR   )r?   r@   r   r   r   rA     s    z&_Traces.__getitem__.<locals>.<genexpr>)rB   rC   rD   rU   rR   )r   rE   r   r   r   rF     s    z_Traces.__getitem__c             C   s   | j  |  j k S)N)rS   rU   )r   r@   r   r   r   rG      s    z_Traces.__contains__c             C   s   |  j  | j  k S)N)rU   )r   r   r   r   r   r   #  s    z_Traces.__eq__c             C   s   d t  |   S)Nz<Traces len=%s>)r=   )r   r   r   r   r    &  s    z_Traces.__repr__N)	r"   r#   r$   r   r>   rF   rG   r   r    r   r   r   r   rT     s   rT   c             C   s8   t  j j |   }  |  j d  r4 |  d  d  }  n  |  S)N.pyc.pyor6   )rW   rX   )ospathnormcaseendswith)r5   r   r   r   _normalize_filename*  s    r^   c               @   sX   e  Z d  Z d d d d  Z e d d    Z d d   Z d	 d
   Z d d   Z d S)FilterNFc             C   s.   | |  _  t |  |  _ | |  _ | |  _ d  S)N)	inclusiver^   _filename_patternr7   
all_frames)r   r`   filename_patternr7   rb   r   r   r   r   2  s    		zFilter.__init__c             C   s   |  j  S)N)ra   )r   r   r   r   rc   9  s    zFilter.filename_patternc             C   sI   t  |  } t j | |  j  s% d S|  j d  k r8 d S| |  j k Sd  S)NFT)r^   fnmatchra   r7   )r   r5   r7   r   r   r   Z__match_frame=  s    zFilter.__match_framec             C   s   |  j  | |  |  j AS)N)_Filter__match_framer`   )r   r5   r7   r   r   r   _match_frameF  s    zFilter._match_framec                s^     j  r: t   f d d   | D  r/   j S  j Sn  | d \ } }   j | |  Sd  S)Nc             3   s'   |  ] \ } }   j  | |  Vq d  S)N)re   )r?   r5   r7   )r   r   r   rA   K  s   z*Filter._match_traceback.<locals>.<genexpr>r   )rb   anyr`   rf   )r   r   r5   r7   r   )r   r   _match_tracebackI  s    	zFilter._match_traceback)	r"   r#   r$   r   r9   rc   re   rf   rh   r   r   r   r   r_   1  s
   	r_   c               @   s   e  Z d  Z d Z d d   Z d d   Z e d d    Z d d	   Z d
 d   Z	 d d   Z
 d d d  Z d d d  Z d S)SnapshotzB
    Snapshot of traces of memory blocks allocated by Python.
    c             C   s   t  |  |  _ | |  _ d  S)N)rT   rV   traceback_limit)r   rV   rj   r   r   r   r   Z  s    zSnapshot.__init__c             C   s2   t  | d   } t j |  | t j  Wd QXd S)z1
        Write the snapshot into a file.
        wbN)openpickledumpZHIGHEST_PROTOCOL)r   r5   fpr   r   r   rn   `  s    zSnapshot.dumpc          	   C   s)   t  |  d   } t j |  SWd QXd S)z.
        Load a snapshot from a file.
        rbN)rl   rm   load)r5   ro   r   r   r   rq   g  s    zSnapshot.loadc                sf   | d   | r6 t    f d d   | D  s6 d Sn  | rb t    f d d   | D  rb d Sn  d S)Nr6   c             3   s   |  ] } | j     Vq d  S)N)rh   )r?   trace_filter)r   r   r   rA   r  s   z)Snapshot._filter_trace.<locals>.<genexpr>Fc             3   s   |  ] } | j     Vq d  S)N)rh   )r?   rr   )r   r   r   rA   v  s   T)rg   )r   include_filtersexclude_filtersr@   r   )r   r   _filter_traceo  s    
zSnapshot._filter_tracec                s   t  | t  s+ t d t |  j   n  | r g   g    x4 | D], } | j rc  j |  qD   j |  qD W    f d d    j j D } n  j j j	   } t
 |  j  S)z
        Create a new Snapshot instance with a filtered traces sequence, filters
        is a list of Filter instances.  If filters is an empty list, return a
        new Snapshot instance with a copy of the traces.
        z)filters must be a list of filters, not %sc                s+   g  |  ]! }  j     |  r |  q Sr   )ru   )r?   r@   )rt   rs   r   r   r   
<listcomp>  s   	 	z*Snapshot.filter_traces.<locals>.<listcomp>)rB   r   	TypeErrortyper"   r`   r,   rV   rU   copyri   rj   )r   filtersrr   Z
new_tracesr   )rt   rs   r   r   filter_traces{  s    	(zSnapshot.filter_tracesc             C   sM  | d k r" t  d | f   n  | rG | d	 k rG t  d |   n  i  } i  } | sZx|  j j D] } | \ } } y | | } Wns t k
 r | d k r | }	 n6 | d k r | d  d  }	 n | d d d f f }	 t |	  } | | | <Yn Xy, | | }
 |
 j | 7_ |
 j d 7_ Wqf t k
 rRt | | d  | | <Yqf Xqf Wn x |  j j D] } | \ } } x | D] } y | | } WnS t k
 r| d k r| f }	 n | d d f f }	 t |	  } | | | <Yn Xy, | | }
 |
 j | 7_ |
 j d 7_ Wqt k
 r@t | | d  | | <YqXqWqgW| S)
Nr   r5   r7   zunknown key_type: %rz/cumulative mode cannot by used with key type %rr6   r   )z	tracebackfilenamelineno)r}   r|   )
ValueErrorrV   rU   KeyErrorr:   r   r   r   )r   key_type
cumulativeZstatsZ
tracebacksr@   r   Ztrace_tracebackr   r<   r0   r4   r   r   r   	_group_by  sX    	
"
#zSnapshot._group_byFc             C   sA   |  j  | |  } t | j    } | j d d d t j  | S)zd
        Group statistics by key_type. Return a sorted list of Statistic
        instances.
        reverseTkey)r   listvaluessortr   r!   )r   r   r   Zgroupedr/   r   r   r   r/     s    zSnapshot.statisticsc             C   sP   |  j  | |  } | j  | |  } t | |  } | j d d d t j  | S)z
        Compute the differences with an old snapshot old_snapshot. Get
        statistics as a sorted list of StatisticDiff instances, grouped by
        group_by.
        r   Tr   )r   r1   r   r'   r!   )r   Zold_snapshotr   r   r.   r-   r/   r   r   r   
compare_to  s
    zSnapshot.compare_toN)r"   r#   r$   r%   r   rn   staticmethodrq   ru   r{   r   r/   r   r   r   r   r   ri   U  s   3
ri   c              C   s7   t    s t d   n  t   }  t   } t |  |  S)zI
    Take a snapshot of traces of memory blocks allocated by Python.
    zLthe tracemalloc module must be tracing memory allocations to take a snapshot)Z
is_tracingRuntimeErrorr   Zget_traceback_limitri   )rV   rj   r   r   r   take_snapshot  s
    			r   )collectionsr   r   	functoolsr   rd   rI   os.pathrZ   rm   Z_tracemallocr   r   r   r   r'   r1   r2   r:   rQ   rR   rT   r^   r_   ri   r   r   r   r   r   <module>   s*   
&0#5!$