diff -r 76c5f3371db6 -r 26c435168228 Lib/test/test_zlib.py --- a/Lib/test/test_zlib.py Sun Dec 23 15:35:23 2012 -0800 +++ b/Lib/test/test_zlib.py Mon Dec 24 12:41:45 2012 +0100 @@ -153,6 +153,11 @@ x = zlib.compress(HAMLET_SCENE) self.assertEqual(zlib.decompress(x), HAMLET_SCENE) + def test_keywords(self): + x = zlib.compress(string=HAMLET_SCENE, level=3) + self.assertEqual(zlib.decompress(data=x, wbits=15, bufsize=16384), + HAMLET_SCENE) + def test_speech128(self): # compress more data data = HAMLET_SCENE * 128 @@ -225,6 +230,16 @@ y2 = dco.flush() self.assertEqual(HAMLET_SCENE, y1 + y2) + def test_compressoptionskwrds(self): + co = zlib.compressobj(level=2, method=zlib.DEFLATED, wbits=-12, + memlevel=9, strategy=zlib.Z_FILTERED) + x1 = co.compress(data=HAMLET_SCENE) + x2 = co.flush(mode=zlib.Z_FINISH) + dco = zlib.decompressobj(wbits=-12) + y1 = dco.decompress(data=x1 + x2, max_length=len(HAMLET_SCENE)) + y2 = dco.flush(length=len(HAMLET_SCENE)) + self.assertEqual(HAMLET_SCENE, y1 + y2) + def test_compressincremental(self): # compress object in steps, decompress object as one-shot data = HAMLET_SCENE * 128 diff -r 76c5f3371db6 -r 26c435168228 Modules/zlibmodule.c --- a/Modules/zlibmodule.c Sun Dec 23 15:35:23 2012 -0800 +++ b/Modules/zlibmodule.c Mon Dec 24 12:41:45 2012 +0100 @@ -142,8 +142,9 @@ "Optional arg level is the compression level, in 0-9."); static PyObject * -PyZlib_compress(PyObject *self, PyObject *args) +PyZlib_compress(PyObject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"string", "level", NULL}; PyObject *ReturnVal = NULL; Py_buffer pinput; Byte *input, *output = NULL; @@ -152,7 +153,8 @@ z_stream zst; /* require Python string object, optional 'level' arg */ - if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "y*|i:compress", kwlist, + &pinput, &level)) return NULL; if (pinput.len > UINT_MAX) { @@ -224,14 +226,15 @@ } PyDoc_STRVAR(decompress__doc__, -"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n" +"decompress(data[, wbits[, bufsize]]) -- Return decompressed data.\n" "\n" "Optional arg wbits is the window buffer size. Optional arg bufsize is\n" "the initial output buffer size."); static PyObject * -PyZlib_decompress(PyObject *self, PyObject *args) +PyZlib_decompress(PyObject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"data", "wbits", "bufsize", NULL}; PyObject *result_str = NULL; Py_buffer pinput; Byte *input; @@ -241,8 +244,8 @@ Py_ssize_t r_strlen=DEFAULTALLOC; z_stream zst; - if (!PyArg_ParseTuple(args, "y*|in:decompress", - &pinput, &wsize, &r_strlen)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "y*|in:decompress", kwlist, + &pinput, &wsize, &r_strlen)) return NULL; if (pinput.len > UINT_MAX) { @@ -345,7 +348,7 @@ int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; Py_buffer zdict; static char *kwlist[] = {"level", "method", "wbits", - "memLevel", "strategy", "zdict", NULL}; + "memlevel", "strategy", "zdict", NULL}; zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", @@ -484,10 +487,10 @@ "be stored in internal buffers for later processing.\n" "Call the flush() method to clear these buffers."); - static PyObject * -PyZlib_objcompress(compobject *self, PyObject *args) +PyZlib_objcompress(compobject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"data", NULL}; int err; unsigned int inplen; Py_ssize_t length = DEFAULTALLOC; @@ -496,7 +499,8 @@ Byte *input; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "y*:compress", &pinput)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "y*:compress", kwlist, + &pinput)) return NULL; if (pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, @@ -616,8 +620,9 @@ "the unconsumed_tail attribute."); static PyObject * -PyZlib_objdecompress(compobject *self, PyObject *args) +PyZlib_objdecompress(compobject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"data", "max_length", NULL}; int err, max_length = 0; unsigned int inplen; Py_ssize_t old_length, length = DEFAULTALLOC; @@ -626,8 +631,8 @@ Byte *input; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput, - &max_length)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "y*|i:decompress", kwlist, + &pinput, &max_length)) return NULL; if (pinput.len > UINT_MAX) { PyErr_SetString(PyExc_OverflowError, @@ -753,14 +758,16 @@ "calling the flush() method. Otherwise, more data can still be compressed."); static PyObject * -PyZlib_flush(compobject *self, PyObject *args) +PyZlib_flush(compobject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"mode", NULL}; int err, length = DEFAULTALLOC; PyObject *RetVal; int flushmode = Z_FINISH; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "|i:flush", kwlist, + &flushmode)) return NULL; /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in @@ -956,13 +963,14 @@ "The decompressor object can no longer be used after this call."); static PyObject * -PyZlib_unflush(compobject *self, PyObject *args) +PyZlib_unflush(compobject *self, PyObject *args, PyObject *kwrds) { + char *kwlist[] = {"length", NULL}; int err, length = DEFAULTALLOC; PyObject * retval = NULL; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "|i:flush", &length)) + if (!PyArg_ParseTupleAndKeywords(args, kwrds, "|i:flush", kwlist, &length)) return NULL; if (length <= 0) { PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); @@ -1034,9 +1042,9 @@ static PyMethodDef comp_methods[] = { - {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, + {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS|METH_KEYWORDS, comp_compress__doc__}, - {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, + {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS|METH_KEYWORDS, comp_flush__doc__}, #ifdef HAVE_ZLIB_COPY {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS, @@ -1047,9 +1055,9 @@ static PyMethodDef Decomp_methods[] = { - {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS, + {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS|METH_KEYWORDS, decomp_decompress__doc__}, - {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, + {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS|METH_KEYWORDS, decomp_flush__doc__}, #ifdef HAVE_ZLIB_COPY {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS, @@ -1146,13 +1154,13 @@ { {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, adler32__doc__}, - {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS, + {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS|METH_KEYWORDS, compress__doc__}, {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS, compressobj__doc__}, {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS, crc32__doc__}, - {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS, + {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS|METH_KEYWORDS, decompress__doc__}, {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS, decompressobj__doc__},