Skip to content

Commit 3084703

Browse files
committed
Broaden receive data buffer types
1 parent 62c5068 commit 3084703

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

h11/_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def trailing_data(self) -> Tuple[bytes, bool]:
361361
"""
362362
return (bytes(self._receive_buffer), self._receive_buffer_closed)
363363

364-
def receive_data(self, data: bytes) -> None:
364+
def receive_data(self, data: Union[bytes, bytearray, memoryview]) -> None:
365365
"""Add data to our internal receive buffer.
366366
367367
This does not actually do any processing on the data, just stores

h11/_receivebuffer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def __init__(self) -> None:
5050
self._next_line_search = 0
5151
self._multiple_lines_search = 0
5252

53-
def __iadd__(self, byteslike: Union[bytes, bytearray]) -> "ReceiveBuffer":
53+
def __iadd__(
54+
self, byteslike: Union[bytes, bytearray, memoryview]
55+
) -> "ReceiveBuffer":
5456
self._data += byteslike
5557
return self
5658

h11/tests/test_connection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ def test_chunk_boundaries() -> None:
238238
assert conn.next_event() == EndOfMessage()
239239

240240

241+
@pytest.mark.parametrize("data_wrapper", [bytearray, memoryview])
242+
def test_receive_data_accepts_byteslike_objects(data_wrapper: Any) -> None:
243+
conn = Connection(our_role=SERVER)
244+
245+
conn.receive_data(
246+
data_wrapper(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
247+
)
248+
249+
assert conn.next_event() == Request(
250+
method="GET",
251+
target="/",
252+
headers=[("Host", "example.com")],
253+
)
254+
assert conn.next_event() == EndOfMessage()
255+
256+
241257
def test_client_talking_to_http10_server() -> None:
242258
c = Connection(CLIENT)
243259
c.send(Request(method="GET", target="/", headers=[("Host", "example.com")]))

newsfragments/186.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow ``Connection.receive_data()`` to accept ``bytearray`` and
2+
``memoryview`` inputs in its public type hints.

0 commit comments

Comments
 (0)