Skip to content

Commit dc3170b

Browse files
Use collections.deque for chunk splits (#11892) (#11912)
(cherry picked from commit 271532e) --------- Co-authored-by: Finder <nakamurajames123@gmail.com>
1 parent 64629a0 commit dc3170b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

aiohttp/streams.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(
149149
self._loop = loop
150150
self._size = 0
151151
self._cursor = 0
152-
self._http_chunk_splits: Optional[List[int]] = None
152+
self._http_chunk_splits: Optional[Deque[int]] = None
153153
self._buffer: Deque[bytes] = collections.deque()
154154
self._buffer_offset = 0
155155
self._eof = False
@@ -303,7 +303,7 @@ def begin_http_chunk_receiving(self) -> None:
303303
raise RuntimeError(
304304
"Called begin_http_chunk_receiving when some data was already fed"
305305
)
306-
self._http_chunk_splits = []
306+
self._http_chunk_splits = collections.deque()
307307

308308
def end_http_chunk_receiving(self) -> None:
309309
if self._http_chunk_splits is None:
@@ -462,7 +462,7 @@ async def readchunk(self) -> Tuple[bytes, bool]:
462462
raise self._exception
463463

464464
while self._http_chunk_splits:
465-
pos = self._http_chunk_splits.pop(0)
465+
pos = self._http_chunk_splits.popleft()
466466
if pos == self._cursor:
467467
return (b"", True)
468468
if pos > self._cursor:
@@ -535,7 +535,7 @@ def _read_nowait_chunk(self, n: int) -> bytes:
535535
chunk_splits = self._http_chunk_splits
536536
# Prevent memory leak: drop useless chunk splits
537537
while chunk_splits and chunk_splits[0] < self._cursor:
538-
chunk_splits.pop(0)
538+
chunk_splits.popleft()
539539

540540
if self._size < self._low_water and self._protocol._reading_paused:
541541
self._protocol.resume_reading()

tests/test_http_parser.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,8 @@ def test_http_request_chunked_payload(parser) -> None:
12541254
parser.feed_data(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n")
12551255

12561256
assert b"dataline" == b"".join(d for d in payload._buffer)
1257-
assert [4, 8] == payload._http_chunk_splits
1257+
assert payload._http_chunk_splits is not None
1258+
assert [4, 8] == list(payload._http_chunk_splits)
12581259
assert payload.is_eof()
12591260

12601261

@@ -1269,7 +1270,8 @@ def test_http_request_chunked_payload_and_next_message(parser) -> None:
12691270
)
12701271

12711272
assert b"dataline" == b"".join(d for d in payload._buffer)
1272-
assert [4, 8] == payload._http_chunk_splits
1273+
assert payload._http_chunk_splits is not None
1274+
assert [4, 8] == list(payload._http_chunk_splits)
12731275
assert payload.is_eof()
12741276

12751277
assert len(messages) == 1
@@ -1293,12 +1295,13 @@ def test_http_request_chunked_payload_chunks(parser) -> None:
12931295
parser.feed_data(b"test: test\r\n")
12941296

12951297
assert b"dataline" == b"".join(d for d in payload._buffer)
1296-
assert [4, 8] == payload._http_chunk_splits
1298+
assert payload._http_chunk_splits is not None
1299+
assert [4, 8] == list(payload._http_chunk_splits)
12971300
assert not payload.is_eof()
12981301

12991302
parser.feed_data(b"\r\n")
13001303
assert b"dataline" == b"".join(d for d in payload._buffer)
1301-
assert [4, 8] == payload._http_chunk_splits
1304+
assert [4, 8] == list(payload._http_chunk_splits)
13021305
assert payload.is_eof()
13031306

13041307

@@ -1309,7 +1312,8 @@ def test_parse_chunked_payload_chunk_extension(parser) -> None:
13091312
parser.feed_data(b"4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest: test\r\n\r\n")
13101313

13111314
assert b"dataline" == b"".join(d for d in payload._buffer)
1312-
assert [4, 8] == payload._http_chunk_splits
1315+
assert payload._http_chunk_splits is not None
1316+
assert [4, 8] == list(payload._http_chunk_splits)
13131317
assert payload.is_eof()
13141318

13151319

0 commit comments

Comments
 (0)