2023-10-30 12:36:22 +00:00
|
|
|
# Copyright (c) 2017 PySecretHandshake contributors (see AUTHORS for more details)
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
|
|
"""Helper utilities for testing"""
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
from asyncio import StreamReader, StreamWriter
|
2023-10-30 12:36:22 +00:00
|
|
|
from io import BytesIO
|
2023-10-30 04:47:28 +00:00
|
|
|
from typing import AsyncIterable, List, Optional, TypeVar
|
2023-10-30 12:36:22 +00:00
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
T = TypeVar("T")
|
2023-10-30 12:36:22 +00:00
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
|
|
|
|
class AsyncBuffer(BytesIO, StreamReader, StreamWriter): # type: ignore[misc]
|
2023-10-30 12:36:22 +00:00
|
|
|
"""Just a BytesIO with an async read method."""
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
async def read( # type: ignore[override] # pylint: disable=invalid-overridden-method
|
|
|
|
self, n: Optional[int] = None
|
|
|
|
) -> Optional[bytes]:
|
2023-10-29 08:55:39 +00:00
|
|
|
v = super().read(n)
|
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
return v
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
readexactly = read # type: ignore[assignment]
|
2023-10-30 12:36:22 +00:00
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
def append(self, data: bytes) -> None:
|
2023-10-30 12:36:22 +00:00
|
|
|
"""Append data to the buffer without changing the current position."""
|
2023-10-30 04:47:28 +00:00
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
pos = self.tell()
|
|
|
|
self.write(data)
|
|
|
|
self.seek(pos)
|
|
|
|
|
|
|
|
|
2023-10-30 04:47:28 +00:00
|
|
|
async def async_comprehend(generator: AsyncIterable[T]) -> List[T]:
|
2023-10-30 12:36:22 +00:00
|
|
|
"""Emulate ``[elem async for elem in generator]``."""
|
2023-10-29 09:47:21 +00:00
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
results = []
|
2023-10-29 09:47:21 +00:00
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
async for msg in generator:
|
|
|
|
results.append(msg)
|
2023-10-29 09:47:21 +00:00
|
|
|
|
2023-10-30 12:36:22 +00:00
|
|
|
return results
|