Skip to content

Commit df8ad83

Browse files
authored
Fix zstd decompression for chunked zstd response (#11623)
1 parent 7a404ed commit df8ad83

File tree

17 files changed

+29
-31
lines changed

17 files changed

+29
-31
lines changed

CHANGES/11623.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Switched to `backports.zstd` for Python <3.14 and fixed zstd decompression for chunked zstd streams -- by :user:`ZhaoMJ`.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Mikhail Burshteyn
270270
Mikhail Kashkin
271271
Mikhail Lukyanchenko
272272
Mikhail Nacharov
273+
Mingjie Zhao
273274
Misha Behersky
274275
Mitchell Ferree
275276
Morgan Delahaye-Prat

aiohttp/compression_utils.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@
2222
HAS_BROTLI = False
2323

2424
try:
25-
from compression.zstd import ( # type: ignore[import-not-found] # noqa: I900
26-
ZstdDecompressor,
27-
)
25+
if sys.version_info >= (3, 14):
26+
from compression.zstd import ZstdDecompressor # noqa: I900
27+
else: # TODO(PY314): Remove mentions of backports.zstd across codebase
28+
from backports.zstd import ZstdDecompressor
2829

2930
HAS_ZSTD = True
3031
except ImportError:
31-
try:
32-
from zstandard import ZstdDecompressor
33-
34-
HAS_ZSTD = True
35-
except ImportError:
36-
HAS_ZSTD = False
32+
HAS_ZSTD = False
3733

3834

3935
MAX_SYNC_CHUNK_SIZE = 1024
@@ -298,12 +294,12 @@ def __init__(self) -> None:
298294
if not HAS_ZSTD:
299295
raise RuntimeError(
300296
"The zstd decompression is not available. "
301-
"Please install `zstandard` module"
297+
"Please install `backports.zstd` module"
302298
)
303299
self._obj = ZstdDecompressor()
304300

305301
def decompress_sync(self, data: bytes) -> bytes:
306-
return self._obj.decompress(data) # type: ignore[no-any-return]
302+
return self._obj.decompress(data)
307303

308304
def flush(self) -> bytes:
309305
return b""

aiohttp/http_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def __init__(self, out: StreamReader, encoding: str | None) -> None:
938938
if not HAS_ZSTD:
939939
raise ContentEncodingError(
940940
"Can not decode content-encoding: zstandard (zstd). "
941-
"Please install `zstandard`"
941+
"Please install `backports.zstd`"
942942
)
943943
self.decompressor = ZSTDDecompressor()
944944
else:

docs/client_quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ just install `Brotli <https://pypi.org/project/Brotli/>`_
191191
or `brotlicffi <https://pypi.org/project/brotlicffi/>`_.
192192

193193
You can enable ``zstd`` transfer-encodings support,
194-
install `zstandard <https://pypi.org/project/zstandard/>`_.
194+
install `backports.zstd <https://pypi.org/project/backports.zstd/>`_.
195195
If you are using Python >= 3.14, no dependency should be required.
196196

197197
JSON Request

requirements/base-ft.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ typing-extensions==4.15.0
4444
# multidict
4545
yarl==1.21.0
4646
# via -r requirements/runtime-deps.in
47-
zstandard==0.25.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
47+
backports.zstd==0.5.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
4848
# via -r requirements/runtime-deps.in

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ uvloop==0.21.0 ; platform_system != "Windows" and implementation_name == "cpytho
4646
# via -r requirements/base.in
4747
yarl==1.21.0
4848
# via -r requirements/runtime-deps.in
49-
zstandard==0.25.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
49+
backports.zstd==0.5.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
5050
# via -r requirements/runtime-deps.in

requirements/constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ zlib-ng==1.0.0
299299
# via
300300
# -r requirements/lint.in
301301
# -r requirements/test-common.in
302-
zstandard==0.25.0 ; implementation_name == "cpython"
302+
backports.zstd==0.5.0 ; implementation_name == "cpython"
303303
# via
304304
# -r requirements/lint.in
305305
# -r requirements/runtime-deps.in

requirements/dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ zlib-ng==1.0.0
290290
# via
291291
# -r requirements/lint.in
292292
# -r requirements/test-common.in
293-
zstandard==0.25.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
293+
backports.zstd==0.5.0 ; platform_python_implementation == "CPython" and python_version < "3.14"
294294
# via
295295
# -r requirements/lint.in
296296
# -r requirements/runtime-deps.in

requirements/lint.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
aiodns
2+
backports.zstd; implementation_name == "cpython"
23
blockbuster
34
freezegun
45
isal
@@ -14,4 +15,3 @@ trustme
1415
uvloop; platform_system != "Windows"
1516
valkey
1617
zlib_ng
17-
zstandard; implementation_name == "cpython"

0 commit comments

Comments
 (0)