mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-07 02:10:27 +00:00
_struct module now uses Argument Clinic
refer python/cpython@3f2d10132d
This commit is contained in:
parent
d220f6f9b2
commit
f49003241f
2 changed files with 542 additions and 247 deletions
512
third_party/python/Modules/_struct.c
vendored
512
third_party/python/Modules/_struct.c
vendored
|
@ -46,6 +46,12 @@ PYTHON_PROVIDE("_struct.unpack_from");
|
||||||
/* New version supporting byte order, alignment and size options,
|
/* New version supporting byte order, alignment and size options,
|
||||||
character strings, and unsigned numbers */
|
character strings, and unsigned numbers */
|
||||||
|
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
class Struct "PyStructObject *" "&PyStructType"
|
||||||
|
[clinic start generated code]*/
|
||||||
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b032058a83ed7c3]*/
|
||||||
|
|
||||||
static PyTypeObject PyStructType;
|
static PyTypeObject PyStructType;
|
||||||
|
|
||||||
/* The translation function for each format character is table driven */
|
/* The translation function for each format character is table driven */
|
||||||
|
@ -117,6 +123,8 @@ typedef struct { char c; long long x; } s_long_long;
|
||||||
#pragma options align=reset
|
#pragma options align=reset
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "third_party/python/Modules/clinic/_struct.inc"
|
||||||
|
|
||||||
/* Helper for integer format codes: converts an arbitrary Python object to a
|
/* Helper for integer format codes: converts an arbitrary Python object to a
|
||||||
PyLongObject if possible, otherwise fails. Caller should decref. */
|
PyLongObject if possible, otherwise fails. Caller should decref. */
|
||||||
|
|
||||||
|
@ -576,7 +584,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
|
||||||
"ubyte format requires 0 <= number <= 255");
|
"ubyte format requires 0 <= number <= 255");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
*(unsigned char *)p = (unsigned char)x;
|
*p = (char)x;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -905,7 +913,6 @@ bp_int(char *p, PyObject *v, const formatdef *f)
|
||||||
{
|
{
|
||||||
long x;
|
long x;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
unsigned char *q = (unsigned char *)p;
|
|
||||||
if (get_long(v, &x) < 0)
|
if (get_long(v, &x) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
i = f->size;
|
i = f->size;
|
||||||
|
@ -918,7 +925,7 @@ bp_int(char *p, PyObject *v, const formatdef *f)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
q[--i] = (unsigned char)(x & 0xffL);
|
p[--i] = (char)x;
|
||||||
x >>= 8;
|
x >>= 8;
|
||||||
} while (i > 0);
|
} while (i > 0);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -929,7 +936,6 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
|
||||||
{
|
{
|
||||||
unsigned long x;
|
unsigned long x;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
unsigned char *q = (unsigned char *)p;
|
|
||||||
if (get_ulong(v, &x) < 0)
|
if (get_ulong(v, &x) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
i = f->size;
|
i = f->size;
|
||||||
|
@ -940,7 +946,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
|
||||||
RANGE_ERROR(x, f, 1, maxint - 1);
|
RANGE_ERROR(x, f, 1, maxint - 1);
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
q[--i] = (unsigned char)(x & 0xffUL);
|
p[--i] = (char)x;
|
||||||
x >>= 8;
|
x >>= 8;
|
||||||
} while (i > 0);
|
} while (i > 0);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1126,7 +1132,6 @@ lp_int(char *p, PyObject *v, const formatdef *f)
|
||||||
{
|
{
|
||||||
long x;
|
long x;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
unsigned char *q = (unsigned char *)p;
|
|
||||||
if (get_long(v, &x) < 0)
|
if (get_long(v, &x) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
i = f->size;
|
i = f->size;
|
||||||
|
@ -1139,7 +1144,7 @@ lp_int(char *p, PyObject *v, const formatdef *f)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
*q++ = (unsigned char)(x & 0xffL);
|
*p++ = (char)x;
|
||||||
x >>= 8;
|
x >>= 8;
|
||||||
} while (--i > 0);
|
} while (--i > 0);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1150,7 +1155,6 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
|
||||||
{
|
{
|
||||||
unsigned long x;
|
unsigned long x;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
unsigned char *q = (unsigned char *)p;
|
|
||||||
if (get_ulong(v, &x) < 0)
|
if (get_ulong(v, &x) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
i = f->size;
|
i = f->size;
|
||||||
|
@ -1161,7 +1165,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
|
||||||
RANGE_ERROR(x, f, 1, maxint - 1);
|
RANGE_ERROR(x, f, 1, maxint - 1);
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
*q++ = (unsigned char)(x & 0xffUL);
|
*p++ = (char)x;
|
||||||
x >>= 8;
|
x >>= 8;
|
||||||
} while (--i > 0);
|
} while (--i > 0);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1340,7 +1344,7 @@ prepare_s(PyStructObject *self)
|
||||||
len = 0;
|
len = 0;
|
||||||
ncodes = 0;
|
ncodes = 0;
|
||||||
while ((c = *s++) != '\0') {
|
while ((c = *s++) != '\0') {
|
||||||
if (Py_ISSPACE(c))
|
if (Py_ISSPACE(Py_CHARMASK(c)))
|
||||||
continue;
|
continue;
|
||||||
if ('0' <= c && c <= '9') {
|
if ('0' <= c && c <= '9') {
|
||||||
num = c - '0';
|
num = c - '0';
|
||||||
|
@ -1405,7 +1409,7 @@ prepare_s(PyStructObject *self)
|
||||||
s = fmt;
|
s = fmt;
|
||||||
size = 0;
|
size = 0;
|
||||||
while ((c = *s++) != '\0') {
|
while ((c = *s++) != '\0') {
|
||||||
if (Py_ISSPACE(c))
|
if (Py_ISSPACE(Py_CHARMASK(c)))
|
||||||
continue;
|
continue;
|
||||||
if ('0' <= c && c <= '9') {
|
if ('0' <= c && c <= '9') {
|
||||||
num = c - '0';
|
num = c - '0';
|
||||||
|
@ -1470,42 +1474,46 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
Struct.__init__
|
||||||
|
|
||||||
|
format: object
|
||||||
|
|
||||||
|
Create a compiled struct object.
|
||||||
|
|
||||||
|
Return a new Struct object which writes and reads binary data according to
|
||||||
|
the format string.
|
||||||
|
|
||||||
|
See help(struct) for more on format strings.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
s_init(PyObject *self, PyObject *args, PyObject *kwds)
|
Struct___init___impl(PyStructObject *self, PyObject *format)
|
||||||
|
/*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/
|
||||||
{
|
{
|
||||||
PyStructObject *soself = (PyStructObject *)self;
|
|
||||||
PyObject *o_format = NULL;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
static char *kwlist[] = {"format", 0};
|
|
||||||
|
|
||||||
assert(PyStruct_Check(self));
|
if (PyUnicode_Check(format)) {
|
||||||
|
format = PyUnicode_AsASCIIString(format);
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
|
if (format == NULL)
|
||||||
&o_format))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (PyUnicode_Check(o_format)) {
|
|
||||||
o_format = PyUnicode_AsASCIIString(o_format);
|
|
||||||
if (o_format == NULL)
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* XXX support buffer interface, too */
|
/* XXX support buffer interface, too */
|
||||||
else {
|
else {
|
||||||
Py_INCREF(o_format);
|
Py_INCREF(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!PyBytes_Check(o_format)) {
|
if (!PyBytes_Check(format)) {
|
||||||
Py_DECREF(o_format);
|
Py_DECREF(format);
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"Struct() argument 1 must be a str or bytes object, "
|
"Struct() argument 1 must be a bytes object, not %.200s",
|
||||||
"not %.200s",
|
Py_TYPE(format)->tp_name);
|
||||||
Py_TYPE(o_format)->tp_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_XSETREF(soself->s_format, o_format);
|
Py_XSETREF(self->s_format, format);
|
||||||
|
|
||||||
ret = prepare_s(soself);
|
ret = prepare_s(self);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1559,78 +1567,69 @@ fail:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(s_unpack__doc__,
|
/*[clinic input]
|
||||||
"S.unpack(buffer) -> (v1, v2, ...)\n\
|
Struct.unpack
|
||||||
\n\
|
|
||||||
Return a tuple containing values unpacked according to the format\n\
|
buffer: Py_buffer
|
||||||
string S.format. The buffer's size in bytes must be S.size. See\n\
|
/
|
||||||
help(struct) for more on format strings.");
|
|
||||||
|
Return a tuple containing unpacked values.
|
||||||
|
|
||||||
|
Unpack according to the format string Struct.format. The buffer's size
|
||||||
|
in bytes must be Struct.size.
|
||||||
|
|
||||||
|
See help(struct) for more on format strings.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
s_unpack(PyObject *self, PyObject *input)
|
Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer)
|
||||||
|
/*[clinic end generated code: output=873a24faf02e848a input=3113f8e7038b2f6c]*/
|
||||||
{
|
{
|
||||||
Py_buffer vbuf;
|
assert(self->s_codes != NULL);
|
||||||
PyObject *result;
|
if (buffer->len != self->s_size) {
|
||||||
PyStructObject *soself = (PyStructObject *)self;
|
|
||||||
|
|
||||||
assert(PyStruct_Check(self));
|
|
||||||
assert(soself->s_codes != NULL);
|
|
||||||
if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (vbuf.len != soself->s_size) {
|
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"unpack requires a buffer of %zd bytes",
|
"unpack requires a bytes object of length %zd",
|
||||||
soself->s_size);
|
self->s_size);
|
||||||
PyBuffer_Release(&vbuf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result = s_unpack_internal(soself, vbuf.buf);
|
return s_unpack_internal(self, buffer->buf);
|
||||||
PyBuffer_Release(&vbuf);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(s_unpack_from__doc__,
|
/*[clinic input]
|
||||||
"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
|
Struct.unpack_from
|
||||||
\n\
|
|
||||||
Return a tuple containing values unpacked according to the format\n\
|
buffer: Py_buffer
|
||||||
string S.format. The buffer's size in bytes, minus offset, must be at\n\
|
offset: Py_ssize_t = 0
|
||||||
least S.size. See help(struct) for more on format strings.");
|
|
||||||
|
Return a tuple containing unpacked values.
|
||||||
|
|
||||||
|
Values are unpacked according to the format string Struct.format.
|
||||||
|
|
||||||
|
The buffer's size in bytes, minus offset, must be at least Struct.size.
|
||||||
|
|
||||||
|
See help(struct) for more on format strings.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
|
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
|
||||||
|
Py_ssize_t offset)
|
||||||
|
/*[clinic end generated code: output=57fac875e0977316 input=97ade52422f8962f]*/
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"buffer", "offset", 0};
|
assert(self->s_codes != NULL);
|
||||||
|
|
||||||
PyObject *input;
|
|
||||||
Py_ssize_t offset = 0;
|
|
||||||
Py_buffer vbuf;
|
|
||||||
PyObject *result;
|
|
||||||
PyStructObject *soself = (PyStructObject *)self;
|
|
||||||
|
|
||||||
assert(PyStruct_Check(self));
|
|
||||||
assert(soself->s_codes != NULL);
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds,
|
|
||||||
"O|n:unpack_from", kwlist,
|
|
||||||
&input, &offset))
|
|
||||||
return NULL;
|
|
||||||
if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (offset < 0)
|
if (offset < 0)
|
||||||
offset += vbuf.len;
|
offset += buffer->len;
|
||||||
if (offset < 0 || vbuf.len - offset < soself->s_size) {
|
if (offset < 0 || buffer->len - offset < self->s_size) {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"unpack_from requires a buffer of at least %zd bytes",
|
"unpack_from requires a buffer of at least %zd bytes",
|
||||||
soself->s_size);
|
self->s_size);
|
||||||
PyBuffer_Release(&vbuf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
|
return s_unpack_internal(self, (char*)buffer->buf + offset);
|
||||||
PyBuffer_Release(&vbuf);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Unpack iterator type */
|
/* Unpack iterator type */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -1643,8 +1642,6 @@ typedef struct {
|
||||||
static void
|
static void
|
||||||
unpackiter_dealloc(unpackiterobject *self)
|
unpackiter_dealloc(unpackiterobject *self)
|
||||||
{
|
{
|
||||||
/* bpo-31095: UnTrack is needed before calling any callbacks */
|
|
||||||
PyObject_GC_UnTrack(self);
|
|
||||||
Py_XDECREF(self->so);
|
Py_XDECREF(self->so);
|
||||||
PyBuffer_Release(&self->buf);
|
PyBuffer_Release(&self->buf);
|
||||||
PyObject_GC_Del(self);
|
PyObject_GC_Del(self);
|
||||||
|
@ -1694,7 +1691,7 @@ unpackiter_iternext(unpackiterobject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyTypeObject unpackiter_type = {
|
static PyTypeObject unpackiter_type = {
|
||||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
"unpack_iterator", /* tp_name */
|
"unpack_iterator", /* tp_name */
|
||||||
sizeof(unpackiterobject), /* tp_basicsize */
|
sizeof(unpackiterobject), /* tp_basicsize */
|
||||||
0, /* tp_itemsize */
|
0, /* tp_itemsize */
|
||||||
|
@ -1724,48 +1721,54 @@ static PyTypeObject unpackiter_type = {
|
||||||
unpackiter_methods /* tp_methods */
|
unpackiter_methods /* tp_methods */
|
||||||
};
|
};
|
||||||
|
|
||||||
PyDoc_STRVAR(s_iter_unpack__doc__,
|
/*[clinic input]
|
||||||
"S.iter_unpack(buffer) -> iterator(v1, v2, ...)\n\
|
Struct.iter_unpack
|
||||||
\n\
|
|
||||||
Return an iterator yielding tuples unpacked from the given bytes\n\
|
buffer: object
|
||||||
source, like a repeated invocation of unpack_from(). Requires\n\
|
/
|
||||||
that the bytes length be a multiple of the struct size.");
|
|
||||||
|
Return an iterator yielding tuples.
|
||||||
|
|
||||||
|
Tuples are unpacked from the given bytes source, like a repeated
|
||||||
|
invocation of unpack_from().
|
||||||
|
|
||||||
|
Requires that the bytes length be a multiple of the struct size.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
s_iter_unpack(PyObject *_so, PyObject *input)
|
Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
|
||||||
|
/*[clinic end generated code: output=172d83d0cd15dbab input=6d65b3f3107dbc99]*/
|
||||||
{
|
{
|
||||||
PyStructObject *so = (PyStructObject *) _so;
|
unpackiterobject *iter;
|
||||||
unpackiterobject *self;
|
|
||||||
|
|
||||||
assert(PyStruct_Check(_so));
|
assert(self->s_codes != NULL);
|
||||||
assert(so->s_codes != NULL);
|
|
||||||
|
|
||||||
if (so->s_size == 0) {
|
if (self->s_size == 0) {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"cannot iteratively unpack with a struct of length 0");
|
"cannot iteratively unpack with a struct of length 0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
self = (unpackiterobject *) PyType_GenericAlloc(&unpackiter_type, 0);
|
iter = (unpackiterobject *) PyType_GenericAlloc(&unpackiter_type, 0);
|
||||||
if (self == NULL)
|
if (iter == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (PyObject_GetBuffer(input, &self->buf, PyBUF_SIMPLE) < 0) {
|
if (PyObject_GetBuffer(buffer, &iter->buf, PyBUF_SIMPLE) < 0) {
|
||||||
Py_DECREF(self);
|
Py_DECREF(iter);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (self->buf.len % so->s_size != 0) {
|
if (iter->buf.len % self->s_size != 0) {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"iterative unpacking requires a buffer of "
|
"iterative unpacking requires a bytes length "
|
||||||
"a multiple of %zd bytes",
|
"multiple of %zd",
|
||||||
so->s_size);
|
self->s_size);
|
||||||
Py_DECREF(self);
|
Py_DECREF(iter);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Py_INCREF(so);
|
Py_INCREF(self);
|
||||||
self->so = so;
|
iter->so = self;
|
||||||
self->index = 0;
|
iter->index = 0;
|
||||||
return (PyObject *) self;
|
return (PyObject *)iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1780,20 +1783,21 @@ s_iter_unpack(PyObject *_so, PyObject *input)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
|
s_pack_internal(PyStructObject *soself, PyObject **args, int offset, char* buf)
|
||||||
{
|
{
|
||||||
formatcode *code;
|
formatcode *code;
|
||||||
/* XXX(nnorwitz): why does i need to be a local? can we use
|
/* XXX(nnorwitz): why does i need to be a local? can we use
|
||||||
the offset parameter or do we need the wider width? */
|
the offset parameter or do we need the wider width? */
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
bzero(buf, soself->s_size);
|
|
||||||
|
memset(buf, '\0', soself->s_size);
|
||||||
i = offset;
|
i = offset;
|
||||||
for (code = soself->s_codes; code->fmtdef != NULL; code++) {
|
for (code = soself->s_codes; code->fmtdef != NULL; code++) {
|
||||||
const formatdef *e = code->fmtdef;
|
const formatdef *e = code->fmtdef;
|
||||||
char *res = buf + code->offset;
|
char *res = buf + code->offset;
|
||||||
Py_ssize_t j = code->repeat;
|
Py_ssize_t j = code->repeat;
|
||||||
while (j--) {
|
while (j--) {
|
||||||
PyObject *v = PyTuple_GET_ITEM(args, i++);
|
PyObject *v = args[i++];
|
||||||
if (e->format == 's') {
|
if (e->format == 's') {
|
||||||
Py_ssize_t n;
|
Py_ssize_t n;
|
||||||
int isstring;
|
int isstring;
|
||||||
|
@ -1866,7 +1870,7 @@ to the format string S.format. See help(struct) for more on format\n\
|
||||||
strings.");
|
strings.");
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
s_pack(PyObject *self, PyObject *args)
|
s_pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
PyStructObject *soself;
|
PyStructObject *soself;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
@ -1875,10 +1879,13 @@ s_pack(PyObject *self, PyObject *args)
|
||||||
soself = (PyStructObject *)self;
|
soself = (PyStructObject *)self;
|
||||||
assert(PyStruct_Check(self));
|
assert(PyStruct_Check(self));
|
||||||
assert(soself->s_codes != NULL);
|
assert(soself->s_codes != NULL);
|
||||||
if (PyTuple_GET_SIZE(args) != soself->s_len)
|
if (nargs != soself->s_len)
|
||||||
{
|
{
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));
|
"pack expected %zd items for packing (got %zd)", soself->s_len, nargs);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (!_PyArg_NoStackKeywords("pack", kwnames)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1905,7 +1912,7 @@ offset. Note that the offset is a required argument. See\n\
|
||||||
help(struct) for more on format strings.");
|
help(struct) for more on format strings.");
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
s_pack_into(PyObject *self, PyObject *args)
|
s_pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
PyStructObject *soself;
|
PyStructObject *soself;
|
||||||
Py_buffer buffer;
|
Py_buffer buffer;
|
||||||
|
@ -1915,31 +1922,34 @@ s_pack_into(PyObject *self, PyObject *args)
|
||||||
soself = (PyStructObject *)self;
|
soself = (PyStructObject *)self;
|
||||||
assert(PyStruct_Check(self));
|
assert(PyStruct_Check(self));
|
||||||
assert(soself->s_codes != NULL);
|
assert(soself->s_codes != NULL);
|
||||||
if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
|
if (nargs != (soself->s_len + 2))
|
||||||
{
|
{
|
||||||
if (PyTuple_GET_SIZE(args) == 0) {
|
if (nargs == 0) {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"pack_into expected buffer argument");
|
"pack_into expected buffer argument");
|
||||||
}
|
}
|
||||||
else if (PyTuple_GET_SIZE(args) == 1) {
|
else if (nargs == 1) {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"pack_into expected offset argument");
|
"pack_into expected offset argument");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Format(StructError,
|
PyErr_Format(StructError,
|
||||||
"pack_into expected %zd items for packing (got %zd)",
|
"pack_into expected %zd items for packing (got %zd)",
|
||||||
soself->s_len, (PyTuple_GET_SIZE(args) - 2));
|
soself->s_len, (nargs - 2));
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (!_PyArg_NoStackKeywords("pack_into", kwnames)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Extract a writable memory buffer from the first argument */
|
/* Extract a writable memory buffer from the first argument */
|
||||||
if (!PyArg_Parse(PyTuple_GET_ITEM(args, 0), "w*", &buffer))
|
if (!PyArg_Parse(args[0], "w*", &buffer))
|
||||||
return NULL;
|
return NULL;
|
||||||
assert(buffer.len >= 0);
|
assert(buffer.len >= 0);
|
||||||
|
|
||||||
/* Extract the offset from the first argument */
|
/* Extract the offset from the first argument */
|
||||||
offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
|
offset = PyNumber_AsSsize_t(args[1], PyExc_IndexError);
|
||||||
if (offset == -1 && PyErr_Occurred()) {
|
if (offset == -1 && PyErr_Occurred()) {
|
||||||
PyBuffer_Release(&buffer);
|
PyBuffer_Release(&buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1999,22 +2009,15 @@ s_sizeof(PyStructObject *self, void *unused)
|
||||||
/* List of functions */
|
/* List of functions */
|
||||||
|
|
||||||
static struct PyMethodDef s_methods[] = {
|
static struct PyMethodDef s_methods[] = {
|
||||||
{"iter_unpack", s_iter_unpack, METH_O, s_iter_unpack__doc__},
|
STRUCT_ITER_UNPACK_METHODDEF
|
||||||
{"pack", s_pack, METH_VARARGS, s_pack__doc__},
|
{"pack", (PyCFunction)s_pack, METH_FASTCALL, s_pack__doc__},
|
||||||
{"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
|
{"pack_into", (PyCFunction)s_pack_into, METH_FASTCALL, s_pack_into__doc__},
|
||||||
{"unpack", s_unpack, METH_O, s_unpack__doc__},
|
STRUCT_UNPACK_METHODDEF
|
||||||
{"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
|
STRUCT_UNPACK_FROM_METHODDEF
|
||||||
s_unpack_from__doc__},
|
|
||||||
{"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
|
{"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
PyDoc_STRVAR(s__doc__,
|
|
||||||
"Struct(fmt) --> compiled struct object\n"
|
|
||||||
"\n"
|
|
||||||
"Return a new Struct object which writes and reads binary data according to\n"
|
|
||||||
"the format string fmt. See help(struct) for more on format strings.");
|
|
||||||
|
|
||||||
#define OFF(x) offsetof(PyStructObject, x)
|
#define OFF(x) offsetof(PyStructObject, x)
|
||||||
|
|
||||||
static PyGetSetDef s_getsetlist[] = {
|
static PyGetSetDef s_getsetlist[] = {
|
||||||
|
@ -2041,29 +2044,29 @@ PyTypeObject PyStructType = {
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
PyObject_GenericGetAttr, /* tp_getattro */
|
PyObject_GenericGetAttr, /* tp_getattro */
|
||||||
PyObject_GenericSetAttr, /* tp_setattro */
|
PyObject_GenericSetAttr, /* tp_setattro */
|
||||||
0, /* tp_as_buffer */
|
0, /* tp_as_buffer */
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
||||||
s__doc__, /* tp_doc */
|
Struct___init____doc__, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
0, /* tp_richcompare */
|
||||||
offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
|
offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
s_methods, /* tp_methods */
|
s_methods, /* tp_methods */
|
||||||
NULL, /* tp_members */
|
NULL, /* tp_members */
|
||||||
s_getsetlist, /* tp_getset */
|
s_getsetlist, /* tp_getset */
|
||||||
0, /* tp_base */
|
0, /* tp_base */
|
||||||
0, /* tp_dict */
|
0, /* tp_dict */
|
||||||
0, /* tp_descr_get */
|
0, /* tp_descr_get */
|
||||||
0, /* tp_descr_set */
|
0, /* tp_descr_set */
|
||||||
0, /* tp_dictoffset */
|
0, /* tp_dictoffset */
|
||||||
s_init, /* tp_init */
|
Struct___init__, /* tp_init */
|
||||||
PyType_GenericAlloc,/* tp_alloc */
|
PyType_GenericAlloc, /* tp_alloc */
|
||||||
s_new, /* tp_new */
|
s_new, /* tp_new */
|
||||||
PyObject_Del, /* tp_free */
|
PyObject_Del, /* tp_free */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -2072,7 +2075,7 @@ PyTypeObject PyStructType = {
|
||||||
#define MAXCACHE 100
|
#define MAXCACHE 100
|
||||||
static PyObject *cache = NULL;
|
static PyObject *cache = NULL;
|
||||||
|
|
||||||
static PyObject *
|
static PyStructObject *
|
||||||
cache_struct(PyObject *fmt)
|
cache_struct(PyObject *fmt)
|
||||||
{
|
{
|
||||||
PyObject * s_object;
|
PyObject * s_object;
|
||||||
|
@ -2086,202 +2089,214 @@ cache_struct(PyObject *fmt)
|
||||||
s_object = PyDict_GetItem(cache, fmt);
|
s_object = PyDict_GetItem(cache, fmt);
|
||||||
if (s_object != NULL) {
|
if (s_object != NULL) {
|
||||||
Py_INCREF(s_object);
|
Py_INCREF(s_object);
|
||||||
return s_object;
|
return (PyStructObject *)s_object;
|
||||||
}
|
}
|
||||||
|
|
||||||
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
|
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
|
||||||
if (s_object != NULL) {
|
if (s_object != NULL) {
|
||||||
if (PyDict_Size(cache) >= MAXCACHE)
|
if (PyDict_GET_SIZE(cache) >= MAXCACHE)
|
||||||
PyDict_Clear(cache);
|
PyDict_Clear(cache);
|
||||||
/* Attempt to cache the result */
|
/* Attempt to cache the result */
|
||||||
if (PyDict_SetItem(cache, fmt, s_object) == -1)
|
if (PyDict_SetItem(cache, fmt, s_object) == -1)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
}
|
}
|
||||||
return s_object;
|
return (PyStructObject *)s_object;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(clearcache_doc,
|
/*[clinic input]
|
||||||
"Clear the internal cache.");
|
_clearcache
|
||||||
|
|
||||||
|
Clear the internal cache.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
clearcache(PyObject *self)
|
_clearcache_impl(PyObject *module)
|
||||||
|
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
|
||||||
{
|
{
|
||||||
Py_CLEAR(cache);
|
Py_CLEAR(cache);
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(calcsize_doc,
|
|
||||||
"calcsize(fmt) -> integer\n\
|
/*[clinic input]
|
||||||
\n\
|
calcsize
|
||||||
Return size in bytes of the struct described by the format string fmt.");
|
|
||||||
|
format: object
|
||||||
|
/
|
||||||
|
|
||||||
|
Return size in bytes of the struct described by the format string.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
calcsize(PyObject *self, PyObject *fmt)
|
calcsize(PyObject *module, PyObject *format)
|
||||||
|
/*[clinic end generated code: output=90fbcf191fe9470a input=55488303a06777fa]*/
|
||||||
{
|
{
|
||||||
Py_ssize_t n;
|
Py_ssize_t n;
|
||||||
PyObject *s_object = cache_struct(fmt);
|
PyStructObject *s_object = cache_struct(format);
|
||||||
if (s_object == NULL)
|
if (s_object == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
n = ((PyStructObject *)s_object)->s_size;
|
n = s_object->s_size;
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return PyLong_FromSsize_t(n);
|
return PyLong_FromSsize_t(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(pack_doc,
|
PyDoc_STRVAR(pack_doc,
|
||||||
"pack(fmt, v1, v2, ...) -> bytes\n\
|
"pack(format, v1, v2, ...) -> bytes\n\
|
||||||
\n\
|
\n\
|
||||||
Return a bytes object containing the values v1, v2, ... packed according\n\
|
Return a bytes object containing the values v1, v2, ... packed according\n\
|
||||||
to the format string fmt. See help(struct) for more on format strings.");
|
to the format string. See help(struct) for more on format strings.");
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
pack(PyObject *self, PyObject *args)
|
pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
PyObject *s_object, *fmt, *newargs, *result;
|
PyStructObject *s_object;
|
||||||
Py_ssize_t n = PyTuple_GET_SIZE(args);
|
PyObject *format, *result;
|
||||||
|
|
||||||
if (n == 0) {
|
if (nargs == 0) {
|
||||||
PyErr_SetString(PyExc_TypeError, "missing format argument");
|
PyErr_SetString(PyExc_TypeError, "missing format argument");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fmt = PyTuple_GET_ITEM(args, 0);
|
format = args[0];
|
||||||
newargs = PyTuple_GetSlice(args, 1, n);
|
|
||||||
if (newargs == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
s_object = cache_struct(fmt);
|
s_object = cache_struct(format);
|
||||||
if (s_object == NULL) {
|
if (s_object == NULL) {
|
||||||
Py_DECREF(newargs);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result = s_pack(s_object, newargs);
|
result = s_pack((PyObject *)s_object, args + 1, nargs - 1, kwnames);
|
||||||
Py_DECREF(newargs);
|
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(pack_into_doc,
|
PyDoc_STRVAR(pack_into_doc,
|
||||||
"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
|
"pack_into(format, buffer, offset, v1, v2, ...)\n\
|
||||||
\n\
|
\n\
|
||||||
Pack the values v1, v2, ... according to the format string fmt and write\n\
|
Pack the values v1, v2, ... according to the format string and write\n\
|
||||||
the packed bytes into the writable buffer buf starting at offset. Note\n\
|
the packed bytes into the writable buffer buf starting at offset. Note\n\
|
||||||
that the offset is a required argument. See help(struct) for more\n\
|
that the offset is a required argument. See help(struct) for more\n\
|
||||||
on format strings.");
|
on format strings.");
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
pack_into(PyObject *self, PyObject *args)
|
pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
PyObject *s_object, *fmt, *newargs, *result;
|
PyStructObject *s_object;
|
||||||
Py_ssize_t n = PyTuple_GET_SIZE(args);
|
PyObject *format, *result;
|
||||||
|
|
||||||
if (n == 0) {
|
if (nargs == 0) {
|
||||||
PyErr_SetString(PyExc_TypeError, "missing format argument");
|
PyErr_SetString(PyExc_TypeError, "missing format argument");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fmt = PyTuple_GET_ITEM(args, 0);
|
format = args[0];
|
||||||
newargs = PyTuple_GetSlice(args, 1, n);
|
|
||||||
if (newargs == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
s_object = cache_struct(fmt);
|
s_object = cache_struct(format);
|
||||||
if (s_object == NULL) {
|
if (s_object == NULL) {
|
||||||
Py_DECREF(newargs);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result = s_pack_into(s_object, newargs);
|
result = s_pack_into((PyObject *)s_object, args + 1, nargs - 1, kwnames);
|
||||||
Py_DECREF(newargs);
|
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(unpack_doc,
|
/*[clinic input]
|
||||||
"unpack(fmt, buffer) -> (v1, v2, ...)\n\
|
unpack
|
||||||
\n\
|
|
||||||
Return a tuple containing values unpacked according to the format string\n\
|
format: object
|
||||||
fmt. The buffer's size in bytes must be calcsize(fmt). See help(struct)\n\
|
inputstr: object
|
||||||
for more on format strings.");
|
/
|
||||||
|
|
||||||
|
Return a tuple containing values unpacked according to the format string.
|
||||||
|
|
||||||
|
The buffer's size in bytes must be calcsize(format).
|
||||||
|
|
||||||
|
See help(struct) for more on format strings.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
unpack(PyObject *self, PyObject *args)
|
unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr)
|
||||||
|
/*[clinic end generated code: output=06951d66eae6d63b input=4b81d54988890f5e]*/
|
||||||
{
|
{
|
||||||
PyObject *s_object, *fmt, *inputstr, *result;
|
PyStructObject *s_object;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
|
s_object = cache_struct(format);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
s_object = cache_struct(fmt);
|
|
||||||
if (s_object == NULL)
|
if (s_object == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
result = s_unpack(s_object, inputstr);
|
result = Struct_unpack(s_object, inputstr);
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(unpack_from_doc,
|
/*[clinic input]
|
||||||
"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
|
unpack_from
|
||||||
\n\
|
|
||||||
Return a tuple containing values unpacked according to the format string\n\
|
format: object
|
||||||
fmt. The buffer's size, minus offset, must be at least calcsize(fmt).\n\
|
/
|
||||||
See help(struct) for more on format strings.");
|
buffer: Py_buffer
|
||||||
|
offset: Py_ssize_t = 0
|
||||||
|
|
||||||
|
Return a tuple containing values unpacked according to the format string.
|
||||||
|
|
||||||
|
The buffer's size, minus offset, must be at least calcsize(format).
|
||||||
|
|
||||||
|
See help(struct) for more on format strings.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
|
unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer,
|
||||||
|
Py_ssize_t offset)
|
||||||
|
/*[clinic end generated code: output=2492f0c3a0b82577 input=9ead76c6ac7164f7]*/
|
||||||
{
|
{
|
||||||
PyObject *s_object, *fmt, *newargs, *result;
|
PyStructObject *s_object;
|
||||||
Py_ssize_t n = PyTuple_GET_SIZE(args);
|
PyObject *result;
|
||||||
|
|
||||||
if (n == 0) {
|
s_object = cache_struct(format);
|
||||||
PyErr_SetString(PyExc_TypeError, "missing format argument");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
fmt = PyTuple_GET_ITEM(args, 0);
|
|
||||||
newargs = PyTuple_GetSlice(args, 1, n);
|
|
||||||
if (newargs == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
s_object = cache_struct(fmt);
|
|
||||||
if (s_object == NULL) {
|
if (s_object == NULL) {
|
||||||
Py_DECREF(newargs);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result = s_unpack_from(s_object, newargs, kwds);
|
result = Struct_unpack_from_impl(s_object, buffer, offset);
|
||||||
Py_DECREF(newargs);
|
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(iter_unpack_doc,
|
/*[clinic input]
|
||||||
"iter_unpack(fmt, buffer) -> iterator(v1, v2, ...)\n\
|
iter_unpack
|
||||||
\n\
|
|
||||||
Return an iterator yielding tuples unpacked from the given bytes\n\
|
format: object
|
||||||
source according to the format string, like a repeated invocation of\n\
|
buffer: object
|
||||||
unpack_from(). Requires that the bytes length be a multiple of the\n\
|
/
|
||||||
format struct size.");
|
|
||||||
|
Return an iterator yielding tuples unpacked from the given bytes.
|
||||||
|
|
||||||
|
The bytes are unpacked according to the format string, like
|
||||||
|
a repeated invocation of unpack_from().
|
||||||
|
|
||||||
|
Requires that the bytes length be a multiple of the format struct size.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
iter_unpack(PyObject *self, PyObject *args)
|
iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer)
|
||||||
|
/*[clinic end generated code: output=b1291e97a6d4cf3c input=8674dfd2f0dae416]*/
|
||||||
{
|
{
|
||||||
PyObject *s_object, *fmt, *input, *result;
|
PyStructObject *s_object;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "OO:iter_unpack", &fmt, &input))
|
s_object = cache_struct(format);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
s_object = cache_struct(fmt);
|
|
||||||
if (s_object == NULL)
|
if (s_object == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
result = s_iter_unpack(s_object, input);
|
|
||||||
|
result = Struct_iter_unpack(s_object, buffer);
|
||||||
Py_DECREF(s_object);
|
Py_DECREF(s_object);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct PyMethodDef module_functions[] = {
|
static struct PyMethodDef module_functions[] = {
|
||||||
{"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
|
_CLEARCACHE_METHODDEF
|
||||||
{"calcsize", calcsize, METH_O, calcsize_doc},
|
CALCSIZE_METHODDEF
|
||||||
{"iter_unpack", iter_unpack, METH_VARARGS, iter_unpack_doc},
|
ITER_UNPACK_METHODDEF
|
||||||
{"pack", pack, METH_VARARGS, pack_doc},
|
{"pack", (PyCFunction)pack, METH_FASTCALL, pack_doc},
|
||||||
{"pack_into", pack_into, METH_VARARGS, pack_into_doc},
|
{"pack_into", (PyCFunction)pack_into, METH_FASTCALL, pack_into_doc},
|
||||||
{"unpack", unpack, METH_VARARGS, unpack_doc},
|
UNPACK_METHODDEF
|
||||||
{"unpack_from", (PyCFunction)unpack_from,
|
UNPACK_FROM_METHODDEF
|
||||||
METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
|
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2344,6 +2359,9 @@ PyInit__struct(void)
|
||||||
if (PyType_Ready(&PyStructType) < 0)
|
if (PyType_Ready(&PyStructType) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
if (PyType_Ready(&unpackiter_type) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* Check endian and swap in faster functions */
|
/* Check endian and swap in faster functions */
|
||||||
{
|
{
|
||||||
const formatdef *native = native_table;
|
const formatdef *native = native_table;
|
||||||
|
|
277
third_party/python/Modules/clinic/_struct.inc
vendored
Normal file
277
third_party/python/Modules/clinic/_struct.inc
vendored
Normal file
|
@ -0,0 +1,277 @@
|
||||||
|
/* clang-format off */
|
||||||
|
/*[clinic input]
|
||||||
|
preserve
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
PyDoc_STRVAR(Struct___init____doc__,
|
||||||
|
"Struct(format)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Create a compiled struct object.\n"
|
||||||
|
"\n"
|
||||||
|
"Return a new Struct object which writes and reads binary data according to\n"
|
||||||
|
"the format string.\n"
|
||||||
|
"\n"
|
||||||
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
|
static int
|
||||||
|
Struct___init___impl(PyStructObject *self, PyObject *format);
|
||||||
|
|
||||||
|
static int
|
||||||
|
Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int return_value = -1;
|
||||||
|
static const char * const _keywords[] = {"format", NULL};
|
||||||
|
static _PyArg_Parser _parser = {"O:Struct", _keywords, 0};
|
||||||
|
PyObject *format;
|
||||||
|
|
||||||
|
if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
|
||||||
|
&format)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = Struct___init___impl((PyStructObject *)self, format);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(Struct_unpack__doc__,
|
||||||
|
"unpack($self, buffer, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return a tuple containing unpacked values.\n"
|
||||||
|
"\n"
|
||||||
|
"Unpack according to the format string Struct.format. The buffer\'s size\n"
|
||||||
|
"in bytes must be Struct.size.\n"
|
||||||
|
"\n"
|
||||||
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
|
#define STRUCT_UNPACK_METHODDEF \
|
||||||
|
{"unpack", (PyCFunction)Struct_unpack, METH_O, Struct_unpack__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
Struct_unpack(PyStructObject *self, PyObject *arg)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
Py_buffer buffer = {NULL, NULL};
|
||||||
|
|
||||||
|
if (!PyArg_Parse(arg, "y*:unpack", &buffer)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = Struct_unpack_impl(self, &buffer);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
/* Cleanup for buffer */
|
||||||
|
if (buffer.obj) {
|
||||||
|
PyBuffer_Release(&buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(Struct_unpack_from__doc__,
|
||||||
|
"unpack_from($self, /, buffer, offset=0)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return a tuple containing unpacked values.\n"
|
||||||
|
"\n"
|
||||||
|
"Values are unpacked according to the format string Struct.format.\n"
|
||||||
|
"\n"
|
||||||
|
"The buffer\'s size in bytes, minus offset, must be at least Struct.size.\n"
|
||||||
|
"\n"
|
||||||
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
|
#define STRUCT_UNPACK_FROM_METHODDEF \
|
||||||
|
{"unpack_from", (PyCFunction)Struct_unpack_from, METH_FASTCALL, Struct_unpack_from__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
|
||||||
|
Py_ssize_t offset);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
Struct_unpack_from(PyStructObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
static const char * const _keywords[] = {"buffer", "offset", NULL};
|
||||||
|
static _PyArg_Parser _parser = {"y*|n:unpack_from", _keywords, 0};
|
||||||
|
Py_buffer buffer = {NULL, NULL};
|
||||||
|
Py_ssize_t offset = 0;
|
||||||
|
|
||||||
|
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
|
||||||
|
&buffer, &offset)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = Struct_unpack_from_impl(self, &buffer, offset);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
/* Cleanup for buffer */
|
||||||
|
if (buffer.obj) {
|
||||||
|
PyBuffer_Release(&buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(Struct_iter_unpack__doc__,
|
||||||
|
"iter_unpack($self, buffer, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return an iterator yielding tuples.\n"
|
||||||
|
"\n"
|
||||||
|
"Tuples are unpacked from the given bytes source, like a repeated\n"
|
||||||
|
"invocation of unpack_from().\n"
|
||||||
|
"\n"
|
||||||
|
"Requires that the bytes length be a multiple of the struct size.");
|
||||||
|
|
||||||
|
#define STRUCT_ITER_UNPACK_METHODDEF \
|
||||||
|
{"iter_unpack", (PyCFunction)Struct_iter_unpack, METH_O, Struct_iter_unpack__doc__},
|
||||||
|
|
||||||
|
PyDoc_STRVAR(_clearcache__doc__,
|
||||||
|
"_clearcache($module, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Clear the internal cache.");
|
||||||
|
|
||||||
|
#define _CLEARCACHE_METHODDEF \
|
||||||
|
{"_clearcache", (PyCFunction)_clearcache, METH_NOARGS, _clearcache__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
_clearcache_impl(PyObject *module);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
_clearcache(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||||
|
{
|
||||||
|
return _clearcache_impl(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(calcsize__doc__,
|
||||||
|
"calcsize($module, format, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return size in bytes of the struct described by the format string.");
|
||||||
|
|
||||||
|
#define CALCSIZE_METHODDEF \
|
||||||
|
{"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
|
||||||
|
|
||||||
|
PyDoc_STRVAR(unpack__doc__,
|
||||||
|
"unpack($module, format, inputstr, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return a tuple containing values unpacked according to the format string.\n"
|
||||||
|
"\n"
|
||||||
|
"The buffer\'s size in bytes must be calcsize(format).\n"
|
||||||
|
"\n"
|
||||||
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
|
#define UNPACK_METHODDEF \
|
||||||
|
{"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
PyObject *format;
|
||||||
|
PyObject *inputstr;
|
||||||
|
|
||||||
|
if (!_PyArg_UnpackStack(args, nargs, "unpack",
|
||||||
|
2, 2,
|
||||||
|
&format, &inputstr)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_PyArg_NoStackKeywords("unpack", kwnames)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = unpack_impl(module, format, inputstr);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(unpack_from__doc__,
|
||||||
|
"unpack_from($module, format, /, buffer, offset=0)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return a tuple containing values unpacked according to the format string.\n"
|
||||||
|
"\n"
|
||||||
|
"The buffer\'s size, minus offset, must be at least calcsize(format).\n"
|
||||||
|
"\n"
|
||||||
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
|
#define UNPACK_FROM_METHODDEF \
|
||||||
|
{"unpack_from", (PyCFunction)unpack_from, METH_FASTCALL, unpack_from__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
unpack_from_impl(PyObject *module, PyObject *format, Py_buffer *buffer,
|
||||||
|
Py_ssize_t offset);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
unpack_from(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
static const char * const _keywords[] = {"", "buffer", "offset", NULL};
|
||||||
|
static _PyArg_Parser _parser = {"Oy*|n:unpack_from", _keywords, 0};
|
||||||
|
PyObject *format;
|
||||||
|
Py_buffer buffer = {NULL, NULL};
|
||||||
|
Py_ssize_t offset = 0;
|
||||||
|
|
||||||
|
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
|
||||||
|
&format, &buffer, &offset)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = unpack_from_impl(module, format, &buffer, offset);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
/* Cleanup for buffer */
|
||||||
|
if (buffer.obj) {
|
||||||
|
PyBuffer_Release(&buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(iter_unpack__doc__,
|
||||||
|
"iter_unpack($module, format, buffer, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Return an iterator yielding tuples unpacked from the given bytes.\n"
|
||||||
|
"\n"
|
||||||
|
"The bytes are unpacked according to the format string, like\n"
|
||||||
|
"a repeated invocation of unpack_from().\n"
|
||||||
|
"\n"
|
||||||
|
"Requires that the bytes length be a multiple of the format struct size.");
|
||||||
|
|
||||||
|
#define ITER_UNPACK_METHODDEF \
|
||||||
|
{"iter_unpack", (PyCFunction)iter_unpack, METH_FASTCALL, iter_unpack__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
iter_unpack_impl(PyObject *module, PyObject *format, PyObject *buffer);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *return_value = NULL;
|
||||||
|
PyObject *format;
|
||||||
|
PyObject *buffer;
|
||||||
|
|
||||||
|
if (!_PyArg_UnpackStack(args, nargs, "iter_unpack",
|
||||||
|
2, 2,
|
||||||
|
&format, &buffer)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_PyArg_NoStackKeywords("iter_unpack", kwnames)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = iter_unpack_impl(module, format, buffer);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
/*[clinic end generated code: output=db8152ad222fa3d0 input=a9049054013a1b77]*/
|
Loading…
Add table
Add a link
Reference in a new issue