From: florimondmanca Date: Tue, 14 Jun 2022 21:21:30 +0000 (+0200) Subject: aclosing on py < 3.10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d192a6b7603d79cfaff9fc106294e1ec0616d49b;p=thirdparty%2Fstarlette.git aclosing on py < 3.10 --- diff --git a/setup.py b/setup.py index 1597ef45..f0c160d9 100644 --- a/setup.py +++ b/setup.py @@ -39,6 +39,7 @@ setup( install_requires=[ "anyio>=3.4.0,<5", "typing_extensions>=3.10.0; python_version < '3.10'", + "async_generator; python < '3.10'", ], extras_require={ "full": [ diff --git a/starlette/_compat.py b/starlette/_compat.py index 11656191..bba8821f 100644 --- a/starlette/_compat.py +++ b/starlette/_compat.py @@ -1,5 +1,10 @@ import hashlib +__all__ = [ + "md5_hexdigest", + "aclosing", +] + # Compat wrapper to always include the `usedforsecurity=...` parameter, # which is only added from Python 3.9 onwards. # We use this flag to indicate that we use `md5` hashes only for non-security @@ -23,7 +28,14 @@ try: data, usedforsecurity=usedforsecurity ).hexdigest() + except TypeError: # pragma: no cover def md5_hexdigest(data: bytes, *, usedforsecurity: bool = True) -> str: return hashlib.md5(data).hexdigest() + + +try: + from contextlib import aclosing +except ImportError: # Python < 3.10 + from async_generator import aclosing # type: ignore diff --git a/starlette/middleware/http.py b/starlette/middleware/http.py index 8284e989..f3c07064 100644 --- a/starlette/middleware/http.py +++ b/starlette/middleware/http.py @@ -1,7 +1,7 @@ -from contextlib import aclosing from functools import partial from typing import AsyncGenerator, Callable, Optional +from .._compat import aclosing from ..datastructures import MutableHeaders from ..responses import Response from ..types import ASGIApp, Message, Receive, Scope, Send