Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
trace.CoverageResults Class Reference

List of all members.

Public Member Functions

def __init__
def is_ignored_filename
def update
def write_results
def write_results_file

Public Attributes

 counts
 counter
 calledfuncs
 callers
 infile
 outfile

Detailed Description

Definition at line 216 of file trace.py.


Constructor & Destructor Documentation

def trace.CoverageResults.__init__ (   self,
  counts = None,
  calledfuncs = None,
  infile = None,
  callers = None,
  outfile = None 
)

Definition at line 218 of file trace.py.

00218 
00219                  callers=None, outfile=None):
00220         self.counts = counts
00221         if self.counts is None:
00222             self.counts = {}
00223         self.counter = self.counts.copy() # map (filename, lineno) to count
00224         self.calledfuncs = calledfuncs
00225         if self.calledfuncs is None:
00226             self.calledfuncs = {}
00227         self.calledfuncs = self.calledfuncs.copy()
00228         self.callers = callers
00229         if self.callers is None:
00230             self.callers = {}
00231         self.callers = self.callers.copy()
00232         self.infile = infile
00233         self.outfile = outfile
00234         if self.infile:
00235             # Try to merge existing counts file.
00236             try:
00237                 counts, calledfuncs, callers = \
00238                         pickle.load(open(self.infile, 'rb'))
00239                 self.update(self.__class__(counts, calledfuncs, callers))
00240             except (IOError, EOFError, ValueError) as err:
00241                 print(("Skipping counts file %r: %s"
00242                                       % (self.infile, err)), file=sys.stderr)

Here is the caller graph for this function:


Member Function Documentation

def trace.CoverageResults.is_ignored_filename (   self,
  filename 
)
Return True if the filename does not refer to a file
we want to have reported.

Definition at line 243 of file trace.py.

00243 
00244     def is_ignored_filename(self, filename):
00245         """Return True if the filename does not refer to a file
00246         we want to have reported.
00247         """
00248         return (filename == "<string>" or
00249                 filename.startswith("<doctest "))

Here is the caller graph for this function:

def trace.CoverageResults.update (   self,
  other 
)
Merge in the data from another CoverageResults

Definition at line 250 of file trace.py.

00250 
00251     def update(self, other):
00252         """Merge in the data from another CoverageResults"""
00253         counts = self.counts
00254         calledfuncs = self.calledfuncs
00255         callers = self.callers
00256         other_counts = other.counts
00257         other_calledfuncs = other.calledfuncs
00258         other_callers = other.callers
00259 
00260         for key in other_counts:
00261             counts[key] = counts.get(key, 0) + other_counts[key]
00262 
00263         for key in other_calledfuncs:
00264             calledfuncs[key] = 1
00265 
00266         for key in other_callers:
00267             callers[key] = 1

Here is the caller graph for this function:

def trace.CoverageResults.write_results (   self,
  show_missing = True,
  summary = False,
  coverdir = None 
)
@param coverdir

Definition at line 268 of file trace.py.

00268 
00269     def write_results(self, show_missing=True, summary=False, coverdir=None):
00270         """
00271         @param coverdir
00272         """
00273         if self.calledfuncs:
00274             print()
00275             print("functions called:")
00276             calls = self.calledfuncs
00277             for filename, modulename, funcname in sorted(calls):
00278                 print(("filename: %s, modulename: %s, funcname: %s"
00279                        % (filename, modulename, funcname)))
00280 
00281         if self.callers:
00282             print()
00283             print("calling relationships:")
00284             lastfile = lastcfile = ""
00285             for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) \
00286                     in sorted(self.callers):
00287                 if pfile != lastfile:
00288                     print()
00289                     print("***", pfile, "***")
00290                     lastfile = pfile
00291                     lastcfile = ""
00292                 if cfile != pfile and lastcfile != cfile:
00293                     print("  -->", cfile)
00294                     lastcfile = cfile
00295                 print("    %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc))
00296 
00297         # turn the counts data ("(filename, lineno) = count") into something
00298         # accessible on a per-file basis
00299         per_file = {}
00300         for filename, lineno in self.counts:
00301             lines_hit = per_file[filename] = per_file.get(filename, {})
00302             lines_hit[lineno] = self.counts[(filename, lineno)]
00303 
00304         # accumulate summary info, if needed
00305         sums = {}
00306 
00307         for filename, count in per_file.items():
00308             if self.is_ignored_filename(filename):
00309                 continue
00310 
00311             if filename.endswith((".pyc", ".pyo")):
00312                 filename = filename[:-1]
00313 
00314             if coverdir is None:
00315                 dir = os.path.dirname(os.path.abspath(filename))
00316                 modulename = _modname(filename)
00317             else:
00318                 dir = coverdir
00319                 if not os.path.exists(dir):
00320                     os.makedirs(dir)
00321                 modulename = _fullmodname(filename)
00322 
00323             # If desired, get a list of the line numbers which represent
00324             # executable content (returned as a dict for better lookup speed)
00325             if show_missing:
00326                 lnotab = _find_executable_linenos(filename)
00327             else:
00328                 lnotab = {}
00329 
00330             source = linecache.getlines(filename)
00331             coverpath = os.path.join(dir, modulename + ".cover")
00332             with open(filename, 'rb') as fp:
00333                 encoding, _ = tokenize.detect_encoding(fp.readline)
00334             n_hits, n_lines = self.write_results_file(coverpath, source,
00335                                                       lnotab, count, encoding)
00336             if summary and n_lines:
00337                 percent = int(100 * n_hits / n_lines)
00338                 sums[modulename] = n_lines, percent, modulename, filename
00339 
00340         if summary and sums:
00341             print("lines   cov%   module   (path)")
00342             for m in sorted(sums):
00343                 n_lines, percent, modulename, filename = sums[m]
00344                 print("%5d   %3d%%   %s   (%s)" % sums[m])
00345 
00346         if self.outfile:
00347             # try and store counts and module info into self.outfile
00348             try:
00349                 pickle.dump((self.counts, self.calledfuncs, self.callers),
00350                             open(self.outfile, 'wb'), 1)
00351             except IOError as err:
00352                 print("Can't save counts files because %s" % err, file=sys.stderr)

Here is the call graph for this function:

def trace.CoverageResults.write_results_file (   self,
  path,
  lines,
  lnotab,
  lines_hit,
  encoding = None 
)
Return a coverage results file in path.

Definition at line 353 of file trace.py.

00353 
00354     def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
00355         """Return a coverage results file in path."""
00356 
00357         try:
00358             outfile = open(path, "w", encoding=encoding)
00359         except IOError as err:
00360             print(("trace: Could not open %r for writing: %s"
00361                                   "- skipping" % (path, err)), file=sys.stderr)
00362             return 0, 0
00363 
00364         n_lines = 0
00365         n_hits = 0
00366         for lineno, line in enumerate(lines, 1):
00367             # do the blank/comment match to try to mark more lines
00368             # (help the reader find stuff that hasn't been covered)
00369             if lineno in lines_hit:
00370                 outfile.write("%5d: " % lines_hit[lineno])
00371                 n_hits += 1
00372                 n_lines += 1
00373             elif rx_blank.match(line):
00374                 outfile.write("       ")
00375             else:
00376                 # lines preceded by no marks weren't hit
00377                 # Highlight them if so indicated, unless the line contains
00378                 # #pragma: NO COVER
00379                 if lineno in lnotab and not PRAGMA_NOCOVER in line:
00380                     outfile.write(">>>>>> ")
00381                     n_lines += 1
00382                 else:
00383                     outfile.write("       ")
00384             outfile.write(line.expandtabs(8))
00385         outfile.close()
00386 
00387         return n_hits, n_lines

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 223 of file trace.py.

Definition at line 227 of file trace.py.

Definition at line 222 of file trace.py.

Definition at line 219 of file trace.py.

Definition at line 231 of file trace.py.

Definition at line 232 of file trace.py.


The documentation for this class was generated from the following file: