blob: c273ee3704653fb5838d7086ed38bbff999c24f2 [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 \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030087 {"unpack_from", (PyCFunction)Struct_unpack_from, METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +010088
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 *
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300201unpack(PyObject *module, PyObject **args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100202{
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
Sylvain74453812017-06-10 06:51:48 +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 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200211 return_value = unpack_impl(module, s_object, &buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100212
213exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200214 /* Cleanup for s_object */
215 Py_XDECREF(s_object);
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100216 /* Cleanup for buffer */
217 if (buffer.obj) {
218 PyBuffer_Release(&buffer);
219 }
220
Victor Stinner3f2d1012017-02-02 12:09:30 +0100221 return return_value;
222}
223
224PyDoc_STRVAR(unpack_from__doc__,
225"unpack_from($module, format, /, buffer, offset=0)\n"
226"--\n"
227"\n"
228"Return a tuple containing values unpacked according to the format string.\n"
229"\n"
230"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
231"\n"
232"See help(struct) for more on format strings.");
233
234#define UNPACK_FROM_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300235 {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +0100236
237static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200238unpack_from_impl(PyObject *module, PyStructObject *s_object,
239 Py_buffer *buffer, Py_ssize_t offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100240
241static PyObject *
242unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
243{
244 PyObject *return_value = NULL;
245 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200246 static _PyArg_Parser _parser = {"O&y*|n:unpack_from", _keywords, 0};
247 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100248 Py_buffer buffer = {NULL, NULL};
249 Py_ssize_t offset = 0;
250
251 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200252 cache_struct_converter, &s_object, &buffer, &offset)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100253 goto exit;
254 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200255 return_value = unpack_from_impl(module, s_object, &buffer, offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100256
257exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200258 /* Cleanup for s_object */
259 Py_XDECREF(s_object);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100260 /* Cleanup for buffer */
261 if (buffer.obj) {
262 PyBuffer_Release(&buffer);
263 }
264
265 return return_value;
266}
267
268PyDoc_STRVAR(iter_unpack__doc__,
269"iter_unpack($module, format, buffer, /)\n"
270"--\n"
271"\n"
272"Return an iterator yielding tuples unpacked from the given bytes.\n"
273"\n"
274"The bytes are unpacked according to the format string, like\n"
275"a repeated invocation of unpack_from().\n"
276"\n"
277"Requires that the bytes length be a multiple of the format struct size.");
278
279#define ITER_UNPACK_METHODDEF \
280 {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
281
282static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200283iter_unpack_impl(PyObject *module, PyStructObject *s_object,
284 PyObject *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100285
286static PyObject *
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300287iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100288{
289 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200290 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100291 PyObject *buffer;
292
Sylvain74453812017-06-10 06:51:48 +0200293 if (!_PyArg_ParseStack(args, nargs, "O&O:iter_unpack",
294 cache_struct_converter, &s_object, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100295 goto exit;
296 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200297 return_value = iter_unpack_impl(module, s_object, buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100298
299exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200300 /* Cleanup for s_object */
301 Py_XDECREF(s_object);
302
Victor Stinner3f2d1012017-02-02 12:09:30 +0100303 return return_value;
304}
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300305/*[clinic end generated code: output=fce060787b4c5261 input=a9049054013a1b77]*/