diff --git a/h11/_connection.py b/h11/_connection.py index e37d82a..6949004 100644 --- a/h11/_connection.py +++ b/h11/_connection.py @@ -361,7 +361,7 @@ def trailing_data(self) -> Tuple[bytes, bool]: """ return (bytes(self._receive_buffer), self._receive_buffer_closed) - def receive_data(self, data: bytes) -> None: + def receive_data(self, data: Union[bytes, bytearray, memoryview]) -> None: """Add data to our internal receive buffer. This does not actually do any processing on the data, just stores diff --git a/h11/_receivebuffer.py b/h11/_receivebuffer.py index e5c4e08..7bbb0cb 100644 --- a/h11/_receivebuffer.py +++ b/h11/_receivebuffer.py @@ -50,7 +50,9 @@ def __init__(self) -> None: self._next_line_search = 0 self._multiple_lines_search = 0 - def __iadd__(self, byteslike: Union[bytes, bytearray]) -> "ReceiveBuffer": + def __iadd__( + self, byteslike: Union[bytes, bytearray, memoryview] + ) -> "ReceiveBuffer": self._data += byteslike return self diff --git a/h11/tests/test_connection.py b/h11/tests/test_connection.py index 01260dc..fbf68c0 100644 --- a/h11/tests/test_connection.py +++ b/h11/tests/test_connection.py @@ -238,6 +238,22 @@ def test_chunk_boundaries() -> None: assert conn.next_event() == EndOfMessage() +@pytest.mark.parametrize("data_wrapper", [bytearray, memoryview]) +def test_receive_data_accepts_byteslike_objects(data_wrapper: Any) -> None: + conn = Connection(our_role=SERVER) + + conn.receive_data( + data_wrapper(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") + ) + + assert conn.next_event() == Request( + method="GET", + target="/", + headers=[("Host", "example.com")], + ) + assert conn.next_event() == EndOfMessage() + + def test_client_talking_to_http10_server() -> None: c = Connection(CLIENT) c.send(Request(method="GET", target="/", headers=[("Host", "example.com")])) diff --git a/newsfragments/186.feature.rst b/newsfragments/186.feature.rst new file mode 100644 index 0000000..a3e4750 --- /dev/null +++ b/newsfragments/186.feature.rst @@ -0,0 +1,2 @@ +Allow ``Connection.receive_data()`` to accept ``bytearray`` and +``memoryview`` inputs in its public type hints.