o
    f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)BKiBMiBGiBTiBd   r   z%+.1f %sz%.1f %si (  r   z%+.0f %sz%.0f %si   )abs)sizesign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   text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   r5   )r   r   r   r6   r   r7   r   r   r   r   M   s
   
zStatisticDiff.__init__c                 C   s   t | j| j| j| j| jfS r   )r   r   r   r6   r   r7   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!   r4   r"   r   r   r6   r   r7   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   r6   r   r7   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)>r5   r   r   r   r   r+   m   s
   zStatisticDiff.__repr__c                 C   s    t | j| jt | j| j| jfS r   )r   r6   r   r7   r   r   r   r   r   r   r,   r   s   zStatisticDiff._sort_keyNr.   r   r   r   r   r4   F   s    	r4   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popr4   r   r   append)	old_group	new_group
statisticsr   statpreviousr   r   r   _compare_grouped_statsx   s"   rA   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   rC   r   framer   r   r   r         
zFrame.__init__c                 C   
   | j d S r8   rC   r   r   r   r   filename   rH   zFrame.filenamec                 C   rI   N   rC   r   r   r   r   lineno   rH   zFrame.linenoc                 C      t |tstS | j|jkS r   r!   rB   r"   rD   r#   r   r   r   r%         
zFrame.__eq__c                 C      t |tstS | j|jk S r   rO   r#   r   r   r   __lt__   rP   zFrame.__lt__c                 C   
   t | jS r   )r   rD   r   r   r   r   r         
zFrame.__hash__c                 C      d| j | jf S )Nz%s:%srJ   rM   r   r   r   r   r*      r-   zFrame.__str__c                 C   rU   )Nz<Frame filename=%r lineno=%r>rV   r   r   r   r   r+      r-   zFrame.__repr__N)r/   r0   r1   r2   r3   r   propertyrJ   rM   r%   rR   r   r*   r+   r   r   r   r   rB      s    

rB   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reversedrY   rZ   )r   framestotal_nframer   r   r   r      s   

zTraceback.__init__c                 C      | j S r   )rZ   r   r   r   r   r^         zTraceback.total_nframec                 C   rS   r   )lenrY   r   r   r   r   __len__   rT   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   )rB   .0tracer   r   r   	<genexpr>       z(Traceback.__getitem__.<locals>.<genexpr>)r!   slicer[   rY   rB   r   indexr   r   r   __getitem__      
zTraceback.__getitem__c                 C      |j | jv S r   )rD   rY   rF   r   r   r   __contains__      zTraceback.__contains__c                 C   rS   r   )r   rY   r   r   r   r   r      rT   zTraceback.__hash__c                 C   rN   r   r!   rX   r"   rY   r#   r   r   r   r%      rP   zTraceback.__eq__c                 C   rQ   r   rr   r#   r   r   r   rR      rP   zTraceback.__lt__c                 C   s   t | d S r8   )strr   r   r   r   r*      rq   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=)r[   rZ   r^   )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)r\   r;   rJ   rM   	linecachegetlinestrip)r   limitmost_recent_firstlinesframe_slicerG   liner   r   r   format   s"   
zTraceback.formatr   )NF)r/   r0   r1   r2   r3   r   rW   r^   rb   rm   rp   r   r%   rR   r*   r+   r~   r   r   r   r   rX      s    
	
rX   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   rX   )objr]   r   r   r   get_object_traceback  s   r   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   rE   r   r   r   rg   r   r   r   r     s   
zTrace.__init__c                 C   rI   r8   r   r   r   r   r   domain  rH   zTrace.domainc                 C   rI   rK   r   r   r   r   r   r     rH   z
Trace.sizec                 C   s   t | jdd   S )N   )rX   r   r   r   r   r   r   #  s   zTrace.tracebackc                 C   rN   r   )r!   r   r"   r   r#   r   r   r   r%   '  rP   zTrace.__eq__c                 C   rS   r   )r   r   r   r   r   r   r   ,  rT   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/   r0   r1   r2   r3   r   rW   r   r   r   r%   r   r*   r+   r   r   r   r   r     s    


r   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   rS   r   )ra   r   r   r   r   r   rb   =  rT   z_Traces.__len__c                 C   rc   )Nc                 s   rd   r   )r   re   r   r   r   rh   B  ri   z&_Traces.__getitem__.<locals>.<genexpr>)r!   rj   r[   r   r   rk   r   r   r   rm   @  rn   z_Traces.__getitem__c                 C   ro   r   )r   r   r   r   r   r   rp   F  rq   z_Traces.__contains__c                 C   rN   r   )r!   r   r"   r   r#   r   r   r   r%   I  rP   z_Traces.__eq__c                 C   s   dt |  S )Nz<Traces len=%s>)ra   r   r   r   r   r+   N  rq   z_Traces.__repr__N)	r/   r0   r1   r   rb   rm   rp   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)rJ   r   r   r   _normalize_filenameR  s   
r   c                   @   s   e Zd Zdd Zdd ZdS )
BaseFilterc                 C   rE   r   )	inclusive)r   r   r   r   r   r   Z  rT   zBaseFilter.__init__c                 C   s   t r   )NotImplementedErrorr   r   r   r   _match]  s   zBaseFilter._matchN)r/   r0   r1   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_patternrM   
all_framesr   )r   r   filename_patternrM   r   r   	__class__r   r   r   b  s   

zFilter.__init__c                 C   r_   r   )r   r   r   r   r   r   k  r`   zFilter.filename_patternc                 C   s2   t |}t|| jsdS | jd u rdS || jkS )NFT)r   fnmatchr   rM   r   rJ   rM   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   )rf   rJ   rM   r   r   r   rh   }  s    z*Filter._match_traceback.<locals>.<genexpr>r   )r   anyr   r   )r   r   rJ   rM   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   rg   r   r   r   r^   resr   r   r   r     s   

zFilter._match)NFN)r/   r0   r1   r   rW   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   r_   r   )r   r   r   r   r   r     r`   zDomainFilter.domainc                 C   s   |\}}}}|| j k| j A S r   )r   r   )r   rg   r   r   r   r^   r   r   r   r     s   zDomainFilter._match)r/   r0   r1   r   rW   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   rJ   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)rJ   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   rf   trace_filterrg   r   r   rh     s    z)Snapshot._filter_trace.<locals>.<genexpr>Fc                 3   s    | ]	}|   V  qd S r   r   r   r   r   r   rh     s    T)r   )r   include_filtersexclude_filtersrg   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   re   r   r   r   r   r   
<listcomp>  s    
z*Snapshot.filter_traces.<locals>.<listcomp>)r!   r   	TypeErrortyper/   r   r;   r   r   copyr   r   )r   filtersr   
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   rJ   rM   zunknown key_type: %r)rM   rJ   z/cumulative mode cannot by used with key type %rr   rM   rL   r   )
ValueErrorr   r   KeyErrorrX   r   r   r   )r   key_type
cumulativestats
tracebacksrg   r   r   trace_tracebackr^   r   r]   r?   rG   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   groupedr>   r   r   r   r>     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   rA   r   r4   r,   )r   old_snapshotr   r   r=   r<   r>   r   r   r   
compare_to  s
   
zSnapshot.compare_toN)F)r/   r0   r1   r2   r   r   staticmethodr   r   r   r   r>   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   get_traceback_limitr   )r   r   r   r   r   take_snapshot'  s
   
r   )collections.abcr   r   	functoolsr   r   rv   os.pathr   r   _tracemallocr   r   r   r   r4   rA   rB   rX   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s4    (2&M'0 	