|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | 9 | import codecs |
10 | | -import functools |
11 | 10 | import io |
12 | 11 | import sys |
13 | 12 | import typing |
|
31 | 30 | # Zstandard support is optional on Python <= 3.13. |
32 | 31 | # On Python 3.14+, the stdlib includes an optional built-in zstd implementation. |
33 | 32 | if typing.TYPE_CHECKING: |
34 | | - # We keep checking Python version in the type checker path because try..except doesn't help type checkers. |
35 | 33 | if sys.version_info >= (3, 14): |
36 | 34 | from compression.zstd import ZstdDecompressor, ZstdError |
37 | 35 | else: |
38 | | - from zstandard import ZstdDecompressor as _ZstdDecompressor, ZstdError |
| 36 | + from backports.zstd import ZstdDecompressor, ZstdError |
39 | 37 |
|
40 | | - ZstdDecompressor = functools.partial(_ZstdDecompressor().decompressobj) |
41 | | - |
42 | | - _zstandard_installed: bool = False |
| 38 | + _zstandard_installed: bool |
43 | 39 | else: # pragma: no cover |
44 | | - _zstandard_installed = False |
45 | 40 | try: |
46 | | - from compression.zstd import ZstdDecompressor, ZstdError |
| 41 | + if sys.version_info >= (3, 14): |
| 42 | + from compression.zstd import ZstdDecompressor, ZstdError |
| 43 | + else: |
| 44 | + from backports.zstd import ZstdDecompressor, ZstdError |
47 | 45 |
|
48 | 46 | _zstandard_installed = True |
49 | | - # Either Python <3.14 or the distro doesn't have `compression.zstd`. |
50 | 47 | except ImportError: |
51 | | - try: |
52 | | - from zstandard import ZstdDecompressor as _ZstdDecompressor, ZstdError |
53 | | - |
54 | | - ZstdDecompressor = functools.partial(_ZstdDecompressor().decompressobj) |
55 | | - _zstandard_installed = True |
56 | | - except ImportError: |
57 | | - pass |
| 48 | + _zstandard_installed = False |
58 | 49 |
|
59 | 50 |
|
60 | 51 | class ContentDecoder: |
@@ -185,8 +176,7 @@ def flush(self) -> bytes: |
185 | 176 | class ZStandardDecoder(ContentDecoder): |
186 | 177 | """Handle 'zstd' RFC 8878 decoding. |
187 | 178 |
|
188 | | - If running on Python 3.14+ or a distro that doesn't have the `compression.zstd` stdlib module, requires either: |
189 | | - `pip install zstandard` or `pip install httpx2[zstd]`. |
| 179 | + On Python 3.13 and below, this requires `pip install httpx2[zstd]`. |
190 | 180 | """ |
191 | 181 |
|
192 | 182 | # inspired by the ZstdDecoder implementation in urllib3 |
|
0 commit comments