hy-coverage 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hy_coverage/__init__.py
ADDED
hy_coverage/context.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Compile-time tracking hooks for Hy coverage."""
|
|
2
|
+
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
from contextvars import ContextVar
|
|
5
|
+
from functools import wraps
|
|
6
|
+
|
|
7
|
+
_expansion_active = ContextVar("hy_coverage_expansion_active", default=False)
|
|
8
|
+
_hooks_installed = False
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def expansion_active():
|
|
12
|
+
"""Return whether the current context is executing a macro expansion."""
|
|
13
|
+
return _expansion_active.get()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@contextmanager
|
|
17
|
+
def expansion_context():
|
|
18
|
+
token = _expansion_active.set(True)
|
|
19
|
+
try:
|
|
20
|
+
yield
|
|
21
|
+
finally:
|
|
22
|
+
_expansion_active.reset(token)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _wrap(function):
|
|
26
|
+
if getattr(function, "_hy_coverage_wrapped", False):
|
|
27
|
+
return function
|
|
28
|
+
|
|
29
|
+
@wraps(function)
|
|
30
|
+
def wrapped(*args, **kwargs):
|
|
31
|
+
with expansion_context():
|
|
32
|
+
return function(*args, **kwargs)
|
|
33
|
+
|
|
34
|
+
wrapped._hy_coverage_wrapped = True
|
|
35
|
+
return wrapped
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def install_hooks():
|
|
39
|
+
"""Wrap Hy expansion entry points without changing Hy itself."""
|
|
40
|
+
global _hooks_installed
|
|
41
|
+
if _hooks_installed:
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
import hy.compiler
|
|
45
|
+
import hy.macros
|
|
46
|
+
import hy.reader.hy_reader as hy_reader
|
|
47
|
+
|
|
48
|
+
hy.macros.macroexpand = _wrap(hy.macros.macroexpand)
|
|
49
|
+
hy.compiler.macroexpand = _wrap(hy.compiler.macroexpand)
|
|
50
|
+
|
|
51
|
+
hy_reader.HyReader.tag_dispatch = _wrap(hy_reader.HyReader.tag_dispatch)
|
|
52
|
+
hy_reader.HyReader.DEFAULT_TABLE["#"] = hy_reader.HyReader.tag_dispatch
|
|
53
|
+
|
|
54
|
+
_hooks_installed = True
|
hy_coverage/plugin.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""coverage.py plugin for Hy source files."""
|
|
2
|
+
|
|
3
|
+
import collections
|
|
4
|
+
import dis
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import types
|
|
8
|
+
import uuid
|
|
9
|
+
|
|
10
|
+
from coverage import CoverageData
|
|
11
|
+
from coverage.plugin import CoveragePlugin, FileReporter, FileTracer
|
|
12
|
+
|
|
13
|
+
from hy import read_many
|
|
14
|
+
from hy.compiler import hy_compile
|
|
15
|
+
|
|
16
|
+
from .context import expansion_active, install_hooks
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
COMPILE_TIME_CONTEXT = "hy-compile-time"
|
|
20
|
+
|
|
21
|
+
_UNCONDITIONAL_JUMPS = frozenset(
|
|
22
|
+
{
|
|
23
|
+
"JUMP_ABSOLUTE",
|
|
24
|
+
"JUMP_BACKWARD",
|
|
25
|
+
"JUMP_BACKWARD_NO_INTERRUPT",
|
|
26
|
+
"JUMP_FORWARD",
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
_RETURN_OPS = frozenset({"RAISE_VARARGS", "RERAISE", "RETURN_CONST", "RETURN_VALUE"})
|
|
30
|
+
_SKIPPED_OPS = frozenset({"RESUME", "RETURN_GENERATOR"})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class HyFileTracer(FileTracer):
|
|
34
|
+
def __init__(self, filename):
|
|
35
|
+
self.filename = filename
|
|
36
|
+
|
|
37
|
+
def source_filename(self):
|
|
38
|
+
return self.filename
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class HyFileReporter(FileReporter):
|
|
42
|
+
def __init__(self, filename, data_file):
|
|
43
|
+
super().__init__(filename)
|
|
44
|
+
self.data_file = data_file
|
|
45
|
+
self._measurement = None
|
|
46
|
+
|
|
47
|
+
def _measure(self):
|
|
48
|
+
if self._measurement is None:
|
|
49
|
+
source = self.source()
|
|
50
|
+
executable_lines, compile_time_lines, arcs = _forced_measurement(
|
|
51
|
+
self.filename, source
|
|
52
|
+
)
|
|
53
|
+
runtime_lines = _runtime_lines(self.filename, self.data_file)
|
|
54
|
+
lines = executable_lines - (compile_time_lines - runtime_lines)
|
|
55
|
+
arcs = {
|
|
56
|
+
arc
|
|
57
|
+
for arc in arcs
|
|
58
|
+
if (arc[0] == -1 or arc[0] in lines)
|
|
59
|
+
and (arc[1] == -1 or arc[1] in lines)
|
|
60
|
+
}
|
|
61
|
+
self._measurement = lines, arcs
|
|
62
|
+
return self._measurement
|
|
63
|
+
|
|
64
|
+
def lines(self):
|
|
65
|
+
return self._measure()[0]
|
|
66
|
+
|
|
67
|
+
def arcs(self):
|
|
68
|
+
return self._measure()[1]
|
|
69
|
+
|
|
70
|
+
def exit_counts(self):
|
|
71
|
+
counts = collections.Counter()
|
|
72
|
+
for start, _ in self.arcs():
|
|
73
|
+
if start > 0:
|
|
74
|
+
counts[start] += 1
|
|
75
|
+
return dict(counts)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class HyCoveragePlugin(CoveragePlugin):
|
|
79
|
+
def __init__(self):
|
|
80
|
+
self.data_file = ".coverage"
|
|
81
|
+
|
|
82
|
+
def configure(self, config):
|
|
83
|
+
data_file = config.get_option("run:data_file")
|
|
84
|
+
if data_file:
|
|
85
|
+
self.data_file = os.path.abspath(data_file)
|
|
86
|
+
|
|
87
|
+
def file_tracer(self, filename):
|
|
88
|
+
if filename.endswith(".hy"):
|
|
89
|
+
return HyFileTracer(filename)
|
|
90
|
+
return None
|
|
91
|
+
|
|
92
|
+
def file_reporter(self, filename):
|
|
93
|
+
return HyFileReporter(filename, self.data_file)
|
|
94
|
+
|
|
95
|
+
def dynamic_context(self, frame):
|
|
96
|
+
if expansion_active():
|
|
97
|
+
return COMPILE_TIME_CONTEXT
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def coverage_init(reg, options):
|
|
102
|
+
install_hooks()
|
|
103
|
+
plugin = HyCoveragePlugin()
|
|
104
|
+
reg.add_configurer(plugin)
|
|
105
|
+
reg.add_file_tracer(plugin)
|
|
106
|
+
reg.add_dynamic_context(plugin)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _runtime_lines(filename, data_file):
|
|
110
|
+
data = CoverageData(data_file, read_only=True)
|
|
111
|
+
data.read()
|
|
112
|
+
contexts = data.contexts_by_lineno(filename)
|
|
113
|
+
return {
|
|
114
|
+
line
|
|
115
|
+
for line, line_contexts in contexts.items()
|
|
116
|
+
if any(context != COMPILE_TIME_CONTEXT for context in line_contexts)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _forced_measurement(filename, source):
|
|
121
|
+
compile_time_lines = set()
|
|
122
|
+
forced_lines = set()
|
|
123
|
+
module_name = "_hy_coverage_" + uuid.uuid4().hex
|
|
124
|
+
module = types.ModuleType(module_name)
|
|
125
|
+
module.__file__ = filename
|
|
126
|
+
|
|
127
|
+
def trace(frame, event, arg):
|
|
128
|
+
if event == "line" and frame.f_code.co_filename == filename:
|
|
129
|
+
forced_lines.add(frame.f_lineno)
|
|
130
|
+
if expansion_active():
|
|
131
|
+
compile_time_lines.add(frame.f_lineno)
|
|
132
|
+
return trace
|
|
133
|
+
|
|
134
|
+
previous_module = sys.modules.get(module_name)
|
|
135
|
+
sys.modules[module_name] = module
|
|
136
|
+
previous_trace = sys.gettrace()
|
|
137
|
+
sys.settrace(trace)
|
|
138
|
+
try:
|
|
139
|
+
try:
|
|
140
|
+
tree = read_many(source, filename=filename)
|
|
141
|
+
ast_tree = hy_compile(
|
|
142
|
+
tree, module, filename=filename, source=source, import_stdlib=True
|
|
143
|
+
)
|
|
144
|
+
finally:
|
|
145
|
+
sys.settrace(previous_trace)
|
|
146
|
+
code = compile(ast_tree, filename, "exec")
|
|
147
|
+
finally:
|
|
148
|
+
if previous_module is None:
|
|
149
|
+
sys.modules.pop(module_name, None)
|
|
150
|
+
else:
|
|
151
|
+
sys.modules[module_name] = previous_module
|
|
152
|
+
|
|
153
|
+
line_count = len(source.splitlines())
|
|
154
|
+
forced_lines = {line for line in forced_lines if 0 < line <= line_count}
|
|
155
|
+
compile_time_lines &= forced_lines
|
|
156
|
+
for code_object in _walk_code(code):
|
|
157
|
+
forced_lines.update(
|
|
158
|
+
line
|
|
159
|
+
for _, line in dis.findlinestarts(code_object)
|
|
160
|
+
if line is not None and 0 < line <= line_count
|
|
161
|
+
)
|
|
162
|
+
arcs = set()
|
|
163
|
+
for code_object in _walk_code(code):
|
|
164
|
+
arcs.update(_code_arcs(code_object, forced_lines))
|
|
165
|
+
return forced_lines, compile_time_lines, arcs
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _code_arcs(code, lines):
|
|
169
|
+
line_starts = sorted(
|
|
170
|
+
(start, line) for start, line in dis.findlinestarts(code) if line is not None
|
|
171
|
+
)
|
|
172
|
+
instructions = list(dis.get_instructions(code))
|
|
173
|
+
|
|
174
|
+
def line_at(offset):
|
|
175
|
+
line = None
|
|
176
|
+
for start, candidate in line_starts:
|
|
177
|
+
if start > offset:
|
|
178
|
+
break
|
|
179
|
+
if candidate is not None:
|
|
180
|
+
line = candidate
|
|
181
|
+
return line
|
|
182
|
+
|
|
183
|
+
arcs = set()
|
|
184
|
+
first_line = None
|
|
185
|
+
for instruction in instructions:
|
|
186
|
+
if instruction.opname in _SKIPPED_OPS:
|
|
187
|
+
continue
|
|
188
|
+
line = line_at(instruction.offset)
|
|
189
|
+
if line is not None and line > 0 and line in lines:
|
|
190
|
+
first_line = line
|
|
191
|
+
break
|
|
192
|
+
if first_line is not None:
|
|
193
|
+
arcs.add((-1, first_line))
|
|
194
|
+
|
|
195
|
+
for index, instruction in enumerate(instructions):
|
|
196
|
+
if instruction.opname in _SKIPPED_OPS:
|
|
197
|
+
continue
|
|
198
|
+
current = line_at(instruction.offset)
|
|
199
|
+
if current is None or current <= 0 or current not in lines:
|
|
200
|
+
continue
|
|
201
|
+
next_instruction = instructions[index + 1] if index + 1 < len(instructions) else None
|
|
202
|
+
next_line = line_at(next_instruction.offset) if next_instruction else None
|
|
203
|
+
if instruction.opname in _RETURN_OPS:
|
|
204
|
+
arcs.add((current, -1))
|
|
205
|
+
continue
|
|
206
|
+
if instruction.opcode in dis.hasjabs or instruction.opcode in dis.hasjrel:
|
|
207
|
+
target = line_at(instruction.argval)
|
|
208
|
+
if target is not None and target > 0 and target in lines and target != current:
|
|
209
|
+
arcs.add((current, target))
|
|
210
|
+
if (
|
|
211
|
+
instruction.opname not in _UNCONDITIONAL_JUMPS
|
|
212
|
+
and next_instruction is not None
|
|
213
|
+
and next_line is not None
|
|
214
|
+
and next_line > 0
|
|
215
|
+
and next_line in lines
|
|
216
|
+
and next_line != current
|
|
217
|
+
):
|
|
218
|
+
arcs.add((current, next_line))
|
|
219
|
+
continue
|
|
220
|
+
if (
|
|
221
|
+
next_instruction is not None
|
|
222
|
+
and next_line is not None
|
|
223
|
+
and next_line > 0
|
|
224
|
+
and next_line in lines
|
|
225
|
+
and next_line != current
|
|
226
|
+
):
|
|
227
|
+
arcs.add((current, next_line))
|
|
228
|
+
return arcs
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _walk_code(code):
|
|
232
|
+
yield code
|
|
233
|
+
for constant in code.co_consts:
|
|
234
|
+
if isinstance(constant, types.CodeType):
|
|
235
|
+
yield from _walk_code(constant)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
hy_coverage/__init__.py,sha256=Hdg8sgt0oW1B5iKXqQ7uPt10qYpTLaX6uKO2OTtopuw,121
|
|
2
|
+
hy_coverage/context.py,sha256=ZHtIhvDS_SXRCfzXWiYKswY7B8rdV768Fs8tSBrSRlQ,1403
|
|
3
|
+
hy_coverage/plugin.py,sha256=QbZpy6sAKBRtuIaGr_FcYd3XjdVsMnBwabqf5YKSU2I,7197
|
|
4
|
+
hy_coverage-0.1.0.dist-info/METADATA,sha256=yk5rfbpV2B__02Zh83cuGShe8ZcPag-6Ow3702-3EuA,242
|
|
5
|
+
hy_coverage-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
hy_coverage-0.1.0.dist-info/top_level.txt,sha256=MBkPKnRRa5iwjRsHMhELIYjD3Hci1XZ1_h5jtrVu9Uo,12
|
|
7
|
+
hy_coverage-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hy_coverage
|