Skip to content

Commit bc3f228

Browse files
authored
bpo-32969: Expose some missing constants in zlib and fix the doc (GH-5988)
1 parent 8a38721 commit bc3f228

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

Doc/library/zlib.rst

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,35 @@ The available exception and functions in this module are:
5151

5252
Compresses the bytes in *data*, returning a bytes object containing compressed data.
5353
*level* is an integer from ``0`` to ``9`` or ``-1`` controlling the level of compression;
54-
``1`` is fastest and produces the least compression, ``9`` is slowest and
55-
produces the most. ``0`` is no compression. The default value is ``-1``
56-
(Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
54+
``1`` (Z_BEST_SPEED) is fastest and produces the least compression, ``9`` (Z_BEST_COMPRESSION)
55+
is slowest and produces the most. ``0`` (Z_NO_COMPRESSION) is no compression.
56+
The default value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
5757
compromise between speed and compression (currently equivalent to level 6).
5858
Raises the :exc:`error` exception if any error occurs.
5959

6060
.. versionchanged:: 3.6
6161
*level* can now be used as a keyword parameter.
6262

6363

64-
.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
64+
.. function:: compressobj(level=-1, method=DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY[, zdict])
6565

6666
Returns a compression object, to be used for compressing data streams that won't
6767
fit into memory at once.
6868

6969
*level* is the compression level -- an integer from ``0`` to ``9`` or ``-1``.
70-
A value of ``1`` is fastest and produces the least compression, while a value of
71-
``9`` is slowest and produces the most. ``0`` is no compression. The default
72-
value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
73-
compromise between speed and compression (currently equivalent to level 6).
70+
A value of ``1`` (Z_BEST_SPEED) is fastest and produces the least compression,
71+
while a value of ``9`` (Z_BEST_COMPRESSION) is slowest and produces the most.
72+
``0`` (Z_NO_COMPRESSION) is no compression. The default value is ``-1`` (Z_DEFAULT_COMPRESSION).
73+
Z_DEFAULT_COMPRESSION represents a default compromise between speed and compression
74+
(currently equivalent to level 6).
7475

7576
*method* is the compression algorithm. Currently, the only supported value is
76-
``DEFLATED``.
77+
:const:`DEFLATED`.
7778

7879
The *wbits* argument controls the size of the history buffer (or the
7980
"window size") used when compressing data, and whether a header and
80-
trailer is included in the output. It can take several ranges of values:
81+
trailer is included in the output. It can take several ranges of values,
82+
defaulting to ``15`` (MAX_WBITS):
8183

8284
* +9 to +15: The base-two logarithm of the window size, which
8385
therefore ranges between 512 and 32768. Larger values produce
@@ -97,7 +99,8 @@ The available exception and functions in this module are:
9799
Higher values use more memory, but are faster and produce smaller output.
98100

99101
*strategy* is used to tune the compression algorithm. Possible values are
100-
``Z_DEFAULT_STRATEGY``, ``Z_FILTERED``, and ``Z_HUFFMAN_ONLY``.
102+
:const:`Z_DEFAULT_STRATEGY`, :const:`Z_FILTERED`, :const:`Z_HUFFMAN_ONLY`,
103+
:const:`Z_RLE` (zlib 1.2.0.1) and :const:`Z_FIXED` (zlib 1.2.2.2).
101104

102105
*zdict* is a predefined compression dictionary. This is a sequence of bytes
103106
(such as a :class:`bytes` object) containing subsequences that are expected
@@ -175,7 +178,7 @@ The available exception and functions in this module are:
175178
.. versionchanged:: 3.6
176179
*wbits* and *bufsize* can be used as keyword arguments.
177180

178-
.. function:: decompressobj(wbits=15[, zdict])
181+
.. function:: decompressobj(wbits=MAX_WBITS[, zdict])
179182

180183
Returns a decompression object, to be used for decompressing data streams that
181184
won't fit into memory at once.
@@ -213,13 +216,13 @@ Compression objects support the following methods:
213216

214217
All pending input is processed, and a bytes object containing the remaining compressed
215218
output is returned. *mode* can be selected from the constants
216-
:const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
217-
defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
218-
:const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
219-
:const:`Z_FINISH` finishes the compressed stream and prevents compressing any
220-
more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
221-
the :meth:`compress` method cannot be called again; the only realistic action is
222-
to delete the object.
219+
:const:`Z_NO_FLUSH`, :const:`Z_PARTIAL_FLUSH`, :const:`Z_SYNC_FLUSH`,
220+
:const:`Z_FULL_FLUSH`, :const:`Z_BLOCK` (zlib 1.2.3.4), or :const:`Z_FINISH`,
221+
defaulting to :const:`Z_FINISH`. Except :const:`Z_FINISH`, all constants
222+
allow compressing further bytestrings of data, while :const:`Z_FINISH` finishes the
223+
compressed stream and prevents compressing any more data. After calling :meth:`flush`
224+
with *mode* set to :const:`Z_FINISH`, the :meth:`compress` method cannot be called again;
225+
the only realistic action is to delete the object.
223226

224227

225228
.. method:: Compress.copy()

Lib/test/test_zlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ def test_clear_unconsumed_tail(self):
434434
def test_flushes(self):
435435
# Test flush() with the various options, using all the
436436
# different levels in order to provide more variations.
437-
sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
437+
sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH',
438+
'Z_PARTIAL_FLUSH', 'Z_BLOCK']
438439
sync_opt = [getattr(zlib, opt) for opt in sync_opt
439440
if hasattr(zlib, opt)]
440441
data = HAMLET_SCENE * 8
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expose several missing constants in zlib and fix corresponding
2+
documentation.

Modules/zlibmodule.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,18 +1361,33 @@ PyInit_zlib(void)
13611361
PyModule_AddIntMacro(m, DEFLATED);
13621362
PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
13631363
PyModule_AddIntMacro(m, DEF_BUF_SIZE);
1364+
// compression levels
1365+
PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
13641366
PyModule_AddIntMacro(m, Z_BEST_SPEED);
13651367
PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
13661368
PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1369+
// compression strategies
13671370
PyModule_AddIntMacro(m, Z_FILTERED);
13681371
PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1372+
#ifdef Z_RLE // 1.2.0.1
1373+
PyModule_AddIntMacro(m, Z_RLE);
1374+
#endif
1375+
#ifdef Z_FIXED // 1.2.2.2
1376+
PyModule_AddIntMacro(m, Z_FIXED);
1377+
#endif
13691378
PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
1370-
1371-
PyModule_AddIntMacro(m, Z_FINISH);
1379+
// allowed flush values
13721380
PyModule_AddIntMacro(m, Z_NO_FLUSH);
1381+
PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
13731382
PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
13741383
PyModule_AddIntMacro(m, Z_FULL_FLUSH);
1375-
1384+
PyModule_AddIntMacro(m, Z_FINISH);
1385+
#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1386+
PyModule_AddIntMacro(m, Z_BLOCK);
1387+
#endif
1388+
#ifdef Z_TREES // 1.2.3.4, only for inflate
1389+
PyModule_AddIntMacro(m, Z_TREES);
1390+
#endif
13761391
ver = PyUnicode_FromString(ZLIB_VERSION);
13771392
if (ver != NULL)
13781393
PyModule_AddObject(m, "ZLIB_VERSION", ver);

0 commit comments

Comments
 (0)