aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/_struct.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-02-12 07:53:52 +0300
committerDaniil Cherednik <dcherednik@ydb.tech>2024-02-14 14:26:16 +0000
commit31f2a419764a8ba77c2a970cfc80056c6cd06756 (patch)
treec1995d239eba8571cefc640f6648e1d5dd4ce9e2 /contrib/tools/python3/src/Modules/_struct.c
parentfe2ef02b38d9c85d80060963b265a1df9f38c3bb (diff)
downloadydb-31f2a419764a8ba77c2a970cfc80056c6cd06756.tar.gz
Update Python from 3.11.8 to 3.12.2
Diffstat (limited to 'contrib/tools/python3/src/Modules/_struct.c')
-rw-r--r--contrib/tools/python3/src/Modules/_struct.c311
1 files changed, 209 insertions, 102 deletions
diff --git a/contrib/tools/python3/src/Modules/_struct.c b/contrib/tools/python3/src/Modules/_struct.c
index 9d66568a28..55efc0c6cf 100644
--- a/contrib/tools/python3/src/Modules/_struct.c
+++ b/contrib/tools/python3/src/Modules/_struct.c
@@ -167,9 +167,6 @@ get_long(_structmodulestate *state, PyObject *v, long *p)
x = PyLong_AsLong(v);
Py_DECREF(v);
if (x == (long)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -191,9 +188,6 @@ get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p)
x = PyLong_AsUnsignedLong(v);
Py_DECREF(v);
if (x == (unsigned long)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -214,9 +208,6 @@ get_longlong(_structmodulestate *state, PyObject *v, long long *p)
x = PyLong_AsLongLong(v);
Py_DECREF(v);
if (x == (long long)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -237,9 +228,6 @@ get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p)
x = PyLong_AsUnsignedLongLong(v);
Py_DECREF(v);
if (x == (unsigned long long)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -260,9 +248,6 @@ get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p)
x = PyLong_AsSsize_t(v);
Py_DECREF(v);
if (x == (Py_ssize_t)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -283,9 +268,6 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
x = PyLong_AsSize_t(v);
Py_DECREF(v);
if (x == (size_t)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
- "argument out of range");
return -1;
}
*p = x;
@@ -293,7 +275,7 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
}
-#define RANGE_ERROR(state, x, f, flag, mask) return _range_error(state, f, flag)
+#define RANGE_ERROR(state, f, flag) return _range_error(state, f, flag)
/* Floating point helpers */
@@ -545,12 +527,14 @@ static int
np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
if (x < -128 || x > 127) {
- PyErr_SetString(state->StructError,
- "byte format requires -128 <= number <= 127");
- return -1;
+ RANGE_ERROR(state, f, 0);
}
*p = (char)x;
return 0;
@@ -560,12 +544,14 @@ static int
np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
if (x < 0 || x > 255) {
- PyErr_SetString(state->StructError,
- "ubyte format requires 0 <= number <= 255");
- return -1;
+ RANGE_ERROR(state, f, 1);
}
*(unsigned char *)p = (unsigned char)x;
return 0;
@@ -588,13 +574,14 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
short y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
if (x < SHRT_MIN || x > SHRT_MAX) {
- PyErr_Format(state->StructError,
- "short format requires %d <= number <= %d",
- (int)SHRT_MIN, (int)SHRT_MAX);
- return -1;
+ RANGE_ERROR(state, f, 0);
}
y = (short)x;
memcpy(p, (char *)&y, sizeof y);
@@ -606,13 +593,14 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
unsigned short y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
if (x < 0 || x > USHRT_MAX) {
- PyErr_Format(state->StructError,
- "ushort format requires 0 <= number <= %u",
- (unsigned int)USHRT_MAX);
- return -1;
+ RANGE_ERROR(state, f, 1);
}
y = (unsigned short)x;
memcpy(p, (char *)&y, sizeof y);
@@ -624,11 +612,15 @@ np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
int y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
#if (SIZEOF_LONG > SIZEOF_INT)
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
- RANGE_ERROR(state, x, f, 0, -1);
+ RANGE_ERROR(state, f, 0);
#endif
y = (int)x;
memcpy(p, (char *)&y, sizeof y);
@@ -640,12 +632,16 @@ np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
unsigned int y;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT)
if (x > ((unsigned long)UINT_MAX))
- RANGE_ERROR(state, y, f, 1, -1);
+ RANGE_ERROR(state, f, 1);
#endif
memcpy(p, (char *)&y, sizeof y);
return 0;
@@ -655,8 +651,12 @@ static int
np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -665,8 +665,12 @@ static int
np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -675,8 +679,12 @@ static int
np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
Py_ssize_t x;
- if (get_ssize_t(state, v, &x) < 0)
+ if (get_ssize_t(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -685,8 +693,12 @@ static int
np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
size_t x;
- if (get_size_t(state, v, &x) < 0)
+ if (get_size_t(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -695,8 +707,16 @@ static int
np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long long x;
- if (get_longlong(state, v, &x) < 0)
+ if (get_longlong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires %lld <= number <= %lld",
+ f->format,
+ LLONG_MIN,
+ LLONG_MAX);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -705,8 +725,15 @@ static int
np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
unsigned long long x;
- if (get_ulonglong(state, v, &x) < 0)
+ if (get_ulonglong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires 0 <= number <= %llu",
+ f->format,
+ ULLONG_MAX);
+ }
return -1;
+ }
memcpy(p, (char *)&x, sizeof x);
return 0;
}
@@ -806,18 +833,37 @@ static const formatdef native_table[] = {
/* Big-endian routines. *****************************************************/
static PyObject *
+bu_short(_structmodulestate *state, const char *p, const formatdef *f)
+{
+ unsigned long x = 0;
+
+ /* This function is only ever used in the case f->size == 2. */
+ assert(f->size == 2);
+ Py_ssize_t i = 2;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | *bytes++;
+ } while (--i > 0);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x8000U) - 0x8000U;
+ return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x) : (long)x);
+}
+
+static PyObject *
bu_int(_structmodulestate *state, const char *p, const formatdef *f)
{
- long x = 0;
- Py_ssize_t i = f->size;
+ unsigned long x = 0;
+
+ /* This function is only ever used in the case f->size == 4. */
+ assert(f->size == 4);
+ Py_ssize_t i = 4;
const unsigned char *bytes = (const unsigned char *)p;
do {
x = (x<<8) | *bytes++;
} while (--i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG > f->size)
- x |= -(x & (1L << ((8 * f->size) - 1)));
- return PyLong_FromLong(x);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x80000000U) - 0x80000000U;
+ return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x) : (long)x);
}
static PyObject *
@@ -835,16 +881,19 @@ bu_uint(_structmodulestate *state, const char *p, const formatdef *f)
static PyObject *
bu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
{
- long long x = 0;
- Py_ssize_t i = f->size;
+ unsigned long long x = 0;
+
+ /* This function is only ever used in the case f->size == 8. */
+ assert(f->size == 8);
+ Py_ssize_t i = 8;
const unsigned char *bytes = (const unsigned char *)p;
do {
x = (x<<8) | *bytes++;
} while (--i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG_LONG > f->size)
- x |= -(x & ((long long)1 << ((8 * f->size) - 1)));
- return PyLong_FromLongLong(x);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x8000000000000000U) - 0x8000000000000000U;
+ return PyLong_FromLongLong(
+ x & 0x8000000000000000U ? -1 - (long long)(~x) : (long long)x);
}
static PyObject *
@@ -889,15 +938,19 @@ bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(state, x, f, 0, 0xffffL);
+ RANGE_ERROR(state, f, 0);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(state, x, f, 0, 0xffffffffL);
+ RANGE_ERROR(state, f, 0);
#endif
}
do {
@@ -913,14 +966,18 @@ bp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- RANGE_ERROR(state, x, f, 1, maxint - 1);
+ RANGE_ERROR(state, f, 1);
}
do {
q[--i] = (unsigned char)(x & 0xffUL);
@@ -942,6 +999,14 @@ bp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
0, /* little_endian */
1 /* signed */);
Py_DECREF(v);
+ if (res == -1 && PyErr_Occurred()) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires %lld <= number <= %lld",
+ f->format,
+ LLONG_MIN,
+ LLONG_MAX);
+ return -1;
+ }
return res;
}
@@ -958,6 +1023,13 @@ bp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
0, /* little_endian */
0 /* signed */);
Py_DECREF(v);
+ if (res == -1 && PyErr_Occurred()) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires 0 <= number <= %llu",
+ f->format,
+ ULLONG_MAX);
+ return -1;
+ }
return res;
}
@@ -1009,7 +1081,7 @@ static formatdef bigendian_table[] = {
{'c', 1, 0, nu_char, np_char},
{'s', 1, 0, NULL},
{'p', 1, 0, NULL},
- {'h', 2, 0, bu_int, bp_int},
+ {'h', 2, 0, bu_short, bp_int},
{'H', 2, 0, bu_uint, bp_uint},
{'i', 4, 0, bu_int, bp_int},
{'I', 4, 0, bu_uint, bp_uint},
@@ -1027,18 +1099,37 @@ static formatdef bigendian_table[] = {
/* Little-endian routines. *****************************************************/
static PyObject *
+lu_short(_structmodulestate *state, const char *p, const formatdef *f)
+{
+ unsigned long x = 0;
+
+ /* This function is only ever used in the case f->size == 2. */
+ assert(f->size == 2);
+ Py_ssize_t i = 2;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | bytes[--i];
+ } while (i > 0);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x8000U) - 0x8000U;
+ return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x) : (long)x);
+}
+
+static PyObject *
lu_int(_structmodulestate *state, const char *p, const formatdef *f)
{
- long x = 0;
- Py_ssize_t i = f->size;
+ unsigned long x = 0;
+
+ /* This function is only ever used in the case f->size == 4. */
+ assert(f->size == 4);
+ Py_ssize_t i = 4;
const unsigned char *bytes = (const unsigned char *)p;
do {
x = (x<<8) | bytes[--i];
} while (i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG > f->size)
- x |= -(x & (1L << ((8 * f->size) - 1)));
- return PyLong_FromLong(x);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x80000000U) - 0x80000000U;
+ return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x) : (long)x);
}
static PyObject *
@@ -1056,16 +1147,19 @@ lu_uint(_structmodulestate *state, const char *p, const formatdef *f)
static PyObject *
lu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
{
- long long x = 0;
- Py_ssize_t i = f->size;
+ unsigned long long x = 0;
+
+ /* This function is only ever used in the case f->size == 8. */
+ assert(f->size == 8);
+ Py_ssize_t i = 8;
const unsigned char *bytes = (const unsigned char *)p;
do {
x = (x<<8) | bytes[--i];
} while (i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG_LONG > f->size)
- x |= -(x & ((long long)1 << ((8 * f->size) - 1)));
- return PyLong_FromLongLong(x);
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
+ x = (x ^ 0x8000000000000000U) - 0x8000000000000000U;
+ return PyLong_FromLongLong(
+ x & 0x8000000000000000U ? -1 - (long long)(~x) : (long long)x);
}
static PyObject *
@@ -1104,15 +1198,19 @@ lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_long(state, v, &x) < 0)
+ if (get_long(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 0);
+ }
return -1;
+ }
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(state, x, f, 0, 0xffffL);
+ RANGE_ERROR(state, f, 0);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(state, x, f, 0, 0xffffffffL);
+ RANGE_ERROR(state, f, 0);
#endif
}
do {
@@ -1128,14 +1226,18 @@ lp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(state, v, &x) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ RANGE_ERROR(state, f, 1);
+ }
return -1;
+ }
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- RANGE_ERROR(state, x, f, 1, maxint - 1);
+ RANGE_ERROR(state, f, 1);
}
do {
*q++ = (unsigned char)(x & 0xffUL);
@@ -1157,6 +1259,14 @@ lp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
1, /* little_endian */
1 /* signed */);
Py_DECREF(v);
+ if (res == -1 && PyErr_Occurred()) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires %lld <= number <= %lld",
+ f->format,
+ LLONG_MIN,
+ LLONG_MAX);
+ return -1;
+ }
return res;
}
@@ -1173,6 +1283,13 @@ lp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
1, /* little_endian */
0 /* signed */);
Py_DECREF(v);
+ if (res == -1 && PyErr_Occurred()) {
+ PyErr_Format(state->StructError,
+ "'%c' format requires 0 <= number <= %llu",
+ f->format,
+ ULLONG_MAX);
+ return -1;
+ }
return res;
}
@@ -1213,7 +1330,7 @@ static formatdef lilendian_table[] = {
{'c', 1, 0, nu_char, np_char},
{'s', 1, 0, NULL},
{'p', 1, 0, NULL},
- {'h', 2, 0, lu_int, lp_int},
+ {'h', 2, 0, lu_short, lp_int},
{'H', 2, 0, lu_uint, lp_uint},
{'i', 4, 0, lu_int, lp_int},
{'I', 4, 0, lu_uint, lp_uint},
@@ -1721,11 +1838,6 @@ 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},
@@ -1733,7 +1845,6 @@ static PyType_Slot unpackiter_type_slots[] = {
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, unpackiter_iternext},
{Py_tp_methods, unpackiter_methods},
- {Py_tp_new, unpackiter_new},
{0, 0},
};
@@ -1742,7 +1853,7 @@ static PyType_Spec unpackiter_type_spec = {
sizeof(unpackiterobject),
0,
(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE),
+ Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
unpackiter_type_slots
};
@@ -1791,8 +1902,7 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
Py_DECREF(iter);
return NULL;
}
- Py_INCREF(self);
- iter->so = self;
+ iter->so = (PyStructObject*)Py_NewRef(self);
iter->index = 0;
return (PyObject *)iter;
}
@@ -2053,13 +2163,11 @@ PyDoc_STRVAR(s_sizeof__doc__,
static PyObject *
s_sizeof(PyStructObject *self, void *unused)
{
- Py_ssize_t size;
- formatcode *code;
-
- size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
- for (code = self->s_codes; code->fmtdef != NULL; code++)
+ size_t size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
+ for (formatcode *code = self->s_codes; code->fmtdef != NULL; code++) {
size += sizeof(formatcode);
- return PyLong_FromSsize_t(size);
+ }
+ return PyLong_FromSize_t(size);
}
/* List of functions */
@@ -2128,8 +2236,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
_structmodulestate *state = get_struct_state(module);
if (fmt == NULL) {
- Py_DECREF(*ptr);
- *ptr = NULL;
+ Py_SETREF(*ptr, NULL);
return 1;
}
@@ -2141,8 +2248,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
s_object = PyDict_GetItemWithError(state->cache, fmt);
if (s_object != NULL) {
- Py_INCREF(s_object);
- *ptr = (PyStructObject *)s_object;
+ *ptr = (PyStructObject *)Py_NewRef(s_object);
return Py_CLEANUP_SUPPORTED;
}
else if (PyErr_Occurred()) {
@@ -2467,6 +2573,7 @@ _structmodule_exec(PyObject *m)
static PyModuleDef_Slot _structmodule_slots[] = {
{Py_mod_exec, _structmodule_exec},
+ {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{0, NULL}
};