@@ -73,9 +73,6 @@ def _unsettrace():
7373
7474PRAGMA_NOCOVER = "#pragma NO COVER"
7575
76- # Simple rx to find lines with no code.
77- rx_blank = re .compile (r'^\s*(#.*)?$' )
78-
7976class _Ignore :
8077 def __init__ (self , modules = None , dirs = None ):
8178 self ._mods = set () if not modules else set (modules )
@@ -278,16 +275,15 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
278275 lnotab = _find_executable_linenos (filename )
279276 else :
280277 lnotab = {}
281- if lnotab :
282- source = linecache .getlines (filename )
283- coverpath = os .path .join (dir , modulename + ".cover" )
284- with open (filename , 'rb' ) as fp :
285- encoding , _ = tokenize .detect_encoding (fp .readline )
286- n_hits , n_lines = self .write_results_file (coverpath , source ,
287- lnotab , count , encoding )
288- if summary and n_lines :
289- percent = int (100 * n_hits / n_lines )
290- sums [modulename ] = n_lines , percent , modulename , filename
278+ source = linecache .getlines (filename )
279+ coverpath = os .path .join (dir , modulename + ".cover" )
280+ with open (filename , 'rb' ) as fp :
281+ encoding , _ = tokenize .detect_encoding (fp .readline )
282+ n_hits , n_lines = self .write_results_file (coverpath , source ,
283+ lnotab , count , encoding )
284+ if summary and n_lines :
285+ percent = int (100 * n_hits / n_lines )
286+ sums [modulename ] = n_lines , percent , modulename , filename
291287
292288
293289 if summary and sums :
@@ -306,6 +302,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
306302
307303 def write_results_file (self , path , lines , lnotab , lines_hit , encoding = None ):
308304 """Return a coverage results file in path."""
305+ # ``lnotab`` is a dict of executable lines, or a line number "table"
309306
310307 try :
311308 outfile = open (path , "w" , encoding = encoding )
@@ -324,17 +321,13 @@ def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
324321 outfile .write ("%5d: " % lines_hit [lineno ])
325322 n_hits += 1
326323 n_lines += 1
327- elif rx_blank .match (line ):
328- outfile .write (" " )
329- else :
330- # lines preceded by no marks weren't hit
331- # Highlight them if so indicated, unless the line contains
324+ elif lineno in lnotab and not PRAGMA_NOCOVER in line :
325+ # Highlight never-executed lines, unless the line contains
332326 # #pragma: NO COVER
333- if lineno in lnotab and not PRAGMA_NOCOVER in line :
334- outfile .write (">>>>>> " )
335- n_lines += 1
336- else :
337- outfile .write (" " )
327+ outfile .write (">>>>>> " )
328+ n_lines += 1
329+ else :
330+ outfile .write (" " )
338331 outfile .write (line .expandtabs (8 ))
339332
340333 return n_hits , n_lines
0 commit comments