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