misc.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Miscellaneous stuff for coverage.py."""
  4. import errno
  5. import hashlib
  6. import inspect
  7. import locale
  8. import os
  9. import sys
  10. import types
  11. from coverage import env
  12. from coverage.backward import to_bytes, unicode_class
  13. ISOLATED_MODULES = {}
  14. def isolate_module(mod):
  15. """Copy a module so that we are isolated from aggressive mocking.
  16. If a test suite mocks os.path.exists (for example), and then we need to use
  17. it during the test, everything will get tangled up if we use their mock.
  18. Making a copy of the module when we import it will isolate coverage.py from
  19. those complications.
  20. """
  21. if mod not in ISOLATED_MODULES:
  22. new_mod = types.ModuleType(mod.__name__)
  23. ISOLATED_MODULES[mod] = new_mod
  24. for name in dir(mod):
  25. value = getattr(mod, name)
  26. if isinstance(value, types.ModuleType):
  27. value = isolate_module(value)
  28. setattr(new_mod, name, value)
  29. return ISOLATED_MODULES[mod]
  30. os = isolate_module(os)
  31. def dummy_decorator_with_args(*args_unused, **kwargs_unused):
  32. """Dummy no-op implementation of a decorator with arguments."""
  33. def _decorator(func):
  34. return func
  35. return _decorator
  36. # Use PyContracts for assertion testing on parameters and returns, but only if
  37. # we are running our own test suite.
  38. if env.TESTING:
  39. from contracts import contract # pylint: disable=unused-import
  40. from contracts import new_contract as raw_new_contract
  41. def new_contract(*args, **kwargs):
  42. """A proxy for contracts.new_contract that doesn't mind happening twice."""
  43. try:
  44. return raw_new_contract(*args, **kwargs)
  45. except ValueError:
  46. # During meta-coverage, this module is imported twice, and
  47. # PyContracts doesn't like redefining contracts. It's OK.
  48. pass
  49. # Define contract words that PyContract doesn't have.
  50. new_contract('bytes', lambda v: isinstance(v, bytes))
  51. if env.PY3:
  52. new_contract('unicode', lambda v: isinstance(v, unicode_class))
  53. def one_of(argnames):
  54. """Ensure that only one of the argnames is non-None."""
  55. def _decorator(func):
  56. argnameset = set(name.strip() for name in argnames.split(","))
  57. def _wrapped(*args, **kwargs):
  58. vals = [kwargs.get(name) for name in argnameset]
  59. assert sum(val is not None for val in vals) == 1
  60. return func(*args, **kwargs)
  61. return _wrapped
  62. return _decorator
  63. else: # pragma: not testing
  64. # We aren't using real PyContracts, so just define our decorators as
  65. # stunt-double no-ops.
  66. contract = dummy_decorator_with_args
  67. one_of = dummy_decorator_with_args
  68. def new_contract(*args_unused, **kwargs_unused):
  69. """Dummy no-op implementation of `new_contract`."""
  70. pass
  71. def nice_pair(pair):
  72. """Make a nice string representation of a pair of numbers.
  73. If the numbers are equal, just return the number, otherwise return the pair
  74. with a dash between them, indicating the range.
  75. """
  76. start, end = pair
  77. if start == end:
  78. return "%d" % start
  79. else:
  80. return "%d-%d" % (start, end)
  81. def format_lines(statements, lines):
  82. """Nicely format a list of line numbers.
  83. Format a list of line numbers for printing by coalescing groups of lines as
  84. long as the lines represent consecutive statements. This will coalesce
  85. even if there are gaps between statements.
  86. For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
  87. `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
  88. Both `lines` and `statements` can be any iterable. All of the elements of
  89. `lines` must be in `statements`, and all of the values must be positive
  90. integers.
  91. """
  92. statements = sorted(statements)
  93. lines = sorted(lines)
  94. pairs = []
  95. start = None
  96. lidx = 0
  97. for stmt in statements:
  98. if lidx >= len(lines):
  99. break
  100. if stmt == lines[lidx]:
  101. lidx += 1
  102. if not start:
  103. start = stmt
  104. end = stmt
  105. elif start:
  106. pairs.append((start, end))
  107. start = None
  108. if start:
  109. pairs.append((start, end))
  110. ret = ', '.join(map(nice_pair, pairs))
  111. return ret
  112. def expensive(fn):
  113. """A decorator to indicate that a method shouldn't be called more than once.
  114. Normally, this does nothing. During testing, this raises an exception if
  115. called more than once.
  116. """
  117. if env.TESTING:
  118. attr = "_once_" + fn.__name__
  119. def _wrapped(self):
  120. """Inner function that checks the cache."""
  121. if hasattr(self, attr):
  122. raise AssertionError("Shouldn't have called %s more than once" % fn.__name__)
  123. setattr(self, attr, True)
  124. return fn(self)
  125. return _wrapped
  126. else:
  127. return fn # pragma: not testing
  128. def bool_or_none(b):
  129. """Return bool(b), but preserve None."""
  130. if b is None:
  131. return None
  132. else:
  133. return bool(b)
  134. def join_regex(regexes):
  135. """Combine a list of regexes into one that matches any of them."""
  136. return "|".join("(?:%s)" % r for r in regexes)
  137. def file_be_gone(path):
  138. """Remove a file, and don't get annoyed if it doesn't exist."""
  139. try:
  140. os.remove(path)
  141. except OSError as e:
  142. if e.errno != errno.ENOENT:
  143. raise
  144. def output_encoding(outfile=None):
  145. """Determine the encoding to use for output written to `outfile` or stdout."""
  146. if outfile is None:
  147. outfile = sys.stdout
  148. encoding = (
  149. getattr(outfile, "encoding", None) or
  150. getattr(sys.__stdout__, "encoding", None) or
  151. locale.getpreferredencoding()
  152. )
  153. return encoding
  154. class Hasher(object):
  155. """Hashes Python data into md5."""
  156. def __init__(self):
  157. self.md5 = hashlib.md5()
  158. def update(self, v):
  159. """Add `v` to the hash, recursively if needed."""
  160. self.md5.update(to_bytes(str(type(v))))
  161. if isinstance(v, unicode_class):
  162. self.md5.update(v.encode('utf8'))
  163. elif isinstance(v, bytes):
  164. self.md5.update(v)
  165. elif v is None:
  166. pass
  167. elif isinstance(v, (int, float)):
  168. self.md5.update(to_bytes(str(v)))
  169. elif isinstance(v, (tuple, list)):
  170. for e in v:
  171. self.update(e)
  172. elif isinstance(v, dict):
  173. keys = v.keys()
  174. for k in sorted(keys):
  175. self.update(k)
  176. self.update(v[k])
  177. else:
  178. for k in dir(v):
  179. if k.startswith('__'):
  180. continue
  181. a = getattr(v, k)
  182. if inspect.isroutine(a):
  183. continue
  184. self.update(k)
  185. self.update(a)
  186. def hexdigest(self):
  187. """Retrieve the hex digest of the hash."""
  188. return self.md5.hexdigest()
  189. def _needs_to_implement(that, func_name):
  190. """Helper to raise NotImplementedError in interface stubs."""
  191. if hasattr(that, "_coverage_plugin_name"):
  192. thing = "Plugin"
  193. name = that._coverage_plugin_name
  194. else:
  195. thing = "Class"
  196. klass = that.__class__
  197. name = "{klass.__module__}.{klass.__name__}".format(klass=klass)
  198. raise NotImplementedError(
  199. "{thing} {name!r} needs to implement {func_name}()".format(
  200. thing=thing, name=name, func_name=func_name
  201. )
  202. )
  203. class SimpleRepr(object):
  204. """A mixin implementing a simple __repr__."""
  205. def __repr__(self):
  206. return "<{klass} @{id:x} {attrs}>".format(
  207. klass=self.__class__.__name__,
  208. id=id(self) & 0xFFFFFF,
  209. attrs=" ".join("{}={!r}".format(k, v) for k, v in self.__dict__.items()),
  210. )
  211. class BaseCoverageException(Exception):
  212. """The base of all Coverage exceptions."""
  213. pass
  214. class CoverageException(BaseCoverageException):
  215. """A run-of-the-mill exception specific to coverage.py."""
  216. pass
  217. class NoSource(CoverageException):
  218. """We couldn't find the source for a module."""
  219. pass
  220. class NoCode(NoSource):
  221. """We couldn't find any code at all."""
  222. pass
  223. class NotPython(CoverageException):
  224. """A source file turned out not to be parsable Python."""
  225. pass
  226. class ExceptionDuringRun(CoverageException):
  227. """An exception happened while running customer code.
  228. Construct it with three arguments, the values from `sys.exc_info`.
  229. """
  230. pass
  231. class StopEverything(BaseCoverageException):
  232. """An exception that means everything should stop.
  233. The CoverageTest class converts these to SkipTest, so that when running
  234. tests, raising this exception will automatically skip the test.
  235. """
  236. pass