| OLD | NEW |
| 1 """Miscellaneous stuff for Coverage.""" | 1 """Miscellaneous stuff for Coverage.""" |
| 2 | 2 |
| 3 import errno | 3 import errno |
| 4 import inspect | 4 import inspect |
| 5 import os | 5 import os |
| 6 import sys | 6 import sys |
| 7 | 7 |
| 8 from coverage.backward import md5, sorted # pylint: disable=W0622 | 8 from coverage.backward import md5, sorted # pylint: disable=W0622 |
| 9 from coverage.backward import string_class, to_bytes | 9 from coverage.backward import string_class, to_bytes |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 even if there are gaps between statements. | 31 even if there are gaps between statements. |
| 32 | 32 |
| 33 For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and | 33 For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and |
| 34 `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14". | 34 `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14". |
| 35 | 35 |
| 36 """ | 36 """ |
| 37 pairs = [] | 37 pairs = [] |
| 38 i = 0 | 38 i = 0 |
| 39 j = 0 | 39 j = 0 |
| 40 start = None | 40 start = None |
| 41 statements = sorted(statements) |
| 42 lines = sorted(lines) |
| 41 while i < len(statements) and j < len(lines): | 43 while i < len(statements) and j < len(lines): |
| 42 if statements[i] == lines[j]: | 44 if statements[i] == lines[j]: |
| 43 if start == None: | 45 if start == None: |
| 44 start = lines[j] | 46 start = lines[j] |
| 45 end = lines[j] | 47 end = lines[j] |
| 46 j += 1 | 48 j += 1 |
| 47 elif start: | 49 elif start: |
| 48 pairs.append((start, end)) | 50 pairs.append((start, end)) |
| 49 start = None | 51 start = None |
| 50 i += 1 | 52 i += 1 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 79 """Return bool(b), but preserve None.""" | 81 """Return bool(b), but preserve None.""" |
| 80 if b is None: | 82 if b is None: |
| 81 return None | 83 return None |
| 82 else: | 84 else: |
| 83 return bool(b) | 85 return bool(b) |
| 84 | 86 |
| 85 | 87 |
| 86 def join_regex(regexes): | 88 def join_regex(regexes): |
| 87 """Combine a list of regexes into one that matches any of them.""" | 89 """Combine a list of regexes into one that matches any of them.""" |
| 88 if len(regexes) > 1: | 90 if len(regexes) > 1: |
| 89 return "(" + ")|(".join(regexes) + ")" | 91 return "|".join(["(%s)" % r for r in regexes]) |
| 90 elif regexes: | 92 elif regexes: |
| 91 return regexes[0] | 93 return regexes[0] |
| 92 else: | 94 else: |
| 93 return "" | 95 return "" |
| 94 | 96 |
| 95 | 97 |
| 96 def file_be_gone(path): | 98 def file_be_gone(path): |
| 97 """Remove a file, and don't get annoyed if it doesn't exist.""" | 99 """Remove a file, and don't get annoyed if it doesn't exist.""" |
| 98 try: | 100 try: |
| 99 os.remove(path) | 101 os.remove(path) |
| 100 except OSError: | 102 except OSError: |
| 101 _, e, _ = sys.exc_info() | 103 _, e, _ = sys.exc_info() |
| 102 if e.errno != errno.ENOENT: | 104 if e.errno != errno.ENOENT: |
| 103 raise | 105 raise |
| 104 | 106 |
| 105 | 107 |
| 106 class Hasher(object): | 108 class Hasher(object): |
| 107 """Hashes Python data into md5.""" | 109 """Hashes Python data into md5.""" |
| 108 def __init__(self): | 110 def __init__(self): |
| 109 self.md5 = md5() | 111 self.md5 = md5() |
| 110 | 112 |
| 111 def update(self, v): | 113 def update(self, v): |
| 112 """Add `v` to the hash, recursively if needed.""" | 114 """Add `v` to the hash, recursively if needed.""" |
| 113 self.md5.update(to_bytes(str(type(v)))) | 115 self.md5.update(to_bytes(str(type(v)))) |
| 114 if isinstance(v, string_class): | 116 if isinstance(v, string_class): |
| 115 self.md5.update(to_bytes(v)) | 117 self.md5.update(to_bytes(v)) |
| 118 elif v is None: |
| 119 pass |
| 116 elif isinstance(v, (int, float)): | 120 elif isinstance(v, (int, float)): |
| 117 self.update(str(v)) | 121 self.md5.update(to_bytes(str(v))) |
| 118 elif isinstance(v, (tuple, list)): | 122 elif isinstance(v, (tuple, list)): |
| 119 for e in v: | 123 for e in v: |
| 120 self.update(e) | 124 self.update(e) |
| 121 elif isinstance(v, dict): | 125 elif isinstance(v, dict): |
| 122 keys = v.keys() | 126 keys = v.keys() |
| 123 for k in sorted(keys): | 127 for k in sorted(keys): |
| 124 self.update(k) | 128 self.update(k) |
| 125 self.update(v[k]) | 129 self.update(v[k]) |
| 126 else: | 130 else: |
| 127 for k in dir(v): | 131 for k in dir(v): |
| (...skipping 11 matching lines...) Expand all Loading... |
| 139 | 143 |
| 140 | 144 |
| 141 class CoverageException(Exception): | 145 class CoverageException(Exception): |
| 142 """An exception specific to Coverage.""" | 146 """An exception specific to Coverage.""" |
| 143 pass | 147 pass |
| 144 | 148 |
| 145 class NoSource(CoverageException): | 149 class NoSource(CoverageException): |
| 146 """We couldn't find the source for a module.""" | 150 """We couldn't find the source for a module.""" |
| 147 pass | 151 pass |
| 148 | 152 |
| 153 class NoCode(NoSource): |
| 154 """We couldn't find any code at all.""" |
| 155 pass |
| 156 |
| 149 class NotPython(CoverageException): | 157 class NotPython(CoverageException): |
| 150 """A source file turned out not to be parsable Python.""" | 158 """A source file turned out not to be parsable Python.""" |
| 151 pass | 159 pass |
| 152 | 160 |
| 153 class ExceptionDuringRun(CoverageException): | 161 class ExceptionDuringRun(CoverageException): |
| 154 """An exception happened while running customer code. | 162 """An exception happened while running customer code. |
| 155 | 163 |
| 156 Construct it with three arguments, the values from `sys.exc_info`. | 164 Construct it with three arguments, the values from `sys.exc_info`. |
| 157 | 165 |
| 158 """ | 166 """ |
| 159 pass | 167 pass |
| OLD | NEW |