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@sDeZdZdZdZdd�Zdd�Zdd�Zd d �Zd d �Z d d�Z dS)� StatisticzS Statistic difference on memory allocations between two Snapshot instance. �� 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__�__doc__� __slots__rrrr#r$r%rrrrrs rc@sDeZdZdZdZdd�Zdd�Zdd�Zd d �Zd d �Z d d�Z dS)� StatisticDiffzd Statistic difference on memory allocations between an old and a new Snapshot instance. �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" ��  r8c@s\eZdZdZdZdd�Zedd��Zedd��Zd d �Z d d �Z d d�Z dd�Z dd�Z dS)�Framez Frame of a traceback. ��_framecCs ||_dSrr:�r�framerrrr�szFrame.__init__cCs |jdSr0r:rrrr�filename�szFrame.filenamecCs |jdS�N�r:rrrr�lineno�sz Frame.linenocCst|t�stS|j|jkSr�rr9rr;rrrrr�s z Frame.__eq__cCst|t�stS|j|jkSrrBrrrr�__lt__�s z Frame.__lt__cCs t|j�Sr)rr;rrrrr�szFrame.__hash__cCsd|j|jfS)Nz%s:%s�r>rArrrrr#�sz Frame.__str__cCsd|j|jfS)NzrDrrrrr$�szFrame.__repr__N)r'r(r)r*r+r�propertyr>rArrCrr#r$rrrrr9�s  r9c@steZdZdZdZddd�Zedd��Zdd �Zd d �Z d d �Z dd�Z dd�Z dd�Z dd�Zdd�Zddd�ZdS)� Tracebackz` Sequence of Frame instances sorted from the oldest frame to the most recent frame. )�_frames� _total_nframeNcCs"t�|�tt|��|_||_dSr)rr�tuple�reversedrGrH)r�frames� total_nframerrrr�s zTraceback.__init__cCs|jSr)rHrrrrrL�szTraceback.total_nframecCs t|j�Sr)�lenrGrrrr�__len__�szTraceback.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdSr)r9��.0�tracerrr� ��z(Traceback.__getitem__..)r�slicerIrGr9�r�indexrrr� __getitem__�s zTraceback.__getitem__cCs |j|jvSr)r;rGr<rrr� __contains__�szTraceback.__contains__cCs t|j�Sr)rrGrrrrr�szTraceback.__hash__cCst|t�stS|j|jkSr�rrFrrGrrrrr�s zTraceback.__eq__cCst|t�stS|j|jkSrrYrrrrrC�s zTraceback.__lt__cCs t|d�Sr0)�strrrrrr#�szTraceback.__str__cCs8dt|���}|jdur"|d7}n|d|j�d�7}|S)Nz z total_nframe=)rIrHrL)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)rJr3r>rA� linecache�getline�strip)r�limitZmost_recent_first�linesZ frame_slicer=�linerrr�format�s  �zTraceback.format)N)NF)r'r(r)r*r+rrErLrNrWrXrrrCr#r$rcrrrrrF�s  rFcCs t|�}|durt|�SdSdS)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)rrF)�objrKrrr�get_object_tracebacksrec@s`eZdZdZdZdd�Zedd��Zedd��Zed d ��Z d d �Z d d�Z dd�Z dd�Z dS)�Tracez" Trace of a memory block. ��_tracecCs ||_dSrrg�rrQrrrrszTrace.__init__cCs |jdSr0rgrrrr�domainsz Trace.domaincCs |jdSr?rgrrrrr sz Trace.sizecCst|jdd��S)N�)rFrhrrrrr#szTrace.tracebackcCst|t�stS|j|jkSr)rrfrrhrrrrr's z Trace.__eq__cCs t|j�Sr)rrhrrrrr,szTrace.__hash__cCsd|jt|jd�fS)Nz%s: %sF)rrr rrrrr#/sz Trace.__str__cCsd|jt|jd�|jfS)Nz'F)rjrr rrrrrr$2s�zTrace.__repr__N)r'r(r)r*r+rrErjr rrrr#r$rrrrrfs   rfc@s<eZdZdd�Zdd�Zdd�Zdd�Zd d �Zd d �Zd S)�_TracescCst�|�||_dSr)rr�_traces)r�tracesrrrr8s z_Traces.__init__cCs t|j�Sr)rMrmrrrrrN=sz_Traces.__len__cCs4t|t�r"tdd�|j|D��St|j|�SdS)Ncss|]}t|�VqdSr)rfrOrrrrRBrSz&_Traces.__getitem__..)rrTrIrmrfrUrrrrW@s z_Traces.__getitem__cCs |j|jvSr)rhrmrirrrrXFsz_Traces.__contains__cCst|t�stS|j|jkSr)rrlrrmrrrrrIs z_Traces.__eq__cCs dt|�S)Nz)rMrrrrr$Nsz_Traces.__repr__N) r'r(r)rrNrWrXrr$rrrrrl7s rlcCs&tj�|�}|�d�r"|dd�}|S)Nz.pyc�����)�os�path�normcase�endswith)r>rrr�_normalize_filenameRs   rtc@seZdZdd�Zdd�ZdS)� BaseFiltercCs ||_dSr)� inclusive)rrvrrrrZszBaseFilter.__init__cCst�dSr)�NotImplementedErrorrirrr�_match]szBaseFilter._matchN)r'r(r)rrxrrrrruYsrucsJeZdZd�fdd� Zedd��Zdd�Zd d �Zd d �Zd d�Z �Z S)�FilterNFcs2t��|�||_t|�|_||_||_||_dSr)�superrrvrt�_filename_patternrA� all_framesrj)rrv�filename_patternrAr|rj�� __class__rrrbs   zFilter.__init__cCs|jSr)r{rrrrr}kszFilter.filename_patterncCs6t|�}t�||j�sdS|jdur(dS||jkSdS)NFT)rt�fnmatchr{rA�rr>rArrr�_match_frame_implos  zFilter._match_frame_implcCs|�||�|j ASr)r�rvr�rrr� _match_framexszFilter._match_framecsH�jr,t�fdd�|D��r"�jS�j Sn|d\}}��||�SdS)Nc3s|]\}}��||�VqdSr)r�)rPr>rArrrrR}s�z*Filter._match_traceback..r)r|�anyrvr�)rrr>rArrr�_match_traceback{s �  zFilter._match_tracebackcCsF|\}}}}|�|�}|jdurB|jr4|o2||jkS|p@||jkS|Sr)r�rjrv)rrQrjr rrL�resrrrrx�s   z Filter._match)NFN) r'r(r)rrEr}r�r�r�rx� __classcell__rrr~rryas�    rycs0eZdZ�fdd�Zedd��Zdd�Z�ZS)� DomainFiltercst��|�||_dSr)rzr�_domain)rrvrjr~rrr�s zDomainFilter.__init__cCs|jSr)r�rrrrrj�szDomainFilter.domaincCs|\}}}}||jk|j ASr)rjrv)rrQrjr rrLrrrrx�s zDomainFilter._match)r'r(r)rrErjrxr�rrr~rr��s  r�c@sXeZdZdZdd�Zdd�Zedd��Zdd �Zd d �Z d d �Z ddd�Z ddd�Z dS)�SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst|�|_||_dSr)rlrn�traceback_limit)rrnr�rrrr�s zSnapshot.__init__cCs>t|d�� }t�||tj�Wd�n1s00YdS)z1 Write the snapshot into a file. �wbN)�open�pickle�dump�HIGHEST_PROTOCOL)rr>�fprrrr��s z Snapshot.dumpcCs8t|d��}t�|�Wd�S1s*0YdS)z. Load a snapshot from a file. �rbN)r�r��load)r>r�rrrr��s z Snapshot.loadcs@|rt�fdd�|D��sdS|r.Fc3s|]}|��� VqdSrr�r�r�rrrR�s�T)r�)r�include_filters�exclude_filtersrQrr�r� _filter_trace�s � �zSnapshot._filter_tracecs�t|t�stdt|�j��|rjg�g�|D] }|jrB��|�q,��|�q,���fdd��jjD�}n �jj� �}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 %scsg|]}����|�r|�qSr)r�rO�r�r�rrr� �s ��z*Snapshot.filter_traces..) rr� TypeError�typer'rvr3rnrm�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>rAzunknown key_type: %r)rAr>z/cumulative mode cannot by used with key type %rrrAr@r)� ValueErrorrnrm�KeyErrorrFr rr)r�key_type� cumulativeZstatsZ tracebacksrQrjr Ztrace_tracebackrLrrKr7r=rrr� _group_by�sZ �        zSnapshot._group_byFcCs,|�||�}t|���}|jdtjd�|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. T��reverse�key)r��list�values�sortrr%)rr�r�Zgroupedr6rrrr6s  zSnapshot.statisticscCs6|�||�}|�||�}t||�}|jdtjd�|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�r8r�r,r%)rZ old_snapshotr�r�r5r4r6rrr� compare_tos    zSnapshot.compare_toN)F)F) r'r(r)r*rr�� staticmethodr�r�r�r�r6r�rrrrr��s  3 r�cCs$t�std��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� RuntimeErrorrZget_traceback_limitr�)rnr�rrr� take_snapshot's r�)�collections.abcrr� functoolsrr�r]Zos.pathrpr�Z _tracemallocrrrrr,r8r9rFrerfrlrtruryr�r�r�rrrr�s2 (2&M'0