diff -r f1f707dd7cae Modules/zlibmodule.c --- a/Modules/zlibmodule.c Wed Jan 08 16:01:42 2014 +0100 +++ b/Modules/zlibmodule.c Wed Jan 08 23:54:01 2014 +0200 @@ -88,36 +88,6 @@ [clinic start generated code]*/ /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ -PyDoc_STRVAR(compressobj__doc__, -"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n" -" strategy=Z_DEFAULT_STRATEGY[, zdict])\n" -" -- Return a compressor object.\n" -"\n" -"level is the compression level (an integer in the range 0-9; default is 6).\n" -"Higher compression levels are slower, but produce smaller results.\n" -"\n" -"method is the compression algorithm. If given, this must be DEFLATED.\n" -"\n" -"wbits is the base two logarithm of the window size (range: 8..15).\n" -"\n" -"memlevel controls the amount of memory used for internal compression state.\n" -"Valid values range from 1 to 9. Higher values result in higher memory usage,\n" -"faster compression, and smaller output.\n" -"\n" -"strategy is used to tune the compression algorithm. Possible values are\n" -"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n" -"\n" -"zdict is the predefined compression dictionary - a sequence of bytes\n" -"containing subsequences that are likely to occur in the input data."); - -PyDoc_STRVAR(decompressobj__doc__, -"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n" -"\n" -"Optional arg wbits is the window buffer size.\n" -"\n" -"Optional arg zdict is the predefined compression dictionary. This must be\n" -"the same dictionary as used by the compressor that produced the input data."); - static compobject * newcompobject(PyTypeObject *type) { @@ -165,8 +135,8 @@ } /*[clinic input] +zlib.compress -zlib.compress bytes: Py_buffer Binary data to be compressed. [ @@ -175,13 +145,12 @@ ] / -Returns compressed string. - +Returns a bytes object containing compressed data. [clinic start generated code]*/ PyDoc_STRVAR(zlib_compress__doc__, "compress(bytes, [level])\n" -"Returns compressed string.\n" +"Returns a bytes object containing compressed data.\n" "\n" " bytes\n" " Binary data to be compressed.\n" @@ -227,7 +196,7 @@ static PyObject * zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level) -/*[clinic end generated code: checksum=9f055a396620bc1a8a13d74c3496249528b32b0d]*/ +/*[clinic end generated code: checksum=b32079e4de8722ed74935cc1e22af5818346e48b]*/ { PyObject *ReturnVal = NULL; Byte *input, *output = NULL; @@ -311,6 +280,7 @@ class uint_converter(CConverter): type = 'unsigned int' converter = 'uint_converter' + c_ignored_default = "0" [python start generated code]*/ /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ @@ -346,35 +316,103 @@ return 1; } -PyDoc_STRVAR(decompress__doc__, -"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n" +/*[clinic input] +zlib.decompress + + data: Py_buffer + Compressed data. + [ + wbits: int + The window buffer size. + [ + bufsize: uint + The initial output buffer size. + ] + ] + / + +Returns a bytes object containing the uncompressed data. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_decompress__doc__, +"decompress(data, [wbits, [bufsize]])\n" +"Returns a bytes object containing the uncompressed data.\n" "\n" -"Optional arg wbits is the window buffer size. Optional arg bufsize is\n" -"the initial output buffer size."); +" data\n" +" Compressed data.\n" +" wbits\n" +" The window buffer size.\n" +" bufsize\n" +" The initial output buffer size."); + +#define ZLIB_DECOMPRESS_METHODDEF \ + {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__}, static PyObject * -PyZlib_decompress(PyObject *self, PyObject *args) +zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int group_right_1, int wbits, int group_right_2, unsigned int bufsize); + +static PyObject * +zlib_decompress(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + int group_right_1 = 0; + int wbits = 0; + int group_right_2 = 0; + unsigned int bufsize = 0; + + switch (PyTuple_Size(args)) { + case 1: + if (!PyArg_ParseTuple(args, "y*:decompress", &data)) + return NULL; + break; + case 2: + if (!PyArg_ParseTuple(args, "y*i:decompress", &data, &wbits)) + return NULL; + group_right_1 = 1; + break; + case 3: + if (!PyArg_ParseTuple(args, "y*iO&:decompress", &data, &wbits, uint_converter, &bufsize)) + return NULL; + group_right_1 = 1; + group_right_2 = 1; + break; + default: + PyErr_SetString(PyExc_TypeError, "zlib.decompress requires 1 to 3 arguments"); + return NULL; + } + return_value = zlib_decompress_impl(module, &data, group_right_1, wbits, group_right_2, bufsize); + + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int group_right_1, int wbits, int group_right_2, unsigned int bufsize) +/*[clinic end generated code: checksum=50d4b27ea006810b07086e5a8fe902b5d907561e]*/ { PyObject *result_str = NULL; - Py_buffer pinput; Byte *input; unsigned int length; int err; - int wsize=DEF_WBITS; - unsigned int bufsize = DEFAULTALLOC, new_bufsize; + unsigned int new_bufsize; z_stream zst; - if (!PyArg_ParseTuple(args, "y*|iO&:decompress", - &pinput, &wsize, uint_converter, &bufsize)) - return NULL; + if (!group_right_1) + wbits = DEF_WBITS; + if (!group_right_2) + bufsize = DEFAULTALLOC; - if ((size_t)pinput.len> UINT_MAX) { + if ((size_t)data->len> UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); goto error; } - input = pinput.buf; - length = (unsigned int)pinput.len; + input = data->buf; + length = (unsigned int)data->len; if (bufsize == 0) bufsize = 1; @@ -390,7 +428,7 @@ zst.zfree = PyZlib_Free; zst.next_out = (Byte *)PyBytes_AS_STRING(result_str); zst.next_in = (Byte *)input; - err = inflateInit2(&zst, wsize); + err = inflateInit2(&zst, wbits); switch(err) { case(Z_OK): @@ -456,15 +494,58 @@ if (_PyBytes_Resize(&result_str, zst.total_out) < 0) goto error; - PyBuffer_Release(&pinput); return result_str; error: - PyBuffer_Release(&pinput); Py_XDECREF(result_str); return NULL; } +PyDoc_STRVAR(compressobj__doc__, +"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n" +" strategy=Z_DEFAULT_STRATEGY[, zdict])\n" +" -- Return a compressor object.\n" +"\n" +"level is the compression level (an integer in the range 0-9; default is 6).\n" +"Higher compression levels are slower, but produce smaller results.\n" +"\n" +"method is the compression algorithm. If given, this must be DEFLATED.\n" +"\n" +"wbits is the base two logarithm of the window size (range: 8..15).\n" +"\n" +"memlevel controls the amount of memory used for internal compression state.\n" +"Valid values range from 1 to 9. Higher values result in higher memory usage,\n" +"faster compression, and smaller output.\n" +"\n" +"strategy is used to tune the compression algorithm. Possible values are\n" +"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n" +"\n" +"zdict is the predefined compression dictionary - a sequence of bytes\n" +"containing subsequences that are likely to occur in the input data."); + +/*[-clinic input] +zlib.compressobj + + level: int(c_default = "Z_DEFAULT_COMPRESSION") = None + the compression level (an integer in the range 0-9; default is 6). + Higher compression levels are slower, but produce smaller results. + method: int(c_default = "DEFLATED") = None + the compression algorithm. If given, this must be DEFLATED. + wbits: int(c_default = "MAX_WBITS") = None + the base two logarithm of the window size (range: 8..15). + memLevel: int(c_default = "DEF_MEM_LEVEL") = None + controls the amount of memory used for internal compression state. + Valid values range from 1 to 9. Higher values result in higher memory usage, + faster compression, and smaller output. + strategy: int(c_default = "0") = None + used to tune the compression algorithm. Possible values are + Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY. + zdict: Py_buffer(nullable=True) = None + the predefined compression dictionary - a sequence of bytes + containing subsequences that are likely to occur in the input data. + +Return a compressor object. +[-clinic start generated code]*/ static PyObject * PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs) { @@ -536,17 +617,59 @@ return (PyObject*)self; } +/*[clinic input] +zlib.decompressobj + + wbits: int(c_default="DEF_WBITS") = None + the window buffer size. + zdict: object = NULL + the predefined compression dictionary. This must be + the same dictionary as used by the compressor that produced the input data. + +Return a decompressor object. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_decompressobj__doc__, +"decompressobj(wbits=None, zdict=None)\n" +"Return a decompressor object.\n" +"\n" +" wbits\n" +" the window buffer size.\n" +" zdict\n" +" the predefined compression dictionary. This must be\n" +" the same dictionary as used by the compressor that produced the input data."); + +#define ZLIB_DECOMPRESSOBJ_METHODDEF \ + {"decompressobj", (PyCFunction)zlib_decompressobj, METH_VARARGS|METH_KEYWORDS, zlib_decompressobj__doc__}, + static PyObject * -PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs) +zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict); + +static PyObject * +zlib_decompressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"wbits", "zdict", NULL}; - int wbits=DEF_WBITS, err; + PyObject *return_value = NULL; + static char *_keywords[] = {"wbits", "zdict", NULL}; + int wbits = DEF_WBITS; + PyObject *zdict = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|iO:decompressobj", _keywords, + &wbits, &zdict)) + goto exit; + return_value = zlib_decompressobj_impl(module, wbits, zdict); + +exit: + return return_value; +} + +static PyObject * +zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict) +/*[clinic end generated code: checksum=8fd35aac2fc2c31ec92516742cdf213cc18e180f]*/ +{ + int err; compobject *self; - PyObject *zdict=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", - kwlist, &wbits, &zdict)) - return NULL; if (zdict != NULL && !PyObject_CheckBuffer(zdict)) { PyErr_SetString(PyExc_TypeError, "zdict argument must support the buffer protocol"); @@ -614,37 +737,80 @@ Dealloc(self); } -PyDoc_STRVAR(comp_compress__doc__, -"compress(data) -- Return a string containing data compressed.\n" +/*[clinic input] +zlib.Compress.compress + + self: self(type="compobject *") + data: Py_buffer + Binary data to be compressed. + / + +Returns a bytes object containing compressed data. + +After calling this function, some of the input data may still +be stored in internal buffers for later processing. +Call the flush() method to clear these buffers. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_Compress_compress__doc__, +"compress(data)\n" +"Returns a bytes object containing compressed data.\n" +"\n" +" data\n" +" Binary data to be compressed.\n" "\n" "After calling this function, some of the input data may still\n" "be stored in internal buffers for later processing.\n" "Call the flush() method to clear these buffers."); +#define ZLIB_COMPRESS_COMPRESS_METHODDEF \ + {"compress", (PyCFunction)zlib_Compress_compress, METH_VARARGS, zlib_Compress_compress__doc__}, static PyObject * -PyZlib_objcompress(compobject *self, PyObject *args) +zlib_Compress_compress_impl(compobject *self, Py_buffer *data); + +static PyObject * +zlib_Compress_compress(PyObject *self, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:compress", + &data)) + goto exit; + return_value = zlib_Compress_compress_impl((compobject *)self, &data); + +exit: + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +zlib_Compress_compress_impl(compobject *self, Py_buffer *data) +/*[clinic end generated code: checksum=15d5f21966b284f63df5a8f89ffdb911daf7f1ea]*/ { int err; unsigned int inplen; unsigned int length = DEFAULTALLOC, new_length; - PyObject *RetVal = NULL; + PyObject *RetVal; Py_buffer pinput; Byte *input; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "y*:compress", &pinput)) - return NULL; if ((size_t)pinput.len> UINT_MAX) { PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int"); - goto error_outer; + return NULL; } - input = pinput.buf; - inplen = (unsigned int)pinput.len; + input = data->buf; + inplen = (unsigned int)data->len; if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) - goto error_outer; + return NULL; ENTER_ZLIB(self); @@ -667,7 +833,7 @@ new_length = UINT_MAX; if (_PyBytes_Resize(&RetVal, new_length) < 0) { Py_CLEAR(RetVal); - goto error; + goto done; } self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal) + length; @@ -685,18 +851,15 @@ if (err != Z_OK && err != Z_BUF_ERROR) { zlib_error(self->zst, err, "while compressing data"); - Py_DECREF(RetVal); - RetVal = NULL; - goto error; + Py_CLEAR(RetVal); + goto done; } if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) { Py_CLEAR(RetVal); } - error: + done: LEAVE_ZLIB(self); - error_outer: - PyBuffer_Release(&pinput); return RetVal; } @@ -744,11 +907,9 @@ } /*[clinic input] - zlib.Decompress.decompress self: self(type="compobject *") - data: Py_buffer The binary data to decompress. max_length: uint = 0 @@ -757,7 +918,7 @@ the unconsumed_tail attribute. / -Return a string containing the decompressed version of the data. +Return a bytes object containing the decompressed version of the data. After calling this function, some of the input data may still be stored in internal buffers for later processing. @@ -766,7 +927,7 @@ PyDoc_STRVAR(zlib_Decompress_decompress__doc__, "decompress(data, max_length=0)\n" -"Return a string containing the decompressed version of the data.\n" +"Return a bytes object containing the decompressed version of the data.\n" "\n" " data\n" " The binary data to decompress.\n" @@ -808,7 +969,7 @@ static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length) -/*[clinic end generated code: checksum=5b1e4f9f1ef8eca55fff78356f9df0c81232ed3b]*/ +/*[clinic end generated code: checksum=e071121852dd68241f51553c20bd0406e1b13599]*/ { int err; unsigned int old_length, length = DEFAULTALLOC; @@ -928,29 +1089,62 @@ return RetVal; } -PyDoc_STRVAR(comp_flush__doc__, -"flush( [mode] ) -- Return a string containing any remaining compressed data.\n" +/*[clinic input] +zlib.Compress.flush + + self: self(type="compobject *") + mode: int(c_default="Z_FINISH") = zlib.Z_FINISH + One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH. + If mode == Z_FINISH, the compressor object can no longer be used after + calling the flush() method. Otherwise, more data can still be compressed. + / + +Return a bytes object containing any remaining compressed data. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_Compress_flush__doc__, +"flush(mode=zlib.Z_FINISH)\n" +"Return a bytes object containing any remaining compressed data.\n" "\n" -"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n" -"default value used when mode is not specified is Z_FINISH.\n" -"If mode == Z_FINISH, the compressor object can no longer be used after\n" -"calling the flush() method. Otherwise, more data can still be compressed."); +" mode\n" +" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n" +" If mode == Z_FINISH, the compressor object can no longer be used after\n" +" calling the flush() method. Otherwise, more data can still be compressed."); + +#define ZLIB_COMPRESS_FLUSH_METHODDEF \ + {"flush", (PyCFunction)zlib_Compress_flush, METH_VARARGS, zlib_Compress_flush__doc__}, static PyObject * -PyZlib_flush(compobject *self, PyObject *args) +zlib_Compress_flush_impl(compobject *self, int mode); + +static PyObject * +zlib_Compress_flush(PyObject *self, PyObject *args) +{ + PyObject *return_value = NULL; + int mode = Z_FINISH; + + if (!PyArg_ParseTuple(args, + "|i:flush", + &mode)) + goto exit; + return_value = zlib_Compress_flush_impl((compobject *)self, mode); + +exit: + return return_value; +} + +static PyObject * +zlib_Compress_flush_impl(compobject *self, int mode) +/*[clinic end generated code: checksum=04cea9d4c981ac1e9ba6cc2b91a4e2c7c9bc5645]*/ { int err; unsigned int length = DEFAULTALLOC, new_length; PyObject *RetVal; - int flushmode = Z_FINISH; unsigned long start_total_out; - if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) - return NULL; - /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in doing any work at all; just return an empty string. */ - if (flushmode == Z_NO_FLUSH) { + if (mode == Z_NO_FLUSH) { return PyBytes_FromStringAndSize(NULL, 0); } @@ -965,7 +1159,7 @@ self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal); Py_BEGIN_ALLOW_THREADS - err = deflate(&(self->zst), flushmode); + err = deflate(&(self->zst), mode); Py_END_ALLOW_THREADS /* while Z_OK and the output buffer is full, there might be more output, @@ -985,14 +1179,14 @@ length = new_length; Py_BEGIN_ALLOW_THREADS - err = deflate(&(self->zst), flushmode); + err = deflate(&(self->zst), mode); Py_END_ALLOW_THREADS } - /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free + /* If mode is Z_FINISH, we also have to call deflateEnd() to free various data structures. Note we should only get Z_STREAM_END when - flushmode is Z_FINISH, but checking both for safety*/ - if (err == Z_STREAM_END && flushmode == Z_FINISH) { + mode is Z_FINISH, but checking both for safety*/ + if (err == Z_STREAM_END && mode == Z_FINISH) { err = deflateEnd(&(self->zst)); if (err != Z_OK) { zlib_error(self->zst, err, "while finishing compression"); @@ -1106,11 +1300,37 @@ return NULL; } -PyDoc_STRVAR(decomp_copy__doc__, -"copy() -- Return a copy of the decompression object."); +/*[clinic input] +zlib.Decompress.copy + + self: self(type="compobject *") + +Return a copy of the decompression object. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_Decompress_copy__doc__, +"copy()\n" +"Return a copy of the decompression object."); + +#define ZLIB_DECOMPRESS_COPY_METHODDEF \ + {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__}, static PyObject * -PyZlib_uncopy(compobject *self) +zlib_Decompress_copy_impl(compobject *self); + +static PyObject * +zlib_Decompress_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = zlib_Decompress_copy_impl((compobject *)self); + + return return_value; +} + +static PyObject * +zlib_Decompress_copy_impl(compobject *self) +/*[clinic end generated code: checksum=96d01c85c721bad87296c399e3c6cd6daa742f60]*/ { compobject *retval = NULL; int err; @@ -1162,24 +1382,70 @@ } #endif -PyDoc_STRVAR(decomp_flush__doc__, -"flush( [length] ) -- Return a string containing any remaining\n" -"decompressed data. length, if given, is the initial size of the\n" -"output buffer.\n" +/*[clinic input] +zlib.Decompress.flush + + self: self(type="compobject *") + [ + length: uint + the initial size of the output buffer. + ] + / + +Return a bytes object containing any remaining decompressed data. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_Decompress_flush__doc__, +"flush([length])\n" +"Return a bytes object containing any remaining decompressed data.\n" "\n" -"The decompressor object can no longer be used after this call."); +" length\n" +" the initial size of the output buffer."); + +#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \ + {"flush", (PyCFunction)zlib_Decompress_flush, METH_VARARGS, zlib_Decompress_flush__doc__}, static PyObject * -PyZlib_unflush(compobject *self, PyObject *args) +zlib_Decompress_flush_impl(compobject *self, int group_right_1, unsigned int length); + +static PyObject * +zlib_Decompress_flush(PyObject *self, PyObject *args) +{ + PyObject *return_value = NULL; + int group_right_1 = 0; + unsigned int length = 0; + + switch (PyTuple_Size(args)) { + case 0: + if (!PyArg_ParseTuple(args, ":flush")) + return NULL; + break; + case 1: + if (!PyArg_ParseTuple(args, "O&:flush", uint_converter, &length)) + return NULL; + group_right_1 = 1; + break; + default: + PyErr_SetString(PyExc_TypeError, "zlib.Decompress.flush requires 0 to 1 arguments"); + return NULL; + } + return_value = zlib_Decompress_flush_impl((compobject *)self, group_right_1, length); + + return return_value; +} + +static PyObject * +zlib_Decompress_flush_impl(compobject *self, int group_right_1, unsigned int length) +/*[clinic end generated code: checksum=7367c691b9d4f8d863973b80cffe2c68fe03cb98]*/ { int err; - unsigned int length = DEFAULTALLOC, new_length; + unsigned int new_length; PyObject * retval = NULL; unsigned long start_total_out; Py_ssize_t size; - if (!PyArg_ParseTuple(args, "|O&:flush", uint_converter, &length)) - return NULL; + if (!group_right_1) + length = DEFAULTALLOC; if (length == 0) { PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); return NULL; @@ -1257,10 +1523,8 @@ static PyMethodDef comp_methods[] = { - {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, - comp_compress__doc__}, - {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, - comp_flush__doc__}, + ZLIB_COMPRESS_COMPRESS_METHODDEF + ZLIB_COMPRESS_FLUSH_METHODDEF #ifdef HAVE_ZLIB_COPY ZLIB_COMPRESS_COPY_METHODDEF #endif @@ -1270,11 +1534,9 @@ static PyMethodDef Decomp_methods[] = { ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF - {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, - decomp_flush__doc__}, + ZLIB_DECOMPRESS_FLUSH_METHODDEF #ifdef HAVE_ZLIB_COPY - {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS, - decomp_copy__doc__}, + ZLIB_DECOMPRESS_COPY_METHODDEF #endif {NULL, NULL} }; @@ -1287,95 +1549,168 @@ {NULL}, }; -PyDoc_STRVAR(adler32__doc__, -"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n" +/*[clinic input] +zlib.adler32 + + data: Py_buffer + value: unsigned_int(bitwise=True) = 1 + Starting value of the checksum. + / + +Compute an Adler-32 checksum of data. + +The returned checksum is an integer. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_adler32__doc__, +"adler32(data, value=1)\n" +"Compute an Adler-32 checksum of data.\n" "\n" -"An optional starting value can be specified. The returned checksum is\n" -"an integer."); +" value\n" +" Starting value of the checksum.\n" +"\n" +"The returned checksum is an integer."); + +#define ZLIB_ADLER32_METHODDEF \ + {"adler32", (PyCFunction)zlib_adler32, METH_VARARGS, zlib_adler32__doc__}, static PyObject * -PyZlib_adler32(PyObject *self, PyObject *args) +zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value); + +static PyObject * +zlib_adler32(PyModuleDef *module, PyObject *args) { - unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */ - Py_buffer pbuf; + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + unsigned int value = 1; - if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val)) - return NULL; + if (!PyArg_ParseTuple(args, + "y*|I:adler32", + &data, &value)) + goto exit; + return_value = zlib_adler32_impl(module, &data, value); + +exit: + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value) +/*[clinic end generated code: checksum=defb8da46d75cc66968f5fe12d541ed6f60c8de4]*/ +{ /* Releasing the GIL for very small buffers is inefficient and may lower performance */ - if (pbuf.len> 1024*5) { - unsigned char *buf = pbuf.buf; - Py_ssize_t len = pbuf.len; + if (data->len> 1024*5) { + unsigned char *buf = data->buf; + Py_ssize_t len = data->len; Py_BEGIN_ALLOW_THREADS /* Avoid truncation of length for very large buffers. adler32() takes length as an unsigned int, which may be narrower than Py_ssize_t. */ while ((size_t)len> UINT_MAX) { - adler32val = adler32(adler32val, buf, UINT_MAX); + value = adler32(value, buf, UINT_MAX); buf += (size_t) UINT_MAX; len -= (size_t) UINT_MAX; } - adler32val = adler32(adler32val, buf, (unsigned int)len); + value = adler32(value, buf, (unsigned int)len); Py_END_ALLOW_THREADS } else { - adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len); + value = adler32(value, data->buf, (unsigned int)data->len); } - PyBuffer_Release(&pbuf); - return PyLong_FromUnsignedLong(adler32val & 0xffffffffU); + return PyLong_FromUnsignedLong(value & 0xffffffffU); } -PyDoc_STRVAR(crc32__doc__, -"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n" +/*[clinic input] +zlib.crc32 + + data: Py_buffer + value: unsigned_int(bitwise=True) = 0 + Starting value of the checksum. + / + +Compute a CRC-32 checksum of data. + +The returned checksum is an integer. +[clinic start generated code]*/ + +PyDoc_STRVAR(zlib_crc32__doc__, +"crc32(data, value=0)\n" +"Compute a CRC-32 checksum of data.\n" "\n" -"An optional starting value can be specified. The returned checksum is\n" -"an integer."); +" value\n" +" Starting value of the checksum.\n" +"\n" +"The returned checksum is an integer."); + +#define ZLIB_CRC32_METHODDEF \ + {"crc32", (PyCFunction)zlib_crc32, METH_VARARGS, zlib_crc32__doc__}, static PyObject * -PyZlib_crc32(PyObject *self, PyObject *args) +zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value); + +static PyObject * +zlib_crc32(PyModuleDef *module, PyObject *args) { - unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */ - Py_buffer pbuf; + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + unsigned int value = 0; + + if (!PyArg_ParseTuple(args, + "y*|I:crc32", + &data, &value)) + goto exit; + return_value = zlib_crc32_impl(module, &data, value); + +exit: + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value) +/*[clinic end generated code: checksum=f5c86fc2a3391525fdc39f47f04449e0fb87224a]*/ +{ int signed_val; - if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val)) - return NULL; /* Releasing the GIL for very small buffers is inefficient and may lower performance */ - if (pbuf.len> 1024*5) { - unsigned char *buf = pbuf.buf; - Py_ssize_t len = pbuf.len; + if (data->len> 1024*5) { + unsigned char *buf = data->buf; + Py_ssize_t len = data->len; Py_BEGIN_ALLOW_THREADS /* Avoid truncation of length for very large buffers. crc32() takes length as an unsigned int, which may be narrower than Py_ssize_t. */ while ((size_t)len> UINT_MAX) { - crc32val = crc32(crc32val, buf, UINT_MAX); + value = crc32(value, buf, UINT_MAX); buf += (size_t) UINT_MAX; len -= (size_t) UINT_MAX; } - signed_val = crc32(crc32val, buf, (unsigned int)len); + signed_val = crc32(value, buf, (unsigned int)len); Py_END_ALLOW_THREADS } else { - signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len); + signed_val = crc32(value, data->buf, (unsigned int)data->len); } - PyBuffer_Release(&pbuf); return PyLong_FromUnsignedLong(signed_val & 0xffffffffU); } static PyMethodDef zlib_methods[] = { - {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, - adler32__doc__}, + ZLIB_ADLER32_METHODDEF ZLIB_COMPRESS_METHODDEF {"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__doc__}, - {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS, - decompressobj__doc__}, + ZLIB_CRC32_METHODDEF + ZLIB_DECOMPRESS_METHODDEF + ZLIB_DECOMPRESSOBJ_METHODDEF {NULL, NULL} };

AltStyle によって変換されたページ (->オリジナル) /