diff -r f1f707dd7cae Modules/_lzmamodule.c --- a/Modules/_lzmamodule.c Wed Jan 08 16:01:42 2014 +0100 +++ b/Modules/_lzmamodule.c Wed Jan 08 23:22:27 2014 +0200 @@ -298,36 +298,37 @@ return options; } -static void * -parse_filter_spec(lzma_filter *f, PyObject *spec) +static int +lzma_filter_converter(PyObject *spec, void *ptr) { + lzma_filter *f = (lzma_filter *)ptr; PyObject *id_obj; if (!PyMapping_Check(spec)) { PyErr_SetString(PyExc_TypeError, "Filter specifier must be a dict or dict-like object"); - return NULL; + return 0; } id_obj = PyMapping_GetItemString(spec, "id"); if (id_obj == NULL) { if (PyErr_ExceptionMatches(PyExc_KeyError)) PyErr_SetString(PyExc_ValueError, "Filter specifier must have an \"id\" entry"); - return NULL; + return 0; } f->id = PyLong_AsUnsignedLongLong(id_obj); Py_DECREF(id_obj); if (PyErr_Occurred()) - return NULL; + return 0; switch (f->id) { case LZMA_FILTER_LZMA1: case LZMA_FILTER_LZMA2: f->options = parse_filter_spec_lzma(spec); - return f->options; + return f->options != NULL; case LZMA_FILTER_DELTA: f->options = parse_filter_spec_delta(spec); - return f->options; + return f->options != NULL; case LZMA_FILTER_X86: case LZMA_FILTER_POWERPC: case LZMA_FILTER_IA64: @@ -335,10 +336,10 @@ case LZMA_FILTER_ARMTHUMB: case LZMA_FILTER_SPARC: f->options = parse_filter_spec_bcj(spec); - return f->options; + return f->options != NULL; default: PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); - return NULL; + return 0; } } @@ -369,7 +370,7 @@ for (i = 0; i < num_filters; i++) { int ok = 1; PyObject *spec = PySequence_GetItem(filterspecs, i); - if (spec == NULL || parse_filter_spec(&filters[i], spec) == NULL) + if (spec == NULL || !lzma_filter_converter(spec, &filters[i])) ok = 0; Py_XDECREF(spec); if (!ok) { @@ -468,6 +469,33 @@ } +/*[clinic input] +module lzma +class lzma.LZMACompressor +class lzma.LZMADecompressor +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[python input] + +class lzma_vli_converter(CConverter): + type = 'lzma_vli' + converter = 'lzma_vli_converter' + +class lzma_filter_converter(CConverter): + type = 'lzma_filter' + converter = 'lzma_filter_converter' + c_default = c_ignored_default = "{LZMA_VLI_UNKNOWN, NULL}" + + def cleanup(self): + name = ensure_legal_c_identifier(self.name) + return "".join(["if (", name, ".id != LZMA_VLI_UNKNOWN)\n", + " PyMem_Free(", name, ".options);\n"]) + +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + + /* LZMACompressor class. */ static PyObject * @@ -512,44 +540,110 @@ return NULL; } -PyDoc_STRVAR(Compressor_compress_doc, -"compress(data) -> bytes\n" +/*[clinic input] +lzma.LZMACompressor.compress + + self: self(type="Compressor *") + data: Py_buffer + / + +Provide data to the compressor object. + +Returns a chunk of compressed data if possible, or b'' otherwise. + +When you have finished providing data to the compressor, call the +flush() method to finish the compression process. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma_LZMACompressor_compress__doc__, +"compress(data)\n" +"Provide data to the compressor object.\n" "\n" -"Provide data to the compressor object. Returns a chunk of\n" -"compressed data if possible, or b\"\" otherwise.\n" +"Returns a chunk of compressed data if possible, or b\'\' otherwise.\n" "\n" "When you have finished providing data to the compressor, call the\n" -"flush() method to finish the conversion process.\n"); +"flush() method to finish the compression process."); + +#define LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF \ + {"compress", (PyCFunction)lzma_LZMACompressor_compress, METH_VARARGS, lzma_LZMACompressor_compress__doc__}, static PyObject * -Compressor_compress(Compressor *self, PyObject *args) +lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data); + +static PyObject * +lzma_LZMACompressor_compress(PyObject *self, PyObject *args) { - Py_buffer buffer; + 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 = lzma_LZMACompressor_compress_impl((Compressor *)self, &data); + +exit: + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data) +/*[clinic end generated code: checksum=15cf4ce90ff18ea337a1db26d2b20edffeb4116b]*/ +{ PyObject *result = NULL; - if (!PyArg_ParseTuple(args, "y*:compress", &buffer)) - return NULL; - ACQUIRE_LOCK(self); if (self->flushed) PyErr_SetString(PyExc_ValueError, "Compressor has been flushed"); else - result = compress(self, buffer.buf, buffer.len, LZMA_RUN); + result = compress(self, data->buf, data->len, LZMA_RUN); RELEASE_LOCK(self); - PyBuffer_Release(&buffer); return result; } -PyDoc_STRVAR(Compressor_flush_doc, -"flush() -> bytes\n" +/*[clinic input] +lzma.LZMACompressor.flush + + self: self(type="Compressor *") + +Finish the compression process. + +Returns the compressed data left in internal buffers. + +The compressor object may not be used after this method is called. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma_LZMACompressor_flush__doc__, +"flush()\n" +"Finish the compression process.\n" "\n" -"Finish the compression process. Returns the compressed data left\n" -"in internal buffers.\n" +"Returns the compressed data left in internal buffers.\n" "\n" -"The compressor object cannot be used after this method is called.\n"); +"The compressor object may not be used after this method is called."); + +#define LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF \ + {"flush", (PyCFunction)lzma_LZMACompressor_flush, METH_NOARGS, lzma_LZMACompressor_flush__doc__}, static PyObject * -Compressor_flush(Compressor *self, PyObject *noargs) +lzma_LZMACompressor_flush_impl(Compressor *self); + +static PyObject * +lzma_LZMACompressor_flush(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = lzma_LZMACompressor_flush_impl((Compressor *)self); + + return return_value; +} + +static PyObject * +lzma_LZMACompressor_flush_impl(Compressor *self) +/*[clinic end generated code: checksum=e343d80db15f2225d52c91cc20bea7fad8547f62]*/ { PyObject *result = NULL; @@ -739,10 +833,8 @@ } static PyMethodDef Compressor_methods[] = { - {"compress", (PyCFunction)Compressor_compress, METH_VARARGS, - Compressor_compress_doc}, - {"flush", (PyCFunction)Compressor_flush, METH_NOARGS, - Compressor_flush_doc}, + LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF + LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS}, {NULL} }; @@ -872,32 +964,70 @@ return NULL; } -PyDoc_STRVAR(Decompressor_decompress_doc, -"decompress(data) -> bytes\n" +/*[clinic input] +lzma.LZMADecompressor.decompress + + self: self(type="Decompressor *") + data: Py_buffer + / + +Provide data to the decompressor object. + +Returns a chunk of decompressed data if possible, or b'' otherwise. + +Attempting to decompress data after the end of stream is reached +raises an EOFError. Any data found after the end of the stream +is ignored and saved in the unused_data attribute. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma_LZMADecompressor_decompress__doc__, +"decompress(data)\n" +"Provide data to the decompressor object.\n" "\n" -"Provide data to the decompressor object. Returns a chunk of\n" -"decompressed data if possible, or b\"\" otherwise.\n" +"Returns a chunk of decompressed data if possible, or b\'\' otherwise.\n" "\n" -"Attempting to decompress data after the end of the stream is\n" -"reached raises an EOFError. Any data found after the end of the\n" -"stream is ignored, and saved in the unused_data attribute.\n"); +"Attempting to decompress data after the end of stream is reached\n" +"raises an EOFError. Any data found after the end of the stream\n" +"is ignored and saved in the unused_data attribute."); + +#define LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF \ + {"decompress", (PyCFunction)lzma_LZMADecompressor_decompress, METH_VARARGS, lzma_LZMADecompressor_decompress__doc__}, static PyObject * -Decompressor_decompress(Decompressor *self, PyObject *args) +lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data); + +static PyObject * +lzma_LZMADecompressor_decompress(PyObject *self, PyObject *args) { - Py_buffer buffer; + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:decompress", + &data)) + goto exit; + return_value = lzma_LZMADecompressor_decompress_impl((Decompressor *)self, &data); + +exit: + /* Cleanup for data */ + if (data.buf) + PyBuffer_Release(&data); + + return return_value; +} + +static PyObject * +lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data) +/*[clinic end generated code: checksum=20b1f8dbbbdcef27b9afa2635747d328ab0f2d9f]*/ +{ PyObject *result = NULL; - if (!PyArg_ParseTuple(args, "y*:decompress", &buffer)) - return NULL; - ACQUIRE_LOCK(self); if (self->eof) PyErr_SetString(PyExc_EOFError, "Already at end of stream"); else - result = decompress(self, buffer.buf, buffer.len); + result = decompress(self, data->buf, data->len); RELEASE_LOCK(self); - PyBuffer_Release(&buffer); return result; } @@ -1034,8 +1164,7 @@ } static PyMethodDef Decompressor_methods[] = { - {"decompress", (PyCFunction)Decompressor_decompress, METH_VARARGS, - Decompressor_decompress_doc}, + LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS}, {NULL} }; @@ -1124,48 +1253,102 @@ /* Module-level functions. */ -PyDoc_STRVAR(is_check_supported_doc, -"is_check_supported(check_id) -> bool\n" -"\n" +/*[clinic input] +lzma.is_check_supported + check_id: int + / + +Test whether the given integrity check is supported. + +Always returns True for CHECK_NONE and CHECK_CRC32. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma_is_check_supported__doc__, +"is_check_supported(check_id)\n" "Test whether the given integrity check is supported.\n" "\n" -"Always returns True for CHECK_NONE and CHECK_CRC32.\n"); +"Always returns True for CHECK_NONE and CHECK_CRC32."); + +#define LZMA_IS_CHECK_SUPPORTED_METHODDEF \ + {"is_check_supported", (PyCFunction)lzma_is_check_supported, METH_VARARGS, lzma_is_check_supported__doc__}, static PyObject * -is_check_supported(PyObject *self, PyObject *args) +lzma_is_check_supported_impl(PyModuleDef *module, int check_id); + +static PyObject * +lzma_is_check_supported(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; int check_id; - if (!PyArg_ParseTuple(args, "i:is_check_supported", &check_id)) - return NULL; + if (!PyArg_ParseTuple(args, + "i:is_check_supported", + &check_id)) + goto exit; + return_value = lzma_is_check_supported_impl(module, check_id); +exit: + return return_value; +} + +static PyObject * +lzma_is_check_supported_impl(PyModuleDef *module, int check_id) +/*[clinic end generated code: checksum=189f759f6cd139e8ac27065cfbd24e65643ef4bf]*/ +{ return PyBool_FromLong(lzma_check_is_supported(check_id)); } -PyDoc_STRVAR(_encode_filter_properties_doc, -"_encode_filter_properties(filter) -> bytes\n" +/*[clinic input] +lzma._encode_filter_properties + filter: lzma_filter + / + +Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). + +The result does not include the filter ID itself, only the options. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma__encode_filter_properties__doc__, +"_encode_filter_properties(filter)\n" +"Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).\n" "\n" -"Return a bytes object encoding the options (properties) of the filter\n" -"specified by *filter* (a dict).\n" -"\n" -"The result does not include the filter ID itself, only the options.\n"); +"The result does not include the filter ID itself, only the options."); + +#define LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF \ + {"_encode_filter_properties", (PyCFunction)lzma__encode_filter_properties, METH_VARARGS, lzma__encode_filter_properties__doc__}, static PyObject * -_encode_filter_properties(PyObject *self, PyObject *args) +lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter); + +static PyObject * +lzma__encode_filter_properties(PyModuleDef *module, PyObject *args) { - PyObject *filterspec; - lzma_filter filter; + PyObject *return_value = NULL; + lzma_filter filter = {LZMA_VLI_UNKNOWN, NULL}; + + if (!PyArg_ParseTuple(args, + "O&:_encode_filter_properties", + lzma_filter_converter, &filter)) + goto exit; + return_value = lzma__encode_filter_properties_impl(module, filter); + +exit: + /* Cleanup for filter */ + if (filter.id != LZMA_VLI_UNKNOWN) + PyMem_Free(filter.options); + + return return_value; +} + +static PyObject * +lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter) +/*[clinic end generated code: checksum=46536130607530196fd8aef3587ab6c83ae01eba]*/ +{ lzma_ret lzret; uint32_t encoded_size; PyObject *result = NULL; - if (!PyArg_ParseTuple(args, "O:_encode_filter_properties", &filterspec)) - return NULL; - - if (parse_filter_spec(&filter, filterspec) == NULL) - return NULL; - lzret = lzma_properties_size(&encoded_size, &filter); if (catch_lzma_error(lzret)) goto error; @@ -1179,37 +1362,69 @@ if (catch_lzma_error(lzret)) goto error; - PyMem_Free(filter.options); return result; error: Py_XDECREF(result); - PyMem_Free(filter.options); return NULL; } -PyDoc_STRVAR(_decode_filter_properties_doc, -"_decode_filter_properties(filter_id, encoded_props) -> dict\n" +/*[clinic input] +lzma._decode_filter_properties + filter_id: lzma_vli + encoded_props: Py_buffer + / + +Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). + +The result does not include the filter ID itself, only the options. +[clinic start generated code]*/ + +PyDoc_STRVAR(lzma__decode_filter_properties__doc__, +"_decode_filter_properties(filter_id, encoded_props)\n" +"Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).\n" "\n" -"Return a dict describing a filter with ID *filter_id*, and options\n" -"(properties) decoded from the bytes object *encoded_props*.\n"); +"The result does not include the filter ID itself, only the options."); + +#define LZMA__DECODE_FILTER_PROPERTIES_METHODDEF \ + {"_decode_filter_properties", (PyCFunction)lzma__decode_filter_properties, METH_VARARGS, lzma__decode_filter_properties__doc__}, static PyObject * -_decode_filter_properties(PyObject *self, PyObject *args) +lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props); + +static PyObject * +lzma__decode_filter_properties(PyModuleDef *module, PyObject *args) { - Py_buffer encoded_props; + PyObject *return_value = NULL; + lzma_vli filter_id; + Py_buffer encoded_props = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "O&y*:_decode_filter_properties", + lzma_vli_converter, &filter_id, &encoded_props)) + goto exit; + return_value = lzma__decode_filter_properties_impl(module, filter_id, &encoded_props); + +exit: + /* Cleanup for encoded_props */ + if (encoded_props.buf) + PyBuffer_Release(&encoded_props); + + return return_value; +} + +static PyObject * +lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props) +/*[clinic end generated code: checksum=ff80ebe83342b224dc0edc084a881432439a881f]*/ +{ lzma_filter filter; lzma_ret lzret; PyObject *result = NULL; - - if (!PyArg_ParseTuple(args, "O&y*:_decode_filter_properties", - lzma_vli_converter, &filter.id, &encoded_props)) - return NULL; + filter.id = filter_id; lzret = lzma_properties_decode( - &filter, NULL, encoded_props.buf, encoded_props.len); - PyBuffer_Release(&encoded_props); + &filter, NULL, encoded_props->buf, encoded_props->len); if (catch_lzma_error(lzret)) return NULL; @@ -1225,12 +1440,9 @@ /* Module initialization. */ static PyMethodDef module_methods[] = { - {"is_check_supported", (PyCFunction)is_check_supported, - METH_VARARGS, is_check_supported_doc}, - {"_encode_filter_properties", (PyCFunction)_encode_filter_properties, - METH_VARARGS, _encode_filter_properties_doc}, - {"_decode_filter_properties", (PyCFunction)_decode_filter_properties, - METH_VARARGS, _decode_filter_properties_doc}, + LZMA_IS_CHECK_SUPPORTED_METHODDEF + LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF + LZMA__DECODE_FILTER_PROPERTIES_METHODDEF {NULL} };