B  ZcHu@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_tracescCszxtdD]l}t|dkr:|dkr:|r.d||fSd||fSt|dksN|dkrj|r^d||fSd ||fS|d }qWdS) N)BZKiBZMiBZGiBTiBdrz%+.1f %sz%.1f %si(r z%+.0f %sz%.0f %si)abs)sizesignZunitr./opt/alt/python37/lib/python3.7/tracemalloc.py _format_size s     rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) Statistic) tracebackr countcCs||_||_||_dS)N)rr r)selfrr rrrr__init__%szStatistic.__init__cCst|j|j|jfS)N)hashrr r)rrrr__hash__*szStatistic.__hash__cCs$|j|jko"|j|jko"|j|jkS)N)rr r)rotherrrr__eq__-s  zStatistic.__eq__cCsBd|jt|jd|jf}|jr>|j|j}|dt|d7}|S)Nz%s: size=%s, count=%iFz , average=%s)rrr r)rtextaveragerrr__str__2s   zStatistic.__str__cCsd|j|j|jfS)Nz))rr r)rrrr__repr__<szStatistic.__repr__cCs|j|j|jfS)N)r rr)rrrr _sort_key@szStatistic._sort_keyN) __name__ __module__ __qualname__ __slots__rrrrrrrrrrrs rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) StatisticDiff)rr size_diffr count_diffcCs"||_||_||_||_||_dS)N)rr r$rr%)rrr r$rr%rrrrKs zStatisticDiff.__init__cCst|j|j|j|j|jfS)N)rrr r$rr%)rrrrrRszStatisticDiff.__hash__cCs<|j|jko:|j|jko:|j|jko:|j|jko:|j|jkS)N)rr r$rr%)rrrrrrVs     zStatisticDiff.__eq__cCsPd|jt|jdt|jd|j|jf}|jrL|j|j}|dt|d7}|S)Nz %s: size=%s (%s), count=%i (%+i)FTz , average=%s)rrr r$rr%)rrrrrrr]s    zStatisticDiff.__str__cCsd|j|j|j|j|jfS)Nz9)rr r$rr%)rrrrris zStatisticDiff.__repr__cCs t|j|jt|j|j|jfS)N)r r$r r%rr)rrrrrns  zStatisticDiff._sort_keyN) rr r!r"rrrrrrrrrrr#Ds r#cCsg}xp|D]d\}}||d}|dk rPt||j|j|j|j|j|j}nt||j|j|j|j}||qWx6|D]*\}}t|d|j d|j }||qW|S)Nr)itemspopr#r rappend) old_group new_group statisticsrstatZpreviousrrr_compare_grouped_statsts  r-c@sXeZdZdZddZeddZeddZdd Zd d Z d d Z ddZ ddZ dS)Frame)_framecCs ||_dS)N)r/)rframerrrrszFrame.__init__cCs |jdS)Nr)r/)rrrrfilenameszFrame.filenamecCs |jdS)N)r/)rrrrlinenosz Frame.linenocCs |j|jkS)N)r/)rrrrrrsz Frame.__eq__cCs |j|jkS)N)r/)rrrrr__lt__sz Frame.__lt__cCs t|jS)N)rr/)rrrrrszFrame.__hash__cCsd|j|jfS)Nz%s:%s)r1r3)rrrrrsz Frame.__str__cCsd|j|jfS)Nz)r1r3)rrrrrszFrame.__repr__N) rr r!r"rpropertyr1r3rr4rrrrrrrr.s  r.c@sbeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ dS) Traceback)_framescCst|tt||_dS)N)rrtuplereversedr7)rframesrrrrs zTraceback.__init__cCs t|jS)N)lenr7)rrrr__len__szTraceback.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdS)N)r.).0tracerrr sz(Traceback.__getitem__..) isinstanceslicer8r7r.)rindexrrr __getitem__s zTraceback.__getitem__cCs |j|jkS)N)r/r7)rr0rrr __contains__szTraceback.__contains__cCs t|jS)N)rr7)rrrrrszTraceback.__hash__cCs |j|jkS)N)r7)rrrrrrszTraceback.__eq__cCs |j|jkS)N)r7)rrrrrr4szTraceback.__lt__cCs t|dS)Nr)str)rrrrrszTraceback.__str__cCsdt|fS)Nz)r8)rrrrrszTraceback.__repr__NFcCsg}|dk r2|dkr$|| d}q6|d|}n|}|rBt|}xH|D]@}|d|j|jft|j|j}|rH|d|qHW|S)Nrz File "%s", line %sz %s)r9r(r1r3 linecachegetlinestrip)rlimitZmost_recent_firstlinesZ frame_slicer0linerrrformats zTraceback.format)NF)rr r!r"rr<rCrDrrr4rrrLrrrrr6sr6cCs t|}|dk rt|SdSdS)N)rr6)objr:rrrget_object_tracebacksrNc@s\eZdZdZddZeddZeddZedd Zd d Z d d Z ddZ ddZ dS)Trace)_tracecCs ||_dS)N)rP)rr>rrrrszTrace.__init__cCs |jdS)Nr)rP)rrrrdomainsz Trace.domaincCs |jdS)Nr2)rP)rrrrr sz Trace.sizecCst|jdS)N)r6rP)rrrrr szTrace.tracebackcCs |j|jkS)N)rP)rrrrrrsz Trace.__eq__cCs t|jS)N)rrP)rrrrrszTrace.__hash__cCsd|jt|jdfS)Nz%s: %sF)rrr )rrrrrsz Trace.__str__cCsd|jt|jd|jfS)Nz'F)rQrr r)rrrrrszTrace.__repr__N) rr r!r"rr5rQr rrrrrrrrrrOs   rOc@s<eZdZddZddZddZddZd d Zd d Zd S)_TracescCst|||_dS)N)rr_traces)rtracesrrrr s z_Traces.__init__cCs t|jS)N)r;rT)rrrrr<%sz_Traces.__len__cCs4t|tr"tdd|j|DSt|j|SdS)Ncss|]}t|VqdS)N)rO)r=r>rrrr?*sz&_Traces.__getitem__..)r@rAr8rTrO)rrBrrrrC(s z_Traces.__getitem__cCs |j|jkS)N)rPrT)rr>rrrrD.sz_Traces.__contains__cCs |j|jkS)N)rT)rrrrrr1sz_Traces.__eq__cCs dt|S)Nz)r;)rrrrr4sz_Traces.__repr__N) rr r!rr<rCrDrrrrrrrSs rScCs&tj|}|dr"|dd}|S)Nz.pyc)ospathnormcaseendswith)r1rrr_normalize_filename8s   r[c@seZdZddZddZdS) BaseFiltercCs ||_dS)N) inclusive)rr]rrrr@szBaseFilter.__init__cCstdS)N)NotImplementedError)rr>rrr_matchCszBaseFilter._matchN)rr r!rr_rrrrr\?sr\csJeZdZdfdd ZeddZddZd d Zd d Zd dZ Z S)FilterNFcs2t|||_t||_||_||_||_dS)N)superrr]r[_filename_patternr3 all_framesrQ)rr]filename_patternr3rcrQ) __class__rrrHs   zFilter.__init__cCs|jS)N)rb)rrrrrdQszFilter.filename_patterncCs6t|}t||jsdS|jdkr(dS||jkSdS)NFT)r[fnmatchrbr3)rr1r3rrr_match_frame_implUs  zFilter._match_frame_implcCs||||j AS)N)rgr])rr1r3rrr _match_frame^szFilter._match_framecsHjr,tfdd|Dr"jSj Sn|d\}}||SdS)Nc3s|]\}}||VqdS)N)rg)r=r1r3)rrrr?csz*Filter._match_traceback..r)rcanyr]rh)rrr1r3r)rr_match_tracebackas    zFilter._match_tracebackcCsD|\}}}||}|jdk r@|jr2|o0||jkS|p>||jkS|S)N)rjrQr])rr>rQr rZresrrrr_ls   z Filter._match)NFN) rr r!rr5rdrgrhrjr_ __classcell__rr)rerr`Gs    r`cs0eZdZfddZeddZddZZS) DomainFiltercst|||_dS)N)rar_domain)rr]rQ)rerrrxs zDomainFilter.__init__cCs|jS)N)rm)rrrrrQ|szDomainFilter.domaincCs|\}}}||jk|j AS)N)rQr])rr>rQr rrrrr_s zDomainFilter._match)rr r!rr5rQr_rkrr)rerrlws  rlc@sTeZdZddZddZeddZddZd d Zd d Z dddZ dddZ dS)SnapshotcCst||_||_dS)N)rSrUtraceback_limit)rrUrorrrrs zSnapshot.__init__c Cs*t|d}t||tjWdQRXdS)Nwb)openpickledumpZHIGHEST_PROTOCOL)rr1fprrrrss z Snapshot.dumpc Cs t|d }t|SQRXdS)Nrb)rqrrload)r1rtrrrrvs z Snapshot.loadcs@|rtfdd|DsdS|rrrr?sz)Snapshot._filter_trace..Fc3s|]}| VqdS)N)r_)r=rw)r>rrr?sT)ri)rinclude_filtersexclude_filtersr>r)r>r _filter_traces    zSnapshot._filter_tracecst|tstdt|j|rnggx(|D] }|jrD|q.|q.WfddjjD}n jj }t |j S)Nz)filters must be a list of filters, not %scsg|]}|r|qSr)rz)r=r>)ryrxrrr sz*Snapshot.filter_traces..) r@r TypeErrortyperr]r(rUrTcopyrnro)rfiltersrwZ new_tracesr)ryrxrr filter_tracess    zSnapshot.filter_tracesc Cs|dkrtd|f|r.|dkr.td|i}i}|sx|jjD]}|\}}}y ||} WnZtk r|dkr~|} n(|dkr|dd} n|dddff} t| } | ||<YnXy(|| } | j|7_| jd7_WqFtk r t| |d|| <YqFXqFWnx|jjD]}|\}}}x|D]} y || } WnFtk r|dkrd| f} n| ddff} t| } | || <YnXy(|| } | j|7_| jd7_Wn&tk rt| |d|| <YnXq0WqW|S) N)rr1r3zunknown key_type: %r)r3r1z/cumulative mode cannot by used with key type %rrr3r2r) ValueErrorrUrTKeyErrorr6r rr) rkey_type cumulativeZstatsZ tracebacksr>rQr Ztrace_tracebackrr:r,r0rrr _group_bysX       "zSnapshot._group_byFcCs,|||}t|}|jdtjd|S)NT)reversekey)rlistvaluessortrr)rrrZgroupedr+rrrr+s  zSnapshot.statisticscCs6|||}|||}t||}|jdtjd|S)NT)rr)rr-rr#r)rZ old_snapshotrrr*r)r+rrr compare_tos    zSnapshot.compare_toN)F)F) rr r!rrs staticmethodrvrzrrr+rrrrrrns  3 rncCs$tstdt}t}t||S)NzLthe tracemalloc module must be tracing memory allocations to take a snapshot) is_tracing RuntimeErrorrZget_traceback_limitrn)rUrorrr take_snapshot s r)Zcollections.abcrr functoolsrrfrFZos.pathrWrrZ _tracemallocrrrrr#r-r.r6rNrOrSr[r\r`rlrnrrrrrs0 &0# ?%0