diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Modules/_struct.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Modules/_struct.c')
-rw-r--r-- | contrib/tools/python3/src/Modules/_struct.c | 486 |
1 files changed, 243 insertions, 243 deletions
diff --git a/contrib/tools/python3/src/Modules/_struct.c b/contrib/tools/python3/src/Modules/_struct.c index f759f0b169..e98b68b74b 100644 --- a/contrib/tools/python3/src/Modules/_struct.c +++ b/contrib/tools/python3/src/Modules/_struct.c @@ -6,7 +6,7 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "structmember.h" // PyMemberDef +#include "structmember.h" // PyMemberDef #include <ctype.h> /*[clinic input] @@ -14,24 +14,24 @@ class Struct "PyStructObject *" "&PyStructType" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b032058a83ed7c3]*/ -typedef struct { - PyObject *PyStructType; - PyObject *unpackiter_type; - PyObject *StructError; -} _structmodulestate; - -static inline _structmodulestate* -get_struct_state(PyObject *module) -{ - void *state = PyModule_GetState(module); - assert(state != NULL); - return (_structmodulestate *)state; -} - -static struct PyModuleDef _structmodule; - -#define _structmodulestate_global get_struct_state(PyState_FindModule(&_structmodule)) - +typedef struct { + PyObject *PyStructType; + PyObject *unpackiter_type; + PyObject *StructError; +} _structmodulestate; + +static inline _structmodulestate* +get_struct_state(PyObject *module) +{ + void *state = PyModule_GetState(module); + assert(state != NULL); + return (_structmodulestate *)state; +} + +static struct PyModuleDef _structmodule; + +#define _structmodulestate_global get_struct_state(PyState_FindModule(&_structmodule)) + /* The translation function for each format character is table driven */ typedef struct _formatdef { char format; @@ -62,8 +62,8 @@ typedef struct { } PyStructObject; -#define PyStruct_Check(op) PyObject_TypeCheck(op, (PyTypeObject *)_structmodulestate_global->PyStructType) -#define PyStruct_CheckExact(op) Py_IS_TYPE(op, (PyTypeObject *)_structmodulestate_global->PyStructType) +#define PyStruct_Check(op) PyObject_TypeCheck(op, (PyTypeObject *)_structmodulestate_global->PyStructType) +#define PyStruct_CheckExact(op) Py_IS_TYPE(op, (PyTypeObject *)_structmodulestate_global->PyStructType) /* Define various structs to figure out the alignments of types */ @@ -107,7 +107,7 @@ class cache_struct_converter(CConverter): [python start generated code]*/ /*[python end generated code: output=da39a3ee5e6b4b0d input=49957cca130ffb63]*/ -static int cache_struct_converter(PyObject *, PyStructObject **); +static int cache_struct_converter(PyObject *, PyStructObject **); #include "clinic/_struct.c.h" @@ -126,7 +126,7 @@ get_pylong(PyObject *v) return NULL; } else { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not an integer"); return NULL; } @@ -154,7 +154,7 @@ get_long(PyObject *v, long *p) Py_DECREF(v); if (x == (long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -178,7 +178,7 @@ get_ulong(PyObject *v, unsigned long *p) Py_DECREF(v); if (x == (unsigned long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -201,7 +201,7 @@ get_longlong(PyObject *v, long long *p) Py_DECREF(v); if (x == (long long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -224,7 +224,7 @@ get_ulonglong(PyObject *v, unsigned long long *p) Py_DECREF(v); if (x == (unsigned long long)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -247,7 +247,7 @@ get_ssize_t(PyObject *v, Py_ssize_t *p) Py_DECREF(v); if (x == (Py_ssize_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -270,7 +270,7 @@ get_size_t(PyObject *v, size_t *p) Py_DECREF(v); if (x == (size_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument out of range"); return -1; } @@ -304,7 +304,7 @@ pack_halffloat(char *p, /* start of 2-byte string */ { double x = PyFloat_AsDouble(v); if (x == -1.0 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -350,13 +350,13 @@ _range_error(const formatdef *f, int is_unsigned) const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); if (is_unsigned) - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "'%c' format requires 0 <= number <= %zu", f->format, ulargest); else { const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "'%c' format requires %zd <= number <= %zd", f->format, ~ largest, @@ -372,8 +372,8 @@ _range_error(const formatdef *f, int is_unsigned) [bln][up]_TYPE - [bln] distinguishes among big-endian, little-endian and native. - [pu] distinguishes between pack (to struct) and unpack (from struct). + [bln] distinguishes among big-endian, little-endian and native. + [pu] distinguishes between pack (to struct) and unpack (from struct). TYPE is one of char, byte, ubyte, etc. */ @@ -535,7 +535,7 @@ np_byte(char *p, PyObject *v, const formatdef *f) if (get_long(v, &x) < 0) return -1; if (x < -128 || x > 127) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "byte format requires -128 <= number <= 127"); return -1; } @@ -550,7 +550,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f) if (get_long(v, &x) < 0) return -1; if (x < 0 || x > 255) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "ubyte format requires 0 <= number <= 255"); return -1; } @@ -561,8 +561,8 @@ np_ubyte(char *p, PyObject *v, const formatdef *f) static int np_char(char *p, PyObject *v, const formatdef *f) { - if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { - PyErr_SetString(_structmodulestate_global->StructError, + if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { + PyErr_SetString(_structmodulestate_global->StructError, "char format requires a bytes object of length 1"); return -1; } @@ -578,7 +578,7 @@ np_short(char *p, PyObject *v, const formatdef *f) if (get_long(v, &x) < 0) return -1; if (x < SHRT_MIN || x > SHRT_MAX) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "short format requires " Py_STRINGIFY(SHRT_MIN) " <= number <= " Py_STRINGIFY(SHRT_MAX)); return -1; @@ -596,7 +596,7 @@ np_ushort(char *p, PyObject *v, const formatdef *f) if (get_long(v, &x) < 0) return -1; if (x < 0 || x > USHRT_MAX) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "ushort format requires 0 <= number <= " Py_STRINGIFY(USHRT_MAX)); return -1; @@ -727,7 +727,7 @@ np_float(char *p, PyObject *v, const formatdef *f) { float x = (float)PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -740,7 +740,7 @@ np_double(char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -959,7 +959,7 @@ bp_float(char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -971,7 +971,7 @@ bp_double(char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -1174,7 +1174,7 @@ lp_float(char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -1186,7 +1186,7 @@ lp_double(char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "required argument is not a float"); return -1; } @@ -1253,7 +1253,7 @@ getentry(int c, const formatdef *f) return f; } } - PyErr_SetString(_structmodulestate_global->StructError, "bad char in struct format"); + PyErr_SetString(_structmodulestate_global->StructError, "bad char in struct format"); return NULL; } @@ -1296,11 +1296,11 @@ prepare_s(PyStructObject *self) size_t ncodes; fmt = PyBytes_AS_STRING(self->s_format); - if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) { - PyErr_SetString(_structmodulestate_global->StructError, - "embedded null character"); - return -1; - } + if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) { + PyErr_SetString(_structmodulestate_global->StructError, + "embedded null character"); + return -1; + } f = whichtable(&fmt); @@ -1309,7 +1309,7 @@ prepare_s(PyStructObject *self) len = 0; ncodes = 0; while ((c = *s++) != '\0') { - if (Py_ISSPACE(c)) + if (Py_ISSPACE(c)) continue; if ('0' <= c && c <= '9') { num = c - '0'; @@ -1323,7 +1323,7 @@ prepare_s(PyStructObject *self) num = num*10 + (c - '0'); } if (c == '\0') { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "repeat count given without format specifier"); return -1; } @@ -1374,7 +1374,7 @@ prepare_s(PyStructObject *self) s = fmt; size = 0; while ((c = *s++) != '\0') { - if (Py_ISSPACE(c)) + if (Py_ISSPACE(c)) continue; if ('0' <= c && c <= '9') { num = c - '0'; @@ -1413,7 +1413,7 @@ prepare_s(PyStructObject *self) return 0; overflow: - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "total struct size too long"); return -1; } @@ -1423,11 +1423,11 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *self; - assert(type != NULL); - allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc); - assert(alloc_func != NULL); + assert(type != NULL); + allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc); + assert(alloc_func != NULL); - self = alloc_func(type, 0); + self = alloc_func(type, 0); if (self != NULL) { PyStructObject *s = (PyStructObject*)self; Py_INCREF(Py_None); @@ -1473,7 +1473,7 @@ Struct___init___impl(PyStructObject *self, PyObject *format) PyErr_Format(PyExc_TypeError, "Struct() argument 1 must be a str or bytes object, " "not %.200s", - _PyType_Name(Py_TYPE(format))); + _PyType_Name(Py_TYPE(format))); return -1; } @@ -1486,16 +1486,16 @@ Struct___init___impl(PyStructObject *self, PyObject *format) static void s_dealloc(PyStructObject *s) { - PyTypeObject *tp = Py_TYPE(s); + PyTypeObject *tp = Py_TYPE(s); if (s->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *)s); if (s->s_codes != NULL) { PyMem_FREE(s->s_codes); } - Py_XDECREF(s->s_format); - freefunc free_func = PyType_GetSlot(Py_TYPE(s), Py_tp_free); - free_func(s); - Py_DECREF(tp); + Py_XDECREF(s->s_format); + freefunc free_func = PyType_GetSlot(Py_TYPE(s), Py_tp_free); + free_func(s); + Py_DECREF(tp); } static PyObject * @@ -1556,7 +1556,7 @@ Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer) { assert(self->s_codes != NULL); if (buffer->len != self->s_size) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "unpack requires a buffer of %zd bytes", self->s_size); return NULL; @@ -1574,8 +1574,8 @@ Return a tuple containing unpacked values. Values are unpacked according to the format string Struct.format. -The buffer's size in bytes, starting at position offset, must be -at least Struct.size. +The buffer's size in bytes, starting at position offset, must be +at least Struct.size. See help(struct) for more on format strings. [clinic start generated code]*/ @@ -1583,38 +1583,38 @@ See help(struct) for more on format strings. static PyObject * Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer, Py_ssize_t offset) -/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/ +/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/ { assert(self->s_codes != NULL); - if (offset < 0) { - if (offset + self->s_size > 0) { - PyErr_Format(_structmodulestate_global->StructError, - "not enough data to unpack %zd bytes at offset %zd", - self->s_size, - offset); - return NULL; - } - - if (offset + buffer->len < 0) { - PyErr_Format(_structmodulestate_global->StructError, - "offset %zd out of range for %zd-byte buffer", - offset, - buffer->len); - return NULL; - } + if (offset < 0) { + if (offset + self->s_size > 0) { + PyErr_Format(_structmodulestate_global->StructError, + "not enough data to unpack %zd bytes at offset %zd", + self->s_size, + offset); + return NULL; + } + + if (offset + buffer->len < 0) { + PyErr_Format(_structmodulestate_global->StructError, + "offset %zd out of range for %zd-byte buffer", + offset, + buffer->len); + return NULL; + } offset += buffer->len; - } - - if ((buffer->len - offset) < self->s_size) { - PyErr_Format(_structmodulestate_global->StructError, - "unpack_from requires a buffer of at least %zu bytes for " - "unpacking %zd bytes at offset %zd " - "(actual buffer size is %zd)", - (size_t)self->s_size + (size_t)offset, - self->s_size, - offset, - buffer->len); + } + + if ((buffer->len - offset) < self->s_size) { + PyErr_Format(_structmodulestate_global->StructError, + "unpack_from requires a buffer of at least %zu bytes for " + "unpacking %zd bytes at offset %zd " + "(actual buffer size is %zd)", + (size_t)self->s_size + (size_t)offset, + self->s_size, + offset, + buffer->len); return NULL; } return s_unpack_internal(self, (char*)buffer->buf + offset); @@ -1635,25 +1635,25 @@ static void unpackiter_dealloc(unpackiterobject *self) { /* bpo-31095: UnTrack is needed before calling any callbacks */ - PyTypeObject *tp = Py_TYPE(self); + PyTypeObject *tp = Py_TYPE(self); PyObject_GC_UnTrack(self); Py_XDECREF(self->so); PyBuffer_Release(&self->buf); PyObject_GC_Del(self); - Py_DECREF(tp); + Py_DECREF(tp); } static int unpackiter_traverse(unpackiterobject *self, visitproc visit, void *arg) { - Py_VISIT(Py_TYPE(self)); + Py_VISIT(Py_TYPE(self)); Py_VISIT(self->so); Py_VISIT(self->buf.obj); return 0; } static PyObject * -unpackiter_len(unpackiterobject *self, PyObject *Py_UNUSED(ignored)) +unpackiter_len(unpackiterobject *self, PyObject *Py_UNUSED(ignored)) { Py_ssize_t len; if (self->so == NULL) @@ -1687,30 +1687,30 @@ unpackiter_iternext(unpackiterobject *self) return result; } -PyObject *unpackiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyErr_Format(PyExc_TypeError, "Cannot create '%.200s objects", _PyType_Name(type)); - return NULL; -} - -static PyType_Slot unpackiter_type_slots[] = { - {Py_tp_dealloc, unpackiter_dealloc}, - {Py_tp_getattro, PyObject_GenericGetAttr}, - {Py_tp_traverse, unpackiter_traverse}, - {Py_tp_iter, PyObject_SelfIter}, - {Py_tp_iternext, unpackiter_iternext}, - {Py_tp_methods, unpackiter_methods}, - {Py_tp_new, unpackiter_new}, - {0, 0}, -}; - -static PyType_Spec unpackiter_type_spec = { - "_struct.unpack_iterator", - sizeof(unpackiterobject), - 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, - unpackiter_type_slots +PyObject *unpackiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + PyErr_Format(PyExc_TypeError, "Cannot create '%.200s objects", _PyType_Name(type)); + return NULL; +} + +static PyType_Slot unpackiter_type_slots[] = { + {Py_tp_dealloc, unpackiter_dealloc}, + {Py_tp_getattro, PyObject_GenericGetAttr}, + {Py_tp_traverse, unpackiter_traverse}, + {Py_tp_iter, PyObject_SelfIter}, + {Py_tp_iternext, unpackiter_iternext}, + {Py_tp_methods, unpackiter_methods}, + {Py_tp_new, unpackiter_new}, + {0, 0}, }; +static PyType_Spec unpackiter_type_spec = { + "_struct.unpack_iterator", + sizeof(unpackiterobject), + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + unpackiter_type_slots +}; + /*[clinic input] Struct.iter_unpack @@ -1734,12 +1734,12 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer) assert(self->s_codes != NULL); if (self->s_size == 0) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "cannot iteratively unpack with a struct of length 0"); return NULL; } - iter = (unpackiterobject *) PyType_GenericAlloc((PyTypeObject *)_structmodulestate_global->unpackiter_type, 0); + iter = (unpackiterobject *) PyType_GenericAlloc((PyTypeObject *)_structmodulestate_global->unpackiter_type, 0); if (iter == NULL) return NULL; @@ -1748,7 +1748,7 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer) return NULL; } if (iter->buf.len % self->s_size != 0) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "iterative unpacking requires a buffer of " "a multiple of %zd bytes", self->s_size); @@ -1791,10 +1791,10 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, char* if (e->format == 's') { Py_ssize_t n; int isstring; - const void *p; + const void *p; isstring = PyBytes_Check(v); if (!isstring && !PyByteArray_Check(v)) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument for 's' must be a bytes object"); return -1; } @@ -1813,10 +1813,10 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, char* } else if (e->format == 'p') { Py_ssize_t n; int isstring; - const void *p; + const void *p; isstring = PyBytes_Check(v); if (!isstring && !PyByteArray_Check(v)) { - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "argument for 'p' must be a bytes object"); return -1; } @@ -1838,7 +1838,7 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, char* } else { if (e->pack(res, v, e) < 0) { if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) - PyErr_SetString(_structmodulestate_global->StructError, + PyErr_SetString(_structmodulestate_global->StructError, "int too large to convert"); return -1; } @@ -1862,7 +1862,7 @@ strings."); static PyObject * s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { - char *buf; + char *buf; PyStructObject *soself; /* Validate arguments. */ @@ -1871,27 +1871,27 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs) assert(soself->s_codes != NULL); if (nargs != soself->s_len) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "pack expected %zd items for packing (got %zd)", soself->s_len, nargs); return NULL; } - /* Allocate a new string */ - _PyBytesWriter writer; - _PyBytesWriter_Init(&writer); - buf = _PyBytesWriter_Alloc(&writer, soself->s_size); - if (buf == NULL) { - _PyBytesWriter_Dealloc(&writer); + /* Allocate a new string */ + _PyBytesWriter writer; + _PyBytesWriter_Init(&writer); + buf = _PyBytesWriter_Alloc(&writer, soself->s_size); + if (buf == NULL) { + _PyBytesWriter_Dealloc(&writer); return NULL; - } + } /* Call the guts */ - if ( s_pack_internal(soself, args, 0, buf) != 0 ) { - _PyBytesWriter_Dealloc(&writer); + if ( s_pack_internal(soself, args, 0, buf) != 0 ) { + _PyBytesWriter_Dealloc(&writer); return NULL; } - return _PyBytesWriter_Finish(&writer, buf + soself->s_size); + return _PyBytesWriter_Finish(&writer, buf + soself->s_size); } PyDoc_STRVAR(s_pack_into__doc__, @@ -1916,15 +1916,15 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs) if (nargs != (soself->s_len + 2)) { if (nargs == 0) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "pack_into expected buffer argument"); } else if (nargs == 1) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "pack_into expected offset argument"); } else { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "pack_into expected %zd items for packing (got %zd)", soself->s_len, (nargs - 2)); } @@ -1947,7 +1947,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs) if (offset < 0) { /* Check that negative offset is low enough to fit data */ if (offset + soself->s_size > 0) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "no space to pack %zd bytes at offset %zd", soself->s_size, offset); @@ -1957,7 +1957,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs) /* Check that negative offset is not crossing buffer boundary */ if (offset + buffer.len < 0) { - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "offset %zd out of range for %zd-byte buffer", offset, buffer.len); @@ -1973,7 +1973,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs) assert(offset >= 0); assert(soself->s_size >= 0); - PyErr_Format(_structmodulestate_global->StructError, + PyErr_Format(_structmodulestate_global->StructError, "pack_into requires a buffer of at least %zu bytes for " "packing %zd bytes at offset %zd " "(actual buffer size is %zd)", @@ -2027,19 +2027,19 @@ s_sizeof(PyStructObject *self, void *unused) static struct PyMethodDef s_methods[] = { STRUCT_ITER_UNPACK_METHODDEF - {"pack", (PyCFunction)(void(*)(void))s_pack, METH_FASTCALL, s_pack__doc__}, - {"pack_into", (PyCFunction)(void(*)(void))s_pack_into, METH_FASTCALL, s_pack_into__doc__}, + {"pack", (PyCFunction)(void(*)(void))s_pack, METH_FASTCALL, s_pack__doc__}, + {"pack_into", (PyCFunction)(void(*)(void))s_pack_into, METH_FASTCALL, s_pack_into__doc__}, STRUCT_UNPACK_METHODDEF STRUCT_UNPACK_FROM_METHODDEF {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__}, {NULL, NULL} /* sentinel */ }; -static PyMemberDef s_members[] = { - {"__weaklistoffset__", T_PYSSIZET, offsetof(PyStructObject, weakreflist), READONLY}, - {NULL} /* sentinel */ -}; - +static PyMemberDef s_members[] = { + {"__weaklistoffset__", T_PYSSIZET, offsetof(PyStructObject, weakreflist), READONLY}, + {NULL} /* sentinel */ +}; + #define OFF(x) offsetof(PyStructObject, x) static PyGetSetDef s_getsetlist[] = { @@ -2048,32 +2048,32 @@ static PyGetSetDef s_getsetlist[] = { {NULL} /* sentinel */ }; -PyDoc_STRVAR(s__doc__, -"Struct(fmt) --> compiled struct object\n" -"\n" -); - -static PyType_Slot PyStructType_slots[] = { - {Py_tp_dealloc, s_dealloc}, - {Py_tp_getattro, PyObject_GenericGetAttr}, - {Py_tp_setattro, PyObject_GenericSetAttr}, - {Py_tp_doc, (void*)s__doc__}, - {Py_tp_methods, s_methods}, - {Py_tp_members, s_members}, - {Py_tp_getset, s_getsetlist}, - {Py_tp_init, Struct___init__}, - {Py_tp_alloc, PyType_GenericAlloc}, - {Py_tp_new, s_new}, - {Py_tp_free, PyObject_Del}, - {0, 0}, -}; - -static PyType_Spec PyStructType_spec = { - "_struct.Struct", +PyDoc_STRVAR(s__doc__, +"Struct(fmt) --> compiled struct object\n" +"\n" +); + +static PyType_Slot PyStructType_slots[] = { + {Py_tp_dealloc, s_dealloc}, + {Py_tp_getattro, PyObject_GenericGetAttr}, + {Py_tp_setattro, PyObject_GenericSetAttr}, + {Py_tp_doc, (void*)s__doc__}, + {Py_tp_methods, s_methods}, + {Py_tp_members, s_members}, + {Py_tp_getset, s_getsetlist}, + {Py_tp_init, Struct___init__}, + {Py_tp_alloc, PyType_GenericAlloc}, + {Py_tp_new, s_new}, + {Py_tp_free, PyObject_Del}, + {0, 0}, +}; + +static PyType_Spec PyStructType_spec = { + "_struct.Struct", sizeof(PyStructObject), 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - PyStructType_slots + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + PyStructType_slots }; @@ -2083,7 +2083,7 @@ static PyType_Spec PyStructType_spec = { static PyObject *cache = NULL; static int -cache_struct_converter(PyObject *fmt, PyStructObject **ptr) +cache_struct_converter(PyObject *fmt, PyStructObject **ptr) { PyObject * s_object; @@ -2099,24 +2099,24 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr) return 0; } - s_object = PyDict_GetItemWithError(cache, fmt); + s_object = PyDict_GetItemWithError(cache, fmt); if (s_object != NULL) { Py_INCREF(s_object); - *ptr = (PyStructObject *)s_object; + *ptr = (PyStructObject *)s_object; return Py_CLEANUP_SUPPORTED; } - else if (PyErr_Occurred()) { - return 0; - } + else if (PyErr_Occurred()) { + return 0; + } - s_object = PyObject_CallOneArg(_structmodulestate_global->PyStructType, fmt); + s_object = PyObject_CallOneArg(_structmodulestate_global->PyStructType, fmt); if (s_object != NULL) { if (PyDict_GET_SIZE(cache) >= MAXCACHE) PyDict_Clear(cache); /* Attempt to cache the result */ if (PyDict_SetItem(cache, fmt, s_object) == -1) PyErr_Clear(); - *ptr = (PyStructObject *)s_object; + *ptr = (PyStructObject *)s_object; return Py_CLEANUP_SUPPORTED; } return 0; @@ -2171,7 +2171,7 @@ pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } format = args[0]; - if (!cache_struct_converter(format, (PyStructObject **)&s_object)) { + if (!cache_struct_converter(format, (PyStructObject **)&s_object)) { return NULL; } result = s_pack(s_object, args + 1, nargs - 1); @@ -2199,7 +2199,7 @@ pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } format = args[0]; - if (!cache_struct_converter(format, (PyStructObject **)&s_object)) { + if (!cache_struct_converter(format, (PyStructObject **)&s_object)) { return NULL; } result = s_pack_into(s_object, args + 1, nargs - 1); @@ -2278,8 +2278,8 @@ static struct PyMethodDef module_functions[] = { _CLEARCACHE_METHODDEF CALCSIZE_METHODDEF ITER_UNPACK_METHODDEF - {"pack", (PyCFunction)(void(*)(void))pack, METH_FASTCALL, pack_doc}, - {"pack_into", (PyCFunction)(void(*)(void))pack_into, METH_FASTCALL, pack_into_doc}, + {"pack", (PyCFunction)(void(*)(void))pack, METH_FASTCALL, pack_doc}, + {"pack_into", (PyCFunction)(void(*)(void))pack_into, METH_FASTCALL, pack_into_doc}, UNPACK_METHODDEF UNPACK_FROM_METHODDEF {NULL, NULL} /* sentinel */ @@ -2319,46 +2319,46 @@ Whitespace between formats is ignored.\n\ The variable struct.error is an exception raised on errors.\n"); -static int -_structmodule_traverse(PyObject *module, visitproc visit, void *arg) -{ - _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module); - if (state) { - Py_VISIT(state->PyStructType); - Py_VISIT(state->unpackiter_type); - Py_VISIT(state->StructError); - } - return 0; -} - -static int -_structmodule_clear(PyObject *module) -{ - _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module); - if (state) { - Py_CLEAR(state->PyStructType); - Py_CLEAR(state->unpackiter_type); - Py_CLEAR(state->StructError); - } - return 0; -} - -static void -_structmodule_free(void *module) -{ - _structmodule_clear((PyObject *)module); -} - +static int +_structmodule_traverse(PyObject *module, visitproc visit, void *arg) +{ + _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module); + if (state) { + Py_VISIT(state->PyStructType); + Py_VISIT(state->unpackiter_type); + Py_VISIT(state->StructError); + } + return 0; +} + +static int +_structmodule_clear(PyObject *module) +{ + _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module); + if (state) { + Py_CLEAR(state->PyStructType); + Py_CLEAR(state->unpackiter_type); + Py_CLEAR(state->StructError); + } + return 0; +} + +static void +_structmodule_free(void *module) +{ + _structmodule_clear((PyObject *)module); +} + static struct PyModuleDef _structmodule = { PyModuleDef_HEAD_INIT, "_struct", module_doc, - sizeof(_structmodulestate), + sizeof(_structmodulestate), module_functions, NULL, - _structmodule_traverse, - _structmodule_clear, - _structmodule_free, + _structmodule_traverse, + _structmodule_clear, + _structmodule_free, }; PyMODINIT_FUNC @@ -2370,19 +2370,19 @@ PyInit__struct(void) if (m == NULL) return NULL; - PyObject *PyStructType = PyType_FromSpec(&PyStructType_spec); - if (PyStructType == NULL) { + PyObject *PyStructType = PyType_FromSpec(&PyStructType_spec); + if (PyStructType == NULL) { return NULL; - } - Py_INCREF(PyStructType); - PyModule_AddObject(m, "Struct", PyStructType); - get_struct_state(m)->PyStructType = PyStructType; + } + Py_INCREF(PyStructType); + PyModule_AddObject(m, "Struct", PyStructType); + get_struct_state(m)->PyStructType = PyStructType; - PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec); - if (unpackiter_type == NULL) { + PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec); + if (unpackiter_type == NULL) { return NULL; - } - get_struct_state(m)->unpackiter_type = unpackiter_type; + } + get_struct_state(m)->unpackiter_type = unpackiter_type; /* Check endian and swap in faster functions */ { @@ -2413,9 +2413,9 @@ PyInit__struct(void) "unknown" float format */ if (ptr->format == 'd' || ptr->format == 'f') break; - /* Skip _Bool, semantics are different for standard size */ - if (ptr->format == '?') - break; + /* Skip _Bool, semantics are different for standard size */ + if (ptr->format == '?') + break; ptr->pack = native->pack; ptr->unpack = native->unpack; break; @@ -2427,12 +2427,12 @@ PyInit__struct(void) } /* Add some symbolic constants to the module */ - PyObject *StructError = PyErr_NewException("struct.error", NULL, NULL); - if (StructError == NULL) - return NULL; + PyObject *StructError = PyErr_NewException("struct.error", NULL, NULL); + if (StructError == NULL) + return NULL; Py_INCREF(StructError); PyModule_AddObject(m, "error", StructError); - get_struct_state(m)->StructError = StructError; + get_struct_state(m)->StructError = StructError; return m; } |