11
11
import os .path
12
12
import posixpath
13
13
import re
14
+ import site
14
15
import sys
15
- import sysconfig
16
16
17
17
from pathlib import Path
18
18
from typing import Callable
19
- from collections .abc import Iterable , Iterator
19
+ from collections .abc import Iterable
20
20
21
21
from coverage import env
22
22
from coverage .exceptions import ConfigError
@@ -220,8 +220,8 @@ def prep_patterns(patterns: Iterable[str]) -> list[str]:
220
220
221
221
def create_pth_file () -> Path | None :
222
222
"""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"
225
225
try :
226
226
pth_file .write_text ("import coverage; coverage.process_startup()\n " , encoding = "utf-8" )
227
227
except OSError : # pragma: cant happen
@@ -231,20 +231,6 @@ def create_pth_file() -> Path | None:
231
231
return None # pragma: cant happen
232
232
233
233
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
-
248
234
class TreeMatcher :
249
235
"""A matcher for files in a tree.
250
236
0 commit comments