| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_gc.h" // PyGC_Head |
| 7 |
# include "pycore_runtime.h" // _Py_ID() |
| 8 |
#endif |
| 9 |
#include "pycore_abstract.h" // _PyNumber_Index() |
| 10 |
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
| 11 |
|
| 12 |
PyDoc_STRVAR(Struct___init____doc__, |
| 13 |
"Struct(format)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"Create a compiled struct object.\n" |
| 17 |
"\n" |
| 18 |
"Return a new Struct object which writes and reads binary data according to\n" |
| 19 |
"the format string.\n" |
| 20 |
"\n" |
| 21 |
"See help(struct) for more on format strings."); |
| 22 |
|
| 23 |
static int |
| 24 |
Struct___init___impl(PyStructObject *self, PyObject *format); |
| 25 |
|
| 26 |
static int |
| 27 |
Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 28 |
{ |
| 29 |
int return_value = -1; |
| 30 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 31 |
|
| 32 |
#define NUM_KEYWORDS 1 |
| 33 |
static struct { |
| 34 |
PyGC_Head _this_is_not_used; |
| 35 |
PyObject_VAR_HEAD |
| 36 |
Py_hash_t ob_hash; |
| 37 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 38 |
} _kwtuple = { |
| 39 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 40 |
.ob_hash = -1, |
| 41 |
.ob_item = { &_Py_ID(format), }, |
| 42 |
}; |
| 43 |
#undef NUM_KEYWORDS |
| 44 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 45 |
|
| 46 |
#else // !Py_BUILD_CORE |
| 47 |
# define KWTUPLE NULL |
| 48 |
#endif // !Py_BUILD_CORE |
| 49 |
|
| 50 |
static const char * const _keywords[] = {"format", NULL}; |
| 51 |
static _PyArg_Parser _parser = { |
| 52 |
.keywords = _keywords, |
| 53 |
.fname = "Struct", |
| 54 |
.kwtuple = KWTUPLE, |
| 55 |
}; |
| 56 |
#undef KWTUPLE |
| 57 |
PyObject *argsbuf[1]; |
| 58 |
PyObject * const *fastargs; |
| 59 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 60 |
PyObject *format; |
| 61 |
|
| 62 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
| 63 |
/*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 64 |
if (!fastargs) { |
| 65 |
goto exit; |
| 66 |
} |
| 67 |
format = fastargs[0]; |
| 68 |
return_value = Struct___init___impl((PyStructObject *)self, format); |
| 69 |
|
| 70 |
exit: |
| 71 |
return return_value; |
| 72 |
} |
| 73 |
|
| 74 |
PyDoc_STRVAR(Struct_unpack__doc__, |
| 75 |
"unpack($self, buffer, /)\n" |
| 76 |
"--\n" |
| 77 |
"\n" |
| 78 |
"Return a tuple containing unpacked values.\n" |
| 79 |
"\n" |
| 80 |
"Unpack according to the format string Struct.format. The buffer\'s size\n" |
| 81 |
"in bytes must be Struct.size.\n" |
| 82 |
"\n" |
| 83 |
"See help(struct) for more on format strings."); |
| 84 |
|
| 85 |
#define STRUCT_UNPACK_METHODDEF \ |
| 86 |
{"unpack", (PyCFunction)Struct_unpack, METH_O, Struct_unpack__doc__}, |
| 87 |
|
| 88 |
static PyObject * |
| 89 |
Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer); |
| 90 |
|
| 91 |
static PyObject * |
| 92 |
Struct_unpack(PyObject *self, PyObject *arg) |
| 93 |
{ |
| 94 |
PyObject *return_value = NULL; |
| 95 |
Py_buffer buffer = {NULL, NULL}; |
| 96 |
|
| 97 |
if (PyObject_GetBuffer(arg, &buffer, PyBUF_SIMPLE) != 0) { |
| 98 |
goto exit; |
| 99 |
} |
| 100 |
return_value = Struct_unpack_impl((PyStructObject *)self, &buffer); |
| 101 |
|
| 102 |
exit: |
| 103 |
/* Cleanup for buffer */ |
| 104 |
if (buffer.obj) { |
| 105 |
PyBuffer_Release(&buffer); |
| 106 |
} |
| 107 |
|
| 108 |
return return_value; |
| 109 |
} |
| 110 |
|
| 111 |
PyDoc_STRVAR(Struct_unpack_from__doc__, |
| 112 |
"unpack_from($self, /, buffer, offset=0)\n" |
| 113 |
"--\n" |
| 114 |
"\n" |
| 115 |
"Return a tuple containing unpacked values.\n" |
| 116 |
"\n" |
| 117 |
"Values are unpacked according to the format string Struct.format.\n" |
| 118 |
"\n" |
| 119 |
"The buffer\'s size in bytes, starting at position offset, must be\n" |
| 120 |
"at least Struct.size.\n" |
| 121 |
"\n" |
| 122 |
"See help(struct) for more on format strings."); |
| 123 |
|
| 124 |
#define STRUCT_UNPACK_FROM_METHODDEF \ |
| 125 |
{"unpack_from", _PyCFunction_CAST(Struct_unpack_from), METH_FASTCALL|METH_KEYWORDS, Struct_unpack_from__doc__}, |
| 126 |
|
| 127 |
static PyObject * |
| 128 |
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer, |
| 129 |
Py_ssize_t offset); |
| 130 |
|
| 131 |
static PyObject * |
| 132 |
Struct_unpack_from(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 133 |
{ |
| 134 |
PyObject *return_value = NULL; |
| 135 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 136 |
|
| 137 |
#define NUM_KEYWORDS 2 |
| 138 |
static struct { |
| 139 |
PyGC_Head _this_is_not_used; |
| 140 |
PyObject_VAR_HEAD |
| 141 |
Py_hash_t ob_hash; |
| 142 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 143 |
} _kwtuple = { |
| 144 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 145 |
.ob_hash = -1, |
| 146 |
.ob_item = { &_Py_ID(buffer), &_Py_ID(offset), }, |
| 147 |
}; |
| 148 |
#undef NUM_KEYWORDS |
| 149 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 150 |
|
| 151 |
#else // !Py_BUILD_CORE |
| 152 |
# define KWTUPLE NULL |
| 153 |
#endif // !Py_BUILD_CORE |
| 154 |
|
| 155 |
static const char * const _keywords[] = {"buffer", "offset", NULL}; |
| 156 |
static _PyArg_Parser _parser = { |
| 157 |
.keywords = _keywords, |
| 158 |
.fname = "unpack_from", |
| 159 |
.kwtuple = KWTUPLE, |
| 160 |
}; |
| 161 |
#undef KWTUPLE |
| 162 |
PyObject *argsbuf[2]; |
| 163 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
| 164 |
Py_buffer buffer = {NULL, NULL}; |
| 165 |
Py_ssize_t offset = 0; |
| 166 |
|
| 167 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 168 |
/*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 169 |
if (!args) { |
| 170 |
goto exit; |
| 171 |
} |
| 172 |
if (PyObject_GetBuffer(args[0], &buffer, PyBUF_SIMPLE) != 0) { |
| 173 |
goto exit; |
| 174 |
} |
| 175 |
if (!noptargs) { |
| 176 |
goto skip_optional_pos; |
| 177 |
} |
| 178 |
{ |
| 179 |
Py_ssize_t ival = -1; |
| 180 |
PyObject *iobj = _PyNumber_Index(args[1]); |
| 181 |
if (iobj != NULL) { |
| 182 |
ival = PyLong_AsSsize_t(iobj); |
| 183 |
Py_DECREF(iobj); |
| 184 |
} |
| 185 |
if (ival == -1 && PyErr_Occurred()) { |
| 186 |
goto exit; |
| 187 |
} |
| 188 |
offset = ival; |
| 189 |
} |
| 190 |
skip_optional_pos: |
| 191 |
return_value = Struct_unpack_from_impl((PyStructObject *)self, &buffer, offset); |
| 192 |
|
| 193 |
exit: |
| 194 |
/* Cleanup for buffer */ |
| 195 |
if (buffer.obj) { |
| 196 |
PyBuffer_Release(&buffer); |
| 197 |
} |
| 198 |
|
| 199 |
return return_value; |
| 200 |
} |
| 201 |
|
| 202 |
PyDoc_STRVAR(Struct_iter_unpack__doc__, |
| 203 |
"iter_unpack($self, buffer, /)\n" |
| 204 |
"--\n" |
| 205 |
"\n" |
| 206 |
"Return an iterator yielding tuples.\n" |
| 207 |
"\n" |
| 208 |
"Tuples are unpacked from the given bytes source, like a repeated\n" |
| 209 |
"invocation of unpack_from().\n" |
| 210 |
"\n" |
| 211 |
"Requires that the bytes length be a multiple of the struct size."); |
| 212 |
|
| 213 |
#define STRUCT_ITER_UNPACK_METHODDEF \ |
| 214 |
{"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__}, |
| 215 |
|
| 216 |
static PyObject * |
| 217 |
Struct_iter_unpack_impl(PyStructObject *self, PyObject *buffer); |
| 218 |
|
| 219 |
static PyObject * |
| 220 |
Struct_iter_unpack(PyObject *self, PyObject *buffer) |
| 221 |
{ |
| 222 |
PyObject *return_value = NULL; |
| 223 |
|
| 224 |
return_value = Struct_iter_unpack_impl((PyStructObject *)self, buffer); |
| 225 |
|
| 226 |
return return_value; |
| 227 |
} |
| 228 |
|
| 229 |
PyDoc_STRVAR(_clearcache__doc__, |
| 230 |
"_clearcache($module, /)\n" |
| 231 |
"--\n" |
| 232 |
"\n" |
| 233 |
"Clear the internal cache."); |
| 234 |
|
| 235 |
#define _CLEARCACHE_METHODDEF \ |
| 236 |
{"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__}, |
| 237 |
|
| 238 |
static PyObject * |
| 239 |
_clearcache_impl(PyObject *module); |
| 240 |
|
| 241 |
static PyObject * |
| 242 |
_clearcache(PyObject *module, PyObject *Py_UNUSED(ignored)) |
| 243 |
{ |
| 244 |
return _clearcache_impl(module); |
| 245 |
} |
| 246 |
|
| 247 |
PyDoc_STRVAR(calcsize__doc__, |
| 248 |
"calcsize($module, format, /)\n" |
| 249 |
"--\n" |
| 250 |
"\n" |
| 251 |
"Return size in bytes of the struct described by the format string."); |
| 252 |
|
| 253 |
#define CALCSIZE_METHODDEF \ |
| 254 |
{"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__}, |
| 255 |
|
| 256 |
static Py_ssize_t |
| 257 |
calcsize_impl(PyObject *module, PyStructObject *s_object); |
| 258 |
|
| 259 |
static PyObject * |
| 260 |
calcsize(PyObject *module, PyObject *arg) |
| 261 |
{ |
| 262 |
PyObject *return_value = NULL; |
| 263 |
PyStructObject *s_object = NULL; |
| 264 |
Py_ssize_t _return_value; |
| 265 |
|
| 266 |
if (!cache_struct_converter(module, arg, &s_object)) { |
| 267 |
goto exit; |
| 268 |
} |
| 269 |
_return_value = calcsize_impl(module, s_object); |
| 270 |
if ((_return_value == -1) && PyErr_Occurred()) { |
| 271 |
goto exit; |
| 272 |
} |
| 273 |
return_value = PyLong_FromSsize_t(_return_value); |
| 274 |
|
| 275 |
exit: |
| 276 |
/* Cleanup for s_object */ |
| 277 |
Py_XDECREF(s_object); |
| 278 |
|
| 279 |
return return_value; |
| 280 |
} |
| 281 |
|
| 282 |
PyDoc_STRVAR(unpack__doc__, |
| 283 |
"unpack($module, format, buffer, /)\n" |
| 284 |
"--\n" |
| 285 |
"\n" |
| 286 |
"Return a tuple containing values unpacked according to the format string.\n" |
| 287 |
"\n" |
| 288 |
"The buffer\'s size in bytes must be calcsize(format).\n" |
| 289 |
"\n" |
| 290 |
"See help(struct) for more on format strings."); |
| 291 |
|
| 292 |
#define UNPACK_METHODDEF \ |
| 293 |
{"unpack", _PyCFunction_CAST(unpack), METH_FASTCALL, unpack__doc__}, |
| 294 |
|
| 295 |
static PyObject * |
| 296 |
unpack_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer); |
| 297 |
|
| 298 |
static PyObject * |
| 299 |
unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 300 |
{ |
| 301 |
PyObject *return_value = NULL; |
| 302 |
PyStructObject *s_object = NULL; |
| 303 |
Py_buffer buffer = {NULL, NULL}; |
| 304 |
|
| 305 |
if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) { |
| 306 |
goto exit; |
| 307 |
} |
| 308 |
if (!cache_struct_converter(module, args[0], &s_object)) { |
| 309 |
goto exit; |
| 310 |
} |
| 311 |
if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) { |
| 312 |
goto exit; |
| 313 |
} |
| 314 |
return_value = unpack_impl(module, s_object, &buffer); |
| 315 |
|
| 316 |
exit: |
| 317 |
/* Cleanup for s_object */ |
| 318 |
Py_XDECREF(s_object); |
| 319 |
/* Cleanup for buffer */ |
| 320 |
if (buffer.obj) { |
| 321 |
PyBuffer_Release(&buffer); |
| 322 |
} |
| 323 |
|
| 324 |
return return_value; |
| 325 |
} |
| 326 |
|
| 327 |
PyDoc_STRVAR(unpack_from__doc__, |
| 328 |
"unpack_from($module, format, /, buffer, offset=0)\n" |
| 329 |
"--\n" |
| 330 |
"\n" |
| 331 |
"Return a tuple containing values unpacked according to the format string.\n" |
| 332 |
"\n" |
| 333 |
"The buffer\'s size, minus offset, must be at least calcsize(format).\n" |
| 334 |
"\n" |
| 335 |
"See help(struct) for more on format strings."); |
| 336 |
|
| 337 |
#define UNPACK_FROM_METHODDEF \ |
| 338 |
{"unpack_from", _PyCFunction_CAST(unpack_from), METH_FASTCALL|METH_KEYWORDS, unpack_from__doc__}, |
| 339 |
|
| 340 |
static PyObject * |
| 341 |
unpack_from_impl(PyObject *module, PyStructObject *s_object, |
| 342 |
Py_buffer *buffer, Py_ssize_t offset); |
| 343 |
|
| 344 |
static PyObject * |
| 345 |
unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 346 |
{ |
| 347 |
PyObject *return_value = NULL; |
| 348 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 349 |
|
| 350 |
#define NUM_KEYWORDS 2 |
| 351 |
static struct { |
| 352 |
PyGC_Head _this_is_not_used; |
| 353 |
PyObject_VAR_HEAD |
| 354 |
Py_hash_t ob_hash; |
| 355 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 356 |
} _kwtuple = { |
| 357 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 358 |
.ob_hash = -1, |
| 359 |
.ob_item = { &_Py_ID(buffer), &_Py_ID(offset), }, |
| 360 |
}; |
| 361 |
#undef NUM_KEYWORDS |
| 362 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 363 |
|
| 364 |
#else // !Py_BUILD_CORE |
| 365 |
# define KWTUPLE NULL |
| 366 |
#endif // !Py_BUILD_CORE |
| 367 |
|
| 368 |
static const char * const _keywords[] = {"", "buffer", "offset", NULL}; |
| 369 |
static _PyArg_Parser _parser = { |
| 370 |
.keywords = _keywords, |
| 371 |
.fname = "unpack_from", |
| 372 |
.kwtuple = KWTUPLE, |
| 373 |
}; |
| 374 |
#undef KWTUPLE |
| 375 |
PyObject *argsbuf[3]; |
| 376 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
| 377 |
PyStructObject *s_object = NULL; |
| 378 |
Py_buffer buffer = {NULL, NULL}; |
| 379 |
Py_ssize_t offset = 0; |
| 380 |
|
| 381 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 382 |
/*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 383 |
if (!args) { |
| 384 |
goto exit; |
| 385 |
} |
| 386 |
if (!cache_struct_converter(module, args[0], &s_object)) { |
| 387 |
goto exit; |
| 388 |
} |
| 389 |
if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) { |
| 390 |
goto exit; |
| 391 |
} |
| 392 |
if (!noptargs) { |
| 393 |
goto skip_optional_pos; |
| 394 |
} |
| 395 |
{ |
| 396 |
Py_ssize_t ival = -1; |
| 397 |
PyObject *iobj = _PyNumber_Index(args[2]); |
| 398 |
if (iobj != NULL) { |
| 399 |
ival = PyLong_AsSsize_t(iobj); |
| 400 |
Py_DECREF(iobj); |
| 401 |
} |
| 402 |
if (ival == -1 && PyErr_Occurred()) { |
| 403 |
goto exit; |
| 404 |
} |
| 405 |
offset = ival; |
| 406 |
} |
| 407 |
skip_optional_pos: |
| 408 |
return_value = unpack_from_impl(module, s_object, &buffer, offset); |
| 409 |
|
| 410 |
exit: |
| 411 |
/* Cleanup for s_object */ |
| 412 |
Py_XDECREF(s_object); |
| 413 |
/* Cleanup for buffer */ |
| 414 |
if (buffer.obj) { |
| 415 |
PyBuffer_Release(&buffer); |
| 416 |
} |
| 417 |
|
| 418 |
return return_value; |
| 419 |
} |
| 420 |
|
| 421 |
PyDoc_STRVAR(iter_unpack__doc__, |
| 422 |
"iter_unpack($module, format, buffer, /)\n" |
| 423 |
"--\n" |
| 424 |
"\n" |
| 425 |
"Return an iterator yielding tuples unpacked from the given bytes.\n" |
| 426 |
"\n" |
| 427 |
"The bytes are unpacked according to the format string, like\n" |
| 428 |
"a repeated invocation of unpack_from().\n" |
| 429 |
"\n" |
| 430 |
"Requires that the bytes length be a multiple of the format struct size."); |
| 431 |
|
| 432 |
#define ITER_UNPACK_METHODDEF \ |
| 433 |
{"iter_unpack", _PyCFunction_CAST(iter_unpack), METH_FASTCALL, iter_unpack__doc__}, |
| 434 |
|
| 435 |
static PyObject * |
| 436 |
iter_unpack_impl(PyObject *module, PyStructObject *s_object, |
| 437 |
PyObject *buffer); |
| 438 |
|
| 439 |
static PyObject * |
| 440 |
iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 441 |
{ |
| 442 |
PyObject *return_value = NULL; |
| 443 |
PyStructObject *s_object = NULL; |
| 444 |
PyObject *buffer; |
| 445 |
|
| 446 |
if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) { |
| 447 |
goto exit; |
| 448 |
} |
| 449 |
if (!cache_struct_converter(module, args[0], &s_object)) { |
| 450 |
goto exit; |
| 451 |
} |
| 452 |
buffer = args[1]; |
| 453 |
return_value = iter_unpack_impl(module, s_object, buffer); |
| 454 |
|
| 455 |
exit: |
| 456 |
/* Cleanup for s_object */ |
| 457 |
Py_XDECREF(s_object); |
| 458 |
|
| 459 |
return return_value; |
| 460 |
} |
| 461 |
/*[clinic end generated code: output=caa7f36443e91cb9 input=a9049054013a1b77]*/ |