summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c35
1 files changed, 17 insertions, 18 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index ad9959e..f965541 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -85,8 +85,6 @@ typedef struct { char c; _Bool x; } s_bool;
#define BOOL_ALIGN 0
#endif
-#define STRINGIFY(x) #x
-
#ifdef __powerc
#pragma options align=reset
#endif
@@ -546,8 +544,8 @@ np_short(char *p, PyObject *v, const formatdef *f)
return -1;
if (x < SHRT_MIN || x > SHRT_MAX){
PyErr_SetString(StructError,
- "short format requires " STRINGIFY(SHRT_MIN)
- " <= number <= " STRINGIFY(SHRT_MAX));
+ "short format requires " Py_STRINGIFY(SHRT_MIN)
+ " <= number <= " Py_STRINGIFY(SHRT_MAX));
return -1;
}
y = (short)x;
@@ -564,7 +562,8 @@ np_ushort(char *p, PyObject *v, const formatdef *f)
return -1;
if (x < 0 || x > USHRT_MAX){
PyErr_SetString(StructError,
- "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
+ "ushort format requires 0 <= number <= "
+ Py_STRINGIFY(USHRT_MAX));
return -1;
}
y = (unsigned short)x;
@@ -1264,7 +1263,8 @@ prepare_s(PyStructObject *self)
const char *s;
const char *fmt;
char c;
- Py_ssize_t size, len, ncodes, num, itemsize;
+ Py_ssize_t size, len, num, itemsize;
+ size_t ncodes;
fmt = PyBytes_AS_STRING(self->s_format);
@@ -1320,7 +1320,7 @@ prepare_s(PyStructObject *self)
}
/* check for overflow */
- if ((ncodes + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
+ if ((ncodes + 1) > ((size_t)PY_SSIZE_T_MAX / sizeof(formatcode))) {
PyErr_NoMemory();
return -1;
}
@@ -1437,8 +1437,7 @@ s_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
}
- Py_CLEAR(soself->s_format);
- soself->s_format = o_format;
+ Py_XSETREF(soself->s_format, o_format);
ret = prepare_s(soself);
return ret;
@@ -1498,8 +1497,8 @@ PyDoc_STRVAR(s_unpack__doc__,
"S.unpack(buffer) -> (v1, v2, ...)\n\
\n\
Return a tuple containing values unpacked according to the format\n\
-string S.format. Requires len(buffer) == S.size. See help(struct)\n\
-for more on format strings.");
+string S.format. The buffer's size in bytes must be S.size. See\n\
+help(struct) for more on format strings.");
static PyObject *
s_unpack(PyObject *self, PyObject *input)
@@ -1528,8 +1527,8 @@ PyDoc_STRVAR(s_unpack_from__doc__,
"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
\n\
Return a tuple containing values unpacked according to the format\n\
-string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
-help(struct) for more on format strings.");
+string S.format. The buffer's size in bytes, minus offset, must be at\n\
+least S.size. See help(struct) for more on format strings.");
static PyObject *
s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
@@ -1924,7 +1923,7 @@ s_sizeof(PyStructObject *self, void *unused)
Py_ssize_t size;
formatcode *code;
- size = sizeof(PyStructObject) + sizeof(formatcode);
+ size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
for (code = self->s_codes; code->fmtdef != NULL; code++)
size += sizeof(formatcode);
return PyLong_FromSsize_t(size);
@@ -2131,8 +2130,8 @@ PyDoc_STRVAR(unpack_doc,
"unpack(fmt, buffer) -> (v1, v2, ...)\n\
\n\
Return a tuple containing values unpacked according to the format string\n\
-fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
-on format strings.");
+fmt. The buffer's size in bytes must be calcsize(fmt). See help(struct)\n\
+for more on format strings.");
static PyObject *
unpack(PyObject *self, PyObject *args)
@@ -2154,8 +2153,8 @@ PyDoc_STRVAR(unpack_from_doc,
"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
\n\
Return a tuple containing values unpacked according to the format string\n\
-fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
-for more on format strings.");
+fmt. The buffer's size, minus offset, must be at least calcsize(fmt).\n\
+See help(struct) for more on format strings.");
static PyObject *
unpack_from(PyObject *self, PyObject *args, PyObject *kwds)