blob: fb643338054112ac8910ee40c9bbf1c1dccef35b [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')) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020032 _PyArg_BadArgument("compress", 0, "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};
145 static _PyArg_Parser _parser = {"y*|n:decompress", _keywords, 0};
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200146 Py_buffer data = {NULL, NULL};
Antoine Pitroue71258a2015-02-26 13:08:07 +0100147 Py_ssize_t max_length = -1;
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200148
Victor Stinner3e1fad62017-01-17 01:29:01 +0100149 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300150 &data, &max_length)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200151 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300152 }
Antoine Pitroue71258a2015-02-26 13:08:07 +0100153 return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data, max_length);
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200154
155exit:
156 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300157 if (data.obj) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200158 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300159 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200160
161 return return_value;
162}
163
164PyDoc_STRVAR(_bz2_BZ2Decompressor___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800165"BZ2Decompressor()\n"
166"--\n"
167"\n"
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200168"Create a decompressor object for decompressing data incrementally.\n"
169"\n"
170"For one-shot decompression, use the decompress() function instead.");
171
172static int
173_bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self);
174
175static int
176_bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
177{
178 int return_value = -1;
179
Larry Hastingsf0537e82014-01-25 22:01:12 -0800180 if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300181 !_PyArg_NoPositional("BZ2Decompressor", args)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200182 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300183 }
Larry Hastingsf0537e82014-01-25 22:01:12 -0800184 if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300185 !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) {
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200186 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300187 }
Serhiy Storchaka1bc4bb22014-01-25 12:07:57 +0200188 return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self);
189
190exit:
191 return return_value;
192}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200193/*[clinic end generated code: output=892c6133e97ff840 input=a9049054013a1b77]*/