Skip to content

Commit 463becb

Browse files
authored
Use compression.zstd for gateway compression on Python 3.14
1 parent e2b6fa8 commit 463becb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

discord/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@
7171
import typing
7272
import warnings
7373
import logging
74-
import zlib
7574

7675
import yarl
7776

77+
if sys.version_info >= (3, 14):
78+
import compression.zstd
79+
else:
80+
import zlib
81+
7882
try:
7983
import orjson # type: ignore
8084
except ModuleNotFoundError:
@@ -1437,6 +1441,21 @@ def decompress(self, data: bytes, /) -> str | None:
14371441
# Each WS message is a complete gateway message
14381442
return self.context.decompress(data).decode('utf-8')
14391443

1444+
_ActiveDecompressionContext: Type[_DecompressionContext] = _ZstdDecompressionContext
1445+
elif sys.version_info >= (3, 14):
1446+
1447+
class _ZstdDecompressionContext:
1448+
__slots__ = ('context',)
1449+
1450+
COMPRESSION_TYPE: str = 'zstd-stream'
1451+
1452+
def __init__(self) -> None:
1453+
self.context = compression.zstd.ZstdDecompressor()
1454+
1455+
def decompress(self, data: bytes, /) -> str | None:
1456+
# Each WS message is a complete gateway message
1457+
return self.context.decompress(data).decode('utf-8')
1458+
14401459
_ActiveDecompressionContext: Type[_DecompressionContext] = _ZstdDecompressionContext
14411460
else:
14421461

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ speed = [
5858
"aiodns>=1.1; sys_platform != 'win32'",
5959
"Brotli",
6060
"cchardet==2.1.7; python_version < '3.10'",
61-
"zstandard>=0.23.0"
61+
"zstandard>=0.23.0; python_version <= '3.13'"
6262
]
6363
test = [
6464
"coverage[toml]",

0 commit comments

Comments
 (0)