o w[eF@sddlmZmZddlmZddlZddlZddlZddl Z ddl Tddl m Z m Z ddZ Gdd d ZGd d d Zd d ZeGdddZeGdddeZddZGdddZGdddeZddZGdddZGdddeZGdddeZGd d!d!Zd"d#ZdS)$)SequenceIterable)total_orderingN)*)_get_object_traceback _get_tracescCsdD]>}t|dkr |dkr |rd||fSd||fSt|dks*|dkr<|r4d||fSd ||fS|d }qdS) N)BKiBMiBGiBTiBdrz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)abs)sizesignunitr"/usr/lib/python3.10/tracemalloc.py _format_size s rc@DeZdZdZdZddZddZddZd d Zd d Z d dZ dS) StatisticzS Statistic difference on memory allocations between two Snapshot instance.  tracebackrcountcCs||_||_||_dSNr)selfrrrrrr__init__%s zStatistic.__init__cCst|j|j|jfSr)hashrrrrrrr__hash__*zStatistic.__hash__cCs2t|tstS|j|jko|j|jko|j|jkSr) isinstancerNotImplementedrrrrotherrrr__eq__-s    zStatistic.__eq__cCsBd|jt|jd|jf}|jr|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iF , average=%s)rrrrrtextaveragerrr__str__4s  zStatistic.__str__cCsd|j|j|jfS)Nz)rrrrr__repr__>szStatistic.__repr__cCs|j|j|jfSr)rrrrrrr _sort_keyBzStatistic._sort_keyN __name__ __module__ __qualname____doc__ __slots__rrr%r*r+r,rrrrrs rc@r) StatisticDiffzd Statistic difference on memory allocations between an old and a new Snapshot instance. rr size_diffr count_diffcCs"||_||_||_||_||_dSrr5)rrrr6rr7rrrrMs  zStatisticDiff.__init__cCst|j|j|j|j|jfSr)rrrr6rr7rrrrrTszStatisticDiff.__hash__cCsJt|tstS|j|jko$|j|jko$|j|jko$|j|jko$|j|jkSr)r!r4r"rrr6rr7r#rrrr%Xs      zStatisticDiff.__eq__cCsPd|jt|jdt|jd|j|jf}|jr&|j|j}|dt|d7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTr&)rrrr6rr7r'rrrr*as   zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9r5rrrrr+ms  zStatisticDiff.__repr__cCs t|j|jt|j|j|jfSr)rr6rr7rrrrrrr,rs  zStatisticDiff._sort_keyNr.rrrrr4Fs  r4cCsg}|D]2\}}||d}|dur't||j|j|j|j|j|j}n t||j|j|j|j}||q|D]\}}t|d|j d|j }||q=|SNr)itemspopr4rrappend) old_group new_group statisticsrstatpreviousrrr_compare_grouped_statsxs"   rAc@s\eZdZdZdZddZeddZeddZd d Z d d Z d dZ ddZ ddZ dS)Framez Frame of a traceback. _framecC ||_dSrrCrframerrrr zFrame.__init__cC |jdSr8rCrrrrfilenamerHzFrame.filenamecCrINrCrrrrlinenorHz Frame.linenocCt|tstS|j|jkSrr!rBr"rDr#rrrr%  z Frame.__eq__cCt|tstS|j|jkSrrOr#rrr__lt__rPz Frame.__lt__cC t|jSr)rrDrrrrr zFrame.__hash__cCd|j|jfS)Nz%s:%srJrMrrrrr*r-z Frame.__str__cCrU)NzrVrrrrr+r-zFrame.__repr__N)r/r0r1r2r3rpropertyrJrMr%rRrr*r+rrrrrBs   rBc@steZdZdZdZdddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZdddZdS) Tracebackz` Sequence of Frame instances sorted from the oldest frame to the most recent frame. )_frames _total_nframeNcCs"t|tt||_||_dSr)rrtuplereversedrYrZ)rframes total_nframerrrrs  zTraceback.__init__cC|jSr)rZrrrrr^zTraceback.total_nframecCrSr)lenrYrrrr__len__rTzTraceback.__len__cC0t|trtdd|j|DSt|j|S)Ncs|]}t|VqdSr)rB.0tracerrr z(Traceback.__getitem__..)r!slicer[rYrBrindexrrr __getitem__ zTraceback.__getitem__cC |j|jvSr)rDrYrFrrr __contains__ zTraceback.__contains__cCrSr)rrYrrrrrrTzTraceback.__hash__cCrNrr!rXr"rYr#rrrr%rPzTraceback.__eq__cCrQrrrr#rrrrRrPzTraceback.__lt__cCs t|dSr8)strrrrrr*rqzTraceback.__str__cCs:dt|}|jdur|d7}|S|d|jd7}|S)Nz z total_nframe=)r[rZr^)rsrrrr+s  zTraceback.__repr__FcCsg}|dur|dkr|| d}n |d|}n|}|r!t|}|D] }|d|j|jft|j|j}|rC|d|q#|S)Nrz File "%s", line %sz %s)r\r;rJrM linecachegetlinestrip)rlimitmost_recent_firstlines frame_slicerGlinerrrformats" zTraceback.formatr)NF)r/r0r1r2r3rrWr^rbrmrprr%rRr*r+r~rrrrrXs  rXcCst|}|dur t|SdS)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)rrX)objr]rrrget_object_tracebacksrc@s`eZdZdZdZddZeddZeddZed d Z d d Z d dZ ddZ ddZ dS)Tracez" Trace of a memory block. _tracecCrErrrrgrrrrs zTrace.__init__cCrIr8rrrrrdomainrHz Trace.domaincCrIrKrrrrrrrHz Trace.sizecCst|jddS)N)rXrrrrrr#szTrace.tracebackcCrNr)r!rr"rr#rrrr%'rPz Trace.__eq__cCrSr)rrrrrrr,rTzTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrrrrrrr*/sz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rrrrrrrrr+2szTrace.__repr__N)r/r0r1r2r3rrWrrrr%rr*r+rrrrrs    rc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dSr)rr_traces)rtracesrrrr8s  z_Traces.__init__cCrSr)rarrrrrrb=rTz_Traces.__len__cCrc)Ncsrdr)rrerrrrhBriz&_Traces.__getitem__..)r!rjr[rrrkrrrrm@rnz_Traces.__getitem__cCror)rrrrrrrpFrqz_Traces.__contains__cCrNr)r!rr"rr#rrrr%IrPz_Traces.__eq__cCs dt|S)Nz)rarrrrr+Nrqz_Traces.__repr__N) r/r0r1rrbrmrpr%r+rrrrr7s rcCs&tj|}|dr|dd}|S)Nz.pyc)ospathnormcaseendswith)rJrrr_normalize_filenameRs   rc@seZdZddZddZdS) BaseFiltercCrEr) inclusive)rrrrrrZrTzBaseFilter.__init__cCstr)NotImplementedErrorrrrr_match]szBaseFilter._matchN)r/r0r1rrrrrrrYs rcsLeZdZ dfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dSr)superrrr_filename_patternrM all_framesr)rrfilename_patternrMrr __class__rrrbs   zFilter.__init__cCr_r)rrrrrrkr`zFilter.filename_patterncCs2t|}t||js dS|jdurdS||jkS)NFT)rfnmatchrrMrrJrMrrr_match_frame_implos   zFilter._match_frame_implcCs||||j ASr)rrrrrr _match_framexr zFilter._match_framecsBjrtfdd|DrjSj S|d\}}||S)Nc3s |] \}}||VqdSr)r)rfrJrMrrrrh}sz*Filter._match_traceback..r)ranyrr)rrrJrMrrr_match_traceback{s   zFilter._match_tracebackcCsF|\}}}}||}|jdur!|jr|o||jkS|p ||jkS|Sr)rrr)rrgrrrr^resrrrrs   z Filter._match)NFN) r/r0r1rrWrrrrr __classcell__rrrrras   rcs0eZdZfddZeddZddZZS) DomainFiltercst|||_dSr)rr_domain)rrrrrrrs  zDomainFilter.__init__cCr_r)rrrrrrr`zDomainFilter.domaincCs|\}}}}||jk|j ASr)rr)rrgrrrr^rrrrs zDomainFilter._match)r/r0r1rrWrrrrrrrrs   rc@sXeZdZdZddZddZeddZdd Zd d Z d d Z dddZ dddZ dS)SnapshotzB Snapshot of traces of memory blocks allocated by Python. cCst||_||_dSr)rrtraceback_limit)rrrrrrrs  zSnapshot.__init__cCs@t|d}t||tjWddS1swYdS)z1 Write the snapshot into a file. wbN)openpickledumpHIGHEST_PROTOCOL)rrJfprrrrs "z Snapshot.dumpcCs8t|d }t|WdS1swYdS)z. Load a snapshot from a file. rbN)rrload)rJrrrrrs $z Snapshot.loadcs@|rtfdd|DsdS|rtfdd|DrdSdS)Nc3s|]}|VqdSrrrf trace_filterrgrrrhsz)Snapshot._filter_trace..Fc3s|] }| VqdSrrrrrrrhsT)r)rinclude_filtersexclude_filtersrgrrr _filter_traces  zSnapshot._filter_tracecst|tstdt|j|r5gg|D]}|jr!|q|qfddjjD}njj }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)rrerrrrr s  z*Snapshot.filter_traces..) r!r TypeErrortyper/rr;rrcopyrr)rfiltersr new_tracesrrr filter_tracess     zSnapshot.filter_tracesc Cs|dvr td|f|r|dvrtd|i}i}|s|jjD]`}|\}}}} z||} Wn,ty[|dkr=|} n|dkrH|dd} n |dddff} t| } | ||<Ynwz|| } | j|7_| jd7_Wq!tyt| |d|| <Yq!w|S|jjD]Y}|\}}}} |D]N} z|| } Wn ty|dkr| f} n| ddff} t| } | || <Ynwz|| } | j|7_| jd7_Wqtyt| |d|| <Yqwq|S) N)rrJrMzunknown key_type: %r)rMrJz/cumulative mode cannot by used with key type %rrrMrLr) ValueErrorrrKeyErrorrXrrr)rkey_type cumulativestats tracebacksrgrrtrace_tracebackr^rr]r?rGrrr _group_bysf             zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)zd Group statistics by key_type. Return a sorted list of Statistic instances. Treversekey)rlistvaluessortrr,)rrrgroupedr>rrrr>s  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)rrArr4r,)r old_snapshotrrr=r<r>rrr compare_tos   zSnapshot.compare_toN)F) r/r0r1r2rr staticmethodrrrrr>rrrrrrs   3 rcCs$tstdt}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 RuntimeErrorrget_traceback_limitr)rrrrr take_snapshot's  r)collections.abcrr functoolsrrrvos.pathrr _tracemallocrrrrr4rArBrXrrrrrrrrrrrrrs4 (2&M'0