Skip to content

Commit 2134e57

Browse files
committed
fix: use getsitepackages for writing .pth files. #2006
1 parent a4300a7 commit 2134e57

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

CHANGES.rst

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

26-
Nothing yet.
26+
- Fix: changed where .pth files are written for ``patch = subprocess``, closing
27+
`issue 2006`_.
28+
29+
.. _issue 2006: https://github.com/nedbat/coveragepy/issues/2006
2730

2831

2932
.. start-releases

coverage/files.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import os.path
1212
import posixpath
1313
import re
14+
import site
1415
import sys
15-
import sysconfig
1616

1717
from pathlib import Path
1818
from typing import Callable
19-
from collections.abc import Iterable, Iterator
19+
from collections.abc import Iterable
2020

2121
from coverage import env
2222
from coverage.exceptions import ConfigError
@@ -220,8 +220,8 @@ def prep_patterns(patterns: Iterable[str]) -> list[str]:
220220

221221
def create_pth_file() -> Path | None:
222222
"""Create .pth file for measuring subprocesses."""
223-
for pth_dir in _possible_pth_dirs(): # pragma: part covered
224-
pth_file = pth_dir / "subcover.pth"
223+
for pth_dir in site.getsitepackages(): # pragma: part covered
224+
pth_file = Path(pth_dir) / "subcover.pth"
225225
try:
226226
pth_file.write_text("import coverage; coverage.process_startup()\n", encoding="utf-8")
227227
except OSError: # pragma: cant happen
@@ -231,20 +231,6 @@ def create_pth_file() -> Path | None:
231231
return None # pragma: cant happen
232232

233233

234-
def _possible_pth_dirs() -> Iterator[Path]:
235-
"""Produce a sequence of directories for trying to write .pth files."""
236-
# First look through sys.path, and if we find a .pth file, then it's a good
237-
# place to put ours.
238-
for pth_dir in map(Path, sys.path): # pragma: part covered
239-
pth_files = list(pth_dir.glob("*.pth"))
240-
if pth_files:
241-
yield pth_dir
242-
243-
# If we're still looking, then try the Python library directory.
244-
# https://github.com/nedbat/coveragepy/issues/339
245-
yield Path(sysconfig.get_path("purelib")) # pragma: cant happen
246-
247-
248234
class TreeMatcher:
249235
"""A matcher for files in a tree.
250236

0 commit comments

Comments
 (0)