a
    zeF                     @   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
T d dl
mZmZ dd ZG dd	 d	ZG d
d dZdd ZeG dd dZeG dd deZdd ZG dd dZG dd deZdd ZG dd dZG dd deZG dd deZG d d! d!Zd"d# ZdS )$    )SequenceIterable)total_orderingN)*)_get_object_traceback_get_tracesc                 C   s   dD ]|}t | dk r@|dkr@|r0d| |f   S d| |f   S t | dk sT|dkrx|rhd| |f   S d	| |f   S | d
 } qd S )N)BZKiBZMiBZGiBTiBd   r   z%+.1f %sz%.1f %si (  r	   z%+.0f %sz%.0f %si   )abs)sizesignZunit r   !/usr/lib/python3.9/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 Nr   )selfr   r   r   r   r   r   __init__%   s    zStatistic.__init__c                 C   s   t | j| j| jfS r   )hashr   r   r   r   r   r   r   __hash__*   s    zStatistic.__hash__c                 C   s2   t |tstS | j|jko0| j|jko0| j|jkS r   )
isinstancer   NotImplementedr   r   r   r   otherr   r   r   __eq__-   s    


zStatistic.__eq__c                 C   sB   d| j t| jd| jf }| jr>| j| j }|dt|d 7 }|S )Nz%s: size=%s, count=%iF, average=%s)r   r   r   r   r   textZaverager   r   r   __str__4   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   __repr__>   s    zStatistic.__repr__c                 C   s   | j | j| jfS r   )r   r   r   r   r   r   r   	_sort_keyB   s    zStatistic._sort_keyN__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   s"   || _ || _|| _|| _|| _d S r   r-   )r   r   r   r.   r   r/   r   r   r   r   M   s
    zStatisticDiff.__init__c                 C   s   t | j| j| j| j| jfS r   )r   r   r   r.   r   r/   r   r   r   r   r   T   s    zStatisticDiff.__hash__c                 C   sJ   t |tstS | j|jkoH| j|jkoH| j|jkoH| j|jkoH| j|jkS r   )r   r,   r   r   r   r.   r   r/   r   r   r   r   r   X   s    




zStatisticDiff.__eq__c                 C   sP   d| j t| jdt| jd| j| jf }| jrL| j| j }|dt|d 7 }|S )Nz %s: size=%s (%s), count=%i (%+i)FTr    )r   r   r   r.   r   r/   r!   r   r   r   r#   a   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$   m   s
    zStatisticDiff.__repr__c                 C   s    t | j| jt | j| j| jfS r   )r   r.   r   r/   r   r   r   r   r   r   r%   r   s    zStatisticDiff._sort_keyNr&   r   r   r   r   r,   F   s   	r,   c                 C   s   g }|  D ]d\}}| |d }|d urNt||j|j|j |j|j|j }nt||j|j|j|j}|| q|   D ]*\}}t|d|j d|j }|| qz|S Nr   )itemspopr,   r   r   append)	old_group	new_group
statisticsr   statZpreviousr   r   r   _compare_grouped_statsx   s"    r8   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 r   r:   r   framer   r   r   r      s    zFrame.__init__c                 C   s
   | j d S r0   r:   r   r   r   r   filename   s    zFrame.filenamec                 C   s
   | j d S N   r:   r   r   r   r   lineno   s    zFrame.linenoc                 C   s   t |tstS | j|jkS r   r   r9   r   r;   r   r   r   r   r      s    
zFrame.__eq__c                 C   s   t |tstS | j|jk S r   rB   r   r   r   r   __lt__   s    
zFrame.__lt__c                 C   s
   t | jS r   )r   r;   r   r   r   r   r      s    zFrame.__hash__c                 C   s   d| j | jf S )Nz%s:%sr>   rA   r   r   r   r   r#      s    zFrame.__str__c                 C   s   d| j | jf S )Nz<Frame filename=%r lineno=%r>rD   r   r   r   r   r$      s    zFrame.__repr__N)r'   r(   r)   r*   r+   r   propertyr>   rA   r   rC   r   r#   r$   r   r   r   r   r9      s   

r9   c                   @   st   e Zd ZdZdZd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d Zdd ZdddZdS )	Tracebackz`
    Sequence of Frame instances sorted from the oldest frame
    to the most recent frame.
    )_frames_total_nframeNc                 C   s"   t |  tt|| _|| _d S r   )r   r   tuplereversedrG   rH   )r   framestotal_nframer   r   r   r      s    
zTraceback.__init__c                 C   s   | j S r   )rH   r   r   r   r   rL      s    zTraceback.total_nframec                 C   s
   t | jS r   )lenrG   r   r   r   r   __len__   s    zTraceback.__len__c                 C   s4   t |tr"tdd | j| D S t| j| S d S )Nc                 s   s   | ]}t |V  qd S r   )r9   .0tracer   r   r   	<genexpr>       z(Traceback.__getitem__.<locals>.<genexpr>)r   slicerI   rG   r9   r   indexr   r   r   __getitem__   s    
zTraceback.__getitem__c                 C   s   |j | jv S r   )r;   rG   r<   r   r   r   __contains__   s    zTraceback.__contains__c                 C   s
   t | jS r   )r   rG   r   r   r   r   r      s    zTraceback.__hash__c                 C   s   t |tstS | j|jkS r   r   rF   r   rG   r   r   r   r   r      s    
zTraceback.__eq__c                 C   s   t |tstS | j|jk S r   rY   r   r   r   r   rC      s    
zTraceback.__lt__c                 C   s   t | d S r0   )strr   r   r   r   r#      s    zTraceback.__str__c                 C   s8   dt |  }| jd u r"|d7 }n|d| j d7 }|S )Nz<Traceback >z total_nframe=)rI   rH   rL   )r   sr   r   r   r$      s
    

zTraceback.__repr__Fc                 C   s   g }|d ur2|dkr$| | d  }q6| d | }n| }|rBt |}|D ]@}|d|j|jf  t|j|j }|rF|d|  qF|S )Nr   z  File "%s", line %sz    %s)rJ   r3   r>   rA   	linecachegetlinestrip)r   limitZmost_recent_firstlinesZframe_slicer=   liner   r   r   format   s     
zTraceback.format)N)NF)r'   r(   r)   r*   r+   r   rE   rL   rN   rW   rX   r   r   rC   r#   r$   rc   r   r   r   r   rF      s   
	
rF   c                 C   s    t | }|durt|S dS d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   rF   )objrK   r   r   r   get_object_traceback  s    re   c                   @   s`   e Zd ZdZdZdd Ze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 r   rg   r   rQ   r   r   r   r     s    zTrace.__init__c                 C   s
   | j d S r0   rg   r   r   r   r   domain  s    zTrace.domainc                 C   s
   | j d S r?   rg   r   r   r   r   r     s    z
Trace.sizec                 C   s   t | jdd   S )N   )rF   rh   r   r   r   r   r   #  s    zTrace.tracebackc                 C   s   t |tstS | j|jkS r   )r   rf   r   rh   r   r   r   r   r   '  s    
zTrace.__eq__c                 C   s
   t | jS r   )r   rh   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| j t| jd| jf S )Nz'<Trace domain=%s size=%s, traceback=%r>F)rj   r   r   r   r   r   r   r   r$   2  s    zTrace.__repr__N)r'   r(   r)   r*   r+   r   rE   rj   r   r   r   r   r#   r$   r   r   r   r   rf     s   


rf   c                   @   s<   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 |  || _d S r   )r   r   _traces)r   tracesr   r   r   r   8  s    
z_Traces.__init__c                 C   s
   t | jS r   )rM   rm   r   r   r   r   rN   =  s    z_Traces.__len__c                 C   s4   t |tr"tdd | j| D S t| j| S d S )Nc                 s   s   | ]}t |V  qd S r   )rf   rO   r   r   r   rR   B  rS   z&_Traces.__getitem__.<locals>.<genexpr>)r   rT   rI   rm   rf   rU   r   r   r   rW   @  s    
z_Traces.__getitem__c                 C   s   |j | jv S r   )rh   rm   ri   r   r   r   rX   F  s    z_Traces.__contains__c                 C   s   t |tstS | j|jkS r   )r   rl   r   rm   r   r   r   r   r   I  s    
z_Traces.__eq__c                 C   s   dt |  S )Nz<Traces len=%s>)rM   r   r   r   r   r$   N  s    z_Traces.__repr__N)	r'   r(   r)   r   rN   rW   rX   r   r$   r   r   r   r   rl   7  s   rl   c                 C   s&   t j| } | dr"| d d } | S )Nz.pyc)ospathnormcaseendswith)r>   r   r   r   _normalize_filenameR  s    
rt   c                   @   s   e Zd Zdd Zdd ZdS )
BaseFilterc                 C   s
   || _ d S r   )	inclusive)r   rv   r   r   r   r   Z  s    zBaseFilter.__init__c                 C   s   t d S r   )NotImplementedErrorri   r   r   r   _match]  s    zBaseFilter._matchN)r'   r(   r)   r   rx   r   r   r   r   ru   Y  s   ru   c                       sJ   e Zd Zd fdd	Zedd Zdd Zd	d
 Zdd Zdd Z	  Z
S )FilterNFc                    s2   t  | || _t|| _|| _|| _|| _d S r   )superr   rv   rt   _filename_patternrA   
all_framesrj   )r   rv   filename_patternrA   r|   rj   	__class__r   r   r   b  s    
zFilter.__init__c                 C   s   | j S r   )r{   r   r   r   r   r}   k  s    zFilter.filename_patternc                 C   s6   t |}t|| jsdS | jd u r(dS || jkS d S )NFT)rt   fnmatchr{   rA   r   r>   rA   r   r   r   _match_frame_implo  s    
zFilter._match_frame_implc                 C   s   |  ||| j A S r   )r   rv   r   r   r   r   _match_framex  s    zFilter._match_framec                    sH    j r,t fdd|D r" jS  j S n|d \}} ||S d S )Nc                 3   s   | ]\}}  ||V  qd S r   )r   )rP   r>   rA   r   r   r   rR   }  s   z*Filter._match_traceback.<locals>.<genexpr>r   )r|   anyrv   r   )r   r   r>   rA   r   r   r   _match_traceback{  s    
zFilter._match_tracebackc                 C   sF   |\}}}}|  |}| jd urB| jr4|o2|| jkS |p@|| jkS |S r   )r   rj   rv   )r   rQ   rj   r   r   rL   resr   r   r   rx     s    

zFilter._match)NFN)r'   r(   r)   r   rE   r}   r   r   r   rx   __classcell__r   r   r~   r   ry   a  s    	
	ry   c                       s0   e Zd Z fddZedd Zdd Z  ZS )DomainFilterc                    s   t  | || _d S r   )rz   r   _domain)r   rv   rj   r~   r   r   r     s    zDomainFilter.__init__c                 C   s   | j S r   )r   r   r   r   r   rj     s    zDomainFilter.domainc                 C   s   |\}}}}|| j k| j A S r   )rj   rv   )r   rQ   rj   r   r   rL   r   r   r   rx     s    zDomainFilter._match)r'   r(   r)   r   rE   rj   rx   r   r   r   r~   r   r     s   
r   c                   @   sX   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 r   )rl   rn   traceback_limit)r   rn   r   r   r   r   r     s    
zSnapshot.__init__c                 C   s>   t |d }t| |tj W d   n1 s00    Y  dS )z1
        Write the snapshot into a file.
        wbN)openpickledumpHIGHEST_PROTOCOL)r   r>   fpr   r   r   r     s    zSnapshot.dumpc                 C   s8   t | d}t|W  d   S 1 s*0    Y  dS )z.
        Load a snapshot from a file.
        rbN)r   r   load)r>   r   r   r   r   r     s    zSnapshot.loadc                    s@   |rt  fdd|D sdS |r<t  fdd|D r<dS dS )Nc                 3   s   | ]}|  V  qd S r   rx   rP   trace_filterrQ   r   r   rR     s   z)Snapshot._filter_trace.<locals>.<genexpr>Fc                 3   s   | ]}|   V  qd S r   r   r   r   r   r   rR     s   T)r   )r   include_filtersexclude_filtersrQ   r   r   r   _filter_trace  s    zSnapshot._filter_tracec                    s   t |tstdt|j |rjg g  |D ] }|jrB| q, | q, fddjjD }njj	 }t
|jS )z
        Create a new Snapshot instance with a filtered traces sequence, filters
        is a list of Filter or DomainFilter 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 | ]}  |r|qS r   )r   rO   r   r   r   r   r   
<listcomp>  s
   z*Snapshot.filter_traces.<locals>.<listcomp>)r   r   	TypeErrortyper'   rv   r3   rn   rm   copyr   r   )r   filtersr   Z
new_tracesr   r   r   filter_traces  s    
zSnapshot.filter_tracesc              
   C   s  |dvrt d|f |r.|dvr.t d| i }i }|s| jjD ]}|\}}}}	z|| }
W nX ty   |dkr||}n(|dkr|d d }n|d d dff}t|}
|
||< Y n0 z(||
 }| j|7  _| jd7  _W qD ty   t|
|d||
< Y qD0 qDn| jjD ]}|\}}}}	|D ]}z|| }
W nD ty|   |dkrZ|f}n|d dff}t|}
|
||< Y n0 z(||
 }| j|7  _| jd7  _W n$ ty   t|
|d||
< Y n0 q(q|S )	N)r   r>   rA   zunknown key_type: %r)rA   r>   z/cumulative mode cannot by used with key type %rr   rA   r@   r   )
ValueErrorrn   rm   KeyErrorrF   r   r   r   )r   key_type
cumulativeZstatsZ
tracebacksrQ   rj   r   Ztrace_tracebackrL   r   rK   r7   r=   r   r   r   	_group_by  sZ    
zSnapshot._group_byFc                 C   s,   |  ||}t| }|jdtjd |S )zd
        Group statistics by key_type. Return a sorted list of Statistic
        instances.
        Treversekey)r   listvaluessortr   r%   )r   r   r   Zgroupedr6   r   r   r   r6     s    zSnapshot.statisticsc                 C   s6   |  ||}| ||}t||}|jdtjd |S )z
        Compute the differences with an old snapshot old_snapshot. Get
        statistics as a sorted list of StatisticDiff instances, grouped by
        group_by.
        Tr   )r   r8   r   r,   r%   )r   Zold_snapshotr   r   r5   r4   r6   r   r   r   
compare_to  s
    
zSnapshot.compare_toN)F)F)r'   r(   r)   r*   r   r   staticmethodr   r   r   r   r6   r   r   r   r   r   r     s   
3

r   c                  C   s$   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)
is_tracingRuntimeErrorr   Zget_traceback_limitr   )rn   r   r   r   r   take_snapshot'  s
    r   )collections.abcr   r   	functoolsr   r   r]   Zos.pathrp   r   Z_tracemallocr   r   r   r   r,   r8   r9   rF   re   rf   rl   rt   ru   ry   r   r   r   r   r   r   r   <module>   s2   (2&M'0 	