blob: b0c1eb4587ae231dfe11dfecfdb589fb8ea119ae [file] [log] [blame]
Victor Stinner3f2d1012017-02-02 12:09:30 +01001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(Struct___init____doc__,
6"Struct(format)\n"
7"--\n"
8"\n"
9"Create a compiled struct object.\n"
10"\n"
11"Return a new Struct object which writes and reads binary data according to\n"
12"the format string.\n"
13"\n"
14"See help(struct) for more on format strings.");
15
16static int
17Struct___init___impl(PyStructObject *self, PyObject *format);
18
19static int
20Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs)
21{
22 int return_value = -1;
23 static const char * const _keywords[] = {"format", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020024 static _PyArg_Parser _parser = {NULL, _keywords, "Struct", 0};
25 PyObject *argsbuf[1];
26 PyObject * const *fastargs;
27 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Victor Stinner3f2d1012017-02-02 12:09:30 +010028 PyObject *format;
29
Serhiy Storchaka31913912019-03-14 10:32:22 +020030 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
31 if (!fastargs) {
Victor Stinner3f2d1012017-02-02 12:09:30 +010032 goto exit;
33 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020034 format = fastargs[0];
Victor Stinner3f2d1012017-02-02 12:09:30 +010035 return_value = Struct___init___impl((PyStructObject *)self, format);
36
37exit:
38 return return_value;
39}
40
41PyDoc_STRVAR(Struct_unpack__doc__,
42"unpack($self, buffer, /)\n"
43"--\n"
44"\n"
45"Return a tuple containing unpacked values.\n"
46"\n"
47"Unpack according to the format string Struct.format. The buffer\'s size\n"
48"in bytes must be Struct.size.\n"
49"\n"
50"See help(struct) for more on format strings.");
51
52#define STRUCT_UNPACK_METHODDEF \
53 {"unpack", (PyCFunction)Struct_unpack, METH_O, Struct_unpack__doc__},
54
55static PyObject *
56Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
57
58static PyObject *
59Struct_unpack(PyStructObject *self, PyObject *arg)
60{
61 PyObject *return_value = NULL;
62 Py_buffer buffer = {NULL, NULL};
63
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020064 if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
65 goto exit;
66 }
67 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020068 _PyArg_BadArgument("unpack", "argument", "contiguous buffer", arg);
Victor Stinner3f2d1012017-02-02 12:09:30 +010069 goto exit;
70 }
71 return_value = Struct_unpack_impl(self, &buffer);
72
73exit:
74 /* Cleanup for buffer */
75 if (buffer.obj) {
76 PyBuffer_Release(&buffer);
77 }
78
79 return return_value;
80}
81
82PyDoc_STRVAR(Struct_unpack_from__doc__,
83"unpack_from($self, /, buffer, offset=0)\n"
84"--\n"
85"\n"
86"Return a tuple containing unpacked values.\n"
87"\n"
88"Values are unpacked according to the format string Struct.format.\n"
89"\n"
Xiang Zhangc10b2882018-03-11 02:58:52 +080090"The buffer\'s size in bytes, starting at position offset, must be\n"
91"at least Struct.size.\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +010092"\n"
93"See help(struct) for more on format strings.");
94
95#define STRUCT_UNPACK_FROM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020096 {"unpack_from", (PyCFunction)(void(*)(void))Struct_unpack_from, METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +010097
98static PyObject *
99Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
100 Py_ssize_t offset);
101
102static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200103Struct_unpack_from(PyStructObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100104{
105 PyObject *return_value = NULL;
106 static const char * const _keywords[] = {"buffer", "offset", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200107 static _PyArg_Parser _parser = {NULL, _keywords, "unpack_from", 0};
108 PyObject *argsbuf[2];
109 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100110 Py_buffer buffer = {NULL, NULL};
111 Py_ssize_t offset = 0;
112
Serhiy Storchaka31913912019-03-14 10:32:22 +0200113 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
114 if (!args) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100115 goto exit;
116 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200117 if (PyObject_GetBuffer(args[0], &buffer, PyBUF_SIMPLE) != 0) {
118 goto exit;
119 }
120 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200121 _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200122 goto exit;
123 }
124 if (!noptargs) {
125 goto skip_optional_pos;
126 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200127 {
128 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300129 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200130 if (iobj != NULL) {
131 ival = PyLong_AsSsize_t(iobj);
132 Py_DECREF(iobj);
133 }
134 if (ival == -1 && PyErr_Occurred()) {
135 goto exit;
136 }
137 offset = ival;
138 }
139skip_optional_pos:
Victor Stinner3f2d1012017-02-02 12:09:30 +0100140 return_value = Struct_unpack_from_impl(self, &buffer, offset);
141
142exit:
143 /* Cleanup for buffer */
144 if (buffer.obj) {
145 PyBuffer_Release(&buffer);
146 }
147
148 return return_value;
149}
150
151PyDoc_STRVAR(Struct_iter_unpack__doc__,
152"iter_unpack($self, buffer, /)\n"
153"--\n"
154"\n"
155"Return an iterator yielding tuples.\n"
156"\n"
157"Tuples are unpacked from the given bytes source, like a repeated\n"
158"invocation of unpack_from().\n"
159"\n"
160"Requires that the bytes length be a multiple of the struct size.");
161
162#define STRUCT_ITER_UNPACK_METHODDEF \
163 {"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
164
165PyDoc_STRVAR(_clearcache__doc__,
166"_clearcache($module, /)\n"
167"--\n"
168"\n"
169"Clear the internal cache.");
170
171#define _CLEARCACHE_METHODDEF \
172 {"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
173
174static PyObject *
175_clearcache_impl(PyObject *module);
176
177static PyObject *
178_clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
179{
180 return _clearcache_impl(module);
181}
182
183PyDoc_STRVAR(calcsize__doc__,
184"calcsize($module, format, /)\n"
185"--\n"
186"\n"
187"Return size in bytes of the struct described by the format string.");
188
189#define CALCSIZE_METHODDEF \
190 {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
191
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200192static Py_ssize_t
193calcsize_impl(PyObject *module, PyStructObject *s_object);
194
195static PyObject *
196calcsize(PyObject *module, PyObject *arg)
197{
198 PyObject *return_value = NULL;
199 PyStructObject *s_object = NULL;
200 Py_ssize_t _return_value;
201
Christian Heimescfeb5432020-11-19 14:36:23 +0100202 if (!cache_struct_converter(module, arg, &s_object)) {
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200203 goto exit;
204 }
205 _return_value = calcsize_impl(module, s_object);
206 if ((_return_value == -1) && PyErr_Occurred()) {
207 goto exit;
208 }
209 return_value = PyLong_FromSsize_t(_return_value);
210
211exit:
212 /* Cleanup for s_object */
213 Py_XDECREF(s_object);
214
215 return return_value;
216}
217
Victor Stinner3f2d1012017-02-02 12:09:30 +0100218PyDoc_STRVAR(unpack__doc__,
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100219"unpack($module, format, buffer, /)\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +0100220"--\n"
221"\n"
222"Return a tuple containing values unpacked according to the format string.\n"
223"\n"
224"The buffer\'s size in bytes must be calcsize(format).\n"
225"\n"
226"See help(struct) for more on format strings.");
227
228#define UNPACK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200229 {"unpack", (PyCFunction)(void(*)(void))unpack, METH_FASTCALL, unpack__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +0100230
231static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200232unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100233
234static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200235unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100236{
237 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200238 PyStructObject *s_object = NULL;
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100239 Py_buffer buffer = {NULL, NULL};
Victor Stinner3f2d1012017-02-02 12:09:30 +0100240
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200241 if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) {
242 goto exit;
243 }
Christian Heimescfeb5432020-11-19 14:36:23 +0100244 if (!cache_struct_converter(module, args[0], &s_object)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200245 goto exit;
246 }
247 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
248 goto exit;
249 }
250 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200251 _PyArg_BadArgument("unpack", "argument 2", "contiguous buffer", args[1]);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100252 goto exit;
253 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200254 return_value = unpack_impl(module, s_object, &buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100255
256exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200257 /* Cleanup for s_object */
258 Py_XDECREF(s_object);
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100259 /* Cleanup for buffer */
260 if (buffer.obj) {
261 PyBuffer_Release(&buffer);
262 }
263
Victor Stinner3f2d1012017-02-02 12:09:30 +0100264 return return_value;
265}
266
267PyDoc_STRVAR(unpack_from__doc__,
268"unpack_from($module, format, /, buffer, offset=0)\n"
269"--\n"
270"\n"
271"Return a tuple containing values unpacked according to the format string.\n"
272"\n"
273"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
274"\n"
275"See help(struct) for more on format strings.");
276
277#define UNPACK_FROM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200278 {"unpack_from", (PyCFunction)(void(*)(void))unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +0100279
280static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200281unpack_from_impl(PyObject *module, PyStructObject *s_object,
282 Py_buffer *buffer, Py_ssize_t offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100283
284static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200285unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100286{
287 PyObject *return_value = NULL;
288 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200289 static _PyArg_Parser _parser = {NULL, _keywords, "unpack_from", 0};
290 PyObject *argsbuf[3];
291 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200292 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100293 Py_buffer buffer = {NULL, NULL};
294 Py_ssize_t offset = 0;
295
Serhiy Storchaka31913912019-03-14 10:32:22 +0200296 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
297 if (!args) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100298 goto exit;
299 }
Christian Heimescfeb5432020-11-19 14:36:23 +0100300 if (!cache_struct_converter(module, args[0], &s_object)) {
Serhiy Storchaka31913912019-03-14 10:32:22 +0200301 goto exit;
302 }
303 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
304 goto exit;
305 }
306 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200307 _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200308 goto exit;
309 }
310 if (!noptargs) {
311 goto skip_optional_pos;
312 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200313 {
314 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300315 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200316 if (iobj != NULL) {
317 ival = PyLong_AsSsize_t(iobj);
318 Py_DECREF(iobj);
319 }
320 if (ival == -1 && PyErr_Occurred()) {
321 goto exit;
322 }
323 offset = ival;
324 }
325skip_optional_pos:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200326 return_value = unpack_from_impl(module, s_object, &buffer, offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100327
328exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200329 /* Cleanup for s_object */
330 Py_XDECREF(s_object);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100331 /* Cleanup for buffer */
332 if (buffer.obj) {
333 PyBuffer_Release(&buffer);
334 }
335
336 return return_value;
337}
338
339PyDoc_STRVAR(iter_unpack__doc__,
340"iter_unpack($module, format, buffer, /)\n"
341"--\n"
342"\n"
343"Return an iterator yielding tuples unpacked from the given bytes.\n"
344"\n"
345"The bytes are unpacked according to the format string, like\n"
346"a repeated invocation of unpack_from().\n"
347"\n"
348"Requires that the bytes length be a multiple of the format struct size.");
349
350#define ITER_UNPACK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200351 {"iter_unpack", (PyCFunction)(void(*)(void))iter_unpack, METH_FASTCALL, iter_unpack__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +0100352
353static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200354iter_unpack_impl(PyObject *module, PyStructObject *s_object,
355 PyObject *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100356
357static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200358iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100359{
360 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200361 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100362 PyObject *buffer;
363
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200364 if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100365 goto exit;
366 }
Christian Heimescfeb5432020-11-19 14:36:23 +0100367 if (!cache_struct_converter(module, args[0], &s_object)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200368 goto exit;
369 }
370 buffer = args[1];
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200371 return_value = iter_unpack_impl(module, s_object, buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100372
373exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200374 /* Cleanup for s_object */
375 Py_XDECREF(s_object);
376
Victor Stinner3f2d1012017-02-02 12:09:30 +0100377 return return_value;
378}
Christian Heimescfeb5432020-11-19 14:36:23 +0100379/*[clinic end generated code: output=a3d3cd900091cb1c input=a9049054013a1b77]*/