blob: ac826bd9b5986fe894eb8c2ab4ea9ec14295e647 [file] [log] [blame]
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"compress($self, data, /)\n"
7"--\n"
8"\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +02009"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 Storchaka92e8af62015-04-04 00:12:11 +030017 {"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_O, _bz2_BZ2Compressor_compress__doc__},
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020018
19static PyObject *
20_bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data);
21
22static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030023_bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *arg)
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020024{
25 PyObject *return_value = NULL;
26 Py_buffer data = {NULL, NULL};
27
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020028 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
29 goto exit;
30 }
31 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020032 _PyArg_BadArgument("compress", "argument", "contiguous buffer", arg);
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020033 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030034 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020035 return_value = _bz2_BZ2Compressor_compress_impl(self, &data);
36
37exit:
38 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030039 if (data.obj) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020040 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030041 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020042
43 return return_value;
44}
45
46PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080047"flush($self, /)\n"
48"--\n"
49"\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020050"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
59static PyObject *
60_bz2_BZ2Compressor_flush_impl(BZ2Compressor *self);
61
62static PyObject *
63_bz2_BZ2Compressor_flush(BZ2Compressor *self, PyObject *Py_UNUSED(ignored))
64{
65 return _bz2_BZ2Compressor_flush_impl(self);
66}
67
68PyDoc_STRVAR(_bz2_BZ2Compressor___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080069"BZ2Compressor(compresslevel=9, /)\n"
70"--\n"
71"\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020072"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
79static int
80_bz2_BZ2Compressor___init___impl(BZ2Compressor *self, int compresslevel);
81
82static int
83_bz2_BZ2Compressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
84{
85 int return_value = -1;
86 int compresslevel = 9;
87
Larry Hastingsf0537e82014-01-25 22:01:12 -080088 if ((Py_TYPE(self) == &BZ2Compressor_Type) &&
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030089 !_PyArg_NoKeywords("BZ2Compressor", kwargs)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020090 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030091 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020092 if (!_PyArg_CheckPositional("BZ2Compressor", PyTuple_GET_SIZE(args), 0, 1)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +020093 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030094 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020095 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 }
107skip_optional:
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200108 return_value = _bz2_BZ2Compressor___init___impl((BZ2Compressor *)self, compresslevel);
109
110exit:
111 return return_value;
112}
113
114PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__,
Antoine Pitroue71258a2015-02-26 13:08:07 +0100115"decompress($self, /, data, max_length=-1)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -0800116"--\n"
117"\n"
Antoine Pitroue71258a2015-02-26 13:08:07 +0100118"Decompress *data*, returning uncompressed data as bytes.\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200119"\n"
Antoine Pitroue71258a2015-02-26 13:08:07 +0100120"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 Storchaka1bc4bb22014-01-25 12:07:57 +0200124"\n"
Antoine Pitroue71258a2015-02-26 13:08:07 +0100125"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 Storchaka1bc4bb22014-01-25 12:07:57 +0200132
133#define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200134 {"decompress", (PyCFunction)(void(*)(void))_bz2_BZ2Decompressor_decompress, METH_FASTCALL|METH_KEYWORDS, _bz2_BZ2Decompressor_decompress__doc__},
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200135
136static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400137_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
138 Py_ssize_t max_length);
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200139
140static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200141_bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200142{
143 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300144 static const char * const _keywords[] = {"data", "max_length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200145 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 Storchaka1bc4bb22014-01-25 12:07:57 +0200148 Py_buffer data = {NULL, NULL};
Antoine Pitroue71258a2015-02-26 13:08:07 +0100149 Py_ssize_t max_length = -1;
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200150
Serhiy Storchaka31913912019-03-14 10:32:22 +0200151 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
152 if (!args) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200153 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300154 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200155 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
156 goto exit;
157 }
158 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200159 _PyArg_BadArgument("decompress", "argument 'data'", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200160 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 }
182skip_optional_pos:
Antoine Pitroue71258a2015-02-26 13:08:07 +0100183 return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data, max_length);
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200184
185exit:
186 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300187 if (data.obj) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200188 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300189 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200190
191 return return_value;
192}
193
194PyDoc_STRVAR(_bz2_BZ2Decompressor___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800195"BZ2Decompressor()\n"
196"--\n"
197"\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200198"Create a decompressor object for decompressing data incrementally.\n"
199"\n"
200"For one-shot decompression, use the decompress() function instead.");
201
202static int
203_bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self);
204
205static int
206_bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
207{
208 int return_value = -1;
209
Larry Hastingsf0537e82014-01-25 22:01:12 -0800210 if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300211 !_PyArg_NoPositional("BZ2Decompressor", args)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200212 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300213 }
Larry Hastingsf0537e82014-01-25 22:01:12 -0800214 if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300215 !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200216 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300217 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200218 return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self);
219
220exit:
221 return return_value;
222}
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200223/*[clinic end generated code: output=ec3d1b3652c98823 input=a9049054013a1b77]*/