o
     JAf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 |rd| |f   S d| |f   S t | dk s*|dkr<|r4d| |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.10/tracemalloc.py_format_size   s   
r   c                   @   D   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__*      zStatistic.__hash__c                 C   s2   t |tstS | j|jko| j|jko| 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      zStatistic._sort_keyN__name__
__module____qualname____doc__	__slots__r   r   r!   r%   r&   r'   r   r   r   r   r      s    
r   c                   @   r   )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   r0   )r   r   r   r1   r   r2   r   r   r   r   M   s
   
zStatisticDiff.__init__c                 C   s   t | j| j| j| j| jfS r   )r   r   r   r1   r   r2   r   r   r   r   r   T   s   zStatisticDiff.__hash__c                 C   sJ   t |tstS | j|jko$| j|jko$| j|jko$| j|jko$| j|jkS r   )r   r/   r   r   r   r1   r   r2   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r&| j| j }|dt|d 7 }|S )Nz %s: size=%s (%s), count=%i (%+i)FTr"   )r   r   r   r1   r   r2   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)>r0   r   r   r   r   r&   m   s
   zStatisticDiff.__repr__c                 C   s    t | j| jt | j| j| jfS r   )r   r1   r   r2   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 ]2\}}| |d }|d ur't||j|j|j |j|j|j }nt||j|j|j|j}|| q|   D ]\}}t|d|j d|j }|| q=|S Nr   )itemspopr/   r   r   append)	old_group	new_group
statisticsr   statZpreviousr   r   r   _compare_grouped_statsx   s"   r;   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   
   || _ d S r   r=   r   framer   r   r   r         
zFrame.__init__c                 C   
   | j d S r3   r=   r   r   r   r   filename   rB   zFrame.filenamec                 C   rC   N   r=   r   r   r   r   lineno   rB   zFrame.linenoc                 C      t |tstS | j|jkS r   r   r<   r   r>   r   r   r   r   r!         
zFrame.__eq__c                 C      t |tstS | j|jk S r   rI   r   r   r   r   __lt__   rJ   zFrame.__lt__c                 C   
   t | jS r   )r   r>   r   r   r   r   r         
zFrame.__hash__c                 C      d| j | jf S )Nz%s:%srD   rG   r   r   r   r   r%      r(   zFrame.__str__c                 C   rO   )Nz<Frame filename=%r lineno=%r>rP   r   r   r   r   r&      r(   zFrame.__repr__N)r*   r+   r,   r-   r.   r   propertyrD   rG   r!   rL   r   r%   r&   r   r   r   r   r<      s    

r<   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reversedrS   rT   )r   framestotal_nframer   r   r   r      s   

zTraceback.__init__c                 C      | j S r   )rT   r   r   r   r   rX         zTraceback.total_nframec                 C   rM   r   )lenrS   r   r   r   r   __len__   rN   zTraceback.__len__c                 C   0   t |trtdd | j| D S t| j| S )Nc                 s       | ]}t |V  qd S r   )r<   .0tracer   r   r   	<genexpr>       z(Traceback.__getitem__.<locals>.<genexpr>)r   slicerU   rS   r<   r   indexr   r   r   __getitem__      
zTraceback.__getitem__c                 C      |j | jv S r   )r>   rS   r@   r   r   r   __contains__      zTraceback.__contains__c                 C   rM   r   )r   rS   r   r   r   r   r      rN   zTraceback.__hash__c                 C   rH   r   r   rR   r   rS   r   r   r   r   r!      rJ   zTraceback.__eq__c                 C   rK   r   rl   r   r   r   r   rL      rJ   zTraceback.__lt__c                 C   s   t | d S r3   )strr   r   r   r   r%      rk   zTraceback.__str__c                 C   s:   dt |  }| jd u r|d7 }|S |d| j d7 }|S )Nz<Traceback >z total_nframe=)rU   rT   rX   )r   sr   r   r   r&      s   
zTraceback.__repr__Fc                 C   s   g }|d ur|dkr| | d  }n	| d | }n| }|r!t |}|D ] }|d|j|jf  t|j|j }|rC|d|  q#|S )Nr   z  File "%s", line %sz    %s)rV   r6   rD   rG   	linecachegetlinestrip)r   limitZmost_recent_firstlinesZframe_slicerA   liner   r   r   format   s"   
zTraceback.formatr   )NF)r*   r+   r,   r-   r.   r   rQ   rX   r\   rg   rj   r   r!   rL   r%   r&   rv   r   r   r   r   rR      s    
	
rR   c                 C   s   t | }|durt|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   rR   )objrW   r   r   r   get_object_traceback  s   rx   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   r?   r   rz   r   ra   r   r   r   r     s   
zTrace.__init__c                 C   rC   r3   rz   r   r   r   r   domain  rB   zTrace.domainc                 C   rC   rE   rz   r   r   r   r   r     rB   z
Trace.sizec                 C   s   t | jdd   S )N   )rR   r{   r   r   r   r   r   #  s   zTrace.tracebackc                 C   rH   r   )r   ry   r   r{   r   r   r   r   r!   '  rJ   zTrace.__eq__c                 C   rM   r   )r   r{   r   r   r   r   r   ,  rN   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)r}   r   r   r   r   r   r   r   r&   2  s   zTrace.__repr__N)r*   r+   r,   r-   r.   r   rQ   r}   r   r   r!   r   r%   r&   r   r   r   r   ry     s    


ry   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   rM   r   )r[   r   r   r   r   r   r\   =  rN   z_Traces.__len__c                 C   r]   )Nc                 s   r^   r   )ry   r_   r   r   r   rb   B  rc   z&_Traces.__getitem__.<locals>.<genexpr>)r   rd   rU   r   ry   re   r   r   r   rg   @  rh   z_Traces.__getitem__c                 C   ri   r   )r{   r   r|   r   r   r   rj   F  rk   z_Traces.__contains__c                 C   rH   r   )r   r   r   r   r   r   r   r   r!   I  rJ   z_Traces.__eq__c                 C   s   dt |  S )Nz<Traces len=%s>)r[   r   r   r   r   r&   N  rk   z_Traces.__repr__N)	r*   r+   r,   r   r\   rg   rj   r!   r&   r   r   r   r   r   7  s    r   c                 C   s&   t j| } | dr| d d } | S )Nz.pyc)ospathnormcaseendswith)rD   r   r   r   _normalize_filenameR  s   
r   c                   @   s   e Zd Zdd Zdd ZdS )
BaseFilterc                 C   r?   r   )	inclusive)r   r   r   r   r   r   Z  rN   zBaseFilter.__init__c                 C   s   t r   )NotImplementedErrorr|   r   r   r   _match]  s   zBaseFilter._matchN)r*   r+   r,   r   r   r   r   r   r   r   Y  s    r   c                       sL   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   r   r   _filename_patternrG   
all_framesr}   )r   r   filename_patternrG   r   r}   	__class__r   r   r   b  s   

zFilter.__init__c                 C   rY   r   )r   r   r   r   r   r   k  rZ   zFilter.filename_patternc                 C   s2   t |}t|| jsdS | jd u rdS || jkS )NFT)r   fnmatchr   rG   r   rD   rG   r   r   r   _match_frame_implo  s   

zFilter._match_frame_implc                 C   s   |  ||| j A S r   )r   r   r   r   r   r   _match_framex  r   zFilter._match_framec                    sB    j rt fdd|D r jS  j S |d \}} ||S )Nc                 3   s     | ]\}}  ||V  qd S r   )r   )r`   rD   rG   r   r   r   rb   }  s    z*Filter._match_traceback.<locals>.<genexpr>r   )r   anyr   r   )r   r   rD   rG   r   r   r   _match_traceback{  s   zFilter._match_tracebackc                 C   sF   |\}}}}|  |}| jd ur!| jr|o|| jkS |p || jkS |S r   )r   r}   r   )r   ra   r}   r   r   rX   resr   r   r   r     s   

zFilter._match)NFN)r*   r+   r,   r   rQ   r   r   r   r   r   __classcell__r   r   r   r   r   a  s    	
	r   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   )r   r   _domain)r   r   r}   r   r   r   r     s   
zDomainFilter.__init__c                 C   rY   r   )r   r   r   r   r   r}     rZ   zDomainFilter.domainc                 C   s   |\}}}}|| j k| j A S r   )r}   r   )r   ra   r}   r   r   rX   r   r   r   r     s   zDomainFilter._match)r*   r+   r,   r   rQ   r}   r   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   )r   r   traceback_limit)r   r   r   r   r   r   r     s   

zSnapshot.__init__c                 C   s@   t |d}t| |tj W d   dS 1 sw   Y  dS )z1
        Write the snapshot into a file.
        wbN)openpickledumpHIGHEST_PROTOCOL)r   rD   fpr   r   r   r     s   "zSnapshot.dumpc                 C   s8   t | d}t|W  d   S 1 sw   Y  dS )z.
        Load a snapshot from a file.
        rbN)r   r   load)rD   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   r   r`   trace_filterra   r   r   rb     s    z)Snapshot._filter_trace.<locals>.<genexpr>Fc                 3   s    | ]	}|   V  qd S r   r   r   r   r   r   rb     s    T)r   )r   include_filtersexclude_filtersra   r   r   r   _filter_trace  s   zSnapshot._filter_tracec                    s   t |tstdt|j |r5g g  |D ]}|jr!| 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   r_   r   r   r   r   r   
<listcomp>  s    
z*Snapshot.filter_traces.<locals>.<listcomp>)r   r   	TypeErrortyper*   r   r6   r   r   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 n, ty[   |dkr=|}n|dkrH|d d }n	|d d dff}t|}
|
||< Y nw z||
 }| j|7  _| jd7  _W q! ty   t|
|d||
< Y q!w |S | jjD ]Y}|\}}}}	|D ]N}z|| }
W n  ty   |dkr|f}n|d dff}t|}
|
||< Y nw z||
 }| j|7  _| jd7  _W q ty   t|
|d||
< Y qw q|S )	N)r   rD   rG   zunknown key_type: %r)rG   rD   z/cumulative mode cannot by used with key type %rr   rG   rF   r   )
ValueErrorr   r   KeyErrorrR   r   r   r   )r   key_type
cumulativeZstatsZ
tracebacksra   r}   r   Ztrace_tracebackrX   r   rW   r:   rA   r   r   r   	_group_by  sf   	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groupedr9   r   r   r   r9     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   r;   r   r/   r'   )r   Zold_snapshotr   r   r8   r7   r9   r   r   r   
compare_to  s
   
zSnapshot.compare_toN)F)r*   r+   r,   r-   r   r   staticmethodr   r   r   r   r9   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   )r   r   r   r   r   take_snapshot'  s
   
r   )collections.abcr   r   	functoolsr   r   rp   Zos.pathr   r   Z_tracemallocr   r   r   r   r/   r;   r<   rR   rx   ry   r   r   r   r   r   r   r   r   r   r   r   <module>   s4    (2&M'0 	