Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 6 | "compress($self, data, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 9 | "Provide data to the compressor object.\n" |
| 10 | "\n" |
| 11 | "Returns a chunk of compressed data if possible, or b\'\' otherwise.\n" |
| 12 | "\n" |
| 13 | "When you have finished providing data to the compressor, call the\n" |
| 14 | "flush() method to finish the compression process."); |
| 15 | |
| 16 | #define _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 17 | {"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_O, _bz2_BZ2Compressor_compress__doc__}, |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
| 20 | _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data); |
| 21 | |
| 22 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 23 | _bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *arg) |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 24 | { |
| 25 | PyObject *return_value = NULL; |
| 26 | Py_buffer data = {NULL, NULL}; |
| 27 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 28 | if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) { |
| 29 | goto exit; |
| 30 | } |
| 31 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 32 | _PyArg_BadArgument("compress", "argument", "contiguous buffer", arg); |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 33 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 34 | } |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 35 | return_value = _bz2_BZ2Compressor_compress_impl(self, &data); |
| 36 | |
| 37 | exit: |
| 38 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 39 | if (data.obj) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 40 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 41 | } |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 42 | |
| 43 | return return_value; |
| 44 | } |
| 45 | |
| 46 | PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 47 | "flush($self, /)\n" |
| 48 | "--\n" |
| 49 | "\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 50 | "Finish the compression process.\n" |
| 51 | "\n" |
| 52 | "Returns the compressed data left in internal buffers.\n" |
| 53 | "\n" |
| 54 | "The compressor object may not be used after this method is called."); |
| 55 | |
| 56 | #define _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF \ |
| 57 | {"flush", (PyCFunction)_bz2_BZ2Compressor_flush, METH_NOARGS, _bz2_BZ2Compressor_flush__doc__}, |
| 58 | |
| 59 | static PyObject * |
| 60 | _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self); |
| 61 | |
| 62 | static PyObject * |
| 63 | _bz2_BZ2Compressor_flush(BZ2Compressor *self, PyObject *Py_UNUSED(ignored)) |
| 64 | { |
| 65 | return _bz2_BZ2Compressor_flush_impl(self); |
| 66 | } |
| 67 | |
| 68 | PyDoc_STRVAR(_bz2_BZ2Compressor___init____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 69 | "BZ2Compressor(compresslevel=9, /)\n" |
| 70 | "--\n" |
| 71 | "\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 72 | "Create a compressor object for compressing data incrementally.\n" |
| 73 | "\n" |
| 74 | " compresslevel\n" |
| 75 | " Compression level, as a number between 1 and 9.\n" |
| 76 | "\n" |
| 77 | "For one-shot compression, use the compress() function instead."); |
| 78 | |
| 79 | static int |
| 80 | _bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel); |
| 81 | |
| 82 | static int |
| 83 | _bz2_BZ2Compressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 84 | { |
| 85 | int return_value = -1; |
| 86 | int compresslevel = 9; |
| 87 | |
Larry Hastings | f0537e8 | 2014-01-25 22:01:12 -0800 | [diff] [blame] | 88 | if ((Py_TYPE(self) == &BZ2Compressor_Type) && |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 89 | !_PyArg_NoKeywords("BZ2Compressor", kwargs)) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 90 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 91 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 92 | if (!_PyArg_CheckPositional("BZ2Compressor", PyTuple_GET_SIZE(args), 0, 1)) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 93 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 94 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 95 | if (PyTuple_GET_SIZE(args) < 1) { |
| 96 | goto skip_optional; |
| 97 | } |
| 98 | if (PyFloat_Check(PyTuple_GET_ITEM(args, 0))) { |
| 99 | PyErr_SetString(PyExc_TypeError, |
| 100 | "integer argument expected, got float" ); |
| 101 | goto exit; |
| 102 | } |
| 103 | compresslevel = _PyLong_AsInt(PyTuple_GET_ITEM(args, 0)); |
| 104 | if (compresslevel == -1 && PyErr_Occurred()) { |
| 105 | goto exit; |
| 106 | } |
| 107 | skip_optional: |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 108 | return_value = _bz2_BZ2Compressor___init___impl((BZ2Compressor *)self, compresslevel); |
| 109 | |
| 110 | exit: |
| 111 | return return_value; |
| 112 | } |
| 113 | |
| 114 | PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__, |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 115 | "decompress($self, /, data, max_length=-1)\n" |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 116 | "--\n" |
| 117 | "\n" |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 118 | "Decompress *data*, returning uncompressed data as bytes.\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 119 | "\n" |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 120 | "If *max_length* is nonnegative, returns at most *max_length* bytes of\n" |
| 121 | "decompressed data. If this limit is reached and further output can be\n" |
| 122 | "produced, *self.needs_input* will be set to ``False``. In this case, the next\n" |
| 123 | "call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 124 | "\n" |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 125 | "If all of the input data was decompressed and returned (either because this\n" |
| 126 | "was less than *max_length* bytes, or because *max_length* was negative),\n" |
| 127 | "*self.needs_input* will be set to True.\n" |
| 128 | "\n" |
| 129 | "Attempting to decompress data after the end of stream is reached raises an\n" |
| 130 | "EOFError. Any data found after the end of the stream is ignored and saved in\n" |
| 131 | "the unused_data attribute."); |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 132 | |
| 133 | #define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 134 | {"decompress", (PyCFunction)(void(*)(void))_bz2_BZ2Decompressor_decompress, METH_FASTCALL|METH_KEYWORDS, _bz2_BZ2Decompressor_decompress__doc__}, |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 135 | |
| 136 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 137 | _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data, |
| 138 | Py_ssize_t max_length); |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 139 | |
| 140 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 141 | _bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 142 | { |
| 143 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 144 | static const char * const _keywords[] = {"data", "max_length", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 145 | static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0}; |
| 146 | PyObject *argsbuf[2]; |
| 147 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 148 | Py_buffer data = {NULL, NULL}; |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 149 | Py_ssize_t max_length = -1; |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 150 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 151 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 152 | if (!args) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 153 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 154 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 155 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 156 | goto exit; |
| 157 | } |
| 158 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 159 | _PyArg_BadArgument("decompress", "argument 'data'", "contiguous buffer", args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 160 | goto exit; |
| 161 | } |
| 162 | if (!noptargs) { |
| 163 | goto skip_optional_pos; |
| 164 | } |
| 165 | if (PyFloat_Check(args[1])) { |
| 166 | PyErr_SetString(PyExc_TypeError, |
| 167 | "integer argument expected, got float" ); |
| 168 | goto exit; |
| 169 | } |
| 170 | { |
| 171 | Py_ssize_t ival = -1; |
| 172 | PyObject *iobj = PyNumber_Index(args[1]); |
| 173 | if (iobj != NULL) { |
| 174 | ival = PyLong_AsSsize_t(iobj); |
| 175 | Py_DECREF(iobj); |
| 176 | } |
| 177 | if (ival == -1 && PyErr_Occurred()) { |
| 178 | goto exit; |
| 179 | } |
| 180 | max_length = ival; |
| 181 | } |
| 182 | skip_optional_pos: |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 183 | return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data, max_length); |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 184 | |
| 185 | exit: |
| 186 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 187 | if (data.obj) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 188 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 189 | } |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 190 | |
| 191 | return return_value; |
| 192 | } |
| 193 | |
| 194 | PyDoc_STRVAR(_bz2_BZ2Decompressor___init____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 195 | "BZ2Decompressor()\n" |
| 196 | "--\n" |
| 197 | "\n" |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 198 | "Create a decompressor object for decompressing data incrementally.\n" |
| 199 | "\n" |
| 200 | "For one-shot decompression, use the decompress() function instead."); |
| 201 | |
| 202 | static int |
| 203 | _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self); |
| 204 | |
| 205 | static int |
| 206 | _bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 207 | { |
| 208 | int return_value = -1; |
| 209 | |
Larry Hastings | f0537e8 | 2014-01-25 22:01:12 -0800 | [diff] [blame] | 210 | if ((Py_TYPE(self) == &BZ2Decompressor_Type) && |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 211 | !_PyArg_NoPositional("BZ2Decompressor", args)) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 212 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 213 | } |
Larry Hastings | f0537e8 | 2014-01-25 22:01:12 -0800 | [diff] [blame] | 214 | if ((Py_TYPE(self) == &BZ2Decompressor_Type) && |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 215 | !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) { |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 216 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 217 | } |
Serhiy Storchaka | 1bc4bb2 | 2014-01-25 12:07:57 +0200 | [diff] [blame] | 218 | return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self); |
| 219 | |
| 220 | exit: |
| 221 | return return_value; |
| 222 | } |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 223 | /*[clinic end generated code: output=ec3d1b3652c98823 input=a9049054013a1b77]*/ |