-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecosystem_conftest.py
More file actions
69 lines (51 loc) · 1.84 KB
/
Copy pathecosystem_conftest.py
File metadata and controls
69 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""pytest plugin injected into cloned ecosystem repos.
Primary compat injection happens in check_ecosystem.py by placing a temporary
"httpx" package on PYTHONPATH. This plugin is a minimal safety net that only
loads the shim if real httpx was imported first.
"""
from __future__ import annotations
import importlib.util
import os
import sys
from pathlib import Path
# Locate the compat shim: env var (set by check_ecosystem.py) or sibling file.
_SHIM_PATH = os.environ.get(
"HTTPRS_COMPAT_SHIM",
str(Path(__file__).parent / "httpx_compat.py"),
)
def _patch_httpx() -> None:
"""Patch sys.modules['httpx'] with the httprs compat shim."""
existing = sys.modules.get("httpx")
if existing is not None and getattr(existing, "_httprs_compat", False):
return
shim_path = Path(_SHIM_PATH)
if not shim_path.exists():
import warnings
warnings.warn(
f"httprs compat shim not found at {shim_path}; httpx will not be patched",
stacklevel=1,
)
return
spec = importlib.util.spec_from_file_location("httpx", shim_path)
shim = importlib.util.module_from_spec(spec)
shim.__file__ = str(shim_path)
shim.__package__ = "httpx"
try:
spec.loader.exec_module(shim)
except Exception as exc:
import warnings
warnings.warn(
f"Failed to load httprs compat shim: {exc}",
stacklevel=1,
)
return
sys.modules["httpx"] = shim
def pytest_load_initial_conftests(early_config, parser, args) -> None:
"""Patch as early as possible in pytest startup."""
_patch_httpx()
def pytest_configure(config) -> None:
"""Safety net in case early hooks were bypassed."""
_patch_httpx()
# Import-time patching is intentional as a fallback if any plugin imports
# real httpx before pytest hooks execute.
_patch_httpx()