• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 PyDoc_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 
16 static int
17 Struct___init___impl(PyStructObject *self, PyObject *format);
18 
19 static int
Struct___init__(PyObject * self,PyObject * args,PyObject * kwargs)20 Struct___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 
33 exit:
34     return return_value;
35 }
36 
37 PyDoc_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 
51 static PyObject *
52 Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
53 
54 static PyObject *
Struct_unpack(PyStructObject * self,PyObject * arg)55 Struct_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 
65 exit:
66     /* Cleanup for buffer */
67     if (buffer.obj) {
68        PyBuffer_Release(&buffer);
69     }
70 
71     return return_value;
72 }
73 
74 PyDoc_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|METH_KEYWORDS, Struct_unpack_from__doc__},
88 
89 static PyObject *
90 Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
91                         Py_ssize_t offset);
92 
93 static PyObject *
Struct_unpack_from(PyStructObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)94 Struct_unpack_from(PyStructObject *self, PyObject *const *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 
108 exit:
109     /* Cleanup for buffer */
110     if (buffer.obj) {
111        PyBuffer_Release(&buffer);
112     }
113 
114     return return_value;
115 }
116 
117 PyDoc_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 
131 PyDoc_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 
140 static PyObject *
141 _clearcache_impl(PyObject *module);
142 
143 static PyObject *
_clearcache(PyObject * module,PyObject * Py_UNUSED (ignored))144 _clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
145 {
146     return _clearcache_impl(module);
147 }
148 
149 PyDoc_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 
158 static Py_ssize_t
159 calcsize_impl(PyObject *module, PyStructObject *s_object);
160 
161 static PyObject *
calcsize(PyObject * module,PyObject * arg)162 calcsize(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 
177 exit:
178     /* Cleanup for s_object */
179     Py_XDECREF(s_object);
180 
181     return return_value;
182 }
183 
184 PyDoc_STRVAR(unpack__doc__,
185 "unpack($module, format, buffer, /)\n"
186 "--\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 
197 static PyObject *
198 unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer);
199 
200 static PyObject *
unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)201 unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
202 {
203     PyObject *return_value = NULL;
204     PyStructObject *s_object = NULL;
205     Py_buffer buffer = {NULL, NULL};
206 
207     if (!_PyArg_ParseStack(args, nargs, "O&y*:unpack",
208         cache_struct_converter, &s_object, &buffer)) {
209         goto exit;
210     }
211     return_value = unpack_impl(module, s_object, &buffer);
212 
213 exit:
214     /* Cleanup for s_object */
215     Py_XDECREF(s_object);
216     /* Cleanup for buffer */
217     if (buffer.obj) {
218        PyBuffer_Release(&buffer);
219     }
220 
221     return return_value;
222 }
223 
224 PyDoc_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    \
235     {"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__},
236 
237 static PyObject *
238 unpack_from_impl(PyObject *module, PyStructObject *s_object,
239                  Py_buffer *buffer, Py_ssize_t offset);
240 
241 static PyObject *
unpack_from(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)242 unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
243 {
244     PyObject *return_value = NULL;
245     static const char * const _keywords[] = {"", "buffer", "offset", NULL};
246     static _PyArg_Parser _parser = {"O&y*|n:unpack_from", _keywords, 0};
247     PyStructObject *s_object = NULL;
248     Py_buffer buffer = {NULL, NULL};
249     Py_ssize_t offset = 0;
250 
251     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
252         cache_struct_converter, &s_object, &buffer, &offset)) {
253         goto exit;
254     }
255     return_value = unpack_from_impl(module, s_object, &buffer, offset);
256 
257 exit:
258     /* Cleanup for s_object */
259     Py_XDECREF(s_object);
260     /* Cleanup for buffer */
261     if (buffer.obj) {
262        PyBuffer_Release(&buffer);
263     }
264 
265     return return_value;
266 }
267 
268 PyDoc_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 
282 static PyObject *
283 iter_unpack_impl(PyObject *module, PyStructObject *s_object,
284                  PyObject *buffer);
285 
286 static PyObject *
iter_unpack(PyObject * module,PyObject * const * args,Py_ssize_t nargs)287 iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
288 {
289     PyObject *return_value = NULL;
290     PyStructObject *s_object = NULL;
291     PyObject *buffer;
292 
293     if (!_PyArg_ParseStack(args, nargs, "O&O:iter_unpack",
294         cache_struct_converter, &s_object, &buffer)) {
295         goto exit;
296     }
297     return_value = iter_unpack_impl(module, s_object, buffer);
298 
299 exit:
300     /* Cleanup for s_object */
301     Py_XDECREF(s_object);
302 
303     return return_value;
304 }
305 /*[clinic end generated code: output=9119f213a951e4cc input=a9049054013a1b77]*/
306