Back to index

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

List of all members.

Public Member Functions

def __init__
def run
def runctx
def runfunc
def file_module_function_of
def globaltrace_trackcallers
def globaltrace_countfuncs
def globaltrace_lt
def localtrace_trace_and_count
def localtrace_trace
def localtrace_count
def results

Public Attributes

 infile
 outfile
 ignore
 counts
 pathtobasename
 donothing
 trace
 start_time
 globaltrace
 localtrace

Private Attributes

 _calledfuncs
 _callers
 _caller_cache

Detailed Description

Definition at line 447 of file trace.py.


Constructor & Destructor Documentation

def trace.Trace.__init__ (   self,
  count = 1,
  trace = 1,
  countfuncs = 0,
  countcallers = 0,
  ignoremods = (),
  ignoredirs = (),
  infile = None,
  outfile = None,
  timing = False 
)
@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

Definition at line 450 of file trace.py.

00450 
00451                  timing=False):
00452         """
00453         @param count true iff it should count number of times each
00454                      line is executed
00455         @param trace true iff it should print out each line that is
00456                      being counted
00457         @param countfuncs true iff it should just output a list of
00458                      (filename, modulename, funcname,) for functions
00459                      that were called at least once;  This overrides
00460                      `count' and `trace'
00461         @param ignoremods a list of the names of modules to ignore
00462         @param ignoredirs a list of the names of directories to ignore
00463                      all of the (recursive) contents of
00464         @param infile file from which to read stored counts to be
00465                      added into the results
00466         @param outfile file in which to write the results
00467         @param timing true iff timing information be displayed
00468         """
00469         self.infile = infile
00470         self.outfile = outfile
00471         self.ignore = _Ignore(ignoremods, ignoredirs)
00472         self.counts = {}   # keys are (filename, linenumber)
00473         self.pathtobasename = {} # for memoizing os.path.basename
00474         self.donothing = 0
00475         self.trace = trace
00476         self._calledfuncs = {}
00477         self._callers = {}
00478         self._caller_cache = {}
00479         self.start_time = None
00480         if timing:
00481             self.start_time = time.time()
00482         if countcallers:
00483             self.globaltrace = self.globaltrace_trackcallers
00484         elif countfuncs:
00485             self.globaltrace = self.globaltrace_countfuncs
00486         elif trace and count:
00487             self.globaltrace = self.globaltrace_lt
00488             self.localtrace = self.localtrace_trace_and_count
00489         elif trace:
00490             self.globaltrace = self.globaltrace_lt
00491             self.localtrace = self.localtrace_trace
00492         elif count:
00493             self.globaltrace = self.globaltrace_lt
00494             self.localtrace = self.localtrace_count
00495         else:
00496             # Ahem -- do nothing?  Okay.
00497             self.donothing = 1

Here is the caller graph for this function:


Member Function Documentation

def trace.Trace.file_module_function_of (   self,
  frame 
)

Definition at line 525 of file trace.py.

00525 
00526     def file_module_function_of(self, frame):
00527         code = frame.f_code
00528         filename = code.co_filename
00529         if filename:
00530             modulename = _modname(filename)
00531         else:
00532             modulename = None
00533 
00534         funcname = code.co_name
00535         clsname = None
00536         if code in self._caller_cache:
00537             if self._caller_cache[code] is not None:
00538                 clsname = self._caller_cache[code]
00539         else:
00540             self._caller_cache[code] = None
00541             ## use of gc.get_referrers() was suggested by Michael Hudson
00542             # all functions which refer to this code object
00543             funcs = [f for f in gc.get_referrers(code)
00544                          if inspect.isfunction(f)]
00545             # require len(func) == 1 to avoid ambiguity caused by calls to
00546             # new.function(): "In the face of ambiguity, refuse the
00547             # temptation to guess."
00548             if len(funcs) == 1:
00549                 dicts = [d for d in gc.get_referrers(funcs[0])
00550                              if isinstance(d, dict)]
00551                 if len(dicts) == 1:
00552                     classes = [c for c in gc.get_referrers(dicts[0])
00553                                    if hasattr(c, "__bases__")]
00554                     if len(classes) == 1:
00555                         # ditto for new.classobj()
00556                         clsname = classes[0].__name__
00557                         # cache the result - assumption is that new.* is
00558                         # not called later to disturb this relationship
00559                         # _caller_cache could be flushed if functions in
00560                         # the new module get called.
00561                         self._caller_cache[code] = clsname
00562         if clsname is not None:
00563             funcname = "%s.%s" % (clsname, funcname)
00564 
00565         return filename, modulename, funcname

Here is the call graph for this function:

Here is the caller graph for this function:

def trace.Trace.globaltrace_countfuncs (   self,
  frame,
  why,
  arg 
)
Handler for call events.

Adds (filename, modulename, funcname) to the self._calledfuncs dict.

Definition at line 577 of file trace.py.

00577 
00578     def globaltrace_countfuncs(self, frame, why, arg):
00579         """Handler for call events.
00580 
00581         Adds (filename, modulename, funcname) to the self._calledfuncs dict.
00582         """
00583         if why == 'call':
00584             this_func = self.file_module_function_of(frame)
00585             self._calledfuncs[this_func] = 1

Here is the call graph for this function:

def trace.Trace.globaltrace_lt (   self,
  frame,
  why,
  arg 
)
Handler for call events.

If the code block being entered is to be ignored, returns `None',
else returns self.localtrace.

Definition at line 586 of file trace.py.

00586 
00587     def globaltrace_lt(self, frame, why, arg):
00588         """Handler for call events.
00589 
00590         If the code block being entered is to be ignored, returns `None',
00591         else returns self.localtrace.
00592         """
00593         if why == 'call':
00594             code = frame.f_code
00595             filename = frame.f_globals.get('__file__', None)
00596             if filename:
00597                 # XXX _modname() doesn't work right for packages, so
00598                 # the ignore support won't work right for packages
00599                 modulename = _modname(filename)
00600                 if modulename is not None:
00601                     ignore_it = self.ignore.names(filename, modulename)
00602                     if not ignore_it:
00603                         if self.trace:
00604                             print((" --- modulename: %s, funcname: %s"
00605                                    % (modulename, code.co_name)))
00606                         return self.localtrace
00607             else:
00608                 return None

Here is the call graph for this function:

def trace.Trace.globaltrace_trackcallers (   self,
  frame,
  why,
  arg 
)
Handler for call events.

Adds information about who called who to the self._callers dict.

Definition at line 566 of file trace.py.

00566 
00567     def globaltrace_trackcallers(self, frame, why, arg):
00568         """Handler for call events.
00569 
00570         Adds information about who called who to the self._callers dict.
00571         """
00572         if why == 'call':
00573             # XXX Should do a better job of identifying methods
00574             this_func = self.file_module_function_of(frame)
00575             parent_func = self.file_module_function_of(frame.f_back)
00576             self._callers[(parent_func, this_func)] = 1

Here is the call graph for this function:

def trace.Trace.localtrace_count (   self,
  frame,
  why,
  arg 
)

Definition at line 637 of file trace.py.

00637 
00638     def localtrace_count(self, frame, why, arg):
00639         if why == "line":
00640             filename = frame.f_code.co_filename
00641             lineno = frame.f_lineno
00642             key = filename, lineno
00643             self.counts[key] = self.counts.get(key, 0) + 1
00644         return self.localtrace

def trace.Trace.localtrace_trace (   self,
  frame,
  why,
  arg 
)

Definition at line 624 of file trace.py.

00624 
00625     def localtrace_trace(self, frame, why, arg):
00626         if why == "line":
00627             # record the file name and line number of every trace
00628             filename = frame.f_code.co_filename
00629             lineno = frame.f_lineno
00630 
00631             if self.start_time:
00632                 print('%.2f' % (time.time() - self.start_time), end=' ')
00633             bname = os.path.basename(filename)
00634             print("%s(%d): %s" % (bname, lineno,
00635                                   linecache.getline(filename, lineno)), end='')
00636         return self.localtrace

Here is the call graph for this function:

def trace.Trace.localtrace_trace_and_count (   self,
  frame,
  why,
  arg 
)

Definition at line 609 of file trace.py.

00609 
00610     def localtrace_trace_and_count(self, frame, why, arg):
00611         if why == "line":
00612             # record the file name and line number of every trace
00613             filename = frame.f_code.co_filename
00614             lineno = frame.f_lineno
00615             key = filename, lineno
00616             self.counts[key] = self.counts.get(key, 0) + 1
00617 
00618             if self.start_time:
00619                 print('%.2f' % (time.time() - self.start_time), end=' ')
00620             bname = os.path.basename(filename)
00621             print("%s(%d): %s" % (bname, lineno,
00622                                   linecache.getline(filename, lineno)), end='')
00623         return self.localtrace

Here is the call graph for this function:

def trace.Trace.results (   self)

Definition at line 645 of file trace.py.

00645 
00646     def results(self):
00647         return CoverageResults(self.counts, infile=self.infile,
00648                                outfile=self.outfile,
00649                                calledfuncs=self._calledfuncs,
00650                                callers=self._callers)

def trace.Trace.run (   self,
  cmd 
)

Definition at line 498 of file trace.py.

00498 
00499     def run(self, cmd):
00500         import __main__
00501         dict = __main__.__dict__
00502         self.runctx(cmd, dict, dict)

Here is the call graph for this function:

Here is the caller graph for this function:

def trace.Trace.runctx (   self,
  cmd,
  globals = None,
  locals = None 
)

Definition at line 503 of file trace.py.

00503 
00504     def runctx(self, cmd, globals=None, locals=None):
00505         if globals is None: globals = {}
00506         if locals is None: locals = {}
00507         if not self.donothing:
00508             _settrace(self.globaltrace)
00509         try:
00510             exec(cmd, globals, locals)
00511         finally:
00512             if not self.donothing:
00513                 _unsettrace()

Here is the call graph for this function:

Here is the caller graph for this function:

def trace.Trace.runfunc (   self,
  func,
  args,
  kw 
)

Definition at line 514 of file trace.py.

00514 
00515     def runfunc(self, func, *args, **kw):
00516         result = None
00517         if not self.donothing:
00518             sys.settrace(self.globaltrace)
00519         try:
00520             result = func(*args, **kw)
00521         finally:
00522             if not self.donothing:
00523                 sys.settrace(None)
00524         return result


Member Data Documentation

Definition at line 475 of file trace.py.

Definition at line 477 of file trace.py.

Definition at line 476 of file trace.py.

Definition at line 471 of file trace.py.

Definition at line 473 of file trace.py.

Definition at line 482 of file trace.py.

Definition at line 470 of file trace.py.

Definition at line 468 of file trace.py.

Definition at line 487 of file trace.py.

Definition at line 469 of file trace.py.

Definition at line 472 of file trace.py.

Definition at line 478 of file trace.py.

Definition at line 474 of file trace.py.


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