1313# limitations under the License. 
1414from  __future__ import  annotations 
1515
16+ import  sys 
1617import  warnings 
1718from  typing  import  Any , Iterable , Optional , Union 
1819
@@ -44,7 +45,10 @@ def _have_zlib() -> bool:
4445
4546def  _have_zstd () ->  bool :
4647    try :
47-         import  zstandard   # noqa: F401 
48+         if  sys .version_info  >=  (3 , 14 ):
49+             from  compression  import  zstd 
50+         else :
51+             from  backports  import  zstd   # noqa: F401 
4852
4953        return  True 
5054    except  ImportError :
@@ -79,11 +83,18 @@ def validate_compressors(dummy: Any, value: Union[str, Iterable[str]]) -> list[s
7983            )
8084        elif  compressor  ==  "zstd"  and  not  _have_zstd ():
8185            compressors .remove (compressor )
82-             warnings .warn (
83-                 "Wire protocol compression with zstandard is not available. " 
84-                 "You must install the zstandard module for zstandard support." ,
85-                 stacklevel = 2 ,
86-             )
86+             if  sys .version_info  >=  (3 , 14 ):
87+                 warnings .warn (
88+                     "Wire protocol compression with zstandard is not available. " 
89+                     "The compression.zstd module is not available." ,
90+                     stacklevel = 2 ,
91+                 )
92+             else :
93+                 warnings .warn (
94+                     "Wire protocol compression with zstandard is not available. " 
95+                     "You must install the backports.zstd module for zstandard support." ,
96+                     stacklevel = 2 ,
97+                 )
8798    return  compressors 
8899
89100
@@ -144,12 +155,12 @@ class ZstdContext:
144155
145156    @staticmethod  
146157    def  compress (data : bytes ) ->  bytes :
147-         # ZstdCompressor is not thread safe. 
148-         # TODO: Use a pool? 
149- 
150-         import  zstandard 
158+         if   sys . version_info   >=  ( 3 ,  14 ): 
159+              from   compression   import   zstd 
160+          else : 
161+              from   backports   import  zstd 
151162
152-         return  zstandard . ZstdCompressor () .compress (data )
163+         return  zstd .compress (data )
153164
154165
155166def  decompress (data : bytes  |  memoryview , compressor_id : int ) ->  bytes :
@@ -166,10 +177,11 @@ def decompress(data: bytes | memoryview, compressor_id: int) -> bytes:
166177
167178        return  zlib .decompress (data )
168179    elif  compressor_id  ==  ZstdContext .compressor_id :
169-         # ZstdDecompressor is not thread safe. 
170-         # TODO: Use a pool? 
171-         import  zstandard 
180+         if  sys .version_info  >=  (3 , 14 ):
181+             from  compression  import  zstd 
182+         else :
183+             from  backports  import  zstd 
172184
173-         return  zstandard . ZstdDecompressor () .decompress (data )
185+         return  zstd .decompress (data )
174186    else :
175187        raise  ValueError ("Unknown compressorId %d"  %  (compressor_id ,))
0 commit comments