Debian Codegraph @

src/c/compression_zstd/clinic/compressor.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #include "backports_zstd_edits.h"
6
7 PyDoc_STRVAR(_zstd_ZstdCompressor_new__doc__,
8 "ZstdCompressor(level=None, options=None, zstd_dict=None)\n"
9 "--\n"
10 "\n"
11 "Create a compressor object for compressing data incrementally.\n"
12 "\n"
13 " level\n"
14 " The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.\n"
15 " options\n"
16 " A dict object that contains advanced compression parameters.\n"
17 " zstd_dict\n"
18 " A ZstdDict object, a pre-trained Zstandard dictionary.\n"
19 "\n"
20 "Thread-safe at method level. For one-shot compression, use the compress()\n"
21 "function instead.");
22
23 static PyObject *
24 _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
25 PyObject *options, PyObject *zstd_dict);
26
27 static PyObject *
28 _zstd_ZstdCompressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
29 {
30 PyObject *return_value = NULL;
31 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
32
33 #define NUM_KEYWORDS 3
34 static struct {
35 PyGC_Head _this_is_not_used;
36 PyObject_VAR_HEAD
37 Py_hash_t ob_hash;
38 PyObject *ob_item[NUM_KEYWORDS];
39 } _kwtuple = {
40 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
41 .ob_hash = -1,
42 .ob_item = { &_Py_ID(level), &_Py_ID(options), &_Py_ID(zstd_dict), },
43 };
44 #undef NUM_KEYWORDS
45 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
46
47 #else // !Py_BUILD_CORE
48 # define KWTUPLE NULL
49 #endif // !Py_BUILD_CORE
50
51 static const char * const _keywords[] = {"level", "options", "zstd_dict", NULL};
52 static _PyArg_Parser _parser = {
53 .keywords = _keywords,
54 .fname = "ZstdCompressor",
55 .kwtuple = KWTUPLE,
56 };
57 #undef KWTUPLE
58 PyObject *argsbuf[3];
59 PyObject * const *fastargs;
60 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
61 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
62 PyObject *level = Py_None;
63 PyObject *options = Py_None;
64 PyObject *zstd_dict = Py_None;
65
66 fastargs = BACKPORTSZSTD__PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
67 /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
68 if (!fastargs) {
69 goto exit;
70 }
71 if (!noptargs) {
72 goto skip_optional_pos;
73 }
74 if (fastargs[0]) {
75 level = fastargs[0];
76 if (!--noptargs) {
77 goto skip_optional_pos;
78 }
79 }
80 if (fastargs[1]) {
81 options = fastargs[1];
82 if (!--noptargs) {
83 goto skip_optional_pos;
84 }
85 }
86 zstd_dict = fastargs[2];
87 skip_optional_pos:
88 return_value = _zstd_ZstdCompressor_new_impl(type, level, options, zstd_dict);
89
90 exit:
91 return return_value;
92 }
93
94 PyDoc_STRVAR(_zstd_ZstdCompressor_compress__doc__,
95 "compress($self, /, data, mode=ZstdCompressor.CONTINUE)\n"
96 "--\n"
97 "\n"
98 "Provide data to the compressor object.\n"
99 "\n"
100 " mode\n"
101 " Can be these 3 values ZstdCompressor.CONTINUE,\n"
102 " ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME\n"
103 "\n"
104 "Return a chunk of compressed data if possible, or b\'\' otherwise. When you have\n"
105 "finished providing data to the compressor, call the flush() method to finish\n"
106 "the compression process.");
107
108 #define _ZSTD_ZSTDCOMPRESSOR_COMPRESS_METHODDEF \
109 {"compress", _PyCFunction_CAST(_zstd_ZstdCompressor_compress), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdCompressor_compress__doc__},
110
111 static PyObject *
112 _zstd_ZstdCompressor_compress_impl(ZstdCompressor *self, Py_buffer *data,
113 int mode);
114
115 static PyObject *
116 _zstd_ZstdCompressor_compress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
117 {
118 PyObject *return_value = NULL;
119 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
120
121 #define NUM_KEYWORDS 2
122 static struct {
123 PyGC_Head _this_is_not_used;
124 PyObject_VAR_HEAD
125 Py_hash_t ob_hash;
126 PyObject *ob_item[NUM_KEYWORDS];
127 } _kwtuple = {
128 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
129 .ob_hash = -1,
130 .ob_item = { &_Py_ID(data), &_Py_ID(mode), },
131 };
132 #undef NUM_KEYWORDS
133 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
134
135 #else // !Py_BUILD_CORE
136 # define KWTUPLE NULL
137 #endif // !Py_BUILD_CORE
138
139 static const char * const _keywords[] = {"data", "mode", NULL};
140 static _PyArg_Parser _parser = {
141 .keywords = _keywords,
142 .fname = "compress",
143 .kwtuple = KWTUPLE,
144 };
145 #undef KWTUPLE
146 PyObject *argsbuf[2];
147 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
148 Py_buffer data = {NULL, NULL};
149 int mode = ZSTD_e_continue;
150
151 args = BACKPORTSZSTD__PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
152 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
153 if (!args) {
154 goto exit;
155 }
156 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
157 goto exit;
158 }
159 if (!noptargs) {
160 goto skip_optional_pos;
161 }
162 mode = PyLong_AsInt(args[1]);
163 if (mode == -1 && PyErr_Occurred()) {
164 goto exit;
165 }
166 skip_optional_pos:
167 return_value = _zstd_ZstdCompressor_compress_impl((ZstdCompressor *)self, &data, mode);
168
169 exit:
170 /* Cleanup for data */
171 if (data.obj) {
172 PyBuffer_Release(&data);
173 }
174
175 return return_value;
176 }
177
178 PyDoc_STRVAR(_zstd_ZstdCompressor_flush__doc__,
179 "flush($self, /, mode=ZstdCompressor.FLUSH_FRAME)\n"
180 "--\n"
181 "\n"
182 "Finish the compression process.\n"
183 "\n"
184 " mode\n"
185 " Can be these 2 values ZstdCompressor.FLUSH_FRAME,\n"
186 " ZstdCompressor.FLUSH_BLOCK\n"
187 "\n"
188 "Flush any remaining data left in internal buffers. Since Zstandard data\n"
189 "consists of one or more independent frames, the compressor object can still\n"
190 "be used after this method is called.");
191
192 #define _ZSTD_ZSTDCOMPRESSOR_FLUSH_METHODDEF \
193 {"flush", _PyCFunction_CAST(_zstd_ZstdCompressor_flush), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdCompressor_flush__doc__},
194
195 static PyObject *
196 _zstd_ZstdCompressor_flush_impl(ZstdCompressor *self, int mode);
197
198 static PyObject *
199 _zstd_ZstdCompressor_flush(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
200 {
201 PyObject *return_value = NULL;
202 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
203
204 #define NUM_KEYWORDS 1
205 static struct {
206 PyGC_Head _this_is_not_used;
207 PyObject_VAR_HEAD
208 Py_hash_t ob_hash;
209 PyObject *ob_item[NUM_KEYWORDS];
210 } _kwtuple = {
211 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
212 .ob_hash = -1,
213 .ob_item = { &_Py_ID(mode), },
214 };
215 #undef NUM_KEYWORDS
216 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
217
218 #else // !Py_BUILD_CORE
219 # define KWTUPLE NULL
220 #endif // !Py_BUILD_CORE
221
222 static const char * const _keywords[] = {"mode", NULL};
223 static _PyArg_Parser _parser = {
224 .keywords = _keywords,
225 .fname = "flush",
226 .kwtuple = KWTUPLE,
227 };
228 #undef KWTUPLE
229 PyObject *argsbuf[1];
230 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
231 int mode = ZSTD_e_end;
232
233 args = BACKPORTSZSTD__PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
234 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
235 if (!args) {
236 goto exit;
237 }
238 if (!noptargs) {
239 goto skip_optional_pos;
240 }
241 mode = PyLong_AsInt(args[0]);
242 if (mode == -1 && PyErr_Occurred()) {
243 goto exit;
244 }
245 skip_optional_pos:
246 return_value = _zstd_ZstdCompressor_flush_impl((ZstdCompressor *)self, mode);
247
248 exit:
249 return return_value;
250 }
251
252 PyDoc_STRVAR(_zstd_ZstdCompressor_set_pledged_input_size__doc__,
253 "set_pledged_input_size($self, size, /)\n"
254 "--\n"
255 "\n"
256 "Set the uncompressed content size to be written into the frame header.\n"
257 "\n"
258 " size\n"
259 " The size of the uncompressed data to be provided to the compressor.\n"
260 "\n"
261 "This method can be used to ensure the header of the frame about to be written\n"
262 "includes the size of the data, unless the CompressionParameter.content_size_flag\n"
263 "is set to False. If last_mode != FLUSH_FRAME, then a RuntimeError is raised.\n"
264 "\n"
265 "It is important to ensure that the pledged data size matches the actual data\n"
266 "size. If they do not match the compressed output data may be corrupted and the\n"
267 "final chunk written may be lost.");
268
269 #define _ZSTD_ZSTDCOMPRESSOR_SET_PLEDGED_INPUT_SIZE_METHODDEF \
270 {"set_pledged_input_size", (PyCFunction)_zstd_ZstdCompressor_set_pledged_input_size, METH_O, _zstd_ZstdCompressor_set_pledged_input_size__doc__},
271
272 static PyObject *
273 _zstd_ZstdCompressor_set_pledged_input_size_impl(ZstdCompressor *self,
274 unsigned long long size);
275
276 static PyObject *
277 _zstd_ZstdCompressor_set_pledged_input_size(PyObject *self, PyObject *arg)
278 {
279 PyObject *return_value = NULL;
280 unsigned long long size;
281
282 if (!zstd_contentsize_converter(arg, &size)) {
283 goto exit;
284 }
285 return_value = _zstd_ZstdCompressor_set_pledged_input_size_impl((ZstdCompressor *)self, size);
286
287 exit:
288 return return_value;
289 }
290 /*[clinic end generated code: output=c1d5c2cf06a8becd input=a9049054013a1b77]*/