coverage 7.13.1__cp313-cp313-musllinux_1_2_riscv64.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.
- a1_coverage.pth +1 -0
- coverage/__init__.py +38 -0
- coverage/__main__.py +12 -0
- coverage/annotate.py +113 -0
- coverage/bytecode.py +197 -0
- coverage/cmdline.py +1220 -0
- coverage/collector.py +487 -0
- coverage/config.py +732 -0
- coverage/context.py +74 -0
- coverage/control.py +1514 -0
- coverage/core.py +139 -0
- coverage/data.py +251 -0
- coverage/debug.py +669 -0
- coverage/disposition.py +59 -0
- coverage/env.py +135 -0
- coverage/exceptions.py +85 -0
- coverage/execfile.py +329 -0
- coverage/files.py +553 -0
- coverage/html.py +860 -0
- coverage/htmlfiles/coverage_html.js +735 -0
- coverage/htmlfiles/favicon_32.png +0 -0
- coverage/htmlfiles/index.html +199 -0
- coverage/htmlfiles/keybd_closed.png +0 -0
- coverage/htmlfiles/pyfile.html +149 -0
- coverage/htmlfiles/style.css +389 -0
- coverage/htmlfiles/style.scss +844 -0
- coverage/inorout.py +590 -0
- coverage/jsonreport.py +200 -0
- coverage/lcovreport.py +218 -0
- coverage/misc.py +381 -0
- coverage/multiproc.py +120 -0
- coverage/numbits.py +146 -0
- coverage/parser.py +1215 -0
- coverage/patch.py +118 -0
- coverage/phystokens.py +197 -0
- coverage/plugin.py +617 -0
- coverage/plugin_support.py +299 -0
- coverage/pth_file.py +16 -0
- coverage/py.typed +1 -0
- coverage/python.py +272 -0
- coverage/pytracer.py +370 -0
- coverage/regions.py +127 -0
- coverage/report.py +298 -0
- coverage/report_core.py +117 -0
- coverage/results.py +502 -0
- coverage/sqldata.py +1212 -0
- coverage/sqlitedb.py +226 -0
- coverage/sysmon.py +509 -0
- coverage/templite.py +319 -0
- coverage/tomlconfig.py +212 -0
- coverage/tracer.cpython-313-riscv64-linux-musl.so +0 -0
- coverage/tracer.pyi +43 -0
- coverage/types.py +214 -0
- coverage/version.py +35 -0
- coverage/xmlreport.py +263 -0
- coverage-7.13.1.dist-info/METADATA +200 -0
- coverage-7.13.1.dist-info/RECORD +61 -0
- coverage-7.13.1.dist-info/WHEEL +5 -0
- coverage-7.13.1.dist-info/entry_points.txt +4 -0
- coverage-7.13.1.dist-info/licenses/LICENSE.txt +177 -0
- coverage-7.13.1.dist-info/top_level.txt +1 -0
coverage/control.py
ADDED
|
@@ -0,0 +1,1514 @@
|
|
|
1
|
+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
|
2
|
+
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
|
|
3
|
+
|
|
4
|
+
"""Central control stuff for coverage.py."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import atexit
|
|
9
|
+
import collections
|
|
10
|
+
import contextlib
|
|
11
|
+
import datetime
|
|
12
|
+
import functools
|
|
13
|
+
import os
|
|
14
|
+
import os.path
|
|
15
|
+
import signal
|
|
16
|
+
import sys
|
|
17
|
+
import threading
|
|
18
|
+
import time
|
|
19
|
+
import warnings
|
|
20
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
21
|
+
from types import FrameType
|
|
22
|
+
from typing import IO, Any, cast
|
|
23
|
+
|
|
24
|
+
from coverage import env
|
|
25
|
+
from coverage.annotate import AnnotateReporter
|
|
26
|
+
from coverage.collector import Collector
|
|
27
|
+
from coverage.config import CoverageConfig, read_coverage_config
|
|
28
|
+
from coverage.context import combine_context_switchers, should_start_context_test_function
|
|
29
|
+
from coverage.core import CTRACER_FILE, Core
|
|
30
|
+
from coverage.data import CoverageData, combine_parallel_data
|
|
31
|
+
from coverage.debug import (
|
|
32
|
+
DebugControl,
|
|
33
|
+
NoDebugging,
|
|
34
|
+
relevant_environment_display,
|
|
35
|
+
short_stack,
|
|
36
|
+
write_formatted_info,
|
|
37
|
+
)
|
|
38
|
+
from coverage.disposition import disposition_debug_msg
|
|
39
|
+
from coverage.exceptions import ConfigError, CoverageException, CoverageWarning, PluginError
|
|
40
|
+
from coverage.files import PathAliases, abs_file, relative_filename, set_relative_directory
|
|
41
|
+
from coverage.html import HtmlReporter
|
|
42
|
+
from coverage.inorout import InOrOut
|
|
43
|
+
from coverage.jsonreport import JsonReporter
|
|
44
|
+
from coverage.lcovreport import LcovReporter
|
|
45
|
+
from coverage.misc import (
|
|
46
|
+
DefaultValue,
|
|
47
|
+
bool_or_none,
|
|
48
|
+
ensure_dir_for_file,
|
|
49
|
+
isolate_module,
|
|
50
|
+
join_regex,
|
|
51
|
+
)
|
|
52
|
+
from coverage.multiproc import patch_multiprocessing
|
|
53
|
+
from coverage.patch import apply_patches
|
|
54
|
+
from coverage.plugin import FileReporter
|
|
55
|
+
from coverage.plugin_support import Plugins, TCoverageInit
|
|
56
|
+
from coverage.python import PythonFileReporter
|
|
57
|
+
from coverage.report import SummaryReporter
|
|
58
|
+
from coverage.report_core import render_report
|
|
59
|
+
from coverage.results import Analysis, analysis_from_file_reporter
|
|
60
|
+
from coverage.types import (
|
|
61
|
+
FilePath,
|
|
62
|
+
TConfigSectionIn,
|
|
63
|
+
TConfigurable,
|
|
64
|
+
TConfigValueIn,
|
|
65
|
+
TConfigValueOut,
|
|
66
|
+
TFileDisposition,
|
|
67
|
+
TLineNo,
|
|
68
|
+
TMorf,
|
|
69
|
+
TMorfs,
|
|
70
|
+
)
|
|
71
|
+
from coverage.version import __url__
|
|
72
|
+
from coverage.xmlreport import XmlReporter
|
|
73
|
+
|
|
74
|
+
os = isolate_module(os)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@contextlib.contextmanager
|
|
78
|
+
def override_config(cov: Coverage, **kwargs: TConfigValueIn) -> Iterator[None]:
|
|
79
|
+
"""Temporarily tweak the configuration of `cov`.
|
|
80
|
+
|
|
81
|
+
The arguments are applied to `cov.config` with the `from_args` method.
|
|
82
|
+
At the end of the with-statement, the old configuration is restored.
|
|
83
|
+
"""
|
|
84
|
+
original_config = cov.config
|
|
85
|
+
cov.config = cov.config.copy()
|
|
86
|
+
try:
|
|
87
|
+
cov.config.from_args(**kwargs)
|
|
88
|
+
yield
|
|
89
|
+
finally:
|
|
90
|
+
cov.config = original_config
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
DEFAULT_DATAFILE = DefaultValue("MISSING")
|
|
94
|
+
_DEFAULT_DATAFILE = DEFAULT_DATAFILE # Just in case, for backwards compatibility
|
|
95
|
+
CONFIG_DATA_PREFIX = ":data:"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class Coverage(TConfigurable):
|
|
99
|
+
"""Programmatic access to coverage.py.
|
|
100
|
+
|
|
101
|
+
To use::
|
|
102
|
+
|
|
103
|
+
from coverage import Coverage
|
|
104
|
+
|
|
105
|
+
cov = Coverage()
|
|
106
|
+
cov.start()
|
|
107
|
+
#.. call your code ..
|
|
108
|
+
cov.stop()
|
|
109
|
+
cov.html_report(directory="covhtml")
|
|
110
|
+
|
|
111
|
+
A context manager is available to do the same thing::
|
|
112
|
+
|
|
113
|
+
cov = Coverage()
|
|
114
|
+
with cov.collect():
|
|
115
|
+
#.. call your code ..
|
|
116
|
+
cov.html_report(directory="covhtml")
|
|
117
|
+
|
|
118
|
+
Note: in keeping with Python custom, names starting with underscore are
|
|
119
|
+
not part of the public API. They might stop working at any point. Please
|
|
120
|
+
limit yourself to documented methods to avoid problems.
|
|
121
|
+
|
|
122
|
+
Methods can raise any of the exceptions described in :ref:`api_exceptions`.
|
|
123
|
+
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
# The stack of started Coverage instances.
|
|
127
|
+
_instances: list[Coverage] = []
|
|
128
|
+
|
|
129
|
+
@classmethod
|
|
130
|
+
def current(cls) -> Coverage | None:
|
|
131
|
+
"""Get the latest started `Coverage` instance, if any.
|
|
132
|
+
|
|
133
|
+
Returns: a `Coverage` instance, or None.
|
|
134
|
+
|
|
135
|
+
.. versionadded:: 5.0
|
|
136
|
+
|
|
137
|
+
"""
|
|
138
|
+
if cls._instances:
|
|
139
|
+
return cls._instances[-1]
|
|
140
|
+
else:
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
def __init__( # pylint: disable=too-many-arguments
|
|
144
|
+
self,
|
|
145
|
+
data_file: FilePath | DefaultValue | None = DEFAULT_DATAFILE,
|
|
146
|
+
data_suffix: str | bool | None = None,
|
|
147
|
+
cover_pylib: bool | None = None,
|
|
148
|
+
auto_data: bool = False,
|
|
149
|
+
timid: bool | None = None,
|
|
150
|
+
branch: bool | None = None,
|
|
151
|
+
config_file: FilePath | bool = True,
|
|
152
|
+
source: Iterable[str] | None = None,
|
|
153
|
+
source_pkgs: Iterable[str] | None = None,
|
|
154
|
+
source_dirs: Iterable[str] | None = None,
|
|
155
|
+
omit: str | Iterable[str] | None = None,
|
|
156
|
+
include: str | Iterable[str] | None = None,
|
|
157
|
+
debug: Iterable[str] | None = None,
|
|
158
|
+
concurrency: str | Iterable[str] | None = None,
|
|
159
|
+
check_preimported: bool = False,
|
|
160
|
+
context: str | None = None,
|
|
161
|
+
messages: bool = False,
|
|
162
|
+
plugins: Iterable[Callable[..., None]] | None = None,
|
|
163
|
+
) -> None:
|
|
164
|
+
"""
|
|
165
|
+
Many of these arguments duplicate and override values that can be
|
|
166
|
+
provided in a configuration file. Parameters that are missing here
|
|
167
|
+
will use values from the config file.
|
|
168
|
+
|
|
169
|
+
`data_file` is the base name of the data file to use. The config value
|
|
170
|
+
defaults to ".coverage". None can be provided to prevent writing a data
|
|
171
|
+
file. `data_suffix` is appended (with a dot) to `data_file` to create
|
|
172
|
+
the final file name. If `data_suffix` is simply True, then a suffix is
|
|
173
|
+
created with the machine and process identity included.
|
|
174
|
+
|
|
175
|
+
`cover_pylib` is a boolean determining whether Python code installed
|
|
176
|
+
with the Python interpreter is measured. This includes the Python
|
|
177
|
+
standard library and any packages installed with the interpreter.
|
|
178
|
+
|
|
179
|
+
If `auto_data` is true, then any existing data file will be read when
|
|
180
|
+
coverage measurement starts, and data will be saved automatically when
|
|
181
|
+
measurement stops.
|
|
182
|
+
|
|
183
|
+
If `timid` is true, then a slower and simpler trace function will be
|
|
184
|
+
used. This is important for some environments where manipulation of
|
|
185
|
+
tracing functions breaks the faster trace function.
|
|
186
|
+
|
|
187
|
+
If `branch` is true, then branch coverage will be measured in addition
|
|
188
|
+
to the usual statement coverage.
|
|
189
|
+
|
|
190
|
+
`config_file` determines what configuration file to read:
|
|
191
|
+
|
|
192
|
+
* If it is ".coveragerc", it is interpreted as if it were True,
|
|
193
|
+
for backward compatibility.
|
|
194
|
+
|
|
195
|
+
* If it is a string, it is the name of the file to read. If the
|
|
196
|
+
file can't be read, it is an error.
|
|
197
|
+
|
|
198
|
+
* If it is True, then a few standard files names are tried
|
|
199
|
+
(".coveragerc", "setup.cfg", "tox.ini"). It is not an error for
|
|
200
|
+
these files to not be found.
|
|
201
|
+
|
|
202
|
+
* If it is False, then no configuration file is read.
|
|
203
|
+
|
|
204
|
+
`source` is a list of file paths or package names. Only code located
|
|
205
|
+
in the trees indicated by the file paths or package names will be
|
|
206
|
+
measured.
|
|
207
|
+
|
|
208
|
+
`source_pkgs` is a list of package names. It works the same as
|
|
209
|
+
`source`, but can be used to name packages where the name can also be
|
|
210
|
+
interpreted as a file path.
|
|
211
|
+
|
|
212
|
+
`source_dirs` is a list of file paths. It works the same as
|
|
213
|
+
`source`, but raises an error if the path doesn't exist, rather
|
|
214
|
+
than being treated as a package name.
|
|
215
|
+
|
|
216
|
+
`include` and `omit` are lists of file name patterns. Files that match
|
|
217
|
+
`include` will be measured, files that match `omit` will not. Each
|
|
218
|
+
will also accept a single string argument.
|
|
219
|
+
|
|
220
|
+
`debug` is a list of strings indicating what debugging information is
|
|
221
|
+
desired.
|
|
222
|
+
|
|
223
|
+
`concurrency` is a string indicating the concurrency library being used
|
|
224
|
+
in the measured code. Without this, coverage.py will get incorrect
|
|
225
|
+
results if these libraries are in use. Valid strings are "greenlet",
|
|
226
|
+
"eventlet", "gevent", "multiprocessing", or "thread" (the default).
|
|
227
|
+
This can also be a list of these strings.
|
|
228
|
+
|
|
229
|
+
If `check_preimported` is true, then when coverage is started, the
|
|
230
|
+
already-imported files will be checked to see if they should be
|
|
231
|
+
measured by coverage. Importing measured files before coverage is
|
|
232
|
+
started can mean that code is missed.
|
|
233
|
+
|
|
234
|
+
`context` is a string to use as the :ref:`static context
|
|
235
|
+
<static_contexts>` label for collected data.
|
|
236
|
+
|
|
237
|
+
If `messages` is true, some messages will be printed to stdout
|
|
238
|
+
indicating what is happening.
|
|
239
|
+
|
|
240
|
+
If `plugins` are passed, they are an iterable of function objects
|
|
241
|
+
accepting a `reg` object to register plugins, as described in
|
|
242
|
+
:ref:`api_plugin`. When they are provided, they will override the
|
|
243
|
+
plugins found in the coverage configuration file.
|
|
244
|
+
|
|
245
|
+
.. versionadded:: 4.0
|
|
246
|
+
The `concurrency` parameter.
|
|
247
|
+
|
|
248
|
+
.. versionadded:: 4.2
|
|
249
|
+
The `concurrency` parameter can now be a list of strings.
|
|
250
|
+
|
|
251
|
+
.. versionadded:: 5.0
|
|
252
|
+
The `check_preimported` and `context` parameters.
|
|
253
|
+
|
|
254
|
+
.. versionadded:: 5.3
|
|
255
|
+
The `source_pkgs` parameter.
|
|
256
|
+
|
|
257
|
+
.. versionadded:: 6.0
|
|
258
|
+
The `messages` parameter.
|
|
259
|
+
|
|
260
|
+
.. versionadded:: 7.7
|
|
261
|
+
The `plugins` parameter.
|
|
262
|
+
|
|
263
|
+
.. versionadded:: 7.8
|
|
264
|
+
The `source_dirs` parameter.
|
|
265
|
+
"""
|
|
266
|
+
# Start self.config as a usable default configuration. It will soon be
|
|
267
|
+
# replaced with the real configuration.
|
|
268
|
+
self.config = CoverageConfig()
|
|
269
|
+
|
|
270
|
+
# data_file=None means no disk file at all. data_file missing means
|
|
271
|
+
# use the value from the config file.
|
|
272
|
+
self._no_disk = data_file is None
|
|
273
|
+
if isinstance(data_file, DefaultValue):
|
|
274
|
+
data_file = None
|
|
275
|
+
if data_file is not None:
|
|
276
|
+
data_file = os.fspath(data_file)
|
|
277
|
+
|
|
278
|
+
# This is injectable by tests.
|
|
279
|
+
self._debug_file: IO[str] | None = None
|
|
280
|
+
|
|
281
|
+
self._auto_load = self._auto_save = auto_data
|
|
282
|
+
self._data_suffix_specified = data_suffix
|
|
283
|
+
|
|
284
|
+
# Is it ok for no data to be collected?
|
|
285
|
+
self._warn_no_data = True
|
|
286
|
+
self._warn_unimported_source = True
|
|
287
|
+
self._warn_preimported_source = check_preimported
|
|
288
|
+
self._no_warn_slugs: set[str] = set()
|
|
289
|
+
self._messages = messages
|
|
290
|
+
|
|
291
|
+
# A record of all the warnings that have been issued.
|
|
292
|
+
self._warnings: list[str] = []
|
|
293
|
+
|
|
294
|
+
# Other instance attributes, set with placebos or placeholders.
|
|
295
|
+
# More useful objects will be created later.
|
|
296
|
+
self._debug: DebugControl = NoDebugging()
|
|
297
|
+
self._inorout: InOrOut | None = None
|
|
298
|
+
self._plugins: Plugins = Plugins()
|
|
299
|
+
self._plugin_override = cast(Iterable[TCoverageInit] | None, plugins)
|
|
300
|
+
self._data: CoverageData | None = None
|
|
301
|
+
self._data_to_close: list[CoverageData] = []
|
|
302
|
+
self._core: Core | None = None
|
|
303
|
+
self._collector: Collector | None = None
|
|
304
|
+
self._metacov = False
|
|
305
|
+
|
|
306
|
+
self._file_mapper: Callable[[str], str] = abs_file
|
|
307
|
+
self._data_suffix = self._run_suffix = None
|
|
308
|
+
self._exclude_re: dict[str, str] = {}
|
|
309
|
+
self._old_sigterm: Callable[[int, FrameType | None], Any] | None = None
|
|
310
|
+
|
|
311
|
+
# State machine variables:
|
|
312
|
+
# Have we initialized everything?
|
|
313
|
+
self._inited = False
|
|
314
|
+
self._inited_for_start = False
|
|
315
|
+
# Have we started collecting and not stopped it?
|
|
316
|
+
self._started = False
|
|
317
|
+
# Should we write the debug output?
|
|
318
|
+
self._should_write_debug = True
|
|
319
|
+
|
|
320
|
+
# Build our configuration from a number of sources.
|
|
321
|
+
if isinstance(config_file, str) and config_file.startswith(CONFIG_DATA_PREFIX):
|
|
322
|
+
self.config = CoverageConfig.deserialize(config_file[len(CONFIG_DATA_PREFIX) :])
|
|
323
|
+
else:
|
|
324
|
+
if not isinstance(config_file, bool):
|
|
325
|
+
config_file = os.fspath(config_file)
|
|
326
|
+
self.config = read_coverage_config(
|
|
327
|
+
config_file=config_file,
|
|
328
|
+
warn=self._warn,
|
|
329
|
+
data_file=data_file,
|
|
330
|
+
cover_pylib=cover_pylib,
|
|
331
|
+
timid=timid,
|
|
332
|
+
branch=branch,
|
|
333
|
+
parallel=bool_or_none(data_suffix),
|
|
334
|
+
source=source,
|
|
335
|
+
source_pkgs=source_pkgs,
|
|
336
|
+
source_dirs=source_dirs,
|
|
337
|
+
run_omit=omit,
|
|
338
|
+
run_include=include,
|
|
339
|
+
debug=debug,
|
|
340
|
+
report_omit=omit,
|
|
341
|
+
report_include=include,
|
|
342
|
+
concurrency=concurrency,
|
|
343
|
+
context=context,
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
# If we have subprocess measurement happening automatically, then we
|
|
347
|
+
# want any explicit creation of a Coverage object to mean, this process
|
|
348
|
+
# is already coverage-aware, so don't auto-measure it. By now, the
|
|
349
|
+
# auto-creation of a Coverage object has already happened. But we can
|
|
350
|
+
# find it and tell it not to save its data.
|
|
351
|
+
if not env.METACOV:
|
|
352
|
+
_prevent_sub_process_measurement()
|
|
353
|
+
|
|
354
|
+
def __repr__(self) -> str:
|
|
355
|
+
core_name = self._core.tracer_class.__name__ if self._core is not None else "-none-"
|
|
356
|
+
data_file = repr(self._data._filename) if self._data is not None else "-none-"
|
|
357
|
+
return (
|
|
358
|
+
"<Coverage"
|
|
359
|
+
+ f" @0x{id(self):x}"
|
|
360
|
+
+ f" core={core_name}"
|
|
361
|
+
+ f" data_file={data_file}"
|
|
362
|
+
+ ">"
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
def _init(self) -> None:
|
|
366
|
+
"""Set all the initial state.
|
|
367
|
+
|
|
368
|
+
This is called by the public methods to initialize state. This lets us
|
|
369
|
+
construct a :class:`Coverage` object, then tweak its state before this
|
|
370
|
+
function is called.
|
|
371
|
+
|
|
372
|
+
"""
|
|
373
|
+
if self._inited:
|
|
374
|
+
return
|
|
375
|
+
|
|
376
|
+
self._inited = True
|
|
377
|
+
|
|
378
|
+
# Create and configure the debugging controller.
|
|
379
|
+
self._debug = DebugControl(self.config.debug, self._debug_file, self.config.debug_file)
|
|
380
|
+
if self._debug.should("process"):
|
|
381
|
+
self._debug.write("Coverage._init")
|
|
382
|
+
|
|
383
|
+
if "multiprocessing" in (self.config.concurrency or ()):
|
|
384
|
+
# Multi-processing uses parallel for the subprocesses, so also use
|
|
385
|
+
# it for the main process.
|
|
386
|
+
self.config.parallel = True
|
|
387
|
+
|
|
388
|
+
# _exclude_re is a dict that maps exclusion list names to compiled regexes.
|
|
389
|
+
self._exclude_re = {}
|
|
390
|
+
|
|
391
|
+
set_relative_directory()
|
|
392
|
+
if self.config.relative_files:
|
|
393
|
+
self._file_mapper = relative_filename
|
|
394
|
+
|
|
395
|
+
# Load plugins
|
|
396
|
+
self._plugins = Plugins(self._debug)
|
|
397
|
+
if self._plugin_override:
|
|
398
|
+
self._plugins.load_from_callables(self._plugin_override)
|
|
399
|
+
else:
|
|
400
|
+
self._plugins.load_from_config(self.config.plugins, self.config)
|
|
401
|
+
|
|
402
|
+
# Run configuring plugins.
|
|
403
|
+
for plugin in self._plugins.configurers:
|
|
404
|
+
# We need an object with set_option and get_option. Either self or
|
|
405
|
+
# self.config will do. Choosing randomly stops people from doing
|
|
406
|
+
# other things with those objects, against the public API. Yes,
|
|
407
|
+
# this is a bit childish. :)
|
|
408
|
+
plugin.configure([self, self.config][int(time.time()) % 2])
|
|
409
|
+
|
|
410
|
+
def _post_init(self) -> None:
|
|
411
|
+
"""Stuff to do after everything is initialized."""
|
|
412
|
+
if self._should_write_debug:
|
|
413
|
+
self._should_write_debug = False
|
|
414
|
+
self._write_startup_debug()
|
|
415
|
+
|
|
416
|
+
# "[run] _crash" will raise an exception if the value is close by in
|
|
417
|
+
# the call stack, for testing error handling.
|
|
418
|
+
if self.config._crash and self.config._crash in short_stack():
|
|
419
|
+
raise RuntimeError(f"Crashing because called by {self.config._crash}")
|
|
420
|
+
|
|
421
|
+
def _write_startup_debug(self) -> None:
|
|
422
|
+
"""Write out debug info at startup if needed."""
|
|
423
|
+
wrote_any = False
|
|
424
|
+
with self._debug.without_callers():
|
|
425
|
+
if self._debug.should("config"):
|
|
426
|
+
write_formatted_info(self._debug.write, "config", self.config.debug_info())
|
|
427
|
+
wrote_any = True
|
|
428
|
+
|
|
429
|
+
if self._debug.should("sys"):
|
|
430
|
+
write_formatted_info(self._debug.write, "sys", self.sys_info())
|
|
431
|
+
for plugin in self._plugins:
|
|
432
|
+
header = "sys: " + plugin._coverage_plugin_name
|
|
433
|
+
write_formatted_info(self._debug.write, header, plugin.sys_info())
|
|
434
|
+
wrote_any = True
|
|
435
|
+
|
|
436
|
+
if self._debug.should("pybehave"):
|
|
437
|
+
write_formatted_info(self._debug.write, "pybehave", env.debug_info())
|
|
438
|
+
wrote_any = True
|
|
439
|
+
|
|
440
|
+
if self._debug.should("sqlite"):
|
|
441
|
+
write_formatted_info(self._debug.write, "sqlite", CoverageData.sys_info())
|
|
442
|
+
wrote_any = True
|
|
443
|
+
|
|
444
|
+
if wrote_any:
|
|
445
|
+
write_formatted_info(self._debug.write, "end", ())
|
|
446
|
+
|
|
447
|
+
def _should_trace(self, filename: str, frame: FrameType) -> TFileDisposition:
|
|
448
|
+
"""Decide whether to trace execution in `filename`.
|
|
449
|
+
|
|
450
|
+
Calls `_should_trace_internal`, and returns the FileDisposition.
|
|
451
|
+
|
|
452
|
+
"""
|
|
453
|
+
assert self._inorout is not None
|
|
454
|
+
disp = self._inorout.should_trace(filename, frame)
|
|
455
|
+
if self._debug.should("trace"):
|
|
456
|
+
self._debug.write(disposition_debug_msg(disp))
|
|
457
|
+
return disp
|
|
458
|
+
|
|
459
|
+
def _check_include_omit_etc(self, filename: str, frame: FrameType) -> bool:
|
|
460
|
+
"""Check a file name against the include/omit/etc, rules, verbosely.
|
|
461
|
+
|
|
462
|
+
Returns a boolean: True if the file should be traced, False if not.
|
|
463
|
+
|
|
464
|
+
"""
|
|
465
|
+
assert self._inorout is not None
|
|
466
|
+
reason = self._inorout.check_include_omit_etc(filename, frame)
|
|
467
|
+
if self._debug.should("trace"):
|
|
468
|
+
if not reason:
|
|
469
|
+
msg = f"Including {filename!r}"
|
|
470
|
+
else:
|
|
471
|
+
msg = f"Not including {filename!r}: {reason}"
|
|
472
|
+
self._debug.write(msg)
|
|
473
|
+
|
|
474
|
+
return not reason
|
|
475
|
+
|
|
476
|
+
def _warn(self, msg: str, slug: str | None = None, once: bool = False) -> None:
|
|
477
|
+
"""Use `msg` as a warning.
|
|
478
|
+
|
|
479
|
+
For warning suppression, use `slug` as the shorthand.
|
|
480
|
+
|
|
481
|
+
If `once` is true, only show this warning once (determined by the
|
|
482
|
+
slug.)
|
|
483
|
+
|
|
484
|
+
"""
|
|
485
|
+
if not self._no_warn_slugs:
|
|
486
|
+
self._no_warn_slugs = set(self.config.disable_warnings)
|
|
487
|
+
|
|
488
|
+
if slug in self._no_warn_slugs:
|
|
489
|
+
# Don't issue the warning
|
|
490
|
+
return
|
|
491
|
+
|
|
492
|
+
self._warnings.append(msg)
|
|
493
|
+
if slug:
|
|
494
|
+
msg = f"{msg} ({slug}); see {__url__}/messages.html#warning-{slug}"
|
|
495
|
+
if self._debug.should("pid"):
|
|
496
|
+
msg = f"[{os.getpid()}] {msg}"
|
|
497
|
+
warnings.warn(msg, category=CoverageWarning, stacklevel=2)
|
|
498
|
+
|
|
499
|
+
if once:
|
|
500
|
+
assert slug is not None
|
|
501
|
+
self._no_warn_slugs.add(slug)
|
|
502
|
+
|
|
503
|
+
def _message(self, msg: str) -> None:
|
|
504
|
+
"""Write a message to the user, if configured to do so."""
|
|
505
|
+
if self._messages:
|
|
506
|
+
print(msg)
|
|
507
|
+
|
|
508
|
+
def get_option(self, option_name: str) -> TConfigValueOut | None:
|
|
509
|
+
"""Get an option from the configuration.
|
|
510
|
+
|
|
511
|
+
`option_name` is a colon-separated string indicating the section and
|
|
512
|
+
option name. For example, the ``branch`` option in the ``[run]``
|
|
513
|
+
section of the config file would be indicated with `"run:branch"`.
|
|
514
|
+
|
|
515
|
+
Returns the value of the option. The type depends on the option
|
|
516
|
+
selected.
|
|
517
|
+
|
|
518
|
+
As a special case, an `option_name` of ``"paths"`` will return an
|
|
519
|
+
dictionary with the entire ``[paths]`` section value.
|
|
520
|
+
|
|
521
|
+
.. versionadded:: 4.0
|
|
522
|
+
|
|
523
|
+
"""
|
|
524
|
+
return self.config.get_option(option_name)
|
|
525
|
+
|
|
526
|
+
def set_option(self, option_name: str, value: TConfigValueIn | TConfigSectionIn) -> None:
|
|
527
|
+
"""Set an option in the configuration.
|
|
528
|
+
|
|
529
|
+
`option_name` is a colon-separated string indicating the section and
|
|
530
|
+
option name. For example, the ``branch`` option in the ``[run]``
|
|
531
|
+
section of the config file would be indicated with ``"run:branch"``.
|
|
532
|
+
|
|
533
|
+
`value` is the new value for the option. This should be an
|
|
534
|
+
appropriate Python value. For example, use True for booleans, not the
|
|
535
|
+
string ``"True"``.
|
|
536
|
+
|
|
537
|
+
As an example, calling:
|
|
538
|
+
|
|
539
|
+
.. code-block:: python
|
|
540
|
+
|
|
541
|
+
cov.set_option("run:branch", True)
|
|
542
|
+
|
|
543
|
+
has the same effect as this configuration file:
|
|
544
|
+
|
|
545
|
+
.. code-block:: ini
|
|
546
|
+
|
|
547
|
+
[run]
|
|
548
|
+
branch = True
|
|
549
|
+
|
|
550
|
+
As a special case, an `option_name` of ``"paths"`` will replace the
|
|
551
|
+
entire ``[paths]`` section. The value should be a dictionary.
|
|
552
|
+
|
|
553
|
+
.. versionadded:: 4.0
|
|
554
|
+
|
|
555
|
+
"""
|
|
556
|
+
self.config.set_option(option_name, value)
|
|
557
|
+
|
|
558
|
+
def load(self) -> None:
|
|
559
|
+
"""Load previously-collected coverage data from the data file."""
|
|
560
|
+
self._init()
|
|
561
|
+
if self._collector is not None:
|
|
562
|
+
self._collector.reset()
|
|
563
|
+
should_skip = self.config.parallel and not os.path.exists(self.config.data_file)
|
|
564
|
+
if not should_skip:
|
|
565
|
+
self._init_data(suffix=None)
|
|
566
|
+
self._post_init()
|
|
567
|
+
if not should_skip:
|
|
568
|
+
assert self._data is not None
|
|
569
|
+
self._data.read()
|
|
570
|
+
|
|
571
|
+
def _init_for_start(self) -> None:
|
|
572
|
+
"""Initialization for start()"""
|
|
573
|
+
# Construct the collector.
|
|
574
|
+
concurrency: list[str] = self.config.concurrency
|
|
575
|
+
if "multiprocessing" in concurrency:
|
|
576
|
+
if self.config.config_file is None:
|
|
577
|
+
raise ConfigError("multiprocessing requires a configuration file")
|
|
578
|
+
patch_multiprocessing(rcfile=self.config.config_file)
|
|
579
|
+
|
|
580
|
+
dycon = self.config.dynamic_context
|
|
581
|
+
if not dycon or dycon == "none":
|
|
582
|
+
context_switchers = []
|
|
583
|
+
elif dycon == "test_function":
|
|
584
|
+
context_switchers = [should_start_context_test_function]
|
|
585
|
+
else:
|
|
586
|
+
raise ConfigError(f"Don't understand dynamic_context setting: {dycon!r}")
|
|
587
|
+
|
|
588
|
+
context_switchers.extend(
|
|
589
|
+
plugin.dynamic_context for plugin in self._plugins.context_switchers
|
|
590
|
+
)
|
|
591
|
+
|
|
592
|
+
should_start_context = combine_context_switchers(context_switchers)
|
|
593
|
+
|
|
594
|
+
self._core = Core(
|
|
595
|
+
warn=self._warn,
|
|
596
|
+
debug=(self._debug if self._debug.should("core") else None),
|
|
597
|
+
config=self.config,
|
|
598
|
+
dynamic_contexts=(should_start_context is not None),
|
|
599
|
+
metacov=self._metacov,
|
|
600
|
+
)
|
|
601
|
+
self._collector = Collector(
|
|
602
|
+
core=self._core,
|
|
603
|
+
should_trace=self._should_trace,
|
|
604
|
+
check_include=self._check_include_omit_etc,
|
|
605
|
+
should_start_context=should_start_context,
|
|
606
|
+
file_mapper=self._file_mapper,
|
|
607
|
+
branch=self.config.branch,
|
|
608
|
+
warn=self._warn,
|
|
609
|
+
concurrency=concurrency,
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
suffix = self._data_suffix_specified
|
|
613
|
+
if suffix:
|
|
614
|
+
if not isinstance(suffix, str):
|
|
615
|
+
# if data_suffix=True, use .machinename.pid.random
|
|
616
|
+
suffix = True
|
|
617
|
+
elif self.config.parallel:
|
|
618
|
+
if suffix is None:
|
|
619
|
+
suffix = True
|
|
620
|
+
elif not isinstance(suffix, str):
|
|
621
|
+
suffix = bool(suffix)
|
|
622
|
+
else:
|
|
623
|
+
suffix = None
|
|
624
|
+
|
|
625
|
+
self._init_data(suffix)
|
|
626
|
+
|
|
627
|
+
assert self._data is not None
|
|
628
|
+
self._collector.use_data(self._data, self.config.context)
|
|
629
|
+
|
|
630
|
+
# Early warning if we aren't going to be able to support plugins.
|
|
631
|
+
if self._plugins.file_tracers and not self._core.supports_plugins:
|
|
632
|
+
self._warn(
|
|
633
|
+
"Plugin file tracers ({}) aren't supported with {}".format(
|
|
634
|
+
", ".join(
|
|
635
|
+
plugin._coverage_plugin_name for plugin in self._plugins.file_tracers
|
|
636
|
+
),
|
|
637
|
+
self._collector.tracer_name(),
|
|
638
|
+
),
|
|
639
|
+
)
|
|
640
|
+
for plugin in self._plugins.file_tracers:
|
|
641
|
+
plugin._coverage_enabled = False
|
|
642
|
+
|
|
643
|
+
# Create the file classifying substructure.
|
|
644
|
+
self._inorout = InOrOut(
|
|
645
|
+
config=self.config,
|
|
646
|
+
warn=self._warn,
|
|
647
|
+
debug=(self._debug if self._debug.should("trace") else None),
|
|
648
|
+
include_namespace_packages=self.config.include_namespace_packages,
|
|
649
|
+
)
|
|
650
|
+
self._inorout.plugins = self._plugins
|
|
651
|
+
self._inorout.disp_class = self._core.file_disposition_class
|
|
652
|
+
|
|
653
|
+
# It's useful to write debug info after initing for start.
|
|
654
|
+
self._should_write_debug = True
|
|
655
|
+
|
|
656
|
+
# Register our clean-up handlers.
|
|
657
|
+
atexit.register(self._atexit)
|
|
658
|
+
if self.config.sigterm:
|
|
659
|
+
is_main = (threading.current_thread() == threading.main_thread()) # fmt: skip
|
|
660
|
+
if is_main and not env.WINDOWS:
|
|
661
|
+
# The Python docs seem to imply that SIGTERM works uniformly even
|
|
662
|
+
# on Windows, but that's not my experience, and this agrees:
|
|
663
|
+
# https://stackoverflow.com/questions/35772001/x/35792192#35792192
|
|
664
|
+
self._old_sigterm = signal.signal( # type: ignore[assignment]
|
|
665
|
+
signal.SIGTERM,
|
|
666
|
+
self._on_sigterm,
|
|
667
|
+
)
|
|
668
|
+
|
|
669
|
+
def _init_data(self, suffix: str | bool | None) -> None:
|
|
670
|
+
"""Create a data file if we don't have one yet."""
|
|
671
|
+
if self._data is None:
|
|
672
|
+
# Create the data file. We do this at construction time so that the
|
|
673
|
+
# data file will be written into the directory where the process
|
|
674
|
+
# started rather than wherever the process eventually chdir'd to.
|
|
675
|
+
ensure_dir_for_file(self.config.data_file)
|
|
676
|
+
self._data = CoverageData(
|
|
677
|
+
basename=self.config.data_file,
|
|
678
|
+
suffix=suffix,
|
|
679
|
+
warn=self._warn,
|
|
680
|
+
debug=self._debug,
|
|
681
|
+
no_disk=self._no_disk,
|
|
682
|
+
)
|
|
683
|
+
self._data_to_close.append(self._data)
|
|
684
|
+
|
|
685
|
+
def start(self) -> None:
|
|
686
|
+
"""Start measuring code coverage.
|
|
687
|
+
|
|
688
|
+
Coverage measurement is only collected in functions called after
|
|
689
|
+
:meth:`start` is invoked. Statements in the same scope as
|
|
690
|
+
:meth:`start` won't be measured.
|
|
691
|
+
|
|
692
|
+
Once you invoke :meth:`start`, you must also call :meth:`stop`
|
|
693
|
+
eventually, or your process might not shut down cleanly.
|
|
694
|
+
|
|
695
|
+
The :meth:`collect` method is a context manager to handle both
|
|
696
|
+
starting and stopping collection.
|
|
697
|
+
|
|
698
|
+
"""
|
|
699
|
+
self._init()
|
|
700
|
+
if not self._inited_for_start:
|
|
701
|
+
self._inited_for_start = True
|
|
702
|
+
self._init_for_start()
|
|
703
|
+
self._post_init()
|
|
704
|
+
|
|
705
|
+
assert self._collector is not None
|
|
706
|
+
assert self._inorout is not None
|
|
707
|
+
|
|
708
|
+
# Issue warnings for possible problems.
|
|
709
|
+
self._inorout.warn_conflicting_settings()
|
|
710
|
+
|
|
711
|
+
# See if we think some code that would eventually be measured has
|
|
712
|
+
# already been imported.
|
|
713
|
+
if self._warn_preimported_source:
|
|
714
|
+
self._inorout.warn_already_imported_files()
|
|
715
|
+
|
|
716
|
+
if self._auto_load:
|
|
717
|
+
self.load()
|
|
718
|
+
|
|
719
|
+
apply_patches(self, self.config, self._debug)
|
|
720
|
+
|
|
721
|
+
self._collector.start()
|
|
722
|
+
self._started = True
|
|
723
|
+
self._instances.append(self)
|
|
724
|
+
|
|
725
|
+
def stop(self) -> None:
|
|
726
|
+
"""Stop measuring code coverage."""
|
|
727
|
+
if self._instances:
|
|
728
|
+
if self._instances[-1] is self:
|
|
729
|
+
self._instances.pop()
|
|
730
|
+
if self._started:
|
|
731
|
+
assert self._collector is not None
|
|
732
|
+
self._collector.stop()
|
|
733
|
+
self._started = False
|
|
734
|
+
|
|
735
|
+
@contextlib.contextmanager
|
|
736
|
+
def collect(self) -> Iterator[None]:
|
|
737
|
+
"""A context manager to start/stop coverage measurement collection.
|
|
738
|
+
|
|
739
|
+
.. versionadded:: 7.3
|
|
740
|
+
|
|
741
|
+
"""
|
|
742
|
+
self.start()
|
|
743
|
+
try:
|
|
744
|
+
yield
|
|
745
|
+
finally:
|
|
746
|
+
self.stop() # pragma: nested
|
|
747
|
+
|
|
748
|
+
def _atexit(self, event: str = "atexit") -> None:
|
|
749
|
+
"""Clean up on process shutdown."""
|
|
750
|
+
if self._debug.should("process"):
|
|
751
|
+
self._debug.write(f"{event}: pid: {os.getpid()}, instance: {self!r}")
|
|
752
|
+
if self._started:
|
|
753
|
+
self.stop()
|
|
754
|
+
if self._auto_save or event == "sigterm":
|
|
755
|
+
self.save()
|
|
756
|
+
for d in self._data_to_close:
|
|
757
|
+
d.close(force=True)
|
|
758
|
+
|
|
759
|
+
def _on_sigterm(self, signum_unused: int, frame_unused: FrameType | None) -> None:
|
|
760
|
+
"""A handler for signal.SIGTERM."""
|
|
761
|
+
self._atexit("sigterm")
|
|
762
|
+
# Statements after here won't be seen by metacov because we just wrote
|
|
763
|
+
# the data, and are about to kill the process.
|
|
764
|
+
signal.signal(signal.SIGTERM, self._old_sigterm) # pragma: not covered
|
|
765
|
+
os.kill(os.getpid(), signal.SIGTERM) # pragma: not covered
|
|
766
|
+
|
|
767
|
+
def erase(self) -> None:
|
|
768
|
+
"""Erase previously collected coverage data.
|
|
769
|
+
|
|
770
|
+
This removes the in-memory data collected in this session as well as
|
|
771
|
+
discarding the data file.
|
|
772
|
+
|
|
773
|
+
"""
|
|
774
|
+
self._init()
|
|
775
|
+
self._post_init()
|
|
776
|
+
if self._collector is not None:
|
|
777
|
+
self._collector.reset()
|
|
778
|
+
self._init_data(suffix=None)
|
|
779
|
+
assert self._data is not None
|
|
780
|
+
self._data.erase(parallel=self.config.parallel)
|
|
781
|
+
self._data = None
|
|
782
|
+
self._inited_for_start = False
|
|
783
|
+
|
|
784
|
+
def switch_context(self, new_context: str) -> None:
|
|
785
|
+
"""Switch to a new dynamic context.
|
|
786
|
+
|
|
787
|
+
`new_context` is a string to use as the :ref:`dynamic context
|
|
788
|
+
<dynamic_contexts>` label for collected data. If a :ref:`static
|
|
789
|
+
context <static_contexts>` is in use, the static and dynamic context
|
|
790
|
+
labels will be joined together with a pipe character.
|
|
791
|
+
|
|
792
|
+
Coverage collection must be started already.
|
|
793
|
+
|
|
794
|
+
.. versionadded:: 5.0
|
|
795
|
+
|
|
796
|
+
"""
|
|
797
|
+
if not self._started: # pragma: part started
|
|
798
|
+
raise CoverageException("Cannot switch context, coverage is not started")
|
|
799
|
+
|
|
800
|
+
assert self._collector is not None
|
|
801
|
+
if self._collector.should_start_context:
|
|
802
|
+
self._warn("Conflicting dynamic contexts", slug="dynamic-conflict", once=True)
|
|
803
|
+
|
|
804
|
+
self._collector.switch_context(new_context)
|
|
805
|
+
|
|
806
|
+
def clear_exclude(self, which: str = "exclude") -> None:
|
|
807
|
+
"""Clear the exclude list."""
|
|
808
|
+
self._init()
|
|
809
|
+
setattr(self.config, f"{which}_list", [])
|
|
810
|
+
self._exclude_regex_stale()
|
|
811
|
+
|
|
812
|
+
def exclude(self, regex: str, which: str = "exclude") -> None:
|
|
813
|
+
"""Exclude source lines from execution consideration.
|
|
814
|
+
|
|
815
|
+
A number of lists of regular expressions are maintained. Each list
|
|
816
|
+
selects lines that are treated differently during reporting.
|
|
817
|
+
|
|
818
|
+
`which` determines which list is modified. The "exclude" list selects
|
|
819
|
+
lines that are not considered executable at all. The "partial" list
|
|
820
|
+
indicates lines with branches that are not taken.
|
|
821
|
+
|
|
822
|
+
`regex` is a regular expression. The regex is added to the specified
|
|
823
|
+
list. If any of the regexes in the list is found in a line, the line
|
|
824
|
+
is marked for special treatment during reporting.
|
|
825
|
+
|
|
826
|
+
"""
|
|
827
|
+
self._init()
|
|
828
|
+
excl_list = getattr(self.config, f"{which}_list")
|
|
829
|
+
excl_list.append(regex)
|
|
830
|
+
self._exclude_regex_stale()
|
|
831
|
+
|
|
832
|
+
def _exclude_regex_stale(self) -> None:
|
|
833
|
+
"""Drop all the compiled exclusion regexes, a list was modified."""
|
|
834
|
+
self._exclude_re.clear()
|
|
835
|
+
|
|
836
|
+
def _exclude_regex(self, which: str) -> str:
|
|
837
|
+
"""Return a regex string for the given exclusion list."""
|
|
838
|
+
if which not in self._exclude_re:
|
|
839
|
+
excl_list = getattr(self.config, f"{which}_list")
|
|
840
|
+
self._exclude_re[which] = join_regex(excl_list)
|
|
841
|
+
return self._exclude_re[which]
|
|
842
|
+
|
|
843
|
+
def get_exclude_list(self, which: str = "exclude") -> list[str]:
|
|
844
|
+
"""Return a list of excluded regex strings.
|
|
845
|
+
|
|
846
|
+
`which` indicates which list is desired. See :meth:`exclude` for the
|
|
847
|
+
lists that are available, and their meaning.
|
|
848
|
+
|
|
849
|
+
"""
|
|
850
|
+
self._init()
|
|
851
|
+
return cast(list[str], getattr(self.config, f"{which}_list"))
|
|
852
|
+
|
|
853
|
+
def save(self) -> None:
|
|
854
|
+
"""Save the collected coverage data to the data file."""
|
|
855
|
+
data = self.get_data()
|
|
856
|
+
data.write()
|
|
857
|
+
|
|
858
|
+
def _make_aliases(self) -> PathAliases:
|
|
859
|
+
"""Create a PathAliases from our configuration."""
|
|
860
|
+
aliases = PathAliases(
|
|
861
|
+
debugfn=(self._debug.write if self._debug.should("pathmap") else None),
|
|
862
|
+
relative=self.config.relative_files,
|
|
863
|
+
)
|
|
864
|
+
for paths in self.config.paths.values():
|
|
865
|
+
result = paths[0]
|
|
866
|
+
for pattern in paths[1:]:
|
|
867
|
+
aliases.add(pattern, result)
|
|
868
|
+
return aliases
|
|
869
|
+
|
|
870
|
+
def combine(
|
|
871
|
+
self,
|
|
872
|
+
data_paths: Iterable[str] | None = None,
|
|
873
|
+
strict: bool = False,
|
|
874
|
+
keep: bool = False,
|
|
875
|
+
) -> None:
|
|
876
|
+
"""Combine together a number of similarly-named coverage data files.
|
|
877
|
+
|
|
878
|
+
All coverage data files whose name starts with `data_file` (from the
|
|
879
|
+
coverage() constructor) will be read, and combined together into the
|
|
880
|
+
current measurements.
|
|
881
|
+
|
|
882
|
+
`data_paths` is a list of files or directories from which data should
|
|
883
|
+
be combined. If no list is passed, then the data files from the
|
|
884
|
+
directory indicated by the current data file (probably the current
|
|
885
|
+
directory) will be combined.
|
|
886
|
+
|
|
887
|
+
If `strict` is true, then it is an error to attempt to combine when
|
|
888
|
+
there are no data files to combine.
|
|
889
|
+
|
|
890
|
+
If `keep` is true, then original input data files won't be deleted.
|
|
891
|
+
|
|
892
|
+
.. versionadded:: 4.0
|
|
893
|
+
The `data_paths` parameter.
|
|
894
|
+
|
|
895
|
+
.. versionadded:: 4.3
|
|
896
|
+
The `strict` parameter.
|
|
897
|
+
|
|
898
|
+
.. versionadded: 5.5
|
|
899
|
+
The `keep` parameter.
|
|
900
|
+
"""
|
|
901
|
+
self._init()
|
|
902
|
+
self._init_data(suffix=None)
|
|
903
|
+
self._post_init()
|
|
904
|
+
self.get_data()
|
|
905
|
+
|
|
906
|
+
assert self._data is not None
|
|
907
|
+
combine_parallel_data(
|
|
908
|
+
self._data,
|
|
909
|
+
aliases=self._make_aliases(),
|
|
910
|
+
data_paths=data_paths,
|
|
911
|
+
strict=strict,
|
|
912
|
+
keep=keep,
|
|
913
|
+
message=self._message,
|
|
914
|
+
)
|
|
915
|
+
|
|
916
|
+
def get_data(self) -> CoverageData:
|
|
917
|
+
"""Get the collected data.
|
|
918
|
+
|
|
919
|
+
Also warn about various problems collecting data.
|
|
920
|
+
|
|
921
|
+
Returns a :class:`coverage.CoverageData`, the collected coverage data.
|
|
922
|
+
|
|
923
|
+
.. versionadded:: 4.0
|
|
924
|
+
|
|
925
|
+
"""
|
|
926
|
+
self._init()
|
|
927
|
+
self._init_data(suffix=None)
|
|
928
|
+
self._post_init()
|
|
929
|
+
|
|
930
|
+
if self._collector is not None:
|
|
931
|
+
for plugin in self._plugins:
|
|
932
|
+
if not plugin._coverage_enabled:
|
|
933
|
+
self._collector.plugin_was_disabled(plugin)
|
|
934
|
+
|
|
935
|
+
if self._collector.flush_data():
|
|
936
|
+
self._post_save_work()
|
|
937
|
+
|
|
938
|
+
assert self._data is not None
|
|
939
|
+
return self._data
|
|
940
|
+
|
|
941
|
+
def _post_save_work(self) -> None:
|
|
942
|
+
"""After saving data, look for warnings, post-work, etc.
|
|
943
|
+
|
|
944
|
+
Warn about things that should have happened but didn't.
|
|
945
|
+
Look for un-executed files.
|
|
946
|
+
|
|
947
|
+
"""
|
|
948
|
+
assert self._data is not None
|
|
949
|
+
assert self._inorout is not None
|
|
950
|
+
|
|
951
|
+
# If there are still entries in the source_pkgs_unmatched list,
|
|
952
|
+
# then we never encountered those packages.
|
|
953
|
+
if self._warn_unimported_source:
|
|
954
|
+
self._inorout.warn_unimported_source()
|
|
955
|
+
|
|
956
|
+
# Find out if we got any data.
|
|
957
|
+
if not self._data and self._warn_no_data:
|
|
958
|
+
self._warn("No data was collected.", slug="no-data-collected")
|
|
959
|
+
|
|
960
|
+
# Touch all the files that could have executed, so that we can
|
|
961
|
+
# mark completely un-executed files as 0% covered.
|
|
962
|
+
file_paths = collections.defaultdict(list)
|
|
963
|
+
for file_path, plugin_name in self._inorout.find_possibly_unexecuted_files():
|
|
964
|
+
file_path = self._file_mapper(file_path)
|
|
965
|
+
file_paths[plugin_name].append(file_path)
|
|
966
|
+
for plugin_name, paths in file_paths.items():
|
|
967
|
+
self._data.touch_files(paths, plugin_name)
|
|
968
|
+
|
|
969
|
+
# Backward compatibility with version 1.
|
|
970
|
+
def analysis(self, morf: TMorf) -> tuple[str, list[TLineNo], list[TLineNo], str]:
|
|
971
|
+
"""Like `analysis2` but doesn't return excluded line numbers."""
|
|
972
|
+
f, s, _, m, mf = self.analysis2(morf)
|
|
973
|
+
return f, s, m, mf
|
|
974
|
+
|
|
975
|
+
def analysis2(
|
|
976
|
+
self,
|
|
977
|
+
morf: TMorf,
|
|
978
|
+
) -> tuple[str, list[TLineNo], list[TLineNo], list[TLineNo], str]:
|
|
979
|
+
"""Analyze a module.
|
|
980
|
+
|
|
981
|
+
`morf` is a module or a file name. It will be analyzed to determine
|
|
982
|
+
its coverage statistics. The return value is a 5-tuple:
|
|
983
|
+
|
|
984
|
+
* The file name for the module.
|
|
985
|
+
* A list of line numbers of executable statements.
|
|
986
|
+
* A list of line numbers of excluded statements.
|
|
987
|
+
* A list of line numbers of statements not run (missing from
|
|
988
|
+
execution).
|
|
989
|
+
* A readable formatted string of the missing line numbers.
|
|
990
|
+
|
|
991
|
+
The analysis uses the source file itself and the current measured
|
|
992
|
+
coverage data.
|
|
993
|
+
|
|
994
|
+
"""
|
|
995
|
+
analysis = self._analyze(morf)
|
|
996
|
+
return (
|
|
997
|
+
analysis.filename,
|
|
998
|
+
sorted(analysis.statements),
|
|
999
|
+
sorted(analysis.excluded),
|
|
1000
|
+
sorted(analysis.missing),
|
|
1001
|
+
analysis.missing_formatted(),
|
|
1002
|
+
)
|
|
1003
|
+
|
|
1004
|
+
@functools.lru_cache(maxsize=1)
|
|
1005
|
+
def _analyze(self, morf: TMorf) -> Analysis:
|
|
1006
|
+
"""Analyze a module or file. Private for now."""
|
|
1007
|
+
self._init()
|
|
1008
|
+
self._post_init()
|
|
1009
|
+
|
|
1010
|
+
data = self.get_data()
|
|
1011
|
+
file_reporter = self._get_file_reporter(morf)
|
|
1012
|
+
filename = self._file_mapper(file_reporter.filename)
|
|
1013
|
+
return analysis_from_file_reporter(data, self.config.precision, file_reporter, filename)
|
|
1014
|
+
|
|
1015
|
+
def branch_stats(self, morf: TMorf) -> dict[TLineNo, tuple[int, int]]:
|
|
1016
|
+
"""Get branch statistics about a module.
|
|
1017
|
+
|
|
1018
|
+
`morf` is a module or a file name.
|
|
1019
|
+
|
|
1020
|
+
Returns a dict mapping line numbers to a tuple:
|
|
1021
|
+
(total_exits, taken_exits).
|
|
1022
|
+
|
|
1023
|
+
.. versionadded:: 7.7
|
|
1024
|
+
|
|
1025
|
+
"""
|
|
1026
|
+
analysis = self._analyze(morf)
|
|
1027
|
+
return analysis.branch_stats()
|
|
1028
|
+
|
|
1029
|
+
@functools.lru_cache(maxsize=1)
|
|
1030
|
+
def _get_file_reporter(self, morf: TMorf) -> FileReporter:
|
|
1031
|
+
"""Get a FileReporter for a module or file name."""
|
|
1032
|
+
assert self._data is not None
|
|
1033
|
+
plugin = None
|
|
1034
|
+
file_reporter: str | FileReporter = "python"
|
|
1035
|
+
|
|
1036
|
+
if isinstance(morf, str):
|
|
1037
|
+
mapped_morf = self._file_mapper(morf)
|
|
1038
|
+
plugin_name = self._data.file_tracer(mapped_morf)
|
|
1039
|
+
if plugin_name:
|
|
1040
|
+
plugin = self._plugins.get(plugin_name)
|
|
1041
|
+
|
|
1042
|
+
if plugin:
|
|
1043
|
+
file_reporter = plugin.file_reporter(mapped_morf)
|
|
1044
|
+
if file_reporter is None:
|
|
1045
|
+
raise PluginError(
|
|
1046
|
+
"Plugin {!r} did not provide a file reporter for {!r}.".format(
|
|
1047
|
+
plugin._coverage_plugin_name,
|
|
1048
|
+
morf,
|
|
1049
|
+
),
|
|
1050
|
+
)
|
|
1051
|
+
|
|
1052
|
+
if file_reporter == "python":
|
|
1053
|
+
file_reporter = PythonFileReporter(morf, self)
|
|
1054
|
+
|
|
1055
|
+
assert isinstance(file_reporter, FileReporter)
|
|
1056
|
+
return file_reporter
|
|
1057
|
+
|
|
1058
|
+
def _get_file_reporters(
|
|
1059
|
+
self,
|
|
1060
|
+
morfs: TMorfs = None,
|
|
1061
|
+
) -> list[tuple[FileReporter, TMorf]]:
|
|
1062
|
+
"""Get FileReporters for a list of modules or file names.
|
|
1063
|
+
|
|
1064
|
+
For each module or file name in `morfs`, find a FileReporter. Return
|
|
1065
|
+
a list pairing FileReporters with the morfs.
|
|
1066
|
+
|
|
1067
|
+
If `morfs` is a single module or file name, this returns a list of one
|
|
1068
|
+
FileReporter. If `morfs` is empty or None, then the list of all files
|
|
1069
|
+
measured is used to find the FileReporters.
|
|
1070
|
+
|
|
1071
|
+
"""
|
|
1072
|
+
assert self._data is not None
|
|
1073
|
+
if not morfs:
|
|
1074
|
+
morfs = self._data.measured_files()
|
|
1075
|
+
|
|
1076
|
+
# Be sure we have a collection.
|
|
1077
|
+
if not isinstance(morfs, (list, tuple, set)):
|
|
1078
|
+
morfs = [morfs] # type: ignore[list-item]
|
|
1079
|
+
|
|
1080
|
+
morfs = sorted(morfs, key=lambda m: m if isinstance(m, str) else m.__name__)
|
|
1081
|
+
return [(self._get_file_reporter(morf), morf) for morf in morfs]
|
|
1082
|
+
|
|
1083
|
+
def _prepare_data_for_reporting(self) -> None:
|
|
1084
|
+
"""Re-map data before reporting, to get implicit "combine" behavior."""
|
|
1085
|
+
if self.config.paths:
|
|
1086
|
+
mapped_data = CoverageData(warn=self._warn, debug=self._debug, no_disk=True)
|
|
1087
|
+
if self._data is not None:
|
|
1088
|
+
mapped_data.update(self._data, map_path=self._make_aliases().map)
|
|
1089
|
+
self._data = mapped_data
|
|
1090
|
+
self._data_to_close.append(mapped_data)
|
|
1091
|
+
|
|
1092
|
+
def report(
|
|
1093
|
+
self,
|
|
1094
|
+
morfs: TMorfs = None,
|
|
1095
|
+
show_missing: bool | None = None,
|
|
1096
|
+
ignore_errors: bool | None = None,
|
|
1097
|
+
file: IO[str] | None = None,
|
|
1098
|
+
omit: str | list[str] | None = None,
|
|
1099
|
+
include: str | list[str] | None = None,
|
|
1100
|
+
skip_covered: bool | None = None,
|
|
1101
|
+
contexts: list[str] | None = None,
|
|
1102
|
+
skip_empty: bool | None = None,
|
|
1103
|
+
precision: int | None = None,
|
|
1104
|
+
sort: str | None = None,
|
|
1105
|
+
output_format: str | None = None,
|
|
1106
|
+
) -> float:
|
|
1107
|
+
"""Write a textual summary report to `file`.
|
|
1108
|
+
|
|
1109
|
+
Each module in `morfs` is listed, with counts of statements, executed
|
|
1110
|
+
statements, missing statements, and a list of lines missed.
|
|
1111
|
+
|
|
1112
|
+
If `show_missing` is true, then details of which lines or branches are
|
|
1113
|
+
missing will be included in the report. If `ignore_errors` is true,
|
|
1114
|
+
then a failure while reporting a single file will not stop the entire
|
|
1115
|
+
report.
|
|
1116
|
+
|
|
1117
|
+
`file` is a file-like object, suitable for writing.
|
|
1118
|
+
|
|
1119
|
+
`output_format` determines the format, either "text" (the default),
|
|
1120
|
+
"markdown", or "total".
|
|
1121
|
+
|
|
1122
|
+
`include` is a list of file name patterns. Files that match will be
|
|
1123
|
+
included in the report. Files matching `omit` will not be included in
|
|
1124
|
+
the report.
|
|
1125
|
+
|
|
1126
|
+
If `skip_covered` is true, don't report on files with 100% coverage.
|
|
1127
|
+
|
|
1128
|
+
If `skip_empty` is true, don't report on empty files (those that have
|
|
1129
|
+
no statements).
|
|
1130
|
+
|
|
1131
|
+
`contexts` is a list of regular expression strings. Only data from
|
|
1132
|
+
:ref:`dynamic contexts <dynamic_contexts>` that match one of those
|
|
1133
|
+
expressions (using :func:`re.search <python:re.search>`) will be
|
|
1134
|
+
included in the report.
|
|
1135
|
+
|
|
1136
|
+
`precision` is the number of digits to display after the decimal
|
|
1137
|
+
point for percentages.
|
|
1138
|
+
|
|
1139
|
+
All of the arguments default to the settings read from the
|
|
1140
|
+
:ref:`configuration file <config>`.
|
|
1141
|
+
|
|
1142
|
+
Returns a float, the total percentage covered.
|
|
1143
|
+
|
|
1144
|
+
.. versionadded:: 4.0
|
|
1145
|
+
The `skip_covered` parameter.
|
|
1146
|
+
|
|
1147
|
+
.. versionadded:: 5.0
|
|
1148
|
+
The `contexts` and `skip_empty` parameters.
|
|
1149
|
+
|
|
1150
|
+
.. versionadded:: 5.2
|
|
1151
|
+
The `precision` parameter.
|
|
1152
|
+
|
|
1153
|
+
.. versionadded:: 7.0
|
|
1154
|
+
The `format` parameter.
|
|
1155
|
+
|
|
1156
|
+
"""
|
|
1157
|
+
self._prepare_data_for_reporting()
|
|
1158
|
+
with override_config(
|
|
1159
|
+
self,
|
|
1160
|
+
ignore_errors=ignore_errors,
|
|
1161
|
+
report_omit=omit,
|
|
1162
|
+
report_include=include,
|
|
1163
|
+
show_missing=show_missing,
|
|
1164
|
+
skip_covered=skip_covered,
|
|
1165
|
+
report_contexts=contexts,
|
|
1166
|
+
skip_empty=skip_empty,
|
|
1167
|
+
precision=precision,
|
|
1168
|
+
sort=sort,
|
|
1169
|
+
format=output_format,
|
|
1170
|
+
):
|
|
1171
|
+
reporter = SummaryReporter(self)
|
|
1172
|
+
return reporter.report(morfs, outfile=file)
|
|
1173
|
+
|
|
1174
|
+
def annotate(
|
|
1175
|
+
self,
|
|
1176
|
+
morfs: TMorfs = None,
|
|
1177
|
+
directory: str | None = None,
|
|
1178
|
+
ignore_errors: bool | None = None,
|
|
1179
|
+
omit: str | list[str] | None = None,
|
|
1180
|
+
include: str | list[str] | None = None,
|
|
1181
|
+
contexts: list[str] | None = None,
|
|
1182
|
+
) -> None:
|
|
1183
|
+
"""Annotate a list of modules.
|
|
1184
|
+
|
|
1185
|
+
Each module in `morfs` is annotated. The source is written to a new
|
|
1186
|
+
file, named with a ",cover" suffix, with each line prefixed with a
|
|
1187
|
+
marker to indicate the coverage of the line. Covered lines have ">",
|
|
1188
|
+
excluded lines have "-", and missing lines have "!".
|
|
1189
|
+
|
|
1190
|
+
See :meth:`report` for other arguments.
|
|
1191
|
+
|
|
1192
|
+
"""
|
|
1193
|
+
self._prepare_data_for_reporting()
|
|
1194
|
+
with override_config(
|
|
1195
|
+
self,
|
|
1196
|
+
ignore_errors=ignore_errors,
|
|
1197
|
+
report_omit=omit,
|
|
1198
|
+
report_include=include,
|
|
1199
|
+
report_contexts=contexts,
|
|
1200
|
+
):
|
|
1201
|
+
reporter = AnnotateReporter(self)
|
|
1202
|
+
reporter.report(morfs, directory=directory)
|
|
1203
|
+
|
|
1204
|
+
def html_report(
|
|
1205
|
+
self,
|
|
1206
|
+
morfs: TMorfs = None,
|
|
1207
|
+
directory: str | None = None,
|
|
1208
|
+
ignore_errors: bool | None = None,
|
|
1209
|
+
omit: str | list[str] | None = None,
|
|
1210
|
+
include: str | list[str] | None = None,
|
|
1211
|
+
extra_css: str | None = None,
|
|
1212
|
+
title: str | None = None,
|
|
1213
|
+
skip_covered: bool | None = None,
|
|
1214
|
+
show_contexts: bool | None = None,
|
|
1215
|
+
contexts: list[str] | None = None,
|
|
1216
|
+
skip_empty: bool | None = None,
|
|
1217
|
+
precision: int | None = None,
|
|
1218
|
+
) -> float:
|
|
1219
|
+
"""Generate an HTML report.
|
|
1220
|
+
|
|
1221
|
+
The HTML is written to `directory`. The file "index.html" is the
|
|
1222
|
+
overview starting point, with links to more detailed pages for
|
|
1223
|
+
individual modules.
|
|
1224
|
+
|
|
1225
|
+
`extra_css` is a path to a file of other CSS to apply on the page.
|
|
1226
|
+
It will be copied into the HTML directory.
|
|
1227
|
+
|
|
1228
|
+
`title` is a text string (not HTML) to use as the title of the HTML
|
|
1229
|
+
report.
|
|
1230
|
+
|
|
1231
|
+
See :meth:`report` for other arguments.
|
|
1232
|
+
|
|
1233
|
+
Returns a float, the total percentage covered.
|
|
1234
|
+
|
|
1235
|
+
.. note::
|
|
1236
|
+
|
|
1237
|
+
The HTML report files are generated incrementally based on the
|
|
1238
|
+
source files and coverage results. If you modify the report files,
|
|
1239
|
+
the changes will not be considered. You should be careful about
|
|
1240
|
+
changing the files in the report folder.
|
|
1241
|
+
|
|
1242
|
+
"""
|
|
1243
|
+
self._prepare_data_for_reporting()
|
|
1244
|
+
with override_config(
|
|
1245
|
+
self,
|
|
1246
|
+
ignore_errors=ignore_errors,
|
|
1247
|
+
report_omit=omit,
|
|
1248
|
+
report_include=include,
|
|
1249
|
+
html_dir=directory,
|
|
1250
|
+
extra_css=extra_css,
|
|
1251
|
+
html_title=title,
|
|
1252
|
+
html_skip_covered=skip_covered,
|
|
1253
|
+
show_contexts=show_contexts,
|
|
1254
|
+
report_contexts=contexts,
|
|
1255
|
+
html_skip_empty=skip_empty,
|
|
1256
|
+
precision=precision,
|
|
1257
|
+
):
|
|
1258
|
+
reporter = HtmlReporter(self)
|
|
1259
|
+
return reporter.report(morfs)
|
|
1260
|
+
|
|
1261
|
+
def xml_report(
|
|
1262
|
+
self,
|
|
1263
|
+
morfs: TMorfs = None,
|
|
1264
|
+
outfile: str | None = None,
|
|
1265
|
+
ignore_errors: bool | None = None,
|
|
1266
|
+
omit: str | list[str] | None = None,
|
|
1267
|
+
include: str | list[str] | None = None,
|
|
1268
|
+
contexts: list[str] | None = None,
|
|
1269
|
+
skip_empty: bool | None = None,
|
|
1270
|
+
) -> float:
|
|
1271
|
+
"""Generate an XML report of coverage results.
|
|
1272
|
+
|
|
1273
|
+
The report is compatible with Cobertura reports.
|
|
1274
|
+
|
|
1275
|
+
Each module in `morfs` is included in the report. `outfile` is the
|
|
1276
|
+
path to write the file to, "-" will write to stdout.
|
|
1277
|
+
|
|
1278
|
+
See :meth:`report` for other arguments.
|
|
1279
|
+
|
|
1280
|
+
Returns a float, the total percentage covered.
|
|
1281
|
+
|
|
1282
|
+
"""
|
|
1283
|
+
self._prepare_data_for_reporting()
|
|
1284
|
+
with override_config(
|
|
1285
|
+
self,
|
|
1286
|
+
ignore_errors=ignore_errors,
|
|
1287
|
+
report_omit=omit,
|
|
1288
|
+
report_include=include,
|
|
1289
|
+
xml_output=outfile,
|
|
1290
|
+
report_contexts=contexts,
|
|
1291
|
+
skip_empty=skip_empty,
|
|
1292
|
+
):
|
|
1293
|
+
return render_report(self.config.xml_output, XmlReporter(self), morfs, self._message)
|
|
1294
|
+
|
|
1295
|
+
def json_report(
|
|
1296
|
+
self,
|
|
1297
|
+
morfs: TMorfs = None,
|
|
1298
|
+
outfile: str | None = None,
|
|
1299
|
+
ignore_errors: bool | None = None,
|
|
1300
|
+
omit: str | list[str] | None = None,
|
|
1301
|
+
include: str | list[str] | None = None,
|
|
1302
|
+
contexts: list[str] | None = None,
|
|
1303
|
+
pretty_print: bool | None = None,
|
|
1304
|
+
show_contexts: bool | None = None,
|
|
1305
|
+
) -> float:
|
|
1306
|
+
"""Generate a JSON report of coverage results.
|
|
1307
|
+
|
|
1308
|
+
Each module in `morfs` is included in the report. `outfile` is the
|
|
1309
|
+
path to write the file to, "-" will write to stdout.
|
|
1310
|
+
|
|
1311
|
+
`pretty_print` is a boolean, whether to pretty-print the JSON output or not.
|
|
1312
|
+
|
|
1313
|
+
See :meth:`report` for other arguments.
|
|
1314
|
+
|
|
1315
|
+
Returns a float, the total percentage covered.
|
|
1316
|
+
|
|
1317
|
+
.. versionadded:: 5.0
|
|
1318
|
+
|
|
1319
|
+
"""
|
|
1320
|
+
self._prepare_data_for_reporting()
|
|
1321
|
+
with override_config(
|
|
1322
|
+
self,
|
|
1323
|
+
ignore_errors=ignore_errors,
|
|
1324
|
+
report_omit=omit,
|
|
1325
|
+
report_include=include,
|
|
1326
|
+
json_output=outfile,
|
|
1327
|
+
report_contexts=contexts,
|
|
1328
|
+
json_pretty_print=pretty_print,
|
|
1329
|
+
json_show_contexts=show_contexts,
|
|
1330
|
+
):
|
|
1331
|
+
return render_report(self.config.json_output, JsonReporter(self), morfs, self._message)
|
|
1332
|
+
|
|
1333
|
+
def lcov_report(
|
|
1334
|
+
self,
|
|
1335
|
+
morfs: TMorfs = None,
|
|
1336
|
+
outfile: str | None = None,
|
|
1337
|
+
ignore_errors: bool | None = None,
|
|
1338
|
+
omit: str | list[str] | None = None,
|
|
1339
|
+
include: str | list[str] | None = None,
|
|
1340
|
+
contexts: list[str] | None = None,
|
|
1341
|
+
) -> float:
|
|
1342
|
+
"""Generate an LCOV report of coverage results.
|
|
1343
|
+
|
|
1344
|
+
Each module in `morfs` is included in the report. `outfile` is the
|
|
1345
|
+
path to write the file to, "-" will write to stdout.
|
|
1346
|
+
|
|
1347
|
+
See :meth:`report` for other arguments.
|
|
1348
|
+
|
|
1349
|
+
.. versionadded:: 6.3
|
|
1350
|
+
"""
|
|
1351
|
+
self._prepare_data_for_reporting()
|
|
1352
|
+
with override_config(
|
|
1353
|
+
self,
|
|
1354
|
+
ignore_errors=ignore_errors,
|
|
1355
|
+
report_omit=omit,
|
|
1356
|
+
report_include=include,
|
|
1357
|
+
lcov_output=outfile,
|
|
1358
|
+
report_contexts=contexts,
|
|
1359
|
+
):
|
|
1360
|
+
return render_report(self.config.lcov_output, LcovReporter(self), morfs, self._message)
|
|
1361
|
+
|
|
1362
|
+
def sys_info(self) -> Iterable[tuple[str, Any]]:
|
|
1363
|
+
"""Return a list of (key, value) pairs showing internal information."""
|
|
1364
|
+
|
|
1365
|
+
import glob
|
|
1366
|
+
import platform
|
|
1367
|
+
import site
|
|
1368
|
+
import coverage as covmod
|
|
1369
|
+
|
|
1370
|
+
self._init()
|
|
1371
|
+
self._post_init()
|
|
1372
|
+
|
|
1373
|
+
def plugin_info(plugins: list[Any]) -> list[str]:
|
|
1374
|
+
"""Make an entry for the sys_info from a list of plug-ins."""
|
|
1375
|
+
entries = []
|
|
1376
|
+
for plugin in plugins:
|
|
1377
|
+
entry = plugin._coverage_plugin_name
|
|
1378
|
+
if not plugin._coverage_enabled:
|
|
1379
|
+
entry += " (disabled)"
|
|
1380
|
+
entries.append(entry)
|
|
1381
|
+
return entries
|
|
1382
|
+
|
|
1383
|
+
pth_files = []
|
|
1384
|
+
for spdir in site.getsitepackages():
|
|
1385
|
+
pth_files.extend(glob.glob(f"{spdir}/*cov*.pth"))
|
|
1386
|
+
|
|
1387
|
+
info = [
|
|
1388
|
+
("coverage_version", covmod.__version__),
|
|
1389
|
+
("coverage_module", covmod.__file__),
|
|
1390
|
+
("core", self._collector.tracer_name() if self._collector is not None else "-none-"),
|
|
1391
|
+
("CTracer", f"available from {CTRACER_FILE}" if CTRACER_FILE else "unavailable"),
|
|
1392
|
+
("plugins.file_tracers", plugin_info(self._plugins.file_tracers)),
|
|
1393
|
+
("plugins.configurers", plugin_info(self._plugins.configurers)),
|
|
1394
|
+
("plugins.context_switchers", plugin_info(self._plugins.context_switchers)),
|
|
1395
|
+
("configs_attempted", self.config.config_files_attempted),
|
|
1396
|
+
("configs_read", self.config.config_files_read),
|
|
1397
|
+
("config_file", self.config.config_file),
|
|
1398
|
+
(
|
|
1399
|
+
"config_contents",
|
|
1400
|
+
repr(self.config._config_contents) if self.config._config_contents else "-none-",
|
|
1401
|
+
),
|
|
1402
|
+
("data_file", self._data.data_filename() if self._data is not None else "-none-"),
|
|
1403
|
+
("python", sys.version.replace("\n", "")),
|
|
1404
|
+
("platform", platform.platform()),
|
|
1405
|
+
("implementation", platform.python_implementation()),
|
|
1406
|
+
("build", platform.python_build()),
|
|
1407
|
+
("gil_enabled", getattr(sys, "_is_gil_enabled", lambda: True)()),
|
|
1408
|
+
("executable", sys.executable),
|
|
1409
|
+
("pth_files", pth_files),
|
|
1410
|
+
("def_encoding", sys.getdefaultencoding()),
|
|
1411
|
+
("fs_encoding", sys.getfilesystemencoding()),
|
|
1412
|
+
("pid", os.getpid()),
|
|
1413
|
+
("cwd", os.getcwd()),
|
|
1414
|
+
("path", sys.path),
|
|
1415
|
+
("environment", [f"{k} = {v}" for k, v in relevant_environment_display(os.environ)]),
|
|
1416
|
+
("command_line", " ".join(getattr(sys, "argv", ["-none-"]))),
|
|
1417
|
+
("time", f"{datetime.datetime.now():%Y-%m-%d %H:%M:%S}"),
|
|
1418
|
+
]
|
|
1419
|
+
|
|
1420
|
+
if self._inorout is not None:
|
|
1421
|
+
info.extend(self._inorout.sys_info())
|
|
1422
|
+
|
|
1423
|
+
return info
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
# Mega debugging...
|
|
1427
|
+
# $set_env.py: COVERAGE_DEBUG_CALLS - Lots and lots of output about calls to Coverage.
|
|
1428
|
+
if int(os.getenv("COVERAGE_DEBUG_CALLS", 0)): # pragma: debugging
|
|
1429
|
+
from coverage.debug import decorate_methods, show_calls
|
|
1430
|
+
|
|
1431
|
+
Coverage = decorate_methods( # type: ignore[misc]
|
|
1432
|
+
show_calls(show_args=True),
|
|
1433
|
+
butnot=["get_data"],
|
|
1434
|
+
)(Coverage)
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
def process_startup(
|
|
1438
|
+
*,
|
|
1439
|
+
force: bool = False,
|
|
1440
|
+
slug: str = "default", # pylint: disable=unused-argument
|
|
1441
|
+
) -> Coverage | None:
|
|
1442
|
+
"""Call this at Python start-up to perhaps measure coverage.
|
|
1443
|
+
|
|
1444
|
+
Coverage is started if one of these environment variables is defined:
|
|
1445
|
+
|
|
1446
|
+
- COVERAGE_PROCESS_START: the config file to use.
|
|
1447
|
+
- COVERAGE_PROCESS_CONFIG: the config data to use, a string produced by
|
|
1448
|
+
CoverageConfig.serialize, prefixed by ":data:".
|
|
1449
|
+
|
|
1450
|
+
If one of these is defined, it's used to get the coverage configuration,
|
|
1451
|
+
and coverage is started.
|
|
1452
|
+
|
|
1453
|
+
For details, see https://coverage.readthedocs.io/en/latest/subprocess.html.
|
|
1454
|
+
|
|
1455
|
+
Returns the :class:`Coverage` instance that was started, or None if it was
|
|
1456
|
+
not started by this call.
|
|
1457
|
+
|
|
1458
|
+
"""
|
|
1459
|
+
# This function can be called more than once in a process, for a few
|
|
1460
|
+
# reasons.
|
|
1461
|
+
#
|
|
1462
|
+
# 1) We install a .pth file in multiple places reported by the site module,
|
|
1463
|
+
# so this function can be called more than once even in simple
|
|
1464
|
+
# situations.
|
|
1465
|
+
#
|
|
1466
|
+
# 2) In some virtualenv configurations the same directory is visible twice
|
|
1467
|
+
# in sys.path. This means that the .pth file will be found twice and
|
|
1468
|
+
# executed twice, executing this function twice.
|
|
1469
|
+
# https://github.com/coveragepy/coveragepy/issues/340 has more details.
|
|
1470
|
+
#
|
|
1471
|
+
# We set a global flag (an attribute on this function) to indicate that
|
|
1472
|
+
# coverage.py has already been started, so we can avoid starting it twice.
|
|
1473
|
+
|
|
1474
|
+
if not force and hasattr(process_startup, "coverage"):
|
|
1475
|
+
# We've annotated this function before, so we must have already
|
|
1476
|
+
# auto-started coverage.py in this process. Nothing to do.
|
|
1477
|
+
return None
|
|
1478
|
+
|
|
1479
|
+
# Now check for the environment variables that request coverage. If they
|
|
1480
|
+
# aren't set, do nothing.
|
|
1481
|
+
|
|
1482
|
+
config_data = os.getenv("COVERAGE_PROCESS_CONFIG")
|
|
1483
|
+
cps = os.getenv("COVERAGE_PROCESS_START")
|
|
1484
|
+
if config_data is not None:
|
|
1485
|
+
config_file = CONFIG_DATA_PREFIX + config_data
|
|
1486
|
+
elif cps is not None:
|
|
1487
|
+
config_file = cps
|
|
1488
|
+
else:
|
|
1489
|
+
# No request for coverage, nothing to do.
|
|
1490
|
+
return None
|
|
1491
|
+
|
|
1492
|
+
cov = Coverage(config_file=config_file)
|
|
1493
|
+
process_startup.coverage = cov # type: ignore[attr-defined]
|
|
1494
|
+
cov._warn_no_data = False
|
|
1495
|
+
cov._warn_unimported_source = False
|
|
1496
|
+
cov._warn_preimported_source = False
|
|
1497
|
+
cov._auto_save = True
|
|
1498
|
+
cov.start()
|
|
1499
|
+
|
|
1500
|
+
return cov
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
def _after_fork_in_child() -> None:
|
|
1504
|
+
"""Used by patch=fork in the child process to restart coverage."""
|
|
1505
|
+
if cov := Coverage.current():
|
|
1506
|
+
cov.stop()
|
|
1507
|
+
process_startup(force=True, slug="fork")
|
|
1508
|
+
|
|
1509
|
+
|
|
1510
|
+
def _prevent_sub_process_measurement() -> None:
|
|
1511
|
+
"""Stop any subprocess auto-measurement from writing data."""
|
|
1512
|
+
auto_created_coverage = getattr(process_startup, "coverage", None)
|
|
1513
|
+
if auto_created_coverage is not None:
|
|
1514
|
+
auto_created_coverage._auto_save = False
|