Skip to content

Commit 8465649

Browse files
committed
inlined stream.close calls to better match falcon style
1 parent 729cc11 commit 8465649

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

falcon/app_helpers.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
from inspect import iscoroutinefunction
2020
from typing import (
21-
Any,
22-
Awaitable,
2321
Callable,
2422
Iterable,
2523
List,
@@ -395,17 +393,6 @@ def __next__(self) -> bytes:
395393
return data
396394

397395
def close(self) -> None:
398-
close_maybe(self._stream)
399-
400-
401-
# TODO(jkmnt): Move these to some other module, they don't belong here
402-
def close_maybe(stream: Any) -> None:
403-
close: Optional[Callable[[], None]] = getattr(stream, 'close', None)
404-
if close:
405-
close()
406-
407-
408-
async def async_close_maybe(stream: Any) -> None:
409-
close: Optional[Callable[[], Awaitable[None]]] = getattr(stream, 'close', None)
410-
if close:
411-
await close()
396+
close: Optional[Callable[[], None]] = getattr(self._stream, 'close', None)
397+
if close:
398+
close()

falcon/asgi/app.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
from falcon._typing import AsgiSinkCallable
5353
from falcon._typing import SinkPrefix
5454
import falcon.app
55-
from falcon.app_helpers import async_close_maybe
5655
from falcon.app_helpers import AsyncPreparedMiddlewareResult
5756
from falcon.app_helpers import AsyncPreparedMiddlewareWsResult
5857
from falcon.app_helpers import prepare_middleware
@@ -773,13 +772,16 @@ async def watch_disconnect() -> None:
773772
# (c) async iterator
774773
#
775774

776-
read_meth: Optional[Callable[[int], Awaitable[bytes]]] = getattr(
775+
read: Optional[Callable[[int], Awaitable[bytes]]] = getattr(
777776
stream, 'read', None
778777
)
779-
if read_meth:
778+
close: Optional[Callable[[], Awaitable[None]]] = getattr(
779+
stream, 'close', None
780+
)
781+
if read:
780782
try:
781783
while True:
782-
data = await read_meth(self._STREAM_BLOCK_SIZE)
784+
data = await read(self._STREAM_BLOCK_SIZE)
783785
if data == b'':
784786
break
785787
else:
@@ -793,7 +795,8 @@ async def watch_disconnect() -> None:
793795
}
794796
)
795797
finally:
796-
await async_close_maybe(stream)
798+
if close:
799+
await close()
797800
else:
798801
# NOTE(kgriffs): Works for both async generators and iterators
799802
try:
@@ -829,10 +832,8 @@ async def watch_disconnect() -> None:
829832
'Response.stream: ' + str(ex)
830833
)
831834
finally:
832-
# NOTE(vytas): This could be DRYed with the above identical
833-
# twoliner in a one large block, but OTOH we would be
834-
# unable to reuse the current try.. except.
835-
await async_close_maybe(stream)
835+
if close:
836+
await close()
836837

837838
await send(_EVT_RESP_EOF)
838839

falcon/testing/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
from falcon._typing import CookieArg
6060
from falcon._typing import HeaderArg
6161
from falcon._typing import ResponseStatus
62-
from falcon.app_helpers import close_maybe
6362
import falcon.asgi
6463
from falcon.asgi_spec import AsgiEvent
6564
from falcon.asgi_spec import EventType
@@ -1410,7 +1409,9 @@ def wrapper() -> Iterator[bytes]:
14101409
for item in iterable:
14111410
yield item
14121411
finally:
1413-
close_maybe(iterable)
1412+
close: Optional[Callable[[], None]] = getattr(iterable, 'close', None)
1413+
if close:
1414+
close()
14141415

14151416
wrapped = wrapper()
14161417
head: Tuple[bytes, ...]

0 commit comments

Comments
 (0)