blob: f9c3a5ce66fda52147a11a569d94b3b960bb1e61 [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
158PyDoc_STRVAR(unpack__doc__,
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100159"unpack($module, format, buffer, /)\n"
Victor Stinner3f2d1012017-02-02 12:09:30 +0100160"--\n"
161"\n"
162"Return a tuple containing values unpacked according to the format string.\n"
163"\n"
164"The buffer\'s size in bytes must be calcsize(format).\n"
165"\n"
166"See help(struct) for more on format strings.");
167
168#define UNPACK_METHODDEF \
169 {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__},
170
171static PyObject *
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100172unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100173
174static PyObject *
175unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
176{
177 PyObject *return_value = NULL;
178 PyObject *format;
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100179 Py_buffer buffer = {NULL, NULL};
Victor Stinner3f2d1012017-02-02 12:09:30 +0100180
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100181 if (!_PyArg_ParseStack(args, nargs, "Oy*:unpack",
182 &format, &buffer)) {
Victor Stinner3f2d1012017-02-02 12:09:30 +0100183 goto exit;
184 }
185
186 if (!_PyArg_NoStackKeywords("unpack", kwnames)) {
187 goto exit;
188 }
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100189 return_value = unpack_impl(module, format, &buffer);
Victor Stinner3f2d1012017-02-02 12:09:30 +0100190
191exit:
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100192 /* Cleanup for buffer */
193 if (buffer.obj) {
194 PyBuffer_Release(&buffer);
195 }
196
Victor Stinner3f2d1012017-02-02 12:09:30 +0100197 return return_value;
198}
199
200PyDoc_STRVAR(unpack_from__doc__,
201"unpack_from($module, format, /, buffer, offset=0)\n"
202"--\n"
203"\n"
204"Return a tuple containing values unpacked according to the format string.\n"
205"\n"
206"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
207"\n"
208"See help(struct) for more on format strings.");
209
210#define UNPACK_FROM_METHODDEF \
211 {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL, unpack_from__doc__},
212
213static PyObject *
214unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer,
215 Py_ssize_t offset);
216
217static PyObject *
218unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
219{
220 PyObject *return_value = NULL;
221 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
222 static _PyArg_Parser _parser = {"Oy*|n:unpack_from", _keywords, 0};
223 PyObject *format;
224 Py_buffer buffer = {NULL, NULL};
225 Py_ssize_t offset = 0;
226
227 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
228 &format, &buffer, &offset)) {
229 goto exit;
230 }
231 return_value = unpack_from_impl(module, format, &buffer, offset);
232
233exit:
234 /* Cleanup for buffer */
235 if (buffer.obj) {
236 PyBuffer_Release(&buffer);
237 }
238
239 return return_value;
240}
241
242PyDoc_STRVAR(iter_unpack__doc__,
243"iter_unpack($module, format, buffer, /)\n"
244"--\n"
245"\n"
246"Return an iterator yielding tuples unpacked from the given bytes.\n"
247"\n"
248"The bytes are unpacked according to the format string, like\n"
249"a repeated invocation of unpack_from().\n"
250"\n"
251"Requires that the bytes length be a multiple of the format struct size.");
252
253#define ITER_UNPACK_METHODDEF \
254 {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
255
256static PyObject *
257iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer);
258
259static PyObject *
260iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
261{
262 PyObject *return_value = NULL;
263 PyObject *format;
264 PyObject *buffer;
265
266 if (!_PyArg_UnpackStack(args, nargs, "iter_unpack",
267 2, 2,
268 &format, &buffer)) {
269 goto exit;
270 }
271
272 if (!_PyArg_NoStackKeywords("iter_unpack", kwnames)) {
273 goto exit;
274 }
275 return_value = iter_unpack_impl(module, format, buffer);
276
277exit:
278 return return_value;
279}
Victor Stinnerc0f59ad2017-02-02 14:24:16 +0100280/*[clinic end generated code: output=0714090a5d0ea8ce input=a9049054013a1b77]*/