
g[\z                 @   sl  d  Z  d d g Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l m Z y d d l m Z Wn" e k
 r d d l m Z Yn Xy d d l Z Wn' e k
 re j Z d d	   Z Yn Xd
 d   Z d d	   Z d d   Z d Z e j d  Z Gd d   d  Z d d   Z d d   Z Gd d   d  Z d d   Z d d   Z  d d d  Z! d d   Z" Gd  d   d  Z# d! d"   Z$ d d# d$  Z% d% d&   Z& Gd' d(   d( e  Z' d) d*   Z( d+ d,   Z) d- d.   Z* d/ d0   Z+ d d1 d2  Z, d3 d4   Z- e. d5 k rhe%   n  d S)6a  program/module to trace Python program or function execution

Sample use, command line:
  trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
  trace.py -t --ignore-dir '$prefix' spam.py eggs
  trace.py --trackcalls spam.py eggs

Sample use, programmatically
  import sys

  # create a Trace object, telling it what to ignore, and whether to
  # do tracing or line-counting or both.
  tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
                       trace=0, count=1)
  # run the new command using the given tracer
  tracer.run('main()')
  # make a report, placing output in /tmp
  r = tracer.results()
  r.write_results(show_missing=True, coverdir="/tmp")
TraceCoverageResults    N)warn)	monotonic)timec               C   s   t  j d   d  S)N)syssettrace r	   r	   /usr/lib/python3.4/trace.py_unsettraceH   s    r   c             C   s   t  j |   t j |   d  S)N)	threadingr   r   )funcr	   r	   r
   	_settraceK   s    r   c               C   s   t  j d   t j d   d  S)N)r   r   r   r	   r	   r	   r
   r   O   s    c             C   s   |  j  d t j d  d  S)Na	  Usage: %s [OPTIONS] <file> [ARGS]

Meta-options:
--help                Display this help then exit.
--version             Output version information then exit.

Otherwise, exactly one of the following three options must be given:
-t, --trace           Print each line to sys.stdout before it is executed.
-c, --count           Count the number of times each line is executed
                      and write the counts to <module>.cover for each
                      module executed, in the module's directory.
                      See also `--coverdir', `--file', `--no-report' below.
-l, --listfuncs       Keep track of which functions are executed at least
                      once and write the results to sys.stdout after the
                      program exits.
-T, --trackcalls      Keep track of caller/called pairs and write the
                      results to sys.stdout after the program exits.
-r, --report          Generate a report from a counts file; do not execute
                      any code.  `--file' must specify the results file to
                      read, which must have been created in a previous run
                      with `--count --file=FILE'.

Modifiers:
-f, --file=<file>     File to accumulate counts over several runs.
-R, --no-report       Do not generate the coverage report files.
                      Useful if you want to accumulate over several runs.
-C, --coverdir=<dir>  Directory where the report files.  The coverage
                      report for <package>.<module> is written to file
                      <dir>/<package>/<module>.cover.
-m, --missing         Annotate executable lines that were not executed
                      with '>>>>>> '.
-s, --summary         Write a brief summary on stdout for each file.
                      (Can only be used with --count or --report.)
-g, --timing          Prefix each line with the time since the program started.
                      Only used while tracing.

Filters, may be repeated multiple times:
--ignore-module=<mod> Ignore the given module(s) and its submodules
                      (if it is a package).  Accepts comma separated
                      list of module names
--ignore-dir=<dir>    Ignore files in the given directory (multiple
                      directories can be joined by os.pathsep).
r   )writer   argv)outfiler	   r	   r
   _usageS   s    *r   z#pragma NO COVERz^\s*(#.*)?$c               @   s.   e  Z d  Z d d d d  Z d d   Z d S)_IgnoreNc             C   sT   | s t    n	 t  |  |  _ | s* g  n d d   | D |  _ i d d 6|  _ d  S)Nc             S   s"   g  |  ] } t  j j |   q Sr	   )ospathnormpath).0dr	   r	   r
   
<listcomp>   s   	z$_Ignore.__init__.<locals>.<listcomp>   z<string>)set_mods_dirs_ignore)selfmodulesdirsr	   r	   r
   __init__   s    z_Ignore.__init__c             C   s   | |  j  k r |  j  | S| |  j k r: d |  j  | <d Sx5 |  j D]* } | j | d  rD d |  j  | <d SqD W| d  k r d |  j  | <d Sx8 |  j D]- } | j | t j  r d |  j  | <d Sq Wd |  j  | <d S)Nr   .r   )r   r   
startswithr   r   sep)r   filename
modulenamemodr   r	   r	   r
   names   s$    z_Ignore.names)__name__
__module____qualname__r"   r)   r	   r	   r	   r
   r      s   r   c             C   s.   t  j j |   } t  j j |  \ } } | S)z-Return a plausible module name for the patch.)r   r   basenamesplitext)r   baser&   extr	   r	   r
   _modname   s    r1   c             C   s&  t  j j |   } d } xo t j D]d } t  j j |  } | j |  r" | t |  t  j k r" t |  t |  k r | } q q" q" W| r |  t |  d d  } n |  } t  j j |  \ } } | j t  j d  } t  j	 r| j t  j	 d  } n  t  j j
 |  \ } } | j d  S)z,Return a plausible module name for the path. r   Nr#   )r   r   normcaser   r$   lenr%   
splitdrivereplacealtsepr.   lstrip)r   ZcomparepathZlongestdirr/   Zdriver&   r0   r	   r	   r
   _fullmodname   s     (	r:   c               @   sg   e  Z d  Z d d d d d d d  Z d d   Z d d   Z d d	 d d
 d  Z d d d  Z d S)r   Nc             C   sY  | |  _  |  j  d  k r$ i  |  _  n  |  j  j   |  _ | |  _ |  j d  k rZ i  |  _ n  |  j j   |  _ | |  _ |  j d  k r i  |  _ n  |  j j   |  _ | |  _ | |  _ |  j rUyD t j t	 |  j d   \ } } } |  j
 |  j | | |   WqUt t t f k
 rQ} z% t d |  j | f d t j WYd  d  } ~ XqUXn  d  S)NrbzSkipping counts file %r: %sfile)countscopyZcountercalledfuncscallersinfiler   pickleloadopenupdate	__class__OSErrorEOFError
ValueErrorprintr   stderr)r   r=   r?   rA   r@   r   errr	   r	   r
   r"      s*    						$ zCoverageResults.__init__c             C   s   | j  d  o | j d  S)z_Return True if the filename does not refer to a file
        we want to have reported.
        <>)r$   endswith)r   r&   r	   r	   r
   is_ignored_filename   s    z#CoverageResults.is_ignored_filenamec       	      C   s   |  j  } |  j } |  j } | j  } | j } | j } x, | D]$ } | j | d  | | | | <q= Wx | D] } d | | <ql Wx | D] } d | | <q Wd S)z.Merge in the data from another CoverageResultsr   r   N)r=   r?   r@   get)	r   otherr=   r?   r@   Zother_countsZother_calledfuncsZother_callerskeyr	   r	   r
   rE      s    						"zCoverageResults.updateTFc       !      C   s  |  j  r] t   t d  |  j  } x7 t |  D]& \ } } } t d | | | f  q0 Wn  |  j r0t   t d  d } }	 x t |  j  D] \ \ }
 } } \ } } } |
 | k r t   t d |
 d  |
 } d }	 n  | |
 k r|	 | k rt d |  | }	 n  t d | | | | f  q Wn  i  } xH |  j D]= \ } } | j | i   } | | <|  j | | f | | <q@Wi  } x| j   D]}\ } } |  j |  rqn  | j d  r| d
 d  } n  | d
 k rt	 j
 j t	 j
 j |   } t |  } n4 | } t	 j
 j |  s8t	 j |  n  t |  } | rYt |  } n i  } t j |  } t	 j
 j | | d  } t | d   } t j | j  \ } } Wd
 QX|  j | | | | |  \ } } | r| rt d | |  } | | | | f | | <qqW| rm| rmt d  x? t |  D]. } | | \ } } } } t d | |  q8Wn  |  j ry5 t j |  j |  j  |  j f t |  j d  d  Wqt k
 r}  z t d |  d t j  WYd
 d
 }  ~  XqXn  d
 S)z!
        @param coverdir
        zfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r2   z***z  -->z    %s.%s -> %s.%s.pyc.pyoNr   z.coverr;   d   zlines   cov%   module   (path)z%5d   %3d%%   %s   (%s)wbz"Can't save counts files because %sr<   )rT   rU   )!r?   rJ   sortedr@   r=   rQ   itemsrP   rO   r   r   dirnameabspathr1   existsmakedirsr:   _find_executable_linenos	linecachegetlinesjoinrD   tokenizedetect_encodingreadlinewrite_results_fileintr   rB   dumprG   r   rK   )!r   Zshow_missingsummarycoverdirZcallsr&   r'   funcnameZlastfileZ	lastcfileZpfileZpmodZpfunccfileZcmodZcfuncZper_filelineno	lines_hitZsumscountr9   lnotabsourceZ	coverpathfpencoding_n_hitsn_linesZpercentmrL   r	   r	   r
   write_results  s|    	
		

+		!
	zCoverageResults.write_resultsc             C   sN  y t  | d d | } WnF t k
 ra } z& t d | | f d t j d SWYd d } ~ Xn Xd } d }	 x t | d  D] \ }
 } |
 | k r | j d | |
  |	 d 7}	 | d 7} n^ t j |  r | j d	  n? |
 | k rt	 | k r| j d
  | d 7} n | j d	  | j | j
 d   q~ W| j   |	 | f S)z'Return a coverage results file in path.wrs   z2trace: Could not open %r for writing: %s- skippingr<   r   Nr   z%5d: z       z>>>>>>    )r   r   )rD   rG   rJ   r   rK   	enumerater   rx_blankmatchPRAGMA_NOCOVER
expandtabsclose)r   r   linesrp   rn   rs   r   rL   rv   ru   rm   liner	   r	   r
   rf   b  s,    

z"CoverageResults.write_results_file)r*   r+   r,   r"   rP   rE   rx   rf   r	   r	   r	   r
   r      s   	Uc             C   sC   i  } x6 t  j |   D]% \ } } | | k r d | | <q q W| S)z:Return dict where keys are lines in the line number table.r   )disZfindlinestarts)codestrslinenosrt   rm   r	   r	   r
   _find_lines_from_code  s
    r   c             C   sO   t  |  |  } x9 |  j D]. } t j |  r | j t | |   q q W| S)z<Return lineno dict for all code objects reachable from code.)r   	co_constsinspectZiscoderE   _find_lines)r   r   r   cr	   r	   r
   r     s
    r   c             C   s   i  } t  j } t |  d |  } t j | j  } x | D] \ } } } }	 }
 | t  j k r | t  j k r | \ } } |	 \ } } x( t | | d  D] } d | | <q Wq n  | } q= WWd QX| S)zReturn a dict of possible docstring positions.

    The dict maps line numbers to strings.  There is an entry for
    line that contains only a string or a part of a triple-quoted
    string.
    rs   r   N)tokenINDENTrD   rc   generate_tokensre   STRINGrange)r&   rs   r   Z
prev_ttypeftokZttypeZtstrstartendr   ZslineZscolZelineZecolir	   r	   r
   _find_strings  s    	r   c             C   s   y1 t  j |    } | j   } | j } Wd QXWnF t k
 ry } z& t d |  | f d t j i  SWYd d } ~ Xn Xt | |  d  } t	 |  |  } t
 | |  S)zAReturn dict where keys are line numbers in the line number table.Nz%Not printing coverage data for %r: %sr<   exec)rc   rD   readrs   rG   rJ   r   rK   compiler   r   )r&   r   progrs   rL   r   r   r	   r	   r
   r_     s    r_   c               @   s   e  Z d  Z d d d d f  f  d d d d d 	 Z d d   Z d d 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   Z d S)r   r   r   NFc
       
      C   s!  | |  _  | |  _ t | |  |  _ i  |  _ i  |  _ d |  _ | |  _ i  |  _ i  |  _	 i  |  _
 d |  _ |	 r t   |  _ n  | r |  j |  _ n | r |  j |  _ nr | r | r |  j |  _ |  j |  _ nK | r |  j |  _ |  j |  _ n* | r|  j |  _ |  j |  _ n	 d |  _ d S)ax  
        @param count true iff it should count number of times each
                     line is executed
        @param trace true iff it should print out each line that is
                     being counted
        @param countfuncs true iff it should just output a list of
                     (filename, modulename, funcname,) for functions
                     that were called at least once;  This overrides
                     `count' and `trace'
        @param ignoremods a list of the names of modules to ignore
        @param ignoredirs a list of the names of directories to ignore
                     all of the (recursive) contents of
        @param infile file from which to read stored counts to be
                     added into the results
        @param outfile file in which to write the results
        @param timing true iff timing information be displayed
        r   Nr   )rA   r   r   ignorer=   Zpathtobasename	donothingtrace_calledfuncs_callers_caller_cache
start_time_timeglobaltrace_trackcallersglobaltraceglobaltrace_countfuncsglobaltrace_ltlocaltrace_trace_and_count
localtracelocaltrace_tracelocaltrace_count)
r   ro   r   
countfuncscountcallers
ignoremods
ignoredirsrA   r   timingr	   r	   r
   r"     s6    										zTrace.__init__c             C   s,   d d  l  } | j } |  j | | |  d  S)Nr   )__main____dict__runctx)r   cmdr   dictr	   r	   r
   run  s    	z	Trace.runc          
   C   sr   | d  k r i  } n  | d  k r* i  } n  |  j  sC t |  j  n  z t | | |  Wd  |  j  sm t   n  Xd  S)N)r   r   r   r   r   )r   r   globalslocalsr	   r	   r
   r     s     	 			zTrace.runctxc             O   sV   d  } |  j  s" t j |  j  n  z | | |   } Wd  |  j  sQ t j d   n  X| S)N)r   r   r   r   )r   r   argskwresultr	   r	   r
   runfunc  s    		zTrace.runfuncc       
      C   s_  | j  } | j } | r' t |  } n d  } | j } d  } | |  j k rq |  j | d  k	 r3|  j | } q3n d  |  j | <d d   t j |  D } t |  d k r3d d   t j | d  D } t |  d k r3d d   t j | d  D }	 t |	  d k r0|	 d j } | |  j | <q0q3n  | d  k	 rRd | | f } n  | | | f S)Nc             S   s%   g  |  ] } t  j |  r |  q Sr	   )r   Z
isfunction)r   r   r	   r	   r
   r     s   	 z1Trace.file_module_function_of.<locals>.<listcomp>r   c             S   s%   g  |  ] } t  | t  r |  q Sr	   )
isinstancer   )r   r   r	   r	   r
   r   %  s   	 r   c             S   s%   g  |  ] } t  | d   r |  q S)	__bases__)hasattr)r   r   r	   r	   r
   r   (  s   	 z%s.%s)	f_codeco_filenamer1   co_namer   gcZget_referrersr4   r*   )
r   framer   r&   r'   rk   ZclsnameZfuncsZdictsZclassesr	   r	   r
   file_module_function_of  s,    			  zTrace.file_module_function_ofc             C   sG   | d k rC |  j  |  } |  j  | j  } d |  j | | f <n  d S)zkHandler for call events.

        Adds information about who called who to the self._callers dict.
        callr   N)r   f_backr   )r   r   whyarg	this_funcZparent_funcr	   r	   r
   r   7  s    zTrace.globaltrace_trackcallersc             C   s/   | d k r+ |  j  |  } d |  j | <n  d S)zoHandler for call events.

        Adds (filename, modulename, funcname) to the self._calledfuncs dict.
        r   r   N)r   r   )r   r   r   r   r   r	   r	   r
   r   B  s    zTrace.globaltrace_countfuncsc             C   s   | d k r | j  } | j j d d  } | r t |  } | d k	 r |  j j | |  } | s |  j r t d | | j f  n  |  j	 Sq q d Sn  d S)zHandler for call events.

        If the code block being entered is to be ignored, returns `None',
        else returns self.localtrace.
        r   __file__Nz! --- modulename: %s, funcname: %s)
r   	f_globalsrQ   r1   r   r)   r   rJ   r   r   )r   r   r   r   r   r&   r'   Z	ignore_itr	   r	   r
   r   K  s    		zTrace.globaltrace_ltc             C   s   | d k r | j  j } | j } | | f } |  j j | d  d |  j | <|  j rw t d t   |  j d d n  t j	 j
 |  } t d | | t j | |  f d d n  |  j S)	Nr   r   r   z%.2fr    z
%s(%d): %sr2   )r   r   f_linenor=   rQ   r   rJ   r   r   r   r-   r`   getliner   )r   r   r   r   r&   rm   rS   bnamer	   r	   r
   r   b  s    	 	! z Trace.localtrace_trace_and_countc             C   s   | d k r | j  j } | j } |  j rK t d t   |  j d d n  t j j |  } t d | | t	 j
 | |  f d d n  |  j S)Nr   z%.2fr   r   z
%s(%d): %sr2   )r   r   r   r   rJ   r   r   r   r-   r`   r   r   )r   r   r   r   r&   rm   r   r	   r	   r
   r   q  s    		! zTrace.localtrace_tracec             C   sW   | d k rP | j  j } | j } | | f } |  j j | d  d |  j | <n  |  j S)Nr   r   r   )r   r   r   r=   rQ   r   )r   r   r   r   r&   rm   rS   r	   r	   r
   r   ~  s    	#zTrace.localtrace_countc          
   C   s1   t  |  j d |  j d |  j d |  j d |  j S)NrA   r   r?   r@   )r   r=   rA   r   r   r   )r   r	   r	   r
   results  s    		zTrace.results)r*   r+   r,   r"   r   r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r     s   0)	c             C   s2   t  j j d t  j d |  f  t  j d  d  S)Nz%s: %s
r   r   )r   rK   r   r   exit)msgr	   r	   r
   	_err_exit  s    !r   c          "   C   s  d d  l  } |  d  k r$ t j }  n  yV | j  |  d d   d d d d d d d	 d
 d d d d d d d d g  \ } } Wnq | j k
 r } zN t j j d t j d | f  t j j d t j d  t j d  WYd  d  } ~ Xn Xd } d } d } d } d  }	 d }
 g  } g  } d  } d } d } d } d } x| D]\ } } | d k rxt t j  t j d  n  | d k rt j j d  t j d  n  | d k s| d k rd } qCn  | d k s| d k rd } qCn  | d k s| d k rd } qCn  | d  k s(| d! k r4d } qCn  | d" k sL| d# k rXd } qCn  | d$ k sp| d% k r|d } qCn  | d& k s| d' k rd } qCn  | d( k s| d) k r| }	 qCn  | d* k s| d+ k rd }
 qCn  | d, k s | d- k r| } qCn  | d. k s$| d/ k r0d } qCn  | d0 k rox* | j	 d1  D] } | j
 | j    qLWqCn  | d2 k r?x | j	 t j  D] } t j j |  } | j d3 t j j t j d4 d5 t j d  d6    } | j d7 t j j t j d4 d5 t j d  d6    } t j j |  } | j
 |  qWqCn  d sCt d8   qCW| rt| sg| rtt d9  n  | p| p| p| p| st d:  n  | r| rt d;  n  | r|	 rt d<  n  | rt |  d k rt d=  n  | r.t d> |	 d? |	  } | j |
 d
 | d@ | nQ| t _ | d } t j j	 |  d t j d <t | | dA | dB | dC | dD | d> |	 d? |	 d | } yf t |   } t | j   | dE  } Wd  QXi | dF 6dG dH 6d  dI 6d  dJ 6} | j | | |  WnQ t  k
 r?} z  t dK t j d | f  WYd  d  } ~ Xn t! k
 rPYn X| j"   } | s| j |
 d
 | d@ | n  d  S)LNr   r   ztcrRf:d:msC:lTghelpversionr   ro   reportz	no-reportri   zfile=missingzignore-module=zignore-dir=z	coverdir=	listfuncsZ
trackcallsr   z%s: %s
z%Try `%s --help' for more information
Fz--helpz	--versionz
trace 2.0
z-Tz--trackcallsTz-lz--listfuncsz-gz--timingz-tz--tracez-cz--countz-rz--reportz-Rz--no-reportz-fz--filez-mz	--missingz-Cz
--coverdirz-sz	--summaryz--ignore-module,z--ignore-dirz$prefixZlibZpython   z$exec_prefixzShould never get herez8cannot specify both --listfuncs and (--trace or --count)zLmust specify one of --trace, --count, --report, --listfuncs, or --trackcallsz,cannot specify both --report and --no-reportz--report requires a --filezmissing name of file to runrA   r   rj   r   r   r   r   r   r   r   r*   __package__
__cached__zCannot run file %r because: %s)#getoptr   r   errorrK   r   r   r   stdoutsplitappendstripr   pathsepr   
expandvarsr6   rb   base_prefixr   base_exec_prefixr   AssertionErrorr   r4   r   rx   r   rD   r   r   r   rG   
SystemExitr   )r   r   ZoptsZ	prog_argvr   r   ro   r   Z	no_reportZcounts_filer   Zignore_modulesZignore_dirsrj   ri   r   r   r   Zoptvalr(   sr   Zprognametrr   r   ZglobsrL   r	   r	   r
   main  s    	! 			

-r   c             C   s   t  d t d  t |   d  S)Nz(The trace.usage() function is deprecated   )_warnDeprecationWarningr   )r   r	   r	   r
   usage0  s    
r   c               @   s"   e  Z d  Z d d d d  Z d S)IgnoreNc             C   s'   t  d t d  t j |  | |  d  S)Nz$The class trace.Ignore is deprecatedr   )r   r   r   r"   )r   r    r!   r	   r	   r
   r"   6  s    
zIgnore.__init__)r*   r+   r,   r"   r	   r	   r	   r
   r   5  s   r   c             C   s   t  d t d  t |   S)Nz*The trace.modname() function is deprecatedr   )r   r   r1   )r   r	   r	   r
   modname;  s    
r   c             C   s   t  d t d  t |   S)Nz.The trace.fullmodname() function is deprecatedr   )r   r   r:   )r   r	   r	   r
   fullmodname@  s    
r   c             C   s   t  d t d  t |  |  S)Nz7The trace.find_lines_from_code() function is deprecatedr   )r   r   r   )r   r   r	   r	   r
   find_lines_from_codeE  s    
r   c             C   s   t  d t d  t |  |  S)Nz-The trace.find_lines() function is deprecatedr   )r   r   r   )r   r   r	   r	   r
   
find_linesJ  s    
r   c             C   s    t  d t d  t |  d d  S)Nz/The trace.find_strings() function is deprecatedr   rs   )r   r   r   )r&   rs   r	   r	   r
   find_stringsO  s    
r   c             C   s   t  d t d  t |   S)Nz:The trace.find_executable_linenos() function is deprecatedr   )r   r   r_   )r&   r	   r	   r
   find_executable_linenosT  s    
r   r   )/__doc____all__r`   r   rer   r   rc   r   r   r   rB   warningsr   r   r   r   r   ImportErrorr   r   r   r   r   r~   r   r|   r   r1   r:   r   r   r   r   r_   r   r   r   r   r   r   r   r   r   r   r   r*   r	   r	   r	   r
   <module>1   s^   	-2
