• 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     if (PyFloat_Check(args[1])) {
128         PyErr_SetString(PyExc_TypeError,
129                         "integer argument expected, got float" );
130         goto exit;
131     }
132     {
133         Py_ssize_t ival = -1;
134         PyObject *iobj = PyNumber_Index(args[1]);
135         if (iobj != NULL) {
136             ival = PyLong_AsSsize_t(iobj);
137             Py_DECREF(iobj);
138         }
139         if (ival == -1 && PyErr_Occurred()) {
140             goto exit;
141         }
142         offset = ival;
143     }
144 skip_optional_pos:
145     return_value = Struct_unpack_from_impl(self, &buffer, offset);
146 
147 exit:
148     /* Cleanup for buffer */
149     if (buffer.obj) {
150        PyBuffer_Release(&buffer);
151     }
152 
153     return return_value;
154 }
155 
156 PyDoc_STRVAR(Struct_iter_unpack__doc__,
157 "iter_unpack($self, buffer, /)\n"
158 "--\n"
159 "\n"
160 "Return an iterator yielding tuples.\n"
161 "\n"
162 "Tuples are unpacked from the given bytes source, like a repeated\n"
163 "invocation of unpack_from().\n"
164 "\n"
165 "Requires that the bytes length be a multiple of the struct size.");
166 
167 #define STRUCT_ITER_UNPACK_METHODDEF    \
168     {"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
169 
170 PyDoc_STRVAR(_clearcache__doc__,
171 "_clearcache($module, /)\n"
172 "--\n"
173 "\n"
174 "Clear the internal cache.");
175 
176 #define _CLEARCACHE_METHODDEF    \
177     {"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
178 
179 static PyObject *
180 _clearcache_impl(PyObject *module);
181 
182 static PyObject *
_clearcache(PyObject * module,PyObject * Py_UNUSED (ignored))183 _clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
184 {
185     return _clearcache_impl(module);
186 }
187 
188 PyDoc_STRVAR(calcsize__doc__,
189 "calcsize($module, format, /)\n"
190 "--\n"
191 "\n"
192 "Return size in bytes of the struct described by the format string.");
193 
194 #define CALCSIZE_METHODDEF    \
195     {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
196 
197 static Py_ssize_t
198 calcsize_impl(PyObject *module, PyStructObject *s_object);
199 
200 static PyObject *
calcsize(PyObject * module,PyObject * arg)201 calcsize(PyObject *module, PyObject *arg)
202 {
203     PyObject *return_value = NULL;
204     PyStructObject *s_object = NULL;
205     Py_ssize_t _return_value;
206 
207     if (!cache_struct_converter(arg, &s_object)) {
208         goto exit;
209     }
210     _return_value = calcsize_impl(module, s_object);
211     if ((_return_value == -1) && PyErr_Occurred()) {
212         goto exit;
213     }
214     return_value = PyLong_FromSsize_t(_return_value);
215 
216 exit:
217     /* Cleanup for s_object */
218     Py_XDECREF(s_object);
219 
220     return return_value;
221 }
222 
223 PyDoc_STRVAR(unpack__doc__,
224 "unpack($module, format, buffer, /)\n"
225 "--\n"
226 "\n"
227 "Return a tuple containing values unpacked according to the format string.\n"
228 "\n"
229 "The buffer\'s size in bytes must be calcsize(format).\n"
230 "\n"
231 "See help(struct) for more on format strings.");
232 
233 #define UNPACK_METHODDEF    \
234     {"unpack", (PyCFunction)(void(*)(void))unpack, METH_FASTCALL, unpack__doc__},
235 
236 static PyObject *
237 unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
238 
239 static PyObject *
unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)240 unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
241 {
242     PyObject *return_value = NULL;
243     PyStructObject *s_object = NULL;
244     Py_buffer buffer = {NULL, NULL};
245 
246     if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) {
247         goto exit;
248     }
249     if (!cache_struct_converter(args[0], &s_object)) {
250         goto exit;
251     }
252     if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
253         goto exit;
254     }
255     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
256         _PyArg_BadArgument("unpack", "argument 2", "contiguous buffer", args[1]);
257         goto exit;
258     }
259     return_value = unpack_impl(module, s_object, &buffer);
260 
261 exit:
262     /* Cleanup for s_object */
263     Py_XDECREF(s_object);
264     /* Cleanup for buffer */
265     if (buffer.obj) {
266        PyBuffer_Release(&buffer);
267     }
268 
269     return return_value;
270 }
271 
272 PyDoc_STRVAR(unpack_from__doc__,
273 "unpack_from($module, format, /, buffer, offset=0)\n"
274 "--\n"
275 "\n"
276 "Return a tuple containing values unpacked according to the format string.\n"
277 "\n"
278 "The buffer\'s size, minus offset, must be at least calcsize(format).\n"
279 "\n"
280 "See help(struct) for more on format strings.");
281 
282 #define UNPACK_FROM_METHODDEF    \
283     {"unpack_from", (PyCFunction)(void(*)(void))unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
284 
285 static PyObject *
286 unpack_from_impl(PyObject *module, PyStructObject *s_object,
287                  Py_buffer *buffer, Py_ssize_t offset);
288 
289 static PyObject *
unpack_from(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)290 unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
291 {
292     PyObject *return_value = NULL;
293     static const char * const _keywords[] = {"", "buffer", "offset", NULL};
294     static _PyArg_Parser _parser = {NULL, _keywords, "unpack_from", 0};
295     PyObject *argsbuf[3];
296     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
297     PyStructObject *s_object = NULL;
298     Py_buffer buffer = {NULL, NULL};
299     Py_ssize_t offset = 0;
300 
301     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
302     if (!args) {
303         goto exit;
304     }
305     if (!cache_struct_converter(args[0], &s_object)) {
306         goto exit;
307     }
308     if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
309         goto exit;
310     }
311     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
312         _PyArg_BadArgument("unpack_from", "argument 'buffer'", "contiguous buffer", args[1]);
313         goto exit;
314     }
315     if (!noptargs) {
316         goto skip_optional_pos;
317     }
318     if (PyFloat_Check(args[2])) {
319         PyErr_SetString(PyExc_TypeError,
320                         "integer argument expected, got float" );
321         goto exit;
322     }
323     {
324         Py_ssize_t ival = -1;
325         PyObject *iobj = PyNumber_Index(args[2]);
326         if (iobj != NULL) {
327             ival = PyLong_AsSsize_t(iobj);
328             Py_DECREF(iobj);
329         }
330         if (ival == -1 && PyErr_Occurred()) {
331             goto exit;
332         }
333         offset = ival;
334     }
335 skip_optional_pos:
336     return_value = unpack_from_impl(module, s_object, &buffer, offset);
337 
338 exit:
339     /* Cleanup for s_object */
340     Py_XDECREF(s_object);
341     /* Cleanup for buffer */
342     if (buffer.obj) {
343        PyBuffer_Release(&buffer);
344     }
345 
346     return return_value;
347 }
348 
349 PyDoc_STRVAR(iter_unpack__doc__,
350 "iter_unpack($module, format, buffer, /)\n"
351 "--\n"
352 "\n"
353 "Return an iterator yielding tuples unpacked from the given bytes.\n"
354 "\n"
355 "The bytes are unpacked according to the format string, like\n"
356 "a repeated invocation of unpack_from().\n"
357 "\n"
358 "Requires that the bytes length be a multiple of the format struct size.");
359 
360 #define ITER_UNPACK_METHODDEF    \
361     {"iter_unpack", (PyCFunction)(void(*)(void))iter_unpack, METH_FASTCALL, iter_unpack__doc__},
362 
363 static PyObject *
364 iter_unpack_impl(PyObject *module, PyStructObject *s_object,
365                  PyObject *buffer);
366 
367 static PyObject *
iter_unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)368 iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
369 {
370     PyObject *return_value = NULL;
371     PyStructObject *s_object = NULL;
372     PyObject *buffer;
373 
374     if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) {
375         goto exit;
376     }
377     if (!cache_struct_converter(args[0], &s_object)) {
378         goto exit;
379     }
380     buffer = args[1];
381     return_value = iter_unpack_impl(module, s_object, buffer);
382 
383 exit:
384     /* Cleanup for s_object */
385     Py_XDECREF(s_object);
386 
387     return return_value;
388 }
389 /*[clinic end generated code: output=6a6228cfc4b7099c input=a9049054013a1b77]*/
390