???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................
Yf=                 @   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  rM | d k rM | r? d |  | f Sd |  | f St  |   d k  sk | d k r | r d |  | f Sd |  | f S|  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   0/opt/alt/python35/lib64/python3.5/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   sY   d |  j  t |  j d  |  j f } |  j rU |  j |  j } | d t | d  7} | 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)r   r   r   )__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   sn   d |  j  t |  j d  t |  j d  |  j |  j f } |  j rj |  j |  j } | d t | d  7} | 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)r   r   r(   r   r)   )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)r3   )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" | Sxj |  d  |  D]X } | j  d | j | j f  t j | j | j  j   } | r3 | j  d |  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)r;   )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)rS   )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   s5   t  j j |   }  |  j d  r1 |  d  d  }  |  S)Nz.pycr6   )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   r`   r   r   r   r   2  s    		zFilter.__init__c             C   s   |  j  S)N)r_   )r   r   r   r   ra   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\   fnmatchr_   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)rc   )r?   r5   r7   )r   r   r   rA   K  s   z*Filter._match_traceback.<locals>.<genexpr>r   )r`   anyr^   rd   )r   r   r5   r7   r   )r   r   _match_tracebackI  s    	zFilter._match_traceback)	r"   r#   r$   r   r9   ra   rc   rd   rf   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   rh   r   r   r   r   Z  s    zSnapshot.__init__c             C   s3   t  | d   } t j |  | t j  Wd QRXd S)z1
        Write the snapshot into a file.
        wbN)openpickledumpZHIGHEST_PROTOCOL)r   r5   fpr   r   r   rl   `  s    zSnapshot.dumpc          	   C   s*   t  |  d   } t j |  SWd QRXd S)z.
        Load a snapshot from a file.
        rbN)rj   rk   load)r5   rm   r   r   r   ro   g  s    zSnapshot.loadc                s`   | d   | r3 t    f d d   | D  s3 d S| r\ t    f d d   | D  r\ d Sd S)Nr6   c             3   s   |  ] } | j     Vq d  S)N)rf   )r?   trace_filter)r   r   r   rA   r  s   z)Snapshot._filter_trace.<locals>.<genexpr>Fc             3   s   |  ] } | j     Vq d  S)N)rf   )r?   rp   )r   r   r   rA   v  s   T)re   )r   include_filtersexclude_filtersr@   r   )r   r   _filter_traceo  s    
zSnapshot._filter_tracec                s   t  | t  s( t d t |  j   | r g   g    x4 | D], } | j r`  j |  qA   j |  qA 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   )rs   )r?   r@   )rr   rq   r   r   r   
<listcomp>  s   	 	z*Snapshot.filter_traces.<locals>.<listcomp>)rB   r   	TypeErrortyper"   r^   r,   rV   rU   copyrg   rh   )r   filtersrp   Z
new_tracesr   )rr   rq   r   r   filter_traces{  s    	(zSnapshot.filter_tracesc             C   sG  | d k r t  d | f   | rA | d	 k rA t  d |   i  } i  } | sT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_ Wq` t k
 rLt | | d  | | <Yq` Xq` 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_ Wqzt k
 r:t | | d  | | <YqzXqzWqaW| S)
Nr   r5   r7   zunknown key_type: %rz/cumulative mode cannot by used with key type %rr6   r   )r   r5   r7   )r7   r5   )
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   rl   staticmethodro   rs   ry   r~   r/   r   r   r   r   r   rg   U  s   3
rg   c              C   s4   t    s t d   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_limitrg   )rV   rh   r   r   r   take_snapshot  s
    			r   )collectionsr   r   	functoolsr   rb   rI   Zos.pathrX   rk   Z_tracemallocr   r   r   r   r'   r1   r2   r:   rQ   rR   rT   r\   r]   rg   r   r   r   r   r   <module>   s*   
&0#5!$