blob: f05ea5307218c8df835976836c67e13dc10c8607 [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__,
159"unpack($module, format, inputstr, /)\n"
160"--\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 *
172unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr);
173
174static PyObject *
175unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
176{
177 PyObject *return_value = NULL;
178 PyObject *format;
179 PyObject *inputstr;
180
181 if (!_PyArg_UnpackStack(args, nargs, "unpack",
182 2, 2,
183 &format, &inputstr)) {
184 goto exit;
185 }
186
187 if (!_PyArg_NoStackKeywords("unpack", kwnames)) {
188 goto exit;
189 }
190 return_value = unpack_impl(module, format, inputstr);
191
192exit:
193 return return_value;
194}
195
196PyDoc_STRVAR(unpack_from__doc__,
197"unpack_from($module, format, /, buffer, offset=0)\n"
198"--\n"
199"\n"
200"Return a tuple containing values unpacked according to the format string.\n"
201"\n"
202"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
203"\n"
204"See help(struct) for more on format strings.");
205
206#define UNPACK_FROM_METHODDEF \
207 {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL, unpack_from__doc__},
208
209static PyObject *
210unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer,
211 Py_ssize_t offset);
212
213static PyObject *
214unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
215{
216 PyObject *return_value = NULL;
217 static const char * const _keywords[] = {"", "buffer", "offset", NULL};
218 static _PyArg_Parser _parser = {"Oy*|n:unpack_from", _keywords, 0};
219 PyObject *format;
220 Py_buffer buffer = {NULL, NULL};
221 Py_ssize_t offset = 0;
222
223 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
224 &format, &buffer, &offset)) {
225 goto exit;
226 }
227 return_value = unpack_from_impl(module, format, &buffer, offset);
228
229exit:
230 /* Cleanup for buffer */
231 if (buffer.obj) {
232 PyBuffer_Release(&buffer);
233 }
234
235 return return_value;
236}
237
238PyDoc_STRVAR(iter_unpack__doc__,
239"iter_unpack($module, format, buffer, /)\n"
240"--\n"
241"\n"
242"Return an iterator yielding tuples unpacked from the given bytes.\n"
243"\n"
244"The bytes are unpacked according to the format string, like\n"
245"a repeated invocation of unpack_from().\n"
246"\n"
247"Requires that the bytes length be a multiple of the format struct size.");
248
249#define ITER_UNPACK_METHODDEF \
250 {"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
251
252static PyObject *
253iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer);
254
255static PyObject *
256iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
257{
258 PyObject *return_value = NULL;
259 PyObject *format;
260 PyObject *buffer;
261
262 if (!_PyArg_UnpackStack(args, nargs, "iter_unpack",
263 2, 2,
264 &format, &buffer)) {
265 goto exit;
266 }
267
268 if (!_PyArg_NoStackKeywords("iter_unpack", kwnames)) {
269 goto exit;
270 }
271 return_value = iter_unpack_impl(module, format, buffer);
272
273exit:
274 return return_value;
275}
276/*[clinic end generated code: output=db8152ad222fa3d0 input=a9049054013a1b77]*/