blob: 71ac29016416bd79ce64963ff639da2388c1df83 [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};
24 static _PyArg_Parser _parser = {"O:Struct", _keywords, 0};
25 PyObject *format;
26
27 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
28 &format)) {
29 goto exit;
30 }
31 return_value = Struct___init___impl((PyStructObject *)self, format);
32
33exit:
34 return return_value;
35}
36
37PyDoc_STRVAR(Struct_unpack__doc__,
38"unpack($self, buffer, /)\n"
39"--\n"
40"\n"
41"Return a tuple containing unpacked values.\n"
42"\n"
43"Unpack according to the format string Struct.format. The buffer\'s size\n"
44"in bytes must be Struct.size.\n"
45"\n"
46"See help(struct) for more on format strings.");
47
48#define STRUCT_UNPACK_METHODDEF \
49 {"unpack", (PyCFunction)Struct_unpack, METH_O, Struct_unpack__doc__},
50
51static PyObject *
52Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
53
54static PyObject *
55Struct_unpack(PyStructObject *self, PyObject *arg)
56{
57 PyObject *return_value = NULL;
58 Py_buffer buffer = {NULL, NULL};
59
60 if (!PyArg_Parse(arg, "y*:unpack", &buffer)) {
61 goto exit;
62 }
63 return_value = Struct_unpack_impl(self, &buffer);
64
65exit:
66 /* Cleanup for buffer */
67 if (buffer.obj) {
68 PyBuffer_Release(&buffer);
69 }
70
71 return return_value;
72}
73
74PyDoc_STRVAR(Struct_unpack_from__doc__,
75"unpack_from($self, /, buffer, offset=0)\n"
76"--\n"
77"\n"
78"Return a tuple containing unpacked values.\n"
79"\n"
80"Values are unpacked according to the format string Struct.format.\n"
81"\n"
82"The buffer\'s size in bytes, minus offset, must be at least Struct.size.\n"
83"\n"
84"See help(struct) for more on format strings.");
85
86#define STRUCT_UNPACK_FROM_METHODDEF \
87 {"unpack_from", (PyCFunction)Struct_unpack_from, METH_FASTCALL, Struct_unpack_from__doc__},
88
89static PyObject *
90Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
91 Py_ssize_t offset);
92
93static PyObject *
94Struct_unpack_from(PyStructObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
95{
96 PyObject *return_value = NULL;
97 static const char * const _keywords[] = {"buffer", "offset", NULL};
98 static _PyArg_Parser _parser = {"y*|n:unpack_from", _keywords, 0};
99 Py_buffer buffer = {NULL, NULL};
100 Py_ssize_t offset = 0;
101
102 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
103 &buffer, &offset)) {
104 goto exit;
105 }
106 return_value = Struct_unpack_from_impl(self, &buffer, offset);
107
108exit:
109 /* Cleanup for buffer */
110 if (buffer.obj) {
111 PyBuffer_Release(&buffer);
112 }
113
114 return return_value;
115}
116
117PyDoc_STRVAR(Struct_iter_unpack__doc__,
118"iter_unpack($self, buffer, /)\n"
119"--\n"
120"\n"
121"Return an iterator yielding tuples.\n"
122"\n"
123"Tuples are unpacked from the given bytes source, like a repeated\n"
124"invocation of unpack_from().\n"
125"\n"
126"Requires that the bytes length be a multiple of the struct size.");
127
128#define STRUCT_ITER_UNPACK_METHODDEF \
129 {"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
130
131PyDoc_STRVAR(_clearcache__doc__,
132"_clearcache($module, /)\n"
133"--\n"
134"\n"
135"Clear the internal cache.");
136
137#define _CLEARCACHE_METHODDEF \
138 {"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
139
140static PyObject *
141_clearcache_impl(PyObject *module);
142
143static PyObject *
144_clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
145{
146 return _clearcache_impl(module);
147}
148
149PyDoc_STRVAR(calcsize__doc__,
150"calcsize($module, format, /)\n"
151"--\n"
152"\n"
153"Return size in bytes of the struct described by the format string.");
154
155#define CALCSIZE_METHODDEF \
156 {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
157
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200158static Py_ssize_t
159calcsize_impl(PyObject *module, PyStructObject *s_object);
160
161static PyObject *
162calcsize(PyObject *module, PyObject *arg)
163{
164 PyObject *return_value = NULL;
165 PyStructObject *s_object = NULL;
166 Py_ssize_t _return_value;
167
168 if (!PyArg_Parse(arg, "O&:calcsize", cache_struct_converter, &s_object)) {
169 goto exit;
170 }
171 _return_value = calcsize_impl(module, s_object);
172 if ((_return_value == -1) && PyErr_Occurred()) {
173 goto exit;
174 }
175 return_value = PyLong_FromSsize_t(_return_value);
176
177exit:
178 /* Cleanup for s_object */
179 Py_XDECREF(s_object);
180
181 return return_value;
182}
183
Victor Stinner3f2d1012017-02-02 12:09:30 +0100184PyDoc_STRVAR(unpack__doc__,
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100185"unpack($module, format, buffer, /)\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +0100186"--\n"
187"\n"
188"Return a tuple containing values unpacked according to the format string.\n"
189"\n"
190"The buffer\'s size in bytes must be calcsize(format).\n"
191"\n"
192"See help(struct) for more on format strings.");
193
194#define UNPACK_METHODDEF \
195 {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__},
196
197static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200198unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100199
200static PyObject *
201unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
202{
203 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200204 PyStructObject *s_object = NULL;
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100205 Py_buffer buffer = {NULL, NULL};
Victor Stinner3f2d1012017-02-02 12:09:30 +0100206
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200207 if (!_PyArg_ParseStack(args, nargs, "O&y*:unpack",
208 cache_struct_converter, &s_object, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100209 goto exit;
210 }
211
212 if (!_PyArg_NoStackKeywords("unpack", kwnames)) {
213 goto exit;
214 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200215 return_value = unpack_impl(module, s_object, &buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100216
217exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200218 /* Cleanup for s_object */
219 Py_XDECREF(s_object);
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100220 /* Cleanup for buffer */
221 if (buffer.obj) {
222 PyBuffer_Release(&buffer);
223 }
224
Victor Stinner3f2d1012017-02-02 12:09:30 +0100225 return return_value;
226}
227
228PyDoc_STRVAR(unpack_from__doc__,
229"unpack_from($module, format, /, buffer, offset=0)\n"
230"--\n"
231"\n"
232"Return a tuple containing values unpacked according to the format string.\n"
233"\n"
234"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
235"\n"
236"See help(struct) for more on format strings.");
237
238#define UNPACK_FROM_METHODDEF \
239 {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL, unpack_from__doc__},
240
241static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200242unpack_from_impl(PyObject *module, PyStructObject *s_object,
243 Py_buffer *buffer, Py_ssize_t offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100244
245static PyObject *
246unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
247{
248 PyObject *return_value = NULL;
249 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200250 static _PyArg_Parser _parser = {"O&y*|n:unpack_from", _keywords, 0};
251 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100252 Py_buffer buffer = {NULL, NULL};
253 Py_ssize_t offset = 0;
254
255 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200256 cache_struct_converter, &s_object, &buffer, &offset)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100257 goto exit;
258 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200259 return_value = unpack_from_impl(module, s_object, &buffer, offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100260
261exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200262 /* Cleanup for s_object */
263 Py_XDECREF(s_object);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100264 /* Cleanup for buffer */
265 if (buffer.obj) {
266 PyBuffer_Release(&buffer);
267 }
268
269 return return_value;
270}
271
272PyDoc_STRVAR(iter_unpack__doc__,
273"iter_unpack($module, format, buffer, /)\n"
274"--\n"
275"\n"
276"Return an iterator yielding tuples unpacked from the given bytes.\n"
277"\n"
278"The bytes are unpacked according to the format string, like\n"
279"a repeated invocation of unpack_from().\n"
280"\n"
281"Requires that the bytes length be a multiple of the format struct size.");
282
283#define ITER_UNPACK_METHODDEF \
284 {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
285
286static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200287iter_unpack_impl(PyObject *module, PyStructObject *s_object,
288 PyObject *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100289
290static PyObject *
291iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
292{
293 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200294 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100295 PyObject *buffer;
296
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200297 if (!_PyArg_ParseStack(args, nargs, "O&O:iter_unpack",
298 cache_struct_converter, &s_object, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100299 goto exit;
300 }
301
302 if (!_PyArg_NoStackKeywords("iter_unpack", kwnames)) {
303 goto exit;
304 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200305 return_value = iter_unpack_impl(module, s_object, buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100306
307exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200308 /* Cleanup for s_object */
309 Py_XDECREF(s_object);
310
Victor Stinner3f2d1012017-02-02 12:09:30 +0100311 return return_value;
312}
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200313/*[clinic end generated code: output=03e0d193ab1983f9 input=a9049054013a1b77]*/