• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 PyDoc_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 
16 static int
17 Struct___init___impl(PyStructObject *self, PyObject *format);
18 
19 static int
Struct___init__(PyObject * self,PyObject * args,PyObject * kwargs)20 Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs)
21 {
22     int return_value = -1;
23     static const char * const _keywords[] = {"format", NULL};
24     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);
28     PyObject *format;
29 
30     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
31     if (!fastargs) {
32         goto exit;
33     }
34     format = fastargs[0];
35     return_value = Struct___init___impl((PyStructObject *)self, format);
36 
37 exit:
38     return return_value;
39 }
40 
41 PyDoc_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 
55 static PyObject *
56 Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
57 
58 static PyObject *
Struct_unpack(PyStructObject * self,PyObject * arg)59 Struct_unpack(PyStructObject *self, PyObject *arg)
60 {
61     PyObject *return_value = NULL;
62     Py_buffer buffer = {NULL, NULL};
63 
64     if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) {
65         goto exit;
66     }
67     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
68         _PyArg_BadArgument("unpack", "argument", "contiguous buffer", arg);
69         goto exit;
70     }
71     return_value = Struct_unpack_impl(self, &buffer);
72 
73 exit:
74     /* Cleanup for buffer */
75     if (buffer.obj) {
76        PyBuffer_Release(&buffer);
77     }
78 
79     return return_value;
80 }
81 
82 PyDoc_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"
90 "The buffer\'s size in bytes, starting at position offset, must be\n"
91 "at least Struct.size.\n"
92 "\n"
93 "See help(struct) for more on format strings.");
94 
95 #define STRUCT_UNPACK_FROM_METHODDEF    \
96     {"unpack_from", (PyCFunction)(void(*)(void))Struct_unpack_from, METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__},
97 
98 static PyObject *
99 Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
100                         Py_ssize_t offset);
101 
102 static PyObject *
Struct_unpack_from(PyStructObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)103 Struct_unpack_from(PyStructObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
104 {
105     PyObject *return_value = NULL;
106     static const char * const _keywords[] = {"buffer", "offset", NULL};
107     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;
110     Py_buffer buffer = {NULL, NULL};
111     Py_ssize_t offset = 0;
112 
113     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
114     if (!args) {
115         goto exit;
116     }
117     if (PyObject_GetBuffer(args[0], &buffer, PyBUF_SIMPLE) != 0) {
118         goto exit;
119     }
120     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
121         _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[0]);
122         goto exit;
123     }
124     if (!noptargs) {
125         goto skip_optional_pos;
126     }
127     {
128         Py_ssize_t ival = -1;
129         PyObject *iobj = _PyNumber_Index(args[1]);
130         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     }
139 skip_optional_pos:
140     return_value = Struct_unpack_from_impl(self, &buffer, offset);
141 
142 exit:
143     /* Cleanup for buffer */
144     if (buffer.obj) {
145        PyBuffer_Release(&buffer);
146     }
147 
148     return return_value;
149 }
150 
151 PyDoc_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 
165 PyDoc_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 
174 static PyObject *
175 _clearcache_impl(PyObject *module);
176 
177 static PyObject *
_clearcache(PyObject * module,PyObject * Py_UNUSED (ignored))178 _clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
179 {
180     return _clearcache_impl(module);
181 }
182 
183 PyDoc_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 
192 static Py_ssize_t
193 calcsize_impl(PyObject *module, PyStructObject *s_object);
194 
195 static PyObject *
calcsize(PyObject * module,PyObject * arg)196 calcsize(PyObject *module, PyObject *arg)
197 {
198     PyObject *return_value = NULL;
199     PyStructObject *s_object = NULL;
200     Py_ssize_t _return_value;
201 
202     if (!cache_struct_converter(module, arg, &s_object)) {
203         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 
211 exit:
212     /* Cleanup for s_object */
213     Py_XDECREF(s_object);
214 
215     return return_value;
216 }
217 
218 PyDoc_STRVAR(unpack__doc__,
219 "unpack($module, format, buffer, /)\n"
220 "--\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    \
229     {"unpack", (PyCFunction)(void(*)(void))unpack, METH_FASTCALL, unpack__doc__},
230 
231 static PyObject *
232 unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
233 
234 static PyObject *
unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)235 unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
236 {
237     PyObject *return_value = NULL;
238     PyStructObject *s_object = NULL;
239     Py_buffer buffer = {NULL, NULL};
240 
241     if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) {
242         goto exit;
243     }
244     if (!cache_struct_converter(module, args[0], &s_object)) {
245         goto exit;
246     }
247     if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
248         goto exit;
249     }
250     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
251         _PyArg_BadArgument("unpack", "argument 2", "contiguous buffer", args[1]);
252         goto exit;
253     }
254     return_value = unpack_impl(module, s_object, &buffer);
255 
256 exit:
257     /* Cleanup for s_object */
258     Py_XDECREF(s_object);
259     /* Cleanup for buffer */
260     if (buffer.obj) {
261        PyBuffer_Release(&buffer);
262     }
263 
264     return return_value;
265 }
266 
267 PyDoc_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    \
278     {"unpack_from", (PyCFunction)(void(*)(void))unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
279 
280 static PyObject *
281 unpack_from_impl(PyObject *module, PyStructObject *s_object,
282                  Py_buffer *buffer, Py_ssize_t offset);
283 
284 static PyObject *
unpack_from(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)285 unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
286 {
287     PyObject *return_value = NULL;
288     static const char * const _keywords[] = {"", "buffer", "offset", NULL};
289     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;
292     PyStructObject *s_object = NULL;
293     Py_buffer buffer = {NULL, NULL};
294     Py_ssize_t offset = 0;
295 
296     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
297     if (!args) {
298         goto exit;
299     }
300     if (!cache_struct_converter(module, args[0], &s_object)) {
301         goto exit;
302     }
303     if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
304         goto exit;
305     }
306     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
307         _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[1]);
308         goto exit;
309     }
310     if (!noptargs) {
311         goto skip_optional_pos;
312     }
313     {
314         Py_ssize_t ival = -1;
315         PyObject *iobj = _PyNumber_Index(args[2]);
316         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     }
325 skip_optional_pos:
326     return_value = unpack_from_impl(module, s_object, &buffer, offset);
327 
328 exit:
329     /* Cleanup for s_object */
330     Py_XDECREF(s_object);
331     /* Cleanup for buffer */
332     if (buffer.obj) {
333        PyBuffer_Release(&buffer);
334     }
335 
336     return return_value;
337 }
338 
339 PyDoc_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    \
351     {"iter_unpack", (PyCFunction)(void(*)(void))iter_unpack, METH_FASTCALL, iter_unpack__doc__},
352 
353 static PyObject *
354 iter_unpack_impl(PyObject *module, PyStructObject *s_object,
355                  PyObject *buffer);
356 
357 static PyObject *
iter_unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)358 iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
359 {
360     PyObject *return_value = NULL;
361     PyStructObject *s_object = NULL;
362     PyObject *buffer;
363 
364     if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) {
365         goto exit;
366     }
367     if (!cache_struct_converter(module, args[0], &s_object)) {
368         goto exit;
369     }
370     buffer = args[1];
371     return_value = iter_unpack_impl(module, s_object, buffer);
372 
373 exit:
374     /* Cleanup for s_object */
375     Py_XDECREF(s_object);
376 
377     return return_value;
378 }
379 /*[clinic end generated code: output=a3d3cd900091cb1c input=a9049054013a1b77]*/
380