comparison tests/coverage.py @ 5915:d0576d065993

Prefer i in d over d.has_key(i)
author Christian Ebert <blacktrash@gmx.net>
date Sun, 20 Jan 2008 14:39:25 +0100
parents 0145f9afb0e7
children a7178d4ed8ee
comparison
equal deleted inserted replaced
5914:8e7796a990c5 5915:d0576d065993
184 if self.excluding_suite: 184 if self.excluding_suite:
185 self.excluded[lineno] = 1 185 self.excluded[lineno] = 1
186 return 0 186 return 0
187 # If this line is excluded, or suite_spots maps this line to 187 # If this line is excluded, or suite_spots maps this line to
188 # another line that is exlcuded, then we're excluded. 188 # another line that is exlcuded, then we're excluded.
189 elif self.excluded.has_key(lineno) or \ 189 elif lineno in self.excluded or \
190 self.suite_spots.has_key(lineno) and \ 190 lineno in self.suite_spots and \
191 self.excluded.has_key(self.suite_spots[lineno][1]): 191 self.suite_spots[lineno][1] in self.excluded:
192 return 0 192 return 0
193 # Otherwise, this is an executable line. 193 # Otherwise, this is an executable line.
194 else: 194 else:
195 self.statements[lineno] = 1 195 self.statements[lineno] = 1
196 return 1 196 return 1
215 # and find its last line. If any line between there and the else's 215 # and find its last line. If any line between there and the else's
216 # first line are excluded, then we exclude the else. 216 # first line are excluded, then we exclude the else.
217 lastprev = self.getLastLine(prevsuite) 217 lastprev = self.getLastLine(prevsuite)
218 firstelse = self.getFirstLine(suite) 218 firstelse = self.getFirstLine(suite)
219 for l in range(lastprev+1, firstelse): 219 for l in range(lastprev+1, firstelse):
220 if self.suite_spots.has_key(l): 220 if l in self.suite_spots:
221 self.doSuite(None, suite, exclude=self.excluded.has_key(l)) 221 self.doSuite(None, suite, l in exclude=self.excluded)
222 break 222 break
223 else: 223 else:
224 self.doSuite(None, suite) 224 self.doSuite(None, suite)
225 225
226 def doElse(self, prevsuite, node): 226 def doElse(self, prevsuite, node):
351 } 351 }
352 short_opts = string.join(map(lambda o: o[1:], optmap.keys()), '') 352 short_opts = string.join(map(lambda o: o[1:], optmap.keys()), '')
353 long_opts = optmap.values() 353 long_opts = optmap.values()
354 options, args = getopt.getopt(argv, short_opts, long_opts) 354 options, args = getopt.getopt(argv, short_opts, long_opts)
355 for o, a in options: 355 for o, a in options:
356 if optmap.has_key(o): 356 if o in optmap:
357 settings[optmap[o]] = 1 357 settings[optmap[o]] = 1
358 elif optmap.has_key(o + ':'): 358 elif o + ':' in optmap:
359 settings[optmap[o + ':']] = a 359 settings[optmap[o + ':']] = a
360 elif o[2:] in long_opts: 360 elif o[2:] in long_opts:
361 settings[o[2:]] = 1 361 settings[o[2:]] = 1
362 elif o[2:] + '=' in long_opts: 362 elif o[2:] + '=' in long_opts:
363 settings[o[2:]+'='] = a 363 settings[o[2:]+'='] = a
510 cexecuted = self.restore_file(full_path) 510 cexecuted = self.restore_file(full_path)
511 self.merge_data(cexecuted) 511 self.merge_data(cexecuted)
512 512
513 def merge_data(self, new_data): 513 def merge_data(self, new_data):
514 for file_name, file_data in new_data.items(): 514 for file_name, file_data in new_data.items():
515 if self.cexecuted.has_key(file_name): 515 if file_name in self.cexecuted:
516 self.merge_file_data(self.cexecuted[file_name], file_data) 516 self.merge_file_data(self.cexecuted[file_name], file_data)
517 else: 517 else:
518 self.cexecuted[file_name] = file_data 518 self.cexecuted[file_name] = file_data
519 519
520 def merge_file_data(self, cache_data, new_data): 520 def merge_file_data(self, cache_data, new_data):
521 for line_number in new_data.keys(): 521 for line_number in new_data.keys():
522 if not cache_data.has_key(line_number): 522 if not line_number in cache_data:
523 cache_data[line_number] = new_data[line_number] 523 cache_data[line_number] = new_data[line_number]
524 524
525 # canonical_filename(filename). Return a canonical filename for the 525 # canonical_filename(filename). Return a canonical filename for the
526 # file (that is, an absolute path with no redundant components and 526 # file (that is, an absolute path with no redundant components and
527 # normalized case). See [GDR 2001-12-04b, 3.3]. 527 # normalized case). See [GDR 2001-12-04b, 3.3].
528 528
529 def canonical_filename(self, filename): 529 def canonical_filename(self, filename):
530 if not self.canonical_filename_cache.has_key(filename): 530 if not filename in self.canonical_filename_cache:
531 f = filename 531 f = filename
532 if os.path.isabs(f) and not os.path.exists(f): 532 if os.path.isabs(f) and not os.path.exists(f):
533 f = os.path.basename(f) 533 f = os.path.basename(f)
534 if not os.path.isabs(f): 534 if not os.path.isabs(f):
535 for path in [os.curdir] + sys.path: 535 for path in [os.curdir] + sys.path:
548 for filename, lineno in self.c.keys(): 548 for filename, lineno in self.c.keys():
549 if filename == '<string>': 549 if filename == '<string>':
550 # Can't do anything useful with exec'd strings, so skip them. 550 # Can't do anything useful with exec'd strings, so skip them.
551 continue 551 continue
552 f = self.canonical_filename(filename) 552 f = self.canonical_filename(filename)
553 if not self.cexecuted.has_key(f): 553 if not f in self.cexecuted:
554 self.cexecuted[f] = {} 554 self.cexecuted[f] = {}
555 self.cexecuted[f][lineno] = 1 555 self.cexecuted[f][lineno] = 1
556 self.c = {} 556 self.c = {}
557 557
558 # morf_filename(morf). Return the filename for a module or file. 558 # morf_filename(morf). Return the filename for a module or file.
573 # in the source code, (3) a list of lines of excluded statements, 573 # in the source code, (3) a list of lines of excluded statements,
574 # and (4), a map of line numbers to multi-line line number ranges, for 574 # and (4), a map of line numbers to multi-line line number ranges, for
575 # statements that cross lines. 575 # statements that cross lines.
576 576
577 def analyze_morf(self, morf): 577 def analyze_morf(self, morf):
578 if self.analysis_cache.has_key(morf): 578 if morf in self.analysis_cache:
579 return self.analysis_cache[morf] 579 return self.analysis_cache[morf]
580 filename = self.morf_filename(morf) 580 filename = self.morf_filename(morf)
581 ext = os.path.splitext(filename)[1] 581 ext = os.path.splitext(filename)[1]
582 if ext == '.pyc': 582 if ext == '.pyc':
583 if not os.path.exists(filename[0:-1]): 583 if not os.path.exists(filename[0:-1]):
750 return f, s, m, mf 750 return f, s, m, mf
751 751
752 def analysis2(self, morf): 752 def analysis2(self, morf):
753 filename, statements, excluded, line_map = self.analyze_morf(morf) 753 filename, statements, excluded, line_map = self.analyze_morf(morf)
754 self.canonicalize_filenames() 754 self.canonicalize_filenames()
755 if not self.cexecuted.has_key(filename): 755 if not filename in self.cexecuted:
756 self.cexecuted[filename] = {} 756 self.cexecuted[filename] = {}
757 missing = [] 757 missing = []
758 for line in statements: 758 for line in statements:
759 lines = line_map.get(line, [line, line]) 759 lines = line_map.get(line, [line, line])
760 for l in range(lines[0], lines[1]+1): 760 for l in range(lines[0], lines[1]+1):
761 if self.cexecuted[filename].has_key(l): 761 if l in self.cexecuted[filename]:
762 break 762 break
763 else: 763 else:
764 missing.append(line) 764 missing.append(line)
765 return (filename, statements, excluded, missing, 765 return (filename, statements, excluded, missing,
766 self.format_lines(statements, missing)) 766 self.format_lines(statements, missing))