diff -r 2eb84fe85889 Doc/library/zlib.rst --- a/Doc/library/zlib.rst Wed Aug 10 23:44:54 2016 -0400 +++ b/Doc/library/zlib.rst Thu Aug 11 14:52:56 2016 +0800 @@ -129,7 +129,7 @@ platforms, use ``crc32(data) & 0xffffffff``. -.. function:: decompress(data[, wbits[, bufsize]]) +.. function:: decompress(data, wbits=15, bufsize=16384) Decompresses the bytes in *data*, returning a bytes object containing the uncompressed data. The *wbits* parameter depends on @@ -164,14 +164,16 @@ When decompressing a stream, the window size must not be smaller than the size originally used to compress the stream; using a too-small value may result in an :exc:`error` exception. The default *wbits* value - is 15, which corresponds to the largest window size and requires a zlib - header and trailer to be included. + corresponds to the largest window size and requires a zlib header and + trailer to be included. *bufsize* is the initial size of the buffer used to hold decompressed data. If more space is required, the buffer size will be increased as needed, so you don't have to get this value exactly right; tuning it will only save a few calls - to :c:func:`malloc`. The default size is 16384. + to :c:func:`malloc`. + .. versionchanged:: 3.6 + *wbits* and *bufsize* can be used as keyword arguments. .. function:: decompressobj(wbits=15[, zdict]) @@ -257,7 +259,7 @@ .. versionadded:: 3.3 -.. method:: Decompress.decompress(data[, max_length]) +.. method:: Decompress.decompress(data, max_length=0) Decompress *data*, returning a bytes object containing the uncompressed data corresponding to at least part of the data in *string*. This data should be @@ -269,9 +271,11 @@ no longer than *max_length*. This may mean that not all of the compressed input can be processed; and unconsumed data will be stored in the attribute :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to - :meth:`decompress` if decompression is to continue. If *max_length* is not - supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is - empty. + :meth:`decompress` if decompression is to continue. If *max_length* is zero + then the whole input is decompressed, and :attr:`unconsumed_tail` is empty. + + .. versionchanged:: 3.6 + *max_length* can be used as a keyword argument. .. method:: Decompress.flush([length]) diff -r 2eb84fe85889 Lib/test/test_zlib.py --- a/Lib/test/test_zlib.py Wed Aug 10 23:44:54 2016 -0400 +++ b/Lib/test/test_zlib.py Thu Aug 11 14:52:56 2016 +0800 @@ -169,6 +169,14 @@ self.assertEqual(zlib.decompress(x), HAMLET_SCENE) with self.assertRaises(TypeError): zlib.compress(data=HAMLET_SCENE, level=3) + self.assertEqual(zlib.decompress(x, + wbits=zlib.MAX_WBITS, + bufsize=zlib.DEF_BUF_SIZE), + HAMLET_SCENE) + with self.assertRaises(TypeError): + zlib.decompress(data=x, + wbits=zlib.MAX_WBITS, + bufsize=zlib.DEF_BUF_SIZE) def test_speech128(self): # compress more data @@ -240,6 +248,27 @@ self.assertIsInstance(dco.unconsumed_tail, bytes) self.assertIsInstance(dco.unused_data, bytes) + def test_keywords(self): + level = 2 + method = zlib.DEFLATED + wbits = -12 + memLevel = 9 + strategy = zlib.Z_FILTERED + co = zlib.compressobj(level=level, + method=method, + wbits=wbits, + memLevel=memLevel, + strategy=strategy, + zdict=b"") + do = zlib.decompressobj(wbits=wbits, zdict=b"") + with self.assertRaises(TypeError): + co.compress(data=HAMLET_SCENE) + with self.assertRaises(TypeError): + do.decompress(data=zlib.compress(HAMLET_SCENE)) + x = co.compress(HAMLET_SCENE) + co.flush() + y = do.decompress(x, max_length=len(HAMLET_SCENE)) + do.flush() + self.assertEqual(HAMLET_SCENE, y) + def test_compressoptions(self): # specify lots of options to compressobj() level = 2 @@ -255,10 +284,6 @@ y2 = dco.flush() self.assertEqual(HAMLET_SCENE, y1 + y2) - # keyword arguments should also be supported - zlib.compressobj(level=level, method=method, wbits=wbits, - memLevel=memLevel, strategy=strategy, zdict=b"") - def test_compressincremental(self): # compress object in steps, decompress object as one-shot data = HAMLET_SCENE * 128 diff -r 2eb84fe85889 Modules/clinic/zlibmodule.c.h --- a/Modules/clinic/zlibmodule.c.h Wed Aug 10 23:44:54 2016 -0400 +++ b/Modules/clinic/zlibmodule.c.h Thu Aug 11 14:52:56 2016 +0800 @@ -43,7 +43,7 @@ } PyDoc_STRVAR(zlib_decompress__doc__, -"decompress($module, data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE, /)\n" +"decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n" "--\n" "\n" "Returns a bytes object containing the uncompressed data.\n" @@ -56,21 +56,22 @@ " The initial output buffer size."); #define ZLIB_DECOMPRESS_METHODDEF \ - {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__}, + {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS|METH_KEYWORDS, zlib_decompress__doc__}, static PyObject * zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, Py_ssize_t bufsize); static PyObject * -zlib_decompress(PyObject *module, PyObject *args) +zlib_decompress(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static char *_keywords[] = {"", "wbits", "bufsize", NULL}; Py_buffer data = {NULL, NULL}; int wbits = MAX_WBITS; Py_ssize_t bufsize = DEF_BUF_SIZE; - if (!PyArg_ParseTuple(args, "y*|iO&:decompress", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iO&:decompress", _keywords, &data, &wbits, ssize_t_converter, &bufsize)) { goto exit; } @@ -225,7 +226,7 @@ } PyDoc_STRVAR(zlib_Decompress_decompress__doc__, -"decompress($self, data, max_length=0, /)\n" +"decompress($self, data, /, max_length=0)\n" "--\n" "\n" "Return a bytes object containing the decompressed version of the data.\n" @@ -242,20 +243,21 @@ "Call the flush() method to clear these buffers."); #define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \ - {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__}, + {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS|METH_KEYWORDS, zlib_Decompress_decompress__doc__}, static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, Py_ssize_t max_length); static PyObject * -zlib_Decompress_decompress(compobject *self, PyObject *args) +zlib_Decompress_decompress(compobject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static char *_keywords[] = {"", "max_length", NULL}; Py_buffer data = {NULL, NULL}; Py_ssize_t max_length = 0; - if (!PyArg_ParseTuple(args, "y*|O&:decompress", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|O&:decompress", _keywords, &data, ssize_t_converter, &max_length)) { goto exit; } @@ -460,4 +462,4 @@ #ifndef ZLIB_COMPRESS_COPY_METHODDEF #define ZLIB_COMPRESS_COPY_METHODDEF #endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */ -/*[clinic end generated code: output=9046866b1ac5de7e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d87ea93fb4fc3c6e input=a9049054013a1b77]*/ diff -r 2eb84fe85889 Modules/zlibmodule.c --- a/Modules/zlibmodule.c Wed Aug 10 23:44:54 2016 -0400 +++ b/Modules/zlibmodule.c Thu Aug 11 14:52:56 2016 +0800 @@ -318,11 +318,11 @@ data: Py_buffer Compressed data. + / wbits: int(c_default="MAX_WBITS") = MAX_WBITS The window buffer size and container format. bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE The initial output buffer size. - / Returns a bytes object containing the uncompressed data. [clinic start generated code]*/ @@ -330,7 +330,7 @@ static PyObject * zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, Py_ssize_t bufsize) -/*[clinic end generated code: output=77c7e35111dc8c42 input=c13dd2c5696cd17f]*/ +/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/ { PyObject *RetVal = NULL; Byte *ibuf; @@ -750,11 +750,11 @@ data: Py_buffer The binary data to decompress. + / max_length: ssize_t = 0 The maximum allowable length of the decompressed data. Unconsumed input data will be stored in the unconsumed_tail attribute. - / Return a bytes object containing the decompressed version of the data. @@ -766,7 +766,7 @@ static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, Py_ssize_t max_length) -/*[clinic end generated code: output=6e5173c74e710352 input=d6de9b53c4566b8a]*/ +/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/ { int err = Z_OK; Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;