Skip to content

Commit 657bca7

Browse files
committed
Two minor improvements
Hopefully these help performance.
1 parent 13e88fb commit 657bca7

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎src/quart/globals.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from contextvars import ContextVar
4+
from typing import cast
45
from typing import TYPE_CHECKING
56

67
from werkzeug.local import LocalProxy
@@ -47,13 +48,14 @@
4748

4849

4950
def _session_lookup() -> RequestContext | WebsocketContext:
50-
try:
51-
return _cv_request.get()
52-
except LookupError:
53-
try:
54-
return _cv_websocket.get()
55-
except LookupError:
56-
raise RuntimeError("Not within a request nor websocket context") from None
51+
missing = object()
52+
ctx = _cv_request.get(missing)
53+
if ctx is not missing:
54+
return cast("RequestContext", ctx)
55+
ctx = _cv_websocket.get(missing)
56+
if ctx is not missing:
57+
return cast("WebsocketContext", ctx)
58+
raise RuntimeError("Not within a request nor websocket context")
5759

5860

5961
session: SessionMixin = LocalProxy(_session_lookup, "session") # type: ignore[assignment]

‎src/quart/wrappers/response.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,11 @@ async def get_data(self, as_text: bool = False) -> str | bytes:
321321
"""Return the body data."""
322322
if self.implicit_sequence_conversion:
323323
await self.make_sequence()
324-
result = "" if as_text else b""
324+
chunks = []
325325
async with self.response as body:
326326
async for data in body:
327-
if as_text:
328-
result += data.decode()
329-
else:
330-
result += data
331-
return result
327+
chunks.append(data.decode() if as_text else data)
328+
return ("" if as_text else b"").join(chunks)
332329

333330
def set_data(self, data: str | bytes) -> None:
334331
"""Set the response data.

0 commit comments

Comments
 (0)