Skip to content

Commit 46b0474

Browse files
rathannindygreg
authored andcommitted
py: use Buffer instead of ByteString on Python 3.12+
`ByteString` was [deprecated](https://docs.python.org/3.12/whatsnew/3.12.html#deprecated) in Python 3.12 and [removed](https://docs.python.org/3.14/whatsnew/3.14.html#collections-abc) in 3.14. `collections.abc.Buffer` is available since Python 3.12. Fixes #238. Closes #262.
1 parent a83ac9f commit 46b0474

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

docs/news.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Changes
5959
and we didn't run tests on this platform.
6060
* We now use `Py_REFCNT(obj)` instead of accessing `(*obj)->ob_refcnt` directly.
6161
This fixes a nogil / multi-threaded compile error. (#201)
62+
* We now `collections.abs.Buffer` on Python 3.12+ instead of `typing.ByteString`,
63+
as `typing.ByteString` was deprecated and later removed. (#238, #262)
6264

6365
Backwards Compatibility Notes
6466
-----------------------------

zstandard/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
import io
1919
import os
2020
import platform
21-
from typing import ByteString
21+
import sys
22+
23+
if sys.version_info >= (3, 12):
24+
from collections.abc import Buffer
25+
else:
26+
from typing import ByteString as Buffer
2227

2328
# Some Python implementations don't support C extensions. That's why we have
2429
# a CFFI implementation in the first place. The code here import one of our
@@ -174,7 +179,7 @@ def open(
174179
return fh
175180

176181

177-
def compress(data: ByteString, level: int = 3) -> bytes:
182+
def compress(data: Buffer, level: int = 3) -> bytes:
178183
"""Compress source data using the zstd compression format.
179184
180185
This performs one-shot compression using basic/default compression
@@ -192,7 +197,7 @@ def compress(data: ByteString, level: int = 3) -> bytes:
192197
return cctx.compress(data)
193198

194199

195-
def decompress(data: ByteString, max_output_size: int = 0) -> bytes:
200+
def decompress(data: Buffer, max_output_size: int = 0) -> bytes:
196201
"""Decompress a zstd frame into its original data.
197202
198203
This performs one-shot decompression using basic/default compression

0 commit comments

Comments
 (0)