a �DOgF�@sddlmZmZddlmZddlZddlZddlZddl Z ddl Tddl m Z m Z dd�Z Gdd �d �ZGd d �d �Zd d �ZeGdd�d��ZeGdd�de��Zdd�ZGdd�d�ZGdd�de�Zdd�ZGdd�d�ZGdd�de�ZGdd�de�ZGd d!�d!�Zd"d#�ZdS)$�)�Sequence�Iterable)�total_orderingN)�*)�_get_object_traceback� _get_tracescCs�dD]|}t|�dkr@|dkr@|r0d||fSd||fSt|�dksT|dkrx|rhd||fSd ||fS|d }qdS) N)�BZKiBZMiBZGiB�TiB�drz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)�abs)�size�signZunit�r�#/usr/lib64/python3.9/tracemalloc.py� _format_size src@s@eZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dS)� Statistic�� tracebackr �countcCs||_||_||_dS�Nr)�selfrr rrrr�__init__%szStatistic.__init__cCst|j|j|jf�Sr)�hashrr r�rrrr�__hash__*szStatistic.__hash__cCs2t|t�stS|j|jko0|j|jko0|j|jkSr)� isinstancer�NotImplementedrr r�r�otherrrr�__eq__-s   � �zStatistic.__eq__cCsBd|jt|jd�|jf}|jr>|j|j}|dt|d�7}|S)Nz%s: size=%s, count=%iF� , average=%s)rrr r�r�textZaveragerrr�__str__4s �� zStatistic.__str__cCsd|j|j|jfS)Nz)rrrrr�__repr__>s�zStatistic.__repr__cCs|j|j|jfSr)r rrrrrr� _sort_keyBszStatistic._sort_keyN� �__name__� __module__� __qualname__� __slots__rrrr#r$r%rrrrrs rc@s@eZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dS)� StatisticDiff�rr � size_diffr� count_diffcCs"||_||_||_||_||_dSrr,)rrr r-rr.rrrrMs zStatisticDiff.__init__cCst|j|j|j|j|jf�Sr)rrr r-rr.rrrrrTs�zStatisticDiff.__hash__cCsJt|t�stS|j|jkoH|j|jkoH|j|jkoH|j|jkoH|j|jkSr)rr+rrr r-rr.rrrrrXs   � � � �zStatisticDiff.__eq__cCsPd|jt|jd�t|jd�|j|jf}|jrL|j|j}|dt|d�7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTr )rrr r-rr.r!rrrr#as  �� zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9r,rrrrr$ms  ��zStatisticDiff.__repr__cCs t|j�|jt|j�|j|jfSr)r r-r r.rrrrrrr%rs  �zStatisticDiff._sort_keyNr&rrrrr+Fs  r+cCs�g}|��D]d\}}|�|d�}|durNt||j|j|j|j|j|j�}nt||j|j|j|j�}|�|�q |��D]*\}}t|d|j d|j �}|�|�qz|S�Nr)�items�popr+r r�append)� old_group� new_group� statisticsr�statZpreviousrrr�_compare_grouped_statsxs" ��  r7c@sXeZdZdZdd�Zedd��Zedd��Zdd �Zd d �Z d d �Z dd�Z dd�Z dS)�Frame��_framecCs ||_dSrr9�r�framerrrr�szFrame.__init__cCs |jdSr/r9rrrr�filename�szFrame.filenamecCs |jdS�N�r9rrrr�lineno�sz Frame.linenocCst|t�stS|j|jkSr�rr8rr:rrrrr�s z Frame.__eq__cCst|t�stS|j|jkSrrArrrr�__lt__�s z Frame.__lt__cCs t|j�Sr)rr:rrrrr�szFrame.__hash__cCsd|j|jfS)Nz%s:%s�r=r@rrrrr#�sz Frame.__str__cCsd|j|jfS)NzrCrrrrr$�szFrame.__repr__N) r'r(r)r*r�propertyr=r@rrBrr#r$rrrrr8�s  r8c@speZdZdZddd�Zedd��Zdd�Zd d �Zd d �Z d d�Z dd�Z dd�Z dd�Z dd�Zddd�ZdS)� Traceback)�_frames� _total_nframeNcCs"t�|�tt|��|_||_dSr)rr�tuple�reversedrFrG)r�frames� total_nframerrrr�s zTraceback.__init__cCs|jSr)rGrrrrrK�szTraceback.total_nframecCs t|j�Sr)�lenrFrrrr�__len__�szTraceback.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdSr)r8��.0�tracerrr� ��z(Traceback.__getitem__..)r�slicerHrFr8�r�indexrrr� __getitem__�s zTraceback.__getitem__cCs |j|jvSr)r:rFr;rrr� __contains__�szTraceback.__contains__cCs t|j�Sr)rrFrrrrr�szTraceback.__hash__cCst|t�stS|j|jkSr�rrErrFrrrrr�s zTraceback.__eq__cCst|t�stS|j|jkSrrXrrrrrB�s zTraceback.__lt__cCs t|d�Sr/)�strrrrrr#�szTraceback.__str__cCs8dt|���}|jdur"|d7}n|d|j�d�7}|S)Nz z total_nframe=)rHrGrK)r�srrrr$�s   zTraceback.__repr__FcCs�g}|dur2|dkr$|| d�}q6|d|�}n|}|rBt|�}|D]@}|�d|j|jf�t�|j|j���}|rF|�d|�qF|S)Nrz File "%s", line %sz %s)rIr2r=r@� linecache�getline�strip)r�limitZmost_recent_first�linesZ frame_slicer<�linerrr�format�s  �zTraceback.format)N)NF)r'r(r)r*rrDrKrMrVrWrrrBr#r$rbrrrrrE�s  rEcCs t|�}|durt|�SdSdSr)rrE)�objrJrrr�get_object_tracebacksrdc@s\eZdZdZdd�Zedd��Zedd��Zedd ��Zd d �Z d d �Z dd�Z dd�Z dS)�Trace��_tracecCs ||_dSrrf�rrPrrrrszTrace.__init__cCs |jdSr/rfrrrr�domainsz Trace.domaincCs |jdSr>rfrrrrr sz Trace.sizecCst|jdd��S)N�)rErgrrrrr#szTrace.tracebackcCst|t�stS|j|jkSr)rrerrgrrrrr's z Trace.__eq__cCs t|j�Sr)rrgrrrrr,szTrace.__hash__cCsd|jt|jd�fS)Nz%s: %sF)rrr rrrrr#/sz Trace.__str__cCsd|jt|jd�|jfS)Nz'F)rirr rrrrrr$2s�zTrace.__repr__N) r'r(r)r*rrDrir rrrr#r$rrrrres   rec@s<eZdZdd�Zdd�Zdd�Zdd�Zd d �Zd d �Zd S)�_TracescCst�|�||_dSr)rr�_traces)r�tracesrrrr8s z_Traces.__init__cCs t|j�Sr)rLrlrrrrrM=sz_Traces.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdSr)rerNrrrrQBrRz&_Traces.__getitem__..)rrSrHrlrerTrrrrV@s z_Traces.__getitem__cCs |j|jvSr)rgrlrhrrrrWFsz_Traces.__contains__cCst|t�stS|j|jkSr)rrkrrlrrrrrIs z_Traces.__eq__cCs dt|�S)Nz)rLrrrrr$Nsz_Traces.__repr__N) r'r(r)rrMrVrWrr$rrrrrk7s rkcCs&tj�|�}|�d�r"|dd�}|S)Nz.pyc�����)�os�path�normcase�endswith)r=rrr�_normalize_filenameRs   rsc@seZdZdd�Zdd�ZdS)� BaseFiltercCs ||_dSr)� inclusive)rrurrrrZszBaseFilter.__init__cCst�dSr)�NotImplementedErrorrhrrr�_match]szBaseFilter._matchN)r'r(r)rrwrrrrrtYsrtcsJeZdZd�fdd� Zedd��Zdd�Zd d �Zd d �Zd d�Z �Z S)�FilterNFcs2t��|�||_t|�|_||_||_||_dSr)�superrrurs�_filename_patternr@� all_framesri)rru�filename_patternr@r{ri�� __class__rrrbs   zFilter.__init__cCs|jSr)rzrrrrr|kszFilter.filename_patterncCs6t|�}t�||j�sdS|jdur(dS||jkSdS)NFT)rs�fnmatchrzr@�rr=r@rrr�_match_frame_implos  zFilter._match_frame_implcCs|�||�|j ASr)r�rur�rrr� _match_framexszFilter._match_framecsH�jr,t�fdd�|D��r"�jS�j Sn|d\}}��||�SdS)Nc3s|]\}}��||�VqdSr)r�)rOr=r@rrrrQ}s�z*Filter._match_traceback..r)r{�anyrur�)rrr=r@rrr�_match_traceback{s �  zFilter._match_tracebackcCsF|\}}}}|�|�}|jdurB|jr4|o2||jkS|p@||jkS|Sr)r�riru)rrPrir rrK�resrrrrw�s   z Filter._match)NFN) r'r(r)rrDr|r�r�r�rw� __classcell__rrr}rrxas�    rxcs0eZdZ�fdd�Zedd��Zdd�Z�ZS)� DomainFiltercst��|�||_dSr)ryr�_domain)rrurir}rrr�s zDomainFilter.__init__cCs|jSr)r�rrrrri�szDomainFilter.domaincCs|\}}}}||jk|j ASr)riru)rrPrir rrKrrrrw�s zDomainFilter._match)r'r(r)rrDrirwr�rrr}rr��s  r�c@sTeZdZdd�Zdd�Zedd��Zdd�Zd d �Zd d �Z ddd�Z ddd�Z dS)�SnapshotcCst|�|_||_dSr)rkrm�traceback_limit)rrmr�rrrr�s zSnapshot.__init__cCs>t|d�� }t�||tj�Wd�n1s00YdS)N�wb)�open�pickle�dump�HIGHEST_PROTOCOL)rr=�fprrrr��s z Snapshot.dumpcCs8t|d��}t�|�Wd�S1s*0YdS)N�rb)r�r��load)r=r�rrrr��s z Snapshot.loadcs@|rt�fdd�|D��sdS|r.Fc3s|]}|��� VqdSrr�r�r�rrrQ�s�T)r�)r�include_filters�exclude_filtersrPrr�r� _filter_trace�s � �zSnapshot._filter_tracecs�t|t�stdt|�j��|rjg�g�|D] }|jrB��|�q,��|�q,���fdd��jjD�}n �jj� �}t |�j �S)Nz)filters must be a list of filters, not %scsg|]}����|�r|�qSr)r�rN�r�r�rrr� �s ��z*Snapshot.filter_traces..) rr� TypeError�typer'rur2rmrl�copyr�r�)r�filtersr�Z new_tracesrr�r� filter_traces�s �   zSnapshot.filter_tracesc Cs�|dvrtd|f��|r.|dvr.td|��i}i}|�s |jjD]�}|\}}}} z ||} WnXty�|dkr||} n(|dkr�|dd�} n|dddff} t| �} | ||<Yn0z(|| } | j|7_| jd7_WqDt�yt| |d�|| <YqD0qDn�|jjD]�}|\}}}} |D]�} z || } WnDt�y||dk�rZ| f} n| ddff} t| �} | || <Yn0z(|| } | j|7_| jd7_Wn$t�y�t| |d�|| <Yn0�q(�q|S) N)rr=r@zunknown key_type: %r)r@r=z/cumulative mode cannot by used with key type %rrr@r?r)� ValueErrorrmrl�KeyErrorrEr rr)r�key_type� cumulativeZstatsZ tracebacksrPrir Ztrace_tracebackrKrrJr6r<rrr� _group_by�sZ �        zSnapshot._group_byFcCs,|�||�}t|���}|jdtjd�|S�NT)�reverse�key)r��list�values�sortrr%)rr�r�Zgroupedr5rrrr5s  zSnapshot.statisticscCs6|�||�}|�||�}t||�}|jdtjd�|Sr�)r�r7r�r+r%)rZ old_snapshotr�r�r4r3r5rrr� compare_tos    zSnapshot.compare_toN)F)F) r'r(r)rr�� staticmethodr�r�r�r�r5r�rrrrr��s  3 r�cCs$t�std��t�}t�}t||�S)NzLthe tracemalloc module must be tracing memory allocations to take a snapshot)� is_tracing� RuntimeErrorrZget_traceback_limitr�)rmr�rrr� take_snapshot's r�)�collections.abcrr� functoolsrrr\Zos.pathror�Z _tracemallocrrrrr+r7r8rErdrerkrsrtrxr�r�r�rrrr�s2 (2&M'0