blob: 2ecadfb1de97ccb10bd8a8eb072f7d8e7704da81 [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"
Xiang Zhangc10b2882018-03-11 02:58:52 +080082"The buffer\'s size in bytes, starting at position offset, must be\n"
83"at least Struct.size.\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +010084"\n"
85"See help(struct) for more on format strings.");
86
87#define STRUCT_UNPACK_FROM_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030088 {"unpack_from", (PyCFunction)Struct_unpack_from, METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +010089
90static PyObject *
91Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
92 Py_ssize_t offset);
93
94static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020095Struct_unpack_from(PyStructObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner3f2d1012017-02-02 12:09:30 +010096{
97 PyObject *return_value = NULL;
98 static const char * const _keywords[] = {"buffer", "offset", NULL};
99 static _PyArg_Parser _parser = {"y*|n:unpack_from", _keywords, 0};
100 Py_buffer buffer = {NULL, NULL};
101 Py_ssize_t offset = 0;
102
103 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
104 &buffer, &offset)) {
105 goto exit;
106 }
107 return_value = Struct_unpack_from_impl(self, &buffer, offset);
108
109exit:
110 /* Cleanup for buffer */
111 if (buffer.obj) {
112 PyBuffer_Release(&buffer);
113 }
114
115 return return_value;
116}
117
118PyDoc_STRVAR(Struct_iter_unpack__doc__,
119"iter_unpack($self, buffer, /)\n"
120"--\n"
121"\n"
122"Return an iterator yielding tuples.\n"
123"\n"
124"Tuples are unpacked from the given bytes source, like a repeated\n"
125"invocation of unpack_from().\n"
126"\n"
127"Requires that the bytes length be a multiple of the struct size.");
128
129#define STRUCT_ITER_UNPACK_METHODDEF \
130 {"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
131
132PyDoc_STRVAR(_clearcache__doc__,
133"_clearcache($module, /)\n"
134"--\n"
135"\n"
136"Clear the internal cache.");
137
138#define _CLEARCACHE_METHODDEF \
139 {"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
140
141static PyObject *
142_clearcache_impl(PyObject *module);
143
144static PyObject *
145_clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
146{
147 return _clearcache_impl(module);
148}
149
150PyDoc_STRVAR(calcsize__doc__,
151"calcsize($module, format, /)\n"
152"--\n"
153"\n"
154"Return size in bytes of the struct described by the format string.");
155
156#define CALCSIZE_METHODDEF \
157 {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
158
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200159static Py_ssize_t
160calcsize_impl(PyObject *module, PyStructObject *s_object);
161
162static PyObject *
163calcsize(PyObject *module, PyObject *arg)
164{
165 PyObject *return_value = NULL;
166 PyStructObject *s_object = NULL;
167 Py_ssize_t _return_value;
168
169 if (!PyArg_Parse(arg, "O&:calcsize", cache_struct_converter, &s_object)) {
170 goto exit;
171 }
172 _return_value = calcsize_impl(module, s_object);
173 if ((_return_value == -1) && PyErr_Occurred()) {
174 goto exit;
175 }
176 return_value = PyLong_FromSsize_t(_return_value);
177
178exit:
179 /* Cleanup for s_object */
180 Py_XDECREF(s_object);
181
182 return return_value;
183}
184
Victor Stinner3f2d1012017-02-02 12:09:30 +0100185PyDoc_STRVAR(unpack__doc__,
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100186"unpack($module, format, buffer, /)\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +0100187"--\n"
188"\n"
189"Return a tuple containing values unpacked according to the format string.\n"
190"\n"
191"The buffer\'s size in bytes must be calcsize(format).\n"
192"\n"
193"See help(struct) for more on format strings.");
194
195#define UNPACK_METHODDEF \
196 {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__},
197
198static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200199unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100200
201static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200202unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100203{
204 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200205 PyStructObject *s_object = NULL;
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100206 Py_buffer buffer = {NULL, NULL};
Victor Stinner3f2d1012017-02-02 12:09:30 +0100207
Sylvain74453812017-06-10 06:51:48 +0200208 if (!_PyArg_ParseStack(args, nargs, "O&y*:unpack",
209 cache_struct_converter, &s_object, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100210 goto exit;
211 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200212 return_value = unpack_impl(module, s_object, &buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100213
214exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200215 /* Cleanup for s_object */
216 Py_XDECREF(s_object);
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100217 /* Cleanup for buffer */
218 if (buffer.obj) {
219 PyBuffer_Release(&buffer);
220 }
221
Victor Stinner3f2d1012017-02-02 12:09:30 +0100222 return return_value;
223}
224
225PyDoc_STRVAR(unpack_from__doc__,
226"unpack_from($module, format, /, buffer, offset=0)\n"
227"--\n"
228"\n"
229"Return a tuple containing values unpacked according to the format string.\n"
230"\n"
231"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
232"\n"
233"See help(struct) for more on format strings.");
234
235#define UNPACK_FROM_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300236 {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
Victor Stinner3f2d1012017-02-02 12:09:30 +0100237
238static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200239unpack_from_impl(PyObject *module, PyStructObject *s_object,
240 Py_buffer *buffer, Py_ssize_t offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100241
242static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200243unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100244{
245 PyObject *return_value = NULL;
246 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200247 static _PyArg_Parser _parser = {"O&y*|n:unpack_from", _keywords, 0};
248 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100249 Py_buffer buffer = {NULL, NULL};
250 Py_ssize_t offset = 0;
251
252 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200253 cache_struct_converter, &s_object, &buffer, &offset)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100254 goto exit;
255 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200256 return_value = unpack_from_impl(module, s_object, &buffer, offset);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100257
258exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200259 /* Cleanup for s_object */
260 Py_XDECREF(s_object);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100261 /* Cleanup for buffer */
262 if (buffer.obj) {
263 PyBuffer_Release(&buffer);
264 }
265
266 return return_value;
267}
268
269PyDoc_STRVAR(iter_unpack__doc__,
270"iter_unpack($module, format, buffer, /)\n"
271"--\n"
272"\n"
273"Return an iterator yielding tuples unpacked from the given bytes.\n"
274"\n"
275"The bytes are unpacked according to the format string, like\n"
276"a repeated invocation of unpack_from().\n"
277"\n"
278"Requires that the bytes length be a multiple of the format struct size.");
279
280#define ITER_UNPACK_METHODDEF \
281 {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
282
283static PyObject *
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200284iter_unpack_impl(PyObject *module, PyStructObject *s_object,
285 PyObject *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100286
287static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200288iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Victor Stinner3f2d1012017-02-02 12:09:30 +0100289{
290 PyObject *return_value = NULL;
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200291 PyStructObject *s_object = NULL;
Victor Stinner3f2d1012017-02-02 12:09:30 +0100292 PyObject *buffer;
293
Sylvain74453812017-06-10 06:51:48 +0200294 if (!_PyArg_ParseStack(args, nargs, "O&O:iter_unpack",
295 cache_struct_converter, &s_object, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100296 goto exit;
297 }
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200298 return_value = iter_unpack_impl(module, s_object, buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100299
300exit:
Serhiy Storchakaa5a55902017-02-04 11:14:52 +0200301 /* Cleanup for s_object */
302 Py_XDECREF(s_object);
303
Victor Stinner3f2d1012017-02-02 12:09:30 +0100304 return return_value;
305}
Xiang Zhangc10b2882018-03-11 02:58:52 +0800306/*[clinic end generated code: output=d79b009652ae0b89 input=a9049054013a1b77]*/