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(Struct__doc__,
12 "Struct(format)\n"
13 "--\n"
14 "\n"
15 "Create a compiled struct object.\n"
16 "\n"
17 "Return a new Struct object which writes and reads binary data according to\n"
18 "the format string.\n"
19 "\n"
20 "See help(struct) for more on format strings.");
21
22 static PyObject *
23 Struct_impl(PyTypeObject *type, PyObject *format);
24
25 static PyObject *
26 Struct(PyTypeObject *type, PyObject *args, PyObject *kwargs)
27 {
28 PyObject *return_value = NULL;
29 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
30
31 #define NUM_KEYWORDS 1
32 static struct {
33 PyGC_Head _this_is_not_used;
34 PyObject_VAR_HEAD
35 PyObject *ob_item[NUM_KEYWORDS];
36 } _kwtuple = {
37 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
38 .ob_item = { &_Py_ID(format), },
39 };
40 #undef NUM_KEYWORDS
41 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
42
43 #else // !Py_BUILD_CORE
44 # define KWTUPLE NULL
45 #endif // !Py_BUILD_CORE
46
47 static const char * const _keywords[] = {"format", NULL};
48 static _PyArg_Parser _parser = {
49 .keywords = _keywords,
50 .fname = "Struct",
51 .kwtuple = KWTUPLE,
52 };
53 #undef KWTUPLE
54 PyObject *argsbuf[1];
55 PyObject * const *fastargs;
56 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
57 PyObject *format;
58
59 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
60 if (!fastargs) {
61 goto exit;
62 }
63 format = fastargs[0];
64 return_value = Struct_impl(type, format);
65
66 exit:
67 return return_value;
68 }
69
70 PyDoc_STRVAR(Struct_unpack__doc__,
71 "unpack($self, buffer, /)\n"
72 "--\n"
73 "\n"
74 "Return a tuple containing unpacked values.\n"
75 "\n"
76 "Unpack according to the format string Struct.format. The buffer\'s size\n"
77 "in bytes must be Struct.size.\n"
78 "\n"
79 "See help(struct) for more on format strings.");
80
81 #define STRUCT_UNPACK_METHODDEF \
82 {"unpack", (PyCFunction)Struct_unpack, METH_O, Struct_unpack__doc__},
83
84 static PyObject *
85 Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
86
87 static PyObject *
88 Struct_unpack(PyStructObject *self, PyObject *arg)
89 {
90 PyObject *return_value = NULL;
91 Py_buffer buffer = {NULL, NULL};
92
93 if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
94 goto exit;
95 }
96 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
97 _PyArg_BadArgument("unpack", "argument", "contiguous buffer", arg);
98 goto exit;
99 }
100 return_value = Struct_unpack_impl(self, &buffer);
101
102 exit:
103 /* Cleanup for buffer */
104 if (buffer.obj) {
105 PyBuffer_Release(&buffer);
106 }
107
108 return return_value;
109 }
110
111 PyDoc_STRVAR(Struct_unpack_from__doc__,
112 "unpack_from($self, /, buffer, offset=0)\n"
113 "--\n"
114 "\n"
115 "Return a tuple containing unpacked values.\n"
116 "\n"
117 "Values are unpacked according to the format string Struct.format.\n"
118 "\n"
119 "The buffer\'s size in bytes, starting at position offset, must be\n"
120 "at least Struct.size.\n"
121 "\n"
122 "See help(struct) for more on format strings.");
123
124 #define STRUCT_UNPACK_FROM_METHODDEF \
125 {"unpack_from", _PyCFunction_CAST(Struct_unpack_from), METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__},
126
127 static PyObject *
128 Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
129 Py_ssize_t offset);
130
131 static PyObject *
132 Struct_unpack_from(PyStructObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
133 {
134 PyObject *return_value = NULL;
135 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
136
137 #define NUM_KEYWORDS 2
138 static struct {
139 PyGC_Head _this_is_not_used;
140 PyObject_VAR_HEAD
141 PyObject *ob_item[NUM_KEYWORDS];
142 } _kwtuple = {
143 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
144 .ob_item = { &_Py_ID(buffer), &_Py_ID(offset), },
145 };
146 #undef NUM_KEYWORDS
147 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
148
149 #else // !Py_BUILD_CORE
150 # define KWTUPLE NULL
151 #endif // !Py_BUILD_CORE
152
153 static const char * const _keywords[] = {"buffer", "offset", NULL};
154 static _PyArg_Parser _parser = {
155 .keywords = _keywords,
156 .fname = "unpack_from",
157 .kwtuple = KWTUPLE,
158 };
159 #undef KWTUPLE
160 PyObject *argsbuf[2];
161 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
162 Py_buffer buffer = {NULL, NULL};
163 Py_ssize_t offset = 0;
164
165 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
166 if (!args) {
167 goto exit;
168 }
169 if (PyObject_GetBuffer(args[0], &buffer, PyBUF_SIMPLE) != 0) {
170 goto exit;
171 }
172 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
173 _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[0]);
174 goto exit;
175 }
176 if (!noptargs) {
177 goto skip_optional_pos;
178 }
179 {
180 Py_ssize_t ival = -1;
181 PyObject *iobj = _PyNumber_Index(args[1]);
182 if (iobj != NULL) {
183 ival = PyLong_AsSsize_t(iobj);
184 Py_DECREF(iobj);
185 }
186 if (ival == -1 && PyErr_Occurred()) {
187 goto exit;
188 }
189 offset = ival;
190 }
191 skip_optional_pos:
192 return_value = Struct_unpack_from_impl(self, &buffer, offset);
193
194 exit:
195 /* Cleanup for buffer */
196 if (buffer.obj) {
197 PyBuffer_Release(&buffer);
198 }
199
200 return return_value;
201 }
202
203 PyDoc_STRVAR(Struct_iter_unpack__doc__,
204 "iter_unpack($self, buffer, /)\n"
205 "--\n"
206 "\n"
207 "Return an iterator yielding tuples.\n"
208 "\n"
209 "Tuples are unpacked from the given bytes source, like a repeated\n"
210 "invocation of unpack_from().\n"
211 "\n"
212 "Requires that the bytes length be a multiple of the struct size.");
213
214 #define STRUCT_ITER_UNPACK_METHODDEF \
215 {"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
216
217 PyDoc_STRVAR(_clearcache__doc__,
218 "_clearcache($module, /)\n"
219 "--\n"
220 "\n"
221 "Clear the internal cache.");
222
223 #define _CLEARCACHE_METHODDEF \
224 {"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
225
226 static PyObject *
227 _clearcache_impl(PyObject *module);
228
229 static PyObject *
230 _clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
231 {
232 return _clearcache_impl(module);
233 }
234
235 PyDoc_STRVAR(calcsize__doc__,
236 "calcsize($module, format, /)\n"
237 "--\n"
238 "\n"
239 "Return size in bytes of the struct described by the format string.");
240
241 #define CALCSIZE_METHODDEF \
242 {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
243
244 static Py_ssize_t
245 calcsize_impl(PyObject *module, PyStructObject *s_object);
246
247 static PyObject *
248 calcsize(PyObject *module, PyObject *arg)
249 {
250 PyObject *return_value = NULL;
251 PyStructObject *s_object = NULL;
252 Py_ssize_t _return_value;
253
254 if (!cache_struct_converter(module, arg, &s_object)) {
255 goto exit;
256 }
257 _return_value = calcsize_impl(module, s_object);
258 if ((_return_value == -1) && PyErr_Occurred()) {
259 goto exit;
260 }
261 return_value = PyLong_FromSsize_t(_return_value);
262
263 exit:
264 /* Cleanup for s_object */
265 Py_XDECREF(s_object);
266
267 return return_value;
268 }
269
270 PyDoc_STRVAR(unpack__doc__,
271 "unpack($module, format, buffer, /)\n"
272 "--\n"
273 "\n"
274 "Return a tuple containing values unpacked according to the format string.\n"
275 "\n"
276 "The buffer\'s size in bytes must be calcsize(format).\n"
277 "\n"
278 "See help(struct) for more on format strings.");
279
280 #define UNPACK_METHODDEF \
281 {"unpack", _PyCFunction_CAST(unpack), METH_FASTCALL, unpack__doc__},
282
283 static PyObject *
284 unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
285
286 static PyObject *
287 unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
288 {
289 PyObject *return_value = NULL;
290 PyStructObject *s_object = NULL;
291 Py_buffer buffer = {NULL, NULL};
292
293 if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) {
294 goto exit;
295 }
296 if (!cache_struct_converter(module, args[0], &s_object)) {
297 goto exit;
298 }
299 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
300 goto exit;
301 }
302 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
303 _PyArg_BadArgument("unpack", "argument 2", "contiguous buffer", args[1]);
304 goto exit;
305 }
306 return_value = unpack_impl(module, s_object, &buffer);
307
308 exit:
309 /* Cleanup for s_object */
310 Py_XDECREF(s_object);
311 /* Cleanup for buffer */
312 if (buffer.obj) {
313 PyBuffer_Release(&buffer);
314 }
315
316 return return_value;
317 }
318
319 PyDoc_STRVAR(unpack_from__doc__,
320 "unpack_from($module, format, /, buffer, offset=0)\n"
321 "--\n"
322 "\n"
323 "Return a tuple containing values unpacked according to the format string.\n"
324 "\n"
325 "The buffer\'s size, minus offset, must be at least calcsize(format).\n"
326 "\n"
327 "See help(struct) for more on format strings.");
328
329 #define UNPACK_FROM_METHODDEF \
330 {"unpack_from", _PyCFunction_CAST(unpack_from), METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
331
332 static PyObject *
333 unpack_from_impl(PyObject *module, PyStructObject *s_object,
334 Py_buffer *buffer, Py_ssize_t offset);
335
336 static PyObject *
337 unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
338 {
339 PyObject *return_value = NULL;
340 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
341
342 #define NUM_KEYWORDS 2
343 static struct {
344 PyGC_Head _this_is_not_used;
345 PyObject_VAR_HEAD
346 PyObject *ob_item[NUM_KEYWORDS];
347 } _kwtuple = {
348 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
349 .ob_item = { &_Py_ID(buffer), &_Py_ID(offset), },
350 };
351 #undef NUM_KEYWORDS
352 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
353
354 #else // !Py_BUILD_CORE
355 # define KWTUPLE NULL
356 #endif // !Py_BUILD_CORE
357
358 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
359 static _PyArg_Parser _parser = {
360 .keywords = _keywords,
361 .fname = "unpack_from",
362 .kwtuple = KWTUPLE,
363 };
364 #undef KWTUPLE
365 PyObject *argsbuf[3];
366 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
367 PyStructObject *s_object = NULL;
368 Py_buffer buffer = {NULL, NULL};
369 Py_ssize_t offset = 0;
370
371 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
372 if (!args) {
373 goto exit;
374 }
375 if (!cache_struct_converter(module, args[0], &s_object)) {
376 goto exit;
377 }
378 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
379 goto exit;
380 }
381 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
382 _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[1]);
383 goto exit;
384 }
385 if (!noptargs) {
386 goto skip_optional_pos;
387 }
388 {
389 Py_ssize_t ival = -1;
390 PyObject *iobj = _PyNumber_Index(args[2]);
391 if (iobj != NULL) {
392 ival = PyLong_AsSsize_t(iobj);
393 Py_DECREF(iobj);
394 }
395 if (ival == -1 && PyErr_Occurred()) {
396 goto exit;
397 }
398 offset = ival;
399 }
400 skip_optional_pos:
401 return_value = unpack_from_impl(module, s_object, &buffer, offset);
402
403 exit:
404 /* Cleanup for s_object */
405 Py_XDECREF(s_object);
406 /* Cleanup for buffer */
407 if (buffer.obj) {
408 PyBuffer_Release(&buffer);
409 }
410
411 return return_value;
412 }
413
414 PyDoc_STRVAR(iter_unpack__doc__,
415 "iter_unpack($module, format, buffer, /)\n"
416 "--\n"
417 "\n"
418 "Return an iterator yielding tuples unpacked from the given bytes.\n"
419 "\n"
420 "The bytes are unpacked according to the format string, like\n"
421 "a repeated invocation of unpack_from().\n"
422 "\n"
423 "Requires that the bytes length be a multiple of the format struct size.");
424
425 #define ITER_UNPACK_METHODDEF \
426 {"iter_unpack", _PyCFunction_CAST(iter_unpack), METH_FASTCALL, iter_unpack__doc__},
427
428 static PyObject *
429 iter_unpack_impl(PyObject *module, PyStructObject *s_object,
430 PyObject *buffer);
431
432 static PyObject *
433 iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
434 {
435 PyObject *return_value = NULL;
436 PyStructObject *s_object = NULL;
437 PyObject *buffer;
438
439 if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) {
440 goto exit;
441 }
442 if (!cache_struct_converter(module, args[0], &s_object)) {
443 goto exit;
444 }
445 buffer = args[1];
446 return_value = iter_unpack_impl(module, s_object, buffer);
447
448 exit:
449 /* Cleanup for s_object */
450 Py_XDECREF(s_object);
451
452 return return_value;
453 }
454 /*[clinic end generated code: output=f3d6e06f80368998 input=a9049054013a1b77]*/