-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathsitecustomize.py
More file actions
186 lines (144 loc) · 6.63 KB
/
sitecustomize.py
File metadata and controls
186 lines (144 loc) · 6.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Provide a minimal FastAPI stub when fastapi is unavailable.
This keeps tests runnable in minimal environments without altering the runtime
behavior when FastAPI is installed.
"""
from __future__ import annotations
import importlib.machinery
import types
import sys
import urllib.parse
from contextlib import contextmanager
from typing import Any, Callable, Dict, Iterable, List, Optional
def _install_fastapi_stub() -> None:
# Avoid importing asyncio at interpreter startup. Some profiling environments
# execute sitecustomize with a restricted stdlib path and only need this stub
# when tests exercise FastAPI endpoints.
try:
import asyncio
except Exception:
# Profiling subprocesses can run with constrained startup paths where
# asyncio is unavailable. In that case, skip the test-only FastAPI stub.
return
def _set_module_spec(name: str, module: types.ModuleType, *, is_package: bool) -> None:
module.__spec__ = importlib.machinery.ModuleSpec(name=name, loader=None, is_package=is_package)
if is_package:
module.__path__ = [] # type: ignore[attr-defined]
class Request:
def __init__(
self,
*,
method: str = "GET",
query_params: Optional[Dict[str, Any]] = None,
json_body: Any = None,
) -> None:
self.method = method
self.query_params = query_params or {}
self._json_body = json_body
async def json(self) -> Any:
if self._json_body is None:
raise ValueError("No JSON body")
return self._json_body
async def is_disconnected(self) -> bool:
return False
class JSONResponse:
def __init__(self, content: Any, status_code: int = 200, **_: Any) -> None:
self.content = content
self.status_code = status_code
class StreamingResponse:
def __init__(self, content: Any, status_code: int = 200, **_: Any) -> None:
self.content = content
self.status_code = status_code
class CORSMiddleware:
def __init__(self, *_: Any, **__: Any) -> None:
return None
class _StubRoute:
def __init__(self, path: str, methods: set[str], endpoint: Callable[..., Any]) -> None:
self.path = path
self.methods = methods
self.endpoint = endpoint
class FastAPI:
def __init__(self, *_: Any, **__: Any) -> None:
self.routes: List[_StubRoute] = []
def add_middleware(self, *_: Any, **__: Any) -> None:
return None
def get(self, path: str):
def decorator(fn: Callable[..., Any]):
self.routes.append(_StubRoute(path, {"GET"}, fn))
return fn
return decorator
def post(self, path: str):
def decorator(fn: Callable[..., Any]):
self.routes.append(_StubRoute(path, {"POST"}, fn))
return fn
return decorator
class _Response:
def __init__(self, status_code: int, payload: Any = None, text_chunks: Optional[List[str]] = None) -> None:
self.status_code = status_code
self._payload = payload
self._text_chunks = text_chunks or []
def json(self) -> Any:
return self._payload
def iter_text(self) -> Iterable[str]:
return iter(self._text_chunks)
class TestClient:
def __init__(self, app: Any) -> None:
self._app = app
def _find_route(self, path: str, method: str) -> Optional[_StubRoute]:
for route in getattr(self._app, "routes", []):
if route.path == path and method in route.methods:
return route
return None
def _request(self, method: str, url: str, *, stream: bool = False) -> _Response:
parsed = urllib.parse.urlparse(url)
path = parsed.path
query_params = dict(urllib.parse.parse_qsl(parsed.query, keep_blank_values=True))
route = self._find_route(path, method)
if route is None:
return _Response(404, payload={"error": "Not Found"})
request = Request(method=method, query_params=query_params)
async def _call_endpoint():
return await route.endpoint(request)
result = asyncio.run(_call_endpoint())
if isinstance(result, StreamingResponse):
async def _consume_async_gen(gen: Any) -> List[str]:
chunks: List[str] = []
async for chunk in gen:
chunks.append(str(chunk))
return chunks
chunks = asyncio.run(_consume_async_gen(result.content))
return _Response(result.status_code, text_chunks=chunks)
if isinstance(result, JSONResponse):
return _Response(result.status_code, payload=result.content)
return _Response(200, payload=result)
def get(self, url: str) -> _Response:
return self._request("GET", url)
@contextmanager
def stream(self, method: str, url: str):
response = self._request(method, url, stream=True)
yield response
fastapi_module = types.ModuleType("fastapi")
fastapi_module.FastAPI = FastAPI
fastapi_module.Request = Request
fastapi_module.__dict__["__all__"] = ["FastAPI", "Request"]
_set_module_spec("fastapi", fastapi_module, is_package=True)
middleware_module = types.ModuleType("fastapi.middleware")
_set_module_spec("fastapi.middleware", middleware_module, is_package=True)
cors_module = types.ModuleType("fastapi.middleware.cors")
cors_module.CORSMiddleware = CORSMiddleware
_set_module_spec("fastapi.middleware.cors", cors_module, is_package=False)
responses_module = types.ModuleType("fastapi.responses")
responses_module.JSONResponse = JSONResponse
responses_module.StreamingResponse = StreamingResponse
_set_module_spec("fastapi.responses", responses_module, is_package=False)
testclient_module = types.ModuleType("fastapi.testclient")
testclient_module.TestClient = TestClient
_set_module_spec("fastapi.testclient", testclient_module, is_package=False)
sys.modules.setdefault("fastapi", fastapi_module)
sys.modules.setdefault("fastapi.middleware", middleware_module)
sys.modules.setdefault("fastapi.middleware.cors", cors_module)
sys.modules.setdefault("fastapi.responses", responses_module)
sys.modules.setdefault("fastapi.testclient", testclient_module)
try:
import fastapi # noqa: F401
except Exception:
_install_fastapi_stub()