Skip to content

Commit 89b39f3

Browse files
committed
fix: no more ad-hoc lib location guessing
1 parent c4ec6e1 commit 89b39f3

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26-
Nothing yet.
26+
- Fix: coverage.py uses a more disciplined approach to detecting where
27+
third-party code is installed, and avoids measuring it. This shouldn't change
28+
any behavior. If you find that it does, please get in touch.
2729

2830

2931
.. start-releases

coverage/inorout.py

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import itertools
1111
import os
1212
import os.path
13-
import platform
14-
import re
1513
import sys
1614
import sysconfig
1715
import traceback
@@ -39,27 +37,6 @@
3937
from coverage.plugin_support import Plugins
4038

4139

42-
modules_we_happen_to_have: list[ModuleType] = [
43-
inspect,
44-
itertools,
45-
os,
46-
platform,
47-
re,
48-
sysconfig,
49-
traceback,
50-
]
51-
52-
if env.PYPY:
53-
# Pypy has some unusual stuff in the "stdlib". Consider those locations
54-
# when deciding where the stdlib is. These modules are not used for anything,
55-
# they are modules importable from the pypy lib directories, so that we can
56-
# find those directories.
57-
import _pypy_irc_topic # pylint: disable=import-error
58-
import _structseq # pylint: disable=import-error
59-
60-
modules_we_happen_to_have.extend([_structseq, _pypy_irc_topic])
61-
62-
6340
os = isolate_module(os)
6441

6542

@@ -145,30 +122,24 @@ def file_and_path_for_module(modulename: str) -> tuple[str | None, list[str]]:
145122
return filename, path
146123

147124

125+
def add_sysconfig_paths(paths: set[str], path_names: list[str]) -> None:
126+
"""Get paths from `sysconfig.get_paths`"""
127+
scheme_names = set(sysconfig.get_scheme_names())
128+
129+
for scheme in scheme_names:
130+
config_paths = sysconfig.get_paths(scheme)
131+
for path_name in path_names:
132+
paths.add(config_paths[path_name])
133+
134+
148135
def add_stdlib_paths(paths: set[str]) -> None:
149136
"""Add paths where the stdlib can be found to the set `paths`."""
150-
# Look at where some standard modules are located. That's the
151-
# indication for "installed with the interpreter". In some
152-
# environments (virtualenv, for example), these modules may be
153-
# spread across a few locations. Look at all the candidate modules
154-
# we've imported, and take all the different ones.
155-
for m in modules_we_happen_to_have:
156-
if hasattr(m, "__file__"):
157-
paths.add(canonical_path(m, directory=True))
137+
add_sysconfig_paths(paths, ["stdlib", "platstdlib"])
158138

159139

160140
def add_third_party_paths(paths: set[str]) -> None:
161141
"""Add locations for third-party packages to the set `paths`."""
162-
# Get the paths that sysconfig knows about.
163-
scheme_names = set(sysconfig.get_scheme_names())
164-
165-
for scheme in scheme_names:
166-
# https://foss.heptapod.net/pypy/pypy/-/issues/3433
167-
better_scheme = "pypy_posix" if scheme == "pypy" else scheme
168-
if os.name in better_scheme.split("_"):
169-
config_paths = sysconfig.get_paths(scheme)
170-
for path_name in ["platlib", "purelib", "scripts"]:
171-
paths.add(config_paths[path_name])
142+
add_sysconfig_paths(paths, ["platlib", "purelib", "scripts"])
172143

173144

174145
def add_coverage_paths(paths: set[str]) -> None:
@@ -234,11 +205,16 @@ def _debug(msg: str) -> None:
234205
if self.debug:
235206
self.debug.write(msg)
236207

237-
# The matchers for should_trace.
238-
239208
# Generally useful information
240209
_debug("sys.path:" + "".join(f"\n {p}" for p in sys.path))
241210

211+
if self.debug:
212+
_debug("sysconfig paths:")
213+
for scheme in sorted(sysconfig.get_scheme_names()):
214+
_debug(f" {scheme}:")
215+
for k, v in sysconfig.get_paths(scheme).items():
216+
_debug(f" {k}: {v}")
217+
242218
# Create the matchers we need for should_trace
243219
self.source_match = None
244220
self.source_pkgs_match = None

0 commit comments

Comments
 (0)