[3.14] gh-132983: Style improvements for compression.zstd (GH-133547) (#134001)

gh-132983: Style improvements for `compression.zstd` (GH-133547)
(cherry picked from commit b44c824856)

Co-authored-by: Emma Smith <emma@emmatyping.dev>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-05-14 14:39:43 +02:00 committed by GitHub
parent 275c8d5dbd
commit a962934106
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 62 additions and 61 deletions

View file

@ -2,28 +2,28 @@
__all__ = ( __all__ = (
# compression.zstd # compression.zstd
"COMPRESSION_LEVEL_DEFAULT", 'COMPRESSION_LEVEL_DEFAULT',
"compress", 'compress',
"CompressionParameter", 'CompressionParameter',
"decompress", 'decompress',
"DecompressionParameter", 'DecompressionParameter',
"finalize_dict", 'finalize_dict',
"get_frame_info", 'get_frame_info',
"Strategy", 'Strategy',
"train_dict", 'train_dict',
# compression.zstd._zstdfile # compression.zstd._zstdfile
"open", 'open',
"ZstdFile", 'ZstdFile',
# _zstd # _zstd
"get_frame_size", 'get_frame_size',
"zstd_version", 'zstd_version',
"zstd_version_info", 'zstd_version_info',
"ZstdCompressor", 'ZstdCompressor',
"ZstdDecompressor", 'ZstdDecompressor',
"ZstdDict", 'ZstdDict',
"ZstdError", 'ZstdError',
) )
import _zstd import _zstd
@ -43,6 +43,7 @@ COMPRESSION_LEVEL_DEFAULT = _zstd.ZSTD_CLEVEL_DEFAULT
class FrameInfo: class FrameInfo:
"""Information about a Zstandard frame.""" """Information about a Zstandard frame."""
__slots__ = 'decompressed_size', 'dictionary_id' __slots__ = 'decompressed_size', 'dictionary_id'
def __init__(self, decompressed_size, dictionary_id): def __init__(self, decompressed_size, dictionary_id):
@ -125,13 +126,13 @@ def finalize_dict(zstd_dict, /, samples, dict_size, level):
chunks = b''.join(samples) chunks = b''.join(samples)
chunk_sizes = tuple(_nbytes(sample) for sample in samples) chunk_sizes = tuple(_nbytes(sample) for sample in samples)
if not chunks: if not chunks:
raise ValueError("The samples are empty content, can't finalize the" raise ValueError("The samples are empty content, can't finalize the "
"dictionary.") "dictionary.")
dict_content = _zstd.finalize_dict(zstd_dict.dict_content, dict_content = _zstd.finalize_dict(zstd_dict.dict_content, chunks,
chunks, chunk_sizes, chunk_sizes, dict_size, level)
dict_size, level)
return ZstdDict(dict_content) return ZstdDict(dict_content)
def compress(data, level=None, options=None, zstd_dict=None): def compress(data, level=None, options=None, zstd_dict=None):
"""Return Zstandard compressed *data* as bytes. """Return Zstandard compressed *data* as bytes.
@ -147,6 +148,7 @@ def compress(data, level=None, options=None, zstd_dict=None):
comp = ZstdCompressor(level=level, options=options, zstd_dict=zstd_dict) comp = ZstdCompressor(level=level, options=options, zstd_dict=zstd_dict)
return comp.compress(data, mode=ZstdCompressor.FLUSH_FRAME) return comp.compress(data, mode=ZstdCompressor.FLUSH_FRAME)
def decompress(data, zstd_dict=None, options=None): def decompress(data, zstd_dict=None, options=None):
"""Decompress one or more frames of Zstandard compressed *data*. """Decompress one or more frames of Zstandard compressed *data*.
@ -162,12 +164,12 @@ def decompress(data, zstd_dict=None, options=None):
decomp = ZstdDecompressor(options=options, zstd_dict=zstd_dict) decomp = ZstdDecompressor(options=options, zstd_dict=zstd_dict)
results.append(decomp.decompress(data)) results.append(decomp.decompress(data))
if not decomp.eof: if not decomp.eof:
raise ZstdError("Compressed data ended before the " raise ZstdError('Compressed data ended before the '
"end-of-stream marker was reached") 'end-of-stream marker was reached')
data = decomp.unused_data data = decomp.unused_data
if not data: if not data:
break break
return b"".join(results) return b''.join(results)
class CompressionParameter(enum.IntEnum): class CompressionParameter(enum.IntEnum):

View file

@ -4,7 +4,7 @@ from _zstd import (ZstdCompressor, ZstdDecompressor, ZstdError,
ZSTD_DStreamOutSize) ZSTD_DStreamOutSize)
from compression._common import _streams from compression._common import _streams
__all__ = ("ZstdFile", "open") __all__ = ('ZstdFile', 'open')
_MODE_CLOSED = 0 _MODE_CLOSED = 0
_MODE_READ = 1 _MODE_READ = 1
@ -31,15 +31,15 @@ class ZstdFile(_streams.BaseStream):
FLUSH_BLOCK = ZstdCompressor.FLUSH_BLOCK FLUSH_BLOCK = ZstdCompressor.FLUSH_BLOCK
FLUSH_FRAME = ZstdCompressor.FLUSH_FRAME FLUSH_FRAME = ZstdCompressor.FLUSH_FRAME
def __init__(self, file, /, mode="r", *, def __init__(self, file, /, mode='r', *,
level=None, options=None, zstd_dict=None): level=None, options=None, zstd_dict=None):
"""Open a Zstandard compressed file in binary mode. """Open a Zstandard compressed file in binary mode.
*file* can be either an file-like object, or a file name to open. *file* can be either an file-like object, or a file name to open.
*mode* can be "r" for reading (default), "w" for (over)writing, "x" for *mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for
creating exclusively, or "a" for appending. These can equivalently be creating exclusively, or 'a' for appending. These can equivalently be
given as "rb", "wb", "xb" and "ab" respectively. given as 'rb', 'wb', 'xb' and 'ab' respectively.
*level* is an optional int specifying the compression level to use, *level* is an optional int specifying the compression level to use,
or COMPRESSION_LEVEL_DEFAULT if not given. or COMPRESSION_LEVEL_DEFAULT if not given.
@ -57,33 +57,33 @@ class ZstdFile(_streams.BaseStream):
self._buffer = None self._buffer = None
if not isinstance(mode, str): if not isinstance(mode, str):
raise ValueError("mode must be a str") raise ValueError('mode must be a str')
if options is not None and not isinstance(options, dict): if options is not None and not isinstance(options, dict):
raise TypeError("options must be a dict or None") raise TypeError('options must be a dict or None')
mode = mode.removesuffix("b") # handle rb, wb, xb, ab mode = mode.removesuffix('b') # handle rb, wb, xb, ab
if mode == "r": if mode == 'r':
if level is not None: if level is not None:
raise TypeError("level is illegal in read mode") raise TypeError('level is illegal in read mode')
self._mode = _MODE_READ self._mode = _MODE_READ
elif mode in {"w", "a", "x"}: elif mode in {'w', 'a', 'x'}:
if level is not None and not isinstance(level, int): if level is not None and not isinstance(level, int):
raise TypeError("level must be int or None") raise TypeError('level must be int or None')
self._mode = _MODE_WRITE self._mode = _MODE_WRITE
self._compressor = ZstdCompressor(level=level, options=options, self._compressor = ZstdCompressor(level=level, options=options,
zstd_dict=zstd_dict) zstd_dict=zstd_dict)
self._pos = 0 self._pos = 0
else: else:
raise ValueError(f"Invalid mode: {mode!r}") raise ValueError(f'Invalid mode: {mode!r}')
if isinstance(file, (str, bytes, PathLike)): if isinstance(file, (str, bytes, PathLike)):
self._fp = io.open(file, f'{mode}b') self._fp = io.open(file, f'{mode}b')
self._close_fp = True self._close_fp = True
elif ((mode == 'r' and hasattr(file, "read")) elif ((mode == 'r' and hasattr(file, 'read'))
or (mode != 'r' and hasattr(file, "write"))): or (mode != 'r' and hasattr(file, 'write'))):
self._fp = file self._fp = file
else: else:
raise TypeError("file must be a file-like object " raise TypeError('file must be a file-like object '
"or a str, bytes, or PathLike object") 'or a str, bytes, or PathLike object')
if self._mode == _MODE_READ: if self._mode == _MODE_READ:
raw = _streams.DecompressReader( raw = _streams.DecompressReader(
@ -151,22 +151,22 @@ class ZstdFile(_streams.BaseStream):
return return
self._check_not_closed() self._check_not_closed()
if mode not in {self.FLUSH_BLOCK, self.FLUSH_FRAME}: if mode not in {self.FLUSH_BLOCK, self.FLUSH_FRAME}:
raise ValueError("Invalid mode argument, expected either " raise ValueError('Invalid mode argument, expected either '
"ZstdFile.FLUSH_FRAME or " 'ZstdFile.FLUSH_FRAME or '
"ZstdFile.FLUSH_BLOCK") 'ZstdFile.FLUSH_BLOCK')
if self._compressor.last_mode == mode: if self._compressor.last_mode == mode:
return return
# Flush zstd block/frame, and write. # Flush zstd block/frame, and write.
data = self._compressor.flush(mode) data = self._compressor.flush(mode)
self._fp.write(data) self._fp.write(data)
if hasattr(self._fp, "flush"): if hasattr(self._fp, 'flush'):
self._fp.flush() self._fp.flush()
def read(self, size=-1): def read(self, size=-1):
"""Read up to size uncompressed bytes from the file. """Read up to size uncompressed bytes from the file.
If size is negative or omitted, read until EOF is reached. If size is negative or omitted, read until EOF is reached.
Returns b"" if the file is already at EOF. Returns b'' if the file is already at EOF.
""" """
if size is None: if size is None:
size = -1 size = -1
@ -178,7 +178,7 @@ class ZstdFile(_streams.BaseStream):
making multiple reads from the underlying stream. Reads up to a making multiple reads from the underlying stream. Reads up to a
buffer's worth of data if size is negative. buffer's worth of data if size is negative.
Returns b"" if the file is at EOF. Returns b'' if the file is at EOF.
""" """
self._check_can_read() self._check_can_read()
if size < 0: if size < 0:
@ -293,7 +293,7 @@ class ZstdFile(_streams.BaseStream):
return self._mode == _MODE_WRITE return self._mode == _MODE_WRITE
def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None, def open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None,
encoding=None, errors=None, newline=None): encoding=None, errors=None, newline=None):
"""Open a Zstandard compressed file in binary or text mode. """Open a Zstandard compressed file in binary or text mode.
@ -301,8 +301,8 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
in which case the named file is opened, or it can be an existing file object in which case the named file is opened, or it can be an existing file object
to read from or write to. to read from or write to.
The mode parameter can be "r", "rb" (default), "w", "wb", "x", "xb", "a", The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a',
"ab" for binary mode, or "rt", "wt", "xt", "at" for text mode. 'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode.
The level, options, and zstd_dict parameters specify the settings the same The level, options, and zstd_dict parameters specify the settings the same
as ZstdFile. as ZstdFile.
@ -323,19 +323,19 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
behavior, and line ending(s). behavior, and line ending(s).
""" """
text_mode = "t" in mode text_mode = 't' in mode
mode = mode.replace("t", "") mode = mode.replace('t', '')
if text_mode: if text_mode:
if "b" in mode: if 'b' in mode:
raise ValueError(f"Invalid mode: {mode!r}") raise ValueError(f'Invalid mode: {mode!r}')
else: else:
if encoding is not None: if encoding is not None:
raise ValueError("Argument 'encoding' not supported in binary mode") raise ValueError('Argument "encoding" not supported in binary mode')
if errors is not None: if errors is not None:
raise ValueError("Argument 'errors' not supported in binary mode") raise ValueError('Argument "errors" not supported in binary mode')
if newline is not None: if newline is not None:
raise ValueError("Argument 'newline' not supported in binary mode") raise ValueError('Argument "newline" not supported in binary mode')
binary_file = ZstdFile(file, mode, level=level, options=options, binary_file = ZstdFile(file, mode, level=level, options=options,
zstd_dict=zstd_dict) zstd_dict=zstd_dict)

View file

@ -2065,7 +2065,7 @@ class TarFile(object):
"gz": "gzopen", # gzip compressed tar "gz": "gzopen", # gzip compressed tar
"bz2": "bz2open", # bzip2 compressed tar "bz2": "bz2open", # bzip2 compressed tar
"xz": "xzopen", # lzma compressed tar "xz": "xzopen", # lzma compressed tar
"zst": "zstopen" # zstd compressed tar "zst": "zstopen", # zstd compressed tar
} }
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------

View file

@ -2512,8 +2512,7 @@ maninstall: altmaninstall
XMLLIBSUBDIRS= xml xml/dom xml/etree xml/parsers xml/sax XMLLIBSUBDIRS= xml xml/dom xml/etree xml/parsers xml/sax
LIBSUBDIRS= asyncio \ LIBSUBDIRS= asyncio \
collections \ collections \
compression compression/bz2 compression/gzip compression/zstd \ compression compression/_common compression/zstd \
compression/lzma compression/zlib compression/_common \
concurrent concurrent/futures \ concurrent concurrent/futures \
csv \ csv \
ctypes ctypes/macholib \ ctypes ctypes/macholib \