Skip to content

Commit cfbea08

Browse files
emmatypingAA-Turner
authored andcommitted
pythongh-132983: Style improvements for compression.zstd (pythonGH-133547)
(cherry picked from commit b44c824) Co-authored-by: Emma Smith <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 275c8d5 commit cfbea08

File tree

8 files changed

+62
-61
lines changed

8 files changed

+62
-61
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Lib/compression/zstd/__init__.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
__all__ = (
44
# compression.zstd
5-
"COMPRESSION_LEVEL_DEFAULT",
6-
"compress",
7-
"CompressionParameter",
8-
"decompress",
9-
"DecompressionParameter",
10-
"finalize_dict",
11-
"get_frame_info",
12-
"Strategy",
13-
"train_dict",
5+
'COMPRESSION_LEVEL_DEFAULT',
6+
'compress',
7+
'CompressionParameter',
8+
'decompress',
9+
'DecompressionParameter',
10+
'finalize_dict',
11+
'get_frame_info',
12+
'Strategy',
13+
'train_dict',
1414

1515
# compression.zstd._zstdfile
16-
"open",
17-
"ZstdFile",
16+
'open',
17+
'ZstdFile',
1818

1919
# _zstd
20-
"get_frame_size",
21-
"zstd_version",
22-
"zstd_version_info",
23-
"ZstdCompressor",
24-
"ZstdDecompressor",
25-
"ZstdDict",
26-
"ZstdError",
20+
'get_frame_size',
21+
'zstd_version',
22+
'zstd_version_info',
23+
'ZstdCompressor',
24+
'ZstdDecompressor',
25+
'ZstdDict',
26+
'ZstdError',
2727
)
2828

2929
import _zstd
@@ -43,6 +43,7 @@
4343

4444
class FrameInfo:
4545
"""Information about a Zstandard frame."""
46+
4647
__slots__ = 'decompressed_size', 'dictionary_id'
4748

4849
def __init__(self, decompressed_size, dictionary_id):
@@ -125,13 +126,13 @@ def finalize_dict(zstd_dict, /, samples, dict_size, level):
125126
chunks = b''.join(samples)
126127
chunk_sizes = tuple(_nbytes(sample) for sample in samples)
127128
if not chunks:
128-
raise ValueError("The samples are empty content, can't finalize the"
129+
raise ValueError("The samples are empty content, can't finalize the "
129130
"dictionary.")
130-
dict_content = _zstd.finalize_dict(zstd_dict.dict_content,
131-
chunks, chunk_sizes,
132-
dict_size, level)
131+
dict_content = _zstd.finalize_dict(zstd_dict.dict_content, chunks,
132+
chunk_sizes, dict_size, level)
133133
return ZstdDict(dict_content)
134134

135+
135136
def compress(data, level=None, options=None, zstd_dict=None):
136137
"""Return Zstandard compressed *data* as bytes.
137138
@@ -147,6 +148,7 @@ def compress(data, level=None, options=None, zstd_dict=None):
147148
comp = ZstdCompressor(level=level, options=options, zstd_dict=zstd_dict)
148149
return comp.compress(data, mode=ZstdCompressor.FLUSH_FRAME)
149150

151+
150152
def decompress(data, zstd_dict=None, options=None):
151153
"""Decompress one or more frames of Zstandard compressed *data*.
152154
@@ -162,12 +164,12 @@ def decompress(data, zstd_dict=None, options=None):
162164
decomp = ZstdDecompressor(options=options, zstd_dict=zstd_dict)
163165
results.append(decomp.decompress(data))
164166
if not decomp.eof:
165-
raise ZstdError("Compressed data ended before the "
166-
"end-of-stream marker was reached")
167+
raise ZstdError('Compressed data ended before the '
168+
'end-of-stream marker was reached')
167169
data = decomp.unused_data
168170
if not data:
169171
break
170-
return b"".join(results)
172+
return b''.join(results)
171173

172174

173175
class CompressionParameter(enum.IntEnum):

Lib/compression/zstd/_zstdfile.py

+33-33
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ZSTD_DStreamOutSize)
55
from compression._common import _streams
66

7-
__all__ = ("ZstdFile", "open")
7+
__all__ = ('ZstdFile', 'open')
88

99
_MODE_CLOSED = 0
1010
_MODE_READ = 1
@@ -31,15 +31,15 @@ class ZstdFile(_streams.BaseStream):
3131
FLUSH_BLOCK = ZstdCompressor.FLUSH_BLOCK
3232
FLUSH_FRAME = ZstdCompressor.FLUSH_FRAME
3333

34-
def __init__(self, file, /, mode="r", *,
34+
def __init__(self, file, /, mode='r', *,
3535
level=None, options=None, zstd_dict=None):
3636
"""Open a Zstandard compressed file in binary mode.
3737
3838
*file* can be either an file-like object, or a file name to open.
3939
40-
*mode* can be "r" for reading (default), "w" for (over)writing, "x" for
41-
creating exclusively, or "a" for appending. These can equivalently be
42-
given as "rb", "wb", "xb" and "ab" respectively.
40+
*mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for
41+
creating exclusively, or 'a' for appending. These can equivalently be
42+
given as 'rb', 'wb', 'xb' and 'ab' respectively.
4343
4444
*level* is an optional int specifying the compression level to use,
4545
or COMPRESSION_LEVEL_DEFAULT if not given.
@@ -57,33 +57,33 @@ def __init__(self, file, /, mode="r", *,
5757
self._buffer = None
5858

5959
if not isinstance(mode, str):
60-
raise ValueError("mode must be a str")
60+
raise ValueError('mode must be a str')
6161
if options is not None and not isinstance(options, dict):
62-
raise TypeError("options must be a dict or None")
63-
mode = mode.removesuffix("b") # handle rb, wb, xb, ab
64-
if mode == "r":
62+
raise TypeError('options must be a dict or None')
63+
mode = mode.removesuffix('b') # handle rb, wb, xb, ab
64+
if mode == 'r':
6565
if level is not None:
66-
raise TypeError("level is illegal in read mode")
66+
raise TypeError('level is illegal in read mode')
6767
self._mode = _MODE_READ
68-
elif mode in {"w", "a", "x"}:
68+
elif mode in {'w', 'a', 'x'}:
6969
if level is not None and not isinstance(level, int):
70-
raise TypeError("level must be int or None")
70+
raise TypeError('level must be int or None')
7171
self._mode = _MODE_WRITE
7272
self._compressor = ZstdCompressor(level=level, options=options,
7373
zstd_dict=zstd_dict)
7474
self._pos = 0
7575
else:
76-
raise ValueError(f"Invalid mode: {mode!r}")
76+
raise ValueError(f'Invalid mode: {mode!r}')
7777

7878
if isinstance(file, (str, bytes, PathLike)):
7979
self._fp = io.open(file, f'{mode}b')
8080
self._close_fp = True
81-
elif ((mode == 'r' and hasattr(file, "read"))
82-
or (mode != 'r' and hasattr(file, "write"))):
81+
elif ((mode == 'r' and hasattr(file, 'read'))
82+
or (mode != 'r' and hasattr(file, 'write'))):
8383
self._fp = file
8484
else:
85-
raise TypeError("file must be a file-like object "
86-
"or a str, bytes, or PathLike object")
85+
raise TypeError('file must be a file-like object '
86+
'or a str, bytes, or PathLike object')
8787

8888
if self._mode == _MODE_READ:
8989
raw = _streams.DecompressReader(
@@ -151,22 +151,22 @@ def flush(self, mode=FLUSH_BLOCK):
151151
return
152152
self._check_not_closed()
153153
if mode not in {self.FLUSH_BLOCK, self.FLUSH_FRAME}:
154-
raise ValueError("Invalid mode argument, expected either "
155-
"ZstdFile.FLUSH_FRAME or "
156-
"ZstdFile.FLUSH_BLOCK")
154+
raise ValueError('Invalid mode argument, expected either '
155+
'ZstdFile.FLUSH_FRAME or '
156+
'ZstdFile.FLUSH_BLOCK')
157157
if self._compressor.last_mode == mode:
158158
return
159159
# Flush zstd block/frame, and write.
160160
data = self._compressor.flush(mode)
161161
self._fp.write(data)
162-
if hasattr(self._fp, "flush"):
162+
if hasattr(self._fp, 'flush'):
163163
self._fp.flush()
164164

165165
def read(self, size=-1):
166166
"""Read up to size uncompressed bytes from the file.
167167
168168
If size is negative or omitted, read until EOF is reached.
169-
Returns b"" if the file is already at EOF.
169+
Returns b'' if the file is already at EOF.
170170
"""
171171
if size is None:
172172
size = -1
@@ -178,7 +178,7 @@ def read1(self, size=-1):
178178
making multiple reads from the underlying stream. Reads up to a
179179
buffer's worth of data if size is negative.
180180
181-
Returns b"" if the file is at EOF.
181+
Returns b'' if the file is at EOF.
182182
"""
183183
self._check_can_read()
184184
if size < 0:
@@ -293,16 +293,16 @@ def writable(self):
293293
return self._mode == _MODE_WRITE
294294

295295

296-
def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
296+
def open(file, /, mode='rb', *, level=None, options=None, zstd_dict=None,
297297
encoding=None, errors=None, newline=None):
298298
"""Open a Zstandard compressed file in binary or text mode.
299299
300300
file can be either a file name (given as a str, bytes, or PathLike object),
301301
in which case the named file is opened, or it can be an existing file object
302302
to read from or write to.
303303
304-
The mode parameter can be "r", "rb" (default), "w", "wb", "x", "xb", "a",
305-
"ab" for binary mode, or "rt", "wt", "xt", "at" for text mode.
304+
The mode parameter can be 'r', 'rb' (default), 'w', 'wb', 'x', 'xb', 'a',
305+
'ab' for binary mode, or 'rt', 'wt', 'xt', 'at' for text mode.
306306
307307
The level, options, and zstd_dict parameters specify the settings the same
308308
as ZstdFile.
@@ -323,19 +323,19 @@ def open(file, /, mode="rb", *, level=None, options=None, zstd_dict=None,
323323
behavior, and line ending(s).
324324
"""
325325

326-
text_mode = "t" in mode
327-
mode = mode.replace("t", "")
326+
text_mode = 't' in mode
327+
mode = mode.replace('t', '')
328328

329329
if text_mode:
330-
if "b" in mode:
331-
raise ValueError(f"Invalid mode: {mode!r}")
330+
if 'b' in mode:
331+
raise ValueError(f'Invalid mode: {mode!r}')
332332
else:
333333
if encoding is not None:
334-
raise ValueError("Argument 'encoding' not supported in binary mode")
334+
raise ValueError('Argument "encoding" not supported in binary mode')
335335
if errors is not None:
336-
raise ValueError("Argument 'errors' not supported in binary mode")
336+
raise ValueError('Argument "errors" not supported in binary mode')
337337
if newline is not None:
338-
raise ValueError("Argument 'newline' not supported in binary mode")
338+
raise ValueError('Argument "newline" not supported in binary mode')
339339

340340
binary_file = ZstdFile(file, mode, level=level, options=options,
341341
zstd_dict=zstd_dict)

Lib/tarfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ def zstopen(cls, name, mode="r", fileobj=None, level=None, options=None,
20652065
"gz": "gzopen", # gzip compressed tar
20662066
"bz2": "bz2open", # bzip2 compressed tar
20672067
"xz": "xzopen", # lzma compressed tar
2068-
"zst": "zstopen" # zstd compressed tar
2068+
"zst": "zstopen", # zstd compressed tar
20692069
}
20702070

20712071
#--------------------------------------------------------------------------

Makefile.pre.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -2512,8 +2512,7 @@ maninstall: altmaninstall
25122512
XMLLIBSUBDIRS= xml xml/dom xml/etree xml/parsers xml/sax
25132513
LIBSUBDIRS= asyncio \
25142514
collections \
2515-
compression compression/bz2 compression/gzip compression/zstd \
2516-
compression/lzma compression/zlib compression/_common \
2515+
compression compression/_common compression/zstd \
25172516
concurrent concurrent/futures \
25182517
csv \
25192518
ctypes ctypes/macholib \

0 commit comments

Comments
 (0)