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

Skip to content

Commit 15f3228

Browse files
Issue #16764: Support keyword arguments to zlib.decompress(). Patch by
Xiang Zhang.
1 parent eb24988 commit 15f3228

5 files changed

Lines changed: 61 additions & 25 deletions

File tree

Doc/library/zlib.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ The available exception and functions in this module are:
129129
platforms, use ``crc32(data) & 0xffffffff``.
130130

131131

132-
.. function:: decompress(data[, wbits[, bufsize]])
132+
.. function:: decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)
133133

134134
Decompresses the bytes in *data*, returning a bytes object containing the
135135
uncompressed data. The *wbits* parameter depends on
@@ -164,14 +164,16 @@ The available exception and functions in this module are:
164164
When decompressing a stream, the window size must not be smaller
165165
than the size originally used to compress the stream; using a too-small
166166
value may result in an :exc:`error` exception. The default *wbits* value
167-
is 15, which corresponds to the largest window size and requires a zlib
168-
header and trailer to be included.
167+
corresponds to the largest window size and requires a zlib header and
168+
trailer to be included.
169169

170170
*bufsize* is the initial size of the buffer used to hold decompressed data. If
171171
more space is required, the buffer size will be increased as needed, so you
172172
don't have to get this value exactly right; tuning it will only save a few calls
173-
to :c:func:`malloc`. The default size is 16384.
173+
to :c:func:`malloc`.
174174

175+
.. versionchanged:: 3.6
176+
*wbits* and *bufsize* can be used as keyword arguments.
175177

176178
.. function:: decompressobj(wbits=15[, zdict])
177179

@@ -257,7 +259,7 @@ Decompression objects support the following methods and attributes:
257259
.. versionadded:: 3.3
258260

259261

260-
.. method:: Decompress.decompress(data[, max_length])
262+
.. method:: Decompress.decompress(data, max_length=0)
261263

262264
Decompress *data*, returning a bytes object containing the uncompressed data
263265
corresponding to at least part of the data in *string*. This data should be
@@ -269,9 +271,11 @@ Decompression objects support the following methods and attributes:
269271
no longer than *max_length*. This may mean that not all of the compressed input
270272
can be processed; and unconsumed data will be stored in the attribute
271273
:attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
272-
:meth:`decompress` if decompression is to continue. If *max_length* is not
273-
supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
274-
empty.
274+
:meth:`decompress` if decompression is to continue. If *max_length* is zero
275+
then the whole input is decompressed, and :attr:`unconsumed_tail` is empty.
276+
277+
.. versionchanged:: 3.6
278+
*max_length* can be used as a keyword argument.
275279

276280

277281
.. method:: Decompress.flush([length])

Lib/test/test_zlib.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def test_keywords(self):
169169
self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
170170
with self.assertRaises(TypeError):
171171
zlib.compress(data=HAMLET_SCENE, level=3)
172+
self.assertEqual(zlib.decompress(x,
173+
wbits=zlib.MAX_WBITS,
174+
bufsize=zlib.DEF_BUF_SIZE),
175+
HAMLET_SCENE)
176+
with self.assertRaises(TypeError):
177+
zlib.decompress(data=x,
178+
wbits=zlib.MAX_WBITS,
179+
bufsize=zlib.DEF_BUF_SIZE)
172180

173181
def test_speech128(self):
174182
# compress more data
@@ -240,6 +248,27 @@ def test_pair(self):
240248
self.assertIsInstance(dco.unconsumed_tail, bytes)
241249
self.assertIsInstance(dco.unused_data, bytes)
242250

251+
def test_keywords(self):
252+
level = 2
253+
method = zlib.DEFLATED
254+
wbits = -12
255+
memLevel = 9
256+
strategy = zlib.Z_FILTERED
257+
co = zlib.compressobj(level=level,
258+
method=method,
259+
wbits=wbits,
260+
memLevel=memLevel,
261+
strategy=strategy,
262+
zdict=b"")
263+
do = zlib.decompressobj(wbits=wbits, zdict=b"")
264+
with self.assertRaises(TypeError):
265+
co.compress(data=HAMLET_SCENE)
266+
with self.assertRaises(TypeError):
267+
do.decompress(data=zlib.compress(HAMLET_SCENE))
268+
x = co.compress(HAMLET_SCENE) + co.flush()
269+
y = do.decompress(x, max_length=len(HAMLET_SCENE)) + do.flush()
270+
self.assertEqual(HAMLET_SCENE, y)
271+
243272
def test_compressoptions(self):
244273
# specify lots of options to compressobj()
245274
level = 2
@@ -255,10 +284,6 @@ def test_compressoptions(self):
255284
y2 = dco.flush()
256285
self.assertEqual(HAMLET_SCENE, y1 + y2)
257286

258-
# keyword arguments should also be supported
259-
zlib.compressobj(level=level, method=method, wbits=wbits,
260-
memLevel=memLevel, strategy=strategy, zdict=b"")
261-
262287
def test_compressincremental(self):
263288
# compress object in steps, decompress object as one-shot
264289
data = HAMLET_SCENE * 128

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16764: Support keyword arguments to zlib.decompress(). Patch by
14+
Xiang Zhang.
15+
1316
- Issue #27704: Optimized creating bytes and bytearray from byte-like objects
1417
and iterables. Speed up to 3 times for short objects. Original patch by
1518
Naoki Inada.

Modules/clinic/zlibmodule.c.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ zlib_compress(PyObject *module, PyObject *args, PyObject *kwargs)
4444
}
4545

4646
PyDoc_STRVAR(zlib_decompress__doc__,
47-
"decompress($module, data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE, /)\n"
47+
"decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n"
4848
"--\n"
4949
"\n"
5050
"Returns a bytes object containing the uncompressed data.\n"
@@ -57,21 +57,23 @@ PyDoc_STRVAR(zlib_decompress__doc__,
5757
" The initial output buffer size.");
5858

5959
#define ZLIB_DECOMPRESS_METHODDEF \
60-
{"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__},
60+
{"decompress", (PyCFunction)zlib_decompress, METH_VARARGS|METH_KEYWORDS, zlib_decompress__doc__},
6161

6262
static PyObject *
6363
zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
6464
Py_ssize_t bufsize);
6565

6666
static PyObject *
67-
zlib_decompress(PyObject *module, PyObject *args)
67+
zlib_decompress(PyObject *module, PyObject *args, PyObject *kwargs)
6868
{
6969
PyObject *return_value = NULL;
70+
static const char * const _keywords[] = {"", "wbits", "bufsize", NULL};
71+
static _PyArg_Parser _parser = {"y*|iO&:decompress", _keywords, 0};
7072
Py_buffer data = {NULL, NULL};
7173
int wbits = MAX_WBITS;
7274
Py_ssize_t bufsize = DEF_BUF_SIZE;
7375

74-
if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
76+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
7577
&data, &wbits, ssize_t_converter, &bufsize)) {
7678
goto exit;
7779
}
@@ -228,7 +230,7 @@ zlib_Compress_compress(compobject *self, PyObject *arg)
228230
}
229231

230232
PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
231-
"decompress($self, data, max_length=0, /)\n"
233+
"decompress($self, data, /, max_length=0)\n"
232234
"--\n"
233235
"\n"
234236
"Return a bytes object containing the decompressed version of the data.\n"
@@ -245,20 +247,22 @@ PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
245247
"Call the flush() method to clear these buffers.");
246248

247249
#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
248-
{"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
250+
{"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS|METH_KEYWORDS, zlib_Decompress_decompress__doc__},
249251

250252
static PyObject *
251253
zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
252254
Py_ssize_t max_length);
253255

254256
static PyObject *
255-
zlib_Decompress_decompress(compobject *self, PyObject *args)
257+
zlib_Decompress_decompress(compobject *self, PyObject *args, PyObject *kwargs)
256258
{
257259
PyObject *return_value = NULL;
260+
static const char * const _keywords[] = {"", "max_length", NULL};
261+
static _PyArg_Parser _parser = {"y*|O&:decompress", _keywords, 0};
258262
Py_buffer data = {NULL, NULL};
259263
Py_ssize_t max_length = 0;
260264

261-
if (!PyArg_ParseTuple(args, "y*|O&:decompress",
265+
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
262266
&data, ssize_t_converter, &max_length)) {
263267
goto exit;
264268
}
@@ -463,4 +467,4 @@ zlib_crc32(PyObject *module, PyObject *args)
463467
#ifndef ZLIB_COMPRESS_COPY_METHODDEF
464468
#define ZLIB_COMPRESS_COPY_METHODDEF
465469
#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
466-
/*[clinic end generated code: output=1fed251c15a9bffa input=a9049054013a1b77]*/
470+
/*[clinic end generated code: output=48911ef429b65903 input=a9049054013a1b77]*/

Modules/zlibmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,19 @@ zlib.decompress
318318
319319
data: Py_buffer
320320
Compressed data.
321+
/
321322
wbits: int(c_default="MAX_WBITS") = MAX_WBITS
322323
The window buffer size and container format.
323324
bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
324325
The initial output buffer size.
325-
/
326326
327327
Returns a bytes object containing the uncompressed data.
328328
[clinic start generated code]*/
329329

330330
static PyObject *
331331
zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
332332
Py_ssize_t bufsize)
333-
/*[clinic end generated code: output=77c7e35111dc8c42 input=c13dd2c5696cd17f]*/
333+
/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
334334
{
335335
PyObject *RetVal = NULL;
336336
Byte *ibuf;
@@ -750,11 +750,11 @@ zlib.Decompress.decompress
750750
751751
data: Py_buffer
752752
The binary data to decompress.
753+
/
753754
max_length: ssize_t = 0
754755
The maximum allowable length of the decompressed data.
755756
Unconsumed input data will be stored in
756757
the unconsumed_tail attribute.
757-
/
758758
759759
Return a bytes object containing the decompressed version of the data.
760760
@@ -766,7 +766,7 @@ Call the flush() method to clear these buffers.
766766
static PyObject *
767767
zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
768768
Py_ssize_t max_length)
769-
/*[clinic end generated code: output=6e5173c74e710352 input=d6de9b53c4566b8a]*/
769+
/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
770770
{
771771
int err = Z_OK;
772772
Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;

0 commit comments

Comments
 (0)