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 144 of file coverage.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 145 of file coverage.py.

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

Member Function Documentation

◆ analyze()

def python.coverage.Coverage.analyze (   self)

Definition at line 222 of file coverage.py.

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

◆ doctest_cover()

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

Definition at line 296 of file coverage.py.

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

◆ trace()

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

Definition at line 193 of file coverage.py.

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

Member Data Documentation

◆ counts

python.coverage.Coverage.counts

Definition at line 150 of file coverage.py.

◆ modname

python.coverage.Coverage.modname

Definition at line 148 of file coverage.py.

◆ mutex

python.coverage.Coverage.mutex

Definition at line 151 of file coverage.py.

◆ toplev_name

python.coverage.Coverage.toplev_name

Definition at line 149 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:47
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
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:127