a
    ´i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ÚTiBéd   r   z%+.1f %sz%.1f %si (  r	   z%+.0f %sz%.0f %si   )Úabs)ÚsizeÚsignZunit© r   ú#/usr/lib64/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 	