1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9
10
11 PyDoc_STRVAR(_bz2_BZ2Compressor_compress__doc__,
12 "compress($self, data, /)\n"
13 "--\n"
14 "\n"
15 "Provide data to the compressor object.\n"
16 "\n"
17 "Returns a chunk of compressed data if possible, or b\'\' otherwise.\n"
18 "\n"
19 "When you have finished providing data to the compressor, call the\n"
20 "flush() method to finish the compression process.");
21
22 #define _BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF \
23 {"compress", (PyCFunction)_bz2_BZ2Compressor_compress, METH_O, _bz2_BZ2Compressor_compress__doc__},
24
25 static PyObject *
26 _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data);
27
28 static PyObject *
29 _bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *arg)
30 {
31 PyObject *return_value = NULL;
32 Py_buffer data = {NULL, NULL};
33
34 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
35 goto exit;
36 }
37 if (!PyBuffer_IsContiguous(&data, 'C')) {
38 _PyArg_BadArgument("compress", "argument", "contiguous buffer", arg);
39 goto exit;
40 }
41 return_value = _bz2_BZ2Compressor_compress_impl(self, &data);
42
43 exit:
44 /* Cleanup for data */
45 if (data.obj) {
46 PyBuffer_Release(&data);
47 }
48
49 return return_value;
50 }
51
52 PyDoc_STRVAR(_bz2_BZ2Compressor_flush__doc__,
53 "flush($self, /)\n"
54 "--\n"
55 "\n"
56 "Finish the compression process.\n"
57 "\n"
58 "Returns the compressed data left in internal buffers.\n"
59 "\n"
60 "The compressor object may not be used after this method is called.");
61
62 #define _BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF \
63 {"flush", (PyCFunction)_bz2_BZ2Compressor_flush, METH_NOARGS, _bz2_BZ2Compressor_flush__doc__},
64
65 static PyObject *
66 _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self);
67
68 static PyObject *
69 _bz2_BZ2Compressor_flush(BZ2Compressor *self, PyObject *Py_UNUSED(ignored))
70 {
71 return _bz2_BZ2Compressor_flush_impl(self);
72 }
73
74 PyDoc_STRVAR(_bz2_BZ2Compressor__doc__,
75 "BZ2Compressor(compresslevel=9, /)\n"
76 "--\n"
77 "\n"
78 "Create a compressor object for compressing data incrementally.\n"
79 "\n"
80 " compresslevel\n"
81 " Compression level, as a number between 1 and 9.\n"
82 "\n"
83 "For one-shot compression, use the compress() function instead.");
84
85 static PyObject *
86 _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel);
87
88 static PyObject *
89 _bz2_BZ2Compressor(PyTypeObject *type, PyObject *args, PyObject *kwargs)
90 {
91 PyObject *return_value = NULL;
92 PyTypeObject *base_tp = clinic_state()->bz2_compressor_type;
93 int compresslevel = 9;
94
95 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
96 !_PyArg_NoKeywords("BZ2Compressor", kwargs)) {
97 goto exit;
98 }
99 if (!_PyArg_CheckPositional("BZ2Compressor", PyTuple_GET_SIZE(args), 0, 1)) {
100 goto exit;
101 }
102 if (PyTuple_GET_SIZE(args) < 1) {
103 goto skip_optional;
104 }
105 compresslevel = _PyLong_AsInt(PyTuple_GET_ITEM(args, 0));
106 if (compresslevel == -1 && PyErr_Occurred()) {
107 goto exit;
108 }
109 skip_optional:
110 return_value = _bz2_BZ2Compressor_impl(type, compresslevel);
111
112 exit:
113 return return_value;
114 }
115
116 PyDoc_STRVAR(_bz2_BZ2Decompressor_decompress__doc__,
117 "decompress($self, /, data, max_length=-1)\n"
118 "--\n"
119 "\n"
120 "Decompress *data*, returning uncompressed data as bytes.\n"
121 "\n"
122 "If *max_length* is nonnegative, returns at most *max_length* bytes of\n"
123 "decompressed data. If this limit is reached and further output can be\n"
124 "produced, *self.needs_input* will be set to ``False``. In this case, the next\n"
125 "call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n"
126 "\n"
127 "If all of the input data was decompressed and returned (either because this\n"
128 "was less than *max_length* bytes, or because *max_length* was negative),\n"
129 "*self.needs_input* will be set to True.\n"
130 "\n"
131 "Attempting to decompress data after the end of stream is reached raises an\n"
132 "EOFError. Any data found after the end of the stream is ignored and saved in\n"
133 "the unused_data attribute.");
134
135 #define _BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF \
136 {"decompress", _PyCFunction_CAST(_bz2_BZ2Decompressor_decompress), METH_FASTCALL|METH_KEYWORDS, _bz2_BZ2Decompressor_decompress__doc__},
137
138 static PyObject *
139 _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
140 Py_ssize_t max_length);
141
142 static PyObject *
143 _bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
144 {
145 PyObject *return_value = NULL;
146 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
147
148 #define NUM_KEYWORDS 2
149 static struct {
150 PyGC_Head _this_is_not_used;
151 PyObject_VAR_HEAD
152 PyObject *ob_item[NUM_KEYWORDS];
153 } _kwtuple = {
154 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
155 .ob_item = { &_Py_ID(data), &_Py_ID(max_length), },
156 };
157 #undef NUM_KEYWORDS
158 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
159
160 #else // !Py_BUILD_CORE
161 # define KWTUPLE NULL
162 #endif // !Py_BUILD_CORE
163
164 static const char * const _keywords[] = {"data", "max_length", NULL};
165 static _PyArg_Parser _parser = {
166 .keywords = _keywords,
167 .fname = "decompress",
168 .kwtuple = KWTUPLE,
169 };
170 #undef KWTUPLE
171 PyObject *argsbuf[2];
172 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
173 Py_buffer data = {NULL, NULL};
174 Py_ssize_t max_length = -1;
175
176 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
177 if (!args) {
178 goto exit;
179 }
180 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
181 goto exit;
182 }
183 if (!PyBuffer_IsContiguous(&data, 'C')) {
184 _PyArg_BadArgument("decompress", "argument 'data'", "contiguous buffer", args[0]);
185 goto exit;
186 }
187 if (!noptargs) {
188 goto skip_optional_pos;
189 }
190 {
191 Py_ssize_t ival = -1;
192 PyObject *iobj = _PyNumber_Index(args[1]);
193 if (iobj != NULL) {
194 ival = PyLong_AsSsize_t(iobj);
195 Py_DECREF(iobj);
196 }
197 if (ival == -1 && PyErr_Occurred()) {
198 goto exit;
199 }
200 max_length = ival;
201 }
202 skip_optional_pos:
203 return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data, max_length);
204
205 exit:
206 /* Cleanup for data */
207 if (data.obj) {
208 PyBuffer_Release(&data);
209 }
210
211 return return_value;
212 }
213
214 PyDoc_STRVAR(_bz2_BZ2Decompressor__doc__,
215 "BZ2Decompressor()\n"
216 "--\n"
217 "\n"
218 "Create a decompressor object for decompressing data incrementally.\n"
219 "\n"
220 "For one-shot decompression, use the decompress() function instead.");
221
222 static PyObject *
223 _bz2_BZ2Decompressor_impl(PyTypeObject *type);
224
225 static PyObject *
226 _bz2_BZ2Decompressor(PyTypeObject *type, PyObject *args, PyObject *kwargs)
227 {
228 PyObject *return_value = NULL;
229 PyTypeObject *base_tp = clinic_state()->bz2_decompressor_type;
230
231 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
232 !_PyArg_NoPositional("BZ2Decompressor", args)) {
233 goto exit;
234 }
235 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
236 !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) {
237 goto exit;
238 }
239 return_value = _bz2_BZ2Decompressor_impl(type);
240
241 exit:
242 return return_value;
243 }
244 /*[clinic end generated code: output=805400e4805098ec input=a9049054013a1b77]*/