Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fe0d13

Browse files
committed
Issue #26243: zlib.compress() keyword argument support by Aviv Palivoda
1 parent 3841866 commit 1fe0d13

6 files changed

Lines changed: 44 additions & 25 deletions

File tree

Doc/library/zlib.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ The available exception and functions in this module are:
4646
platforms, use ``adler32(data) & 0xffffffff``.
4747

4848

49-
.. function:: compress(data[, level])
49+
.. function:: compress(data, level=-1)
5050

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

59+
.. versionchanged:: 3.6
60+
Keyword arguments are now supported.
61+
5762

5863
.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
5964

Doc/whatsnew/3.6.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ to check if the :class:`~zipfile.ZipInfo` instance represents a directory.
150150
(Contributed by Thomas Kluyver in :issue:`26039`.)
151151

152152

153+
zlib
154+
----
155+
156+
The :func:`~zlib.compress` function now accepts keyword arguments.
157+
(Contributed by Aviv Palivoda in :issue:`26243`.)
158+
159+
153160
Optimizations
154161
=============
155162

Lib/test/test_zlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def test_speech(self):
162162
x = zlib.compress(HAMLET_SCENE)
163163
self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
164164

165+
def test_keywords(self):
166+
x = zlib.compress(data=HAMLET_SCENE, level=3)
167+
self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
168+
165169
def test_speech128(self):
166170
# compress more data
167171
data = HAMLET_SCENE * 128

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ Core and Builtins
175175
Library
176176
-------
177177

178+
- Issue #26243: Support keyword arguments to zlib.compress(). Patch by Aviv
179+
Palivoda.
180+
178181
- Issue #26117: The os.scandir() iterator now closes file descriptor not only
179182
when the iteration is finished, but when it was failed with error.
180183

Modules/clinic/zlibmodule.c.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,39 @@ preserve
33
[clinic start generated code]*/
44

55
PyDoc_STRVAR(zlib_compress__doc__,
6-
"compress($module, bytes, level=Z_DEFAULT_COMPRESSION, /)\n"
6+
"compress($module, /, data, level=Z_DEFAULT_COMPRESSION)\n"
77
"--\n"
88
"\n"
99
"Returns a bytes object containing compressed data.\n"
1010
"\n"
11-
" bytes\n"
11+
" data\n"
1212
" Binary data to be compressed.\n"
1313
" level\n"
1414
" Compression level, in 0-9.");
1515

1616
#define ZLIB_COMPRESS_METHODDEF \
17-
{"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
17+
{"compress", (PyCFunction)zlib_compress, METH_VARARGS|METH_KEYWORDS, zlib_compress__doc__},
1818

1919
static PyObject *
20-
zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level);
20+
zlib_compress_impl(PyModuleDef *module, Py_buffer *data, int level);
2121

2222
static PyObject *
23-
zlib_compress(PyModuleDef *module, PyObject *args)
23+
zlib_compress(PyModuleDef *module, PyObject *args, PyObject *kwargs)
2424
{
2525
PyObject *return_value = NULL;
26-
Py_buffer bytes = {NULL, NULL};
26+
static char *_keywords[] = {"data", "level", NULL};
27+
Py_buffer data = {NULL, NULL};
2728
int level = Z_DEFAULT_COMPRESSION;
2829

29-
if (!PyArg_ParseTuple(args, "y*|i:compress",
30-
&bytes, &level))
30+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:compress", _keywords,
31+
&data, &level))
3132
goto exit;
32-
return_value = zlib_compress_impl(module, &bytes, level);
33+
return_value = zlib_compress_impl(module, &data, level);
3334

3435
exit:
35-
/* Cleanup for bytes */
36-
if (bytes.obj)
37-
PyBuffer_Release(&bytes);
36+
/* Cleanup for data */
37+
if (data.obj)
38+
PyBuffer_Release(&data);
3839

3940
return return_value;
4041
}
@@ -439,4 +440,4 @@ zlib_crc32(PyModuleDef *module, PyObject *args)
439440
#ifndef ZLIB_COMPRESS_COPY_METHODDEF
440441
#define ZLIB_COMPRESS_COPY_METHODDEF
441442
#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
442-
/*[clinic end generated code: output=cf81e1deae3af0ce input=a9049054013a1b77]*/
443+
/*[clinic end generated code: output=3c96b58b923c1273 input=a9049054013a1b77]*/

Modules/zlibmodule.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,31 @@ PyZlib_Free(voidpf ctx, void *ptr)
137137
/*[clinic input]
138138
zlib.compress
139139
140-
bytes: Py_buffer
140+
data: Py_buffer
141141
Binary data to be compressed.
142142
level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
143-
Compression level, in 0-9.
144-
/
143+
Compression level, in 0-9 or -1.
145144
146145
Returns a bytes object containing compressed data.
147146
[clinic start generated code]*/
148147

149148
static PyObject *
150-
zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level)
151-
/*[clinic end generated code: output=5d7dd4588788efd3 input=be3abe9934bda4b3]*/
149+
zlib_compress_impl(PyModuleDef *module, Py_buffer *data, int level)
150+
/*[clinic end generated code: output=1b97589132b203b4 input=671c615a4b2267da]*/
152151
{
153152
PyObject *ReturnVal = NULL;
154153
Byte *input, *output = NULL;
155154
unsigned int length;
156155
int err;
157156
z_stream zst;
158157

159-
if ((size_t)bytes->len > UINT_MAX) {
158+
if ((size_t)data->len > UINT_MAX) {
160159
PyErr_SetString(PyExc_OverflowError,
161160
"Size does not fit in an unsigned int");
162161
goto error;
163162
}
164-
input = bytes->buf;
165-
length = (unsigned int)bytes->len;
163+
input = data->buf;
164+
length = (unsigned int)data->len;
166165

167166
zst.avail_out = length + length/1000 + 12 + 1;
168167

@@ -1323,7 +1322,7 @@ PyDoc_STRVAR(zlib_module_documentation,
13231322
"zlib library, which is based on GNU zip.\n"
13241323
"\n"
13251324
"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1326-
"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
1325+
"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
13271326
"compressobj([level[, ...]]) -- Return a compressor object.\n"
13281327
"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
13291328
"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"

0 commit comments

Comments
 (0)