o
    éT•j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ÚTiBéd   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 q’w 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 	