Debian Codegraph @

Modules/_zstd/clinic/decompressor.c.h

raw
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_UnpackKeywords()
11
12 PyDoc_STRVAR(_zstd_ZstdDecompressor_new__doc__,
13 "ZstdDecompressor(zstd_dict=None, options=None)\n"
14 "--\n"
15 "\n"
16 "Create a decompressor object for decompressing data incrementally.\n"
17 "\n"
18 " zstd_dict\n"
19 " A ZstdDict object, a pre-trained Zstandard dictionary.\n"
20 " options\n"
21 " A dict object that contains advanced decompression parameters.\n"
22 "\n"
23 "Thread-safe at method level. For one-shot decompression, use the\n"
24 "decompress() function instead.");
25
26 static PyObject *
27 _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
28 PyObject *options);
29
30 static PyObject *
31 _zstd_ZstdDecompressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
32 {
33 PyObject *return_value = NULL;
34 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
35
36 #define NUM_KEYWORDS 2
37 static struct {
38 PyGC_Head _this_is_not_used;
39 PyObject_VAR_HEAD
40 Py_hash_t ob_hash;
41 PyObject *ob_item[NUM_KEYWORDS];
42 } _kwtuple = {
43 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
44 .ob_hash = -1,
45 .ob_item = { &_Py_ID(zstd_dict), &_Py_ID(options), },
46 };
47 #undef NUM_KEYWORDS
48 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
49
50 #else // !Py_BUILD_CORE
51 # define KWTUPLE NULL
52 #endif // !Py_BUILD_CORE
53
54 static const char * const _keywords[] = {"zstd_dict", "options", NULL};
55 static _PyArg_Parser _parser = {
56 .keywords = _keywords,
57 .fname = "ZstdDecompressor",
58 .kwtuple = KWTUPLE,
59 };
60 #undef KWTUPLE
61 PyObject *argsbuf[2];
62 PyObject * const *fastargs;
63 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
64 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
65 PyObject *zstd_dict = Py_None;
66 PyObject *options = Py_None;
67
68 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
69 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
70 if (!fastargs) {
71 goto exit;
72 }
73 if (!noptargs) {
74 goto skip_optional_pos;
75 }
76 if (fastargs[0]) {
77 zstd_dict = fastargs[0];
78 if (!--noptargs) {
79 goto skip_optional_pos;
80 }
81 }
82 options = fastargs[1];
83 skip_optional_pos:
84 return_value = _zstd_ZstdDecompressor_new_impl(type, zstd_dict, options);
85
86 exit:
87 return return_value;
88 }
89
90 PyDoc_STRVAR(_zstd_ZstdDecompressor_unused_data__doc__,
91 "A bytes object of un-consumed input data.\n"
92 "\n"
93 "When ZstdDecompressor object stops after a frame is\n"
94 "decompressed, unused input data after the frame. Otherwise this\n"
95 "will be b\'\'.");
96 #if defined(_zstd_ZstdDecompressor_unused_data_DOCSTR)
97 # undef _zstd_ZstdDecompressor_unused_data_DOCSTR
98 #endif
99 #define _zstd_ZstdDecompressor_unused_data_DOCSTR _zstd_ZstdDecompressor_unused_data__doc__
100
101 #if !defined(_zstd_ZstdDecompressor_unused_data_DOCSTR)
102 # define _zstd_ZstdDecompressor_unused_data_DOCSTR NULL
103 #endif
104 #if defined(_ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF)
105 # undef _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF
106 # define _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF {"unused_data", (getter)_zstd_ZstdDecompressor_unused_data_get, (setter)_zstd_ZstdDecompressor_unused_data_set, _zstd_ZstdDecompressor_unused_data_DOCSTR},
107 #else
108 # define _ZSTD_ZSTDDECOMPRESSOR_UNUSED_DATA_GETSETDEF {"unused_data", (getter)_zstd_ZstdDecompressor_unused_data_get, NULL, _zstd_ZstdDecompressor_unused_data_DOCSTR},
109 #endif
110
111 static PyObject *
112 _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self);
113
114 static PyObject *
115 _zstd_ZstdDecompressor_unused_data_get(PyObject *self, void *Py_UNUSED(context))
116 {
117 return _zstd_ZstdDecompressor_unused_data_get_impl((ZstdDecompressor *)self);
118 }
119
120 PyDoc_STRVAR(_zstd_ZstdDecompressor_decompress__doc__,
121 "decompress($self, /, data, max_length=-1)\n"
122 "--\n"
123 "\n"
124 "Decompress *data*, returning uncompressed bytes if possible, or b\'\' otherwise.\n"
125 "\n"
126 " data\n"
127 " A bytes-like object, Zstandard data to be decompressed.\n"
128 " max_length\n"
129 " Maximum size of returned data. When it is negative, the size of\n"
130 " output buffer is unlimited. When it is nonnegative, returns at\n"
131 " most max_length bytes of decompressed data.\n"
132 "\n"
133 "If *max_length* is nonnegative, returns at most *max_length* bytes\n"
134 "of decompressed data. If this limit is reached and further output\n"
135 "can be produced, *self.needs_input* will be set to ``False``. In\n"
136 "this case, the next call to *decompress()* may provide *data* as b\'\'\n"
137 "to obtain more of the output.\n"
138 "\n"
139 "If all of the input data was decompressed and returned (either\n"
140 "because this was less than *max_length* bytes, or because\n"
141 "*max_length* was negative), *self.needs_input* will be set to True.\n"
142 "\n"
143 "Attempting to decompress data after the end of a frame is reached\n"
144 "raises an EOFError. Any data found after the end of the frame is\n"
145 "ignored and saved in the self.unused_data attribute.");
146
147 #define _ZSTD_ZSTDDECOMPRESSOR_DECOMPRESS_METHODDEF \
148 {"decompress", _PyCFunction_CAST(_zstd_ZstdDecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, _zstd_ZstdDecompressor_decompress__doc__},
149
150 static PyObject *
151 _zstd_ZstdDecompressor_decompress_impl(ZstdDecompressor *self,
152 Py_buffer *data,
153 Py_ssize_t max_length);
154
155 static PyObject *
156 _zstd_ZstdDecompressor_decompress(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
157 {
158 PyObject *return_value = NULL;
159 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
160
161 #define NUM_KEYWORDS 2
162 static struct {
163 PyGC_Head _this_is_not_used;
164 PyObject_VAR_HEAD
165 Py_hash_t ob_hash;
166 PyObject *ob_item[NUM_KEYWORDS];
167 } _kwtuple = {
168 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
169 .ob_hash = -1,
170 .ob_item = { &_Py_ID(data), &_Py_ID(max_length), },
171 };
172 #undef NUM_KEYWORDS
173 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
174
175 #else // !Py_BUILD_CORE
176 # define KWTUPLE NULL
177 #endif // !Py_BUILD_CORE
178
179 static const char * const _keywords[] = {"data", "max_length", NULL};
180 static _PyArg_Parser _parser = {
181 .keywords = _keywords,
182 .fname = "decompress",
183 .kwtuple = KWTUPLE,
184 };
185 #undef KWTUPLE
186 PyObject *argsbuf[2];
187 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
188 Py_buffer data = {NULL, NULL};
189 Py_ssize_t max_length = -1;
190
191 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
192 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
193 if (!args) {
194 goto exit;
195 }
196 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
197 goto exit;
198 }
199 if (!noptargs) {
200 goto skip_optional_pos;
201 }
202 {
203 Py_ssize_t ival = -1;
204 PyObject *iobj = _PyNumber_Index(args[1]);
205 if (iobj != NULL) {
206 ival = PyLong_AsSsize_t(iobj);
207 Py_DECREF(iobj);
208 }
209 if (ival == -1 && PyErr_Occurred()) {
210 goto exit;
211 }
212 max_length = ival;
213 }
214 skip_optional_pos:
215 return_value = _zstd_ZstdDecompressor_decompress_impl((ZstdDecompressor *)self, &data, max_length);
216
217 exit:
218 /* Cleanup for data */
219 if (data.obj) {
220 PyBuffer_Release(&data);
221 }
222
223 return return_value;
224 }
225 /*[clinic end generated code: output=70bc308e86463751 input=a9049054013a1b77]*/