ATLAS Offline Software
Public Member Functions | Public Attributes | List of all members
python.coverage.Coverage Class Reference
Collaboration diagram for python.coverage.Coverage:

Public Member Functions

def __init__ (self, modname, toplev_name='main')
 
def trace (self, frame, why, arg)
 
def analyze (self)
 
def doctest_cover (self, *args, **kw)
 

Public Attributes

 modname
 
 toplev_name
 
 counts
 
 mutex
 

Detailed Description

Definition at line 149 of file coverage.py.

Constructor & Destructor Documentation

◆ __init__()

def python.coverage.Coverage.__init__ (   self,
  modname,
  toplev_name = 'main' 
)

Definition at line 150 of file coverage.py.

150  def __init__(self, modname, toplev_name = 'main'):
151  global running_coverage
152  running_coverage = 1
153  self.modname = modname
154  self.toplev_name = toplev_name
155  self.counts = {} # keys are linenumber
156  self.mutex = None
157  if 'NOCOVER' not in os.environ:
158  #self.save_import = __builtin__.__import__
159  #__builtin__.__import__ = self.import_hook
160  sys.settrace (self.trace)
161  return
162 
163 

Member Function Documentation

◆ analyze()

def python.coverage.Coverage.analyze (   self)

Definition at line 227 of file coverage.py.

227  def analyze (self):
228  filename = sys.modules[self.modname].__file__
229  if filename[-4:] == ".pyc" or filename[-4:] == ".pyo":
230  orig_filename = filename[:-4] + '.py'
231  else:
232  orig_filename = filename
233 
234  # Get the original lines from the .py file
235  try:
236  lines = open(orig_filename, 'r').readlines()
237  except IOError as err:
238  sys.stderr.write(
239  "%s: Could not open %s for reading because: %s - skipping\n" %
240 \
241  ("trace", repr(filename), err.strerror))
242  return
243 
244  # there are many places where this is insufficient, like a blank
245  # line embedded in a multiline string.
246  blank = re.compile(r'^\s*(#.*)?$')
247 
248  executable_linenos = find_executable_linenos(orig_filename)
249 
250  lines_hit = self.counts
251  uncovered = 0
252  outlines = []
253  for i in range(len(lines)):
254  line = lines[i]
255 
256  # do the blank/comment match to try to mark more lines
257  # (help the reader find stuff that hasn't been covered)
258  if (i+1) in lines_hit:
259  # count precedes the lines that we captured
260  prefix = '%5d: ' % lines_hit[i+1]
261  elif blank.match(line):
262  # blank lines and comments are preceded by dots
263  prefix = ' . '
264  else:
265  # lines preceded by no marks weren't hit
266  # Highlight them if so indicated, unless the line contains
267  # '#pragma: NO COVER' (it is possible to embed this into
268  # the text as a non-comment; no easy fix)
269  if (i+1) in executable_linenos and \
270  lines[i].find('#pragma: NO COVER') == -1:
271  prefix = '>>>>>> '
272  uncovered = uncovered + 1
273  else:
274  prefix = ' '*7
275  outlines.append (prefix + line.expandtabs(8))
276 
277  if uncovered:
278  print("*** There were %d uncovered lines." % uncovered)
279  else:
280  return
281 
282  # build list file name by appending a ".cover" to the module name
283  # and sticking it into the specified directory
284  listfilename = self.modname + ".cover"
285  try:
286  outfile = open(listfilename, 'w')
287  except IOError as err:
288  sys.stderr.write(
289  '%s: Could not open %s for writing because: %s - skipping\n' %
290  ("trace", repr(listfilename), err.strerror))
291  return
292 
293  for l in outlines:
294  outfile.write (l)
295 
296  outfile.close()
297 
298  return
299 
300 

◆ doctest_cover()

def python.coverage.Coverage.doctest_cover (   self,
args,
**  kw 
)

Definition at line 301 of file coverage.py.

301  def doctest_cover (self, *args, **kw):
302  import doctest
303  m = __import__ (self.modname)
304 
305  # Note a peculiarity of __import__: if modname is like A.B,
306  # then it returns A not B...
307  mm = self.modname.split ('.')
308  if len(mm) > 1:
309  m = getattr (m, mm[-1])
310 
311  oldrun = doctest.DocTestRunner.run
312  def xrun (xself, *args, **kw):
313  sys.settrace (self.trace)
314  return oldrun (xself, *args, **kw)
315  doctest.DocTestRunner.run = xrun
316 
317  import bdb
318  old_set_continue = bdb.Bdb.set_continue
319  def xcontinue (xself):
320  old_set_continue (xself)
321  sys.settrace (self.trace)
322  return
323  bdb.Bdb.set_continue = xcontinue
324 
325  doctest.testmod (m, *args, **kw)
326 
327  main = sys.modules['__main__']
328  if m != main:
329  doctest.testmod (main, *args, **kw)
330 
331  self.analyze()
332  return

◆ trace()

def python.coverage.Coverage.trace (   self,
  frame,
  why,
  arg 
)

Definition at line 198 of file coverage.py.

198  def trace(self, frame, why, arg):
199  # something is fishy about getting the file name
200  modulename = frame.f_globals.get ("__name__")
201  if why == 'call':
202  # Don't bother tracing if we're not in one of these modules.
203  if not (modulename == self.modname or
204  (modulename == '__main__' and
205  frame.f_code.co_name == self.toplev_name)):
206  return None
207  if why == 'line':
208  if modulename == self.modname:
209  lineno = frame.f_lineno
210 
211  # record the file name and line number of every trace
212  if self.mutex: self.mutex.acquire ()
213  self.counts[lineno] = self.counts.get(lineno, 0) + 1
214  if self.mutex: self.mutex.release ()
215 
216  elif why == 'return':
217  if frame.f_code.co_name == self.toplev_name:
218  sys.settrace (None)
219  if self.mutex: self.mutex.acquire ()
220  self.analyze ()
221  m = self.mutex
222  self.mutex = None
223  if m: m.release ()
224 
225  return self.trace
226 

Member Data Documentation

◆ counts

python.coverage.Coverage.counts

Definition at line 155 of file coverage.py.

◆ modname

python.coverage.Coverage.modname

Definition at line 153 of file coverage.py.

◆ mutex

python.coverage.Coverage.mutex

Definition at line 156 of file coverage.py.

◆ toplev_name

python.coverage.Coverage.toplev_name

Definition at line 154 of file coverage.py.


The documentation for this class was generated from the following file:
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
vmem-sz.analyze
def analyze(libname)
Definition: vmem-sz.py:49
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
Trk::open
@ open
Definition: BinningType.h:40
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
python.coverage.find_executable_linenos
def find_executable_linenos(filename)
Definition: coverage.py:132