Skip to content

gh-98248: Normalizing the error messages in function struct.pack #98252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,23 +723,33 @@ def test_issue35714(self):
struct.calcsize(s)

@support.cpython_only
def test_issue45034_unsigned(self):
_testcapi = import_helper.import_module('_testcapi')
error_msg = f'ushort format requires 0 <= number <= {_testcapi.USHRT_MAX}'
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('H', 70000) # too large
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('H', -1) # too small

@support.cpython_only
def test_issue45034_signed(self):
_testcapi = import_helper.import_module('_testcapi')
error_msg = f'short format requires {_testcapi.SHRT_MIN} <= number <= {_testcapi.SHRT_MAX}'
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('h', 70000) # too large
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('h', -70000) # too small

def test_issue92848(self):
def test_error_msg(prefix, int_type, is_unsigned):
fmt_str = prefix + int_type
size = struct.calcsize(fmt_str)
if is_unsigned:
max_ = 2 ** (size * 8) - 1
min_ = 0
else:
max_ = 2 ** (size * 8 - 1) - 1
min_ = -2 ** (size * 8 - 1)
error_msg = f"'{int_type}' format requires {min_} <= number <= {max_}"
for number in [int(-1e50), min_ - 1, max_ + 1, int(1e50)]:
with self.subTest(format_str=fmt_str, number=number):
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack(fmt_str, number)

for prefix in '@=<>':
for int_type in 'BHILQ':
test_error_msg(prefix, int_type, True)
for int_type in 'bhilq':
test_error_msg(prefix, int_type, False)

int_type = 'N'
test_error_msg("@", int_type, True)

int_type = 'n'
test_error_msg("@", int_type, False)

class UnpackIteratorTest(unittest.TestCase):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide informative error messages in :func:`struct.pack` when its integral arguments are not in range.
129 changes: 81 additions & 48 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,8 @@ static int
np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(state, v, &x) < 0)
return -1;
if (x < -128 || x > 127) {
PyErr_SetString(state->StructError,
"byte format requires -128 <= number <= 127");
return -1;
if (get_long(state, v, &x) < 0 || x < -128 || x > 127) {
RANGE_ERROR(state, x, f, 0, (unsigned char)-1);
}
*p = (char)x;
return 0;
Expand All @@ -560,12 +556,8 @@ static int
np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(state, v, &x) < 0)
return -1;
if (x < 0 || x > 255) {
PyErr_SetString(state->StructError,
"ubyte format requires 0 <= number <= 255");
return -1;
if (get_long(state, v, &x) < 0 || x < 0 || x > 255) {
RANGE_ERROR(state, x, f, 1, (unsigned char)-1);
}
*(unsigned char *)p = (unsigned char)x;
return 0;
Expand All @@ -588,13 +580,8 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
short y;
if (get_long(state, v, &x) < 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;
if (get_long(state, v, &x) < 0 || x < SHRT_MIN || x > SHRT_MAX) {
RANGE_ERROR(state, x, f, 0, (unsigned short)-1);
}
y = (short)x;
memcpy(p, (char *)&y, sizeof y);
Expand All @@ -606,13 +593,8 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
unsigned short y;
if (get_long(state, v, &x) < 0)
return -1;
if (x < 0 || x > USHRT_MAX) {
PyErr_Format(state->StructError,
"ushort format requires 0 <= number <= %u",
(unsigned int)USHRT_MAX);
return -1;
if (get_long(state, v, &x) < 0 || x < 0 || x > USHRT_MAX) {
RANGE_ERROR(state, x, f, 1, (unsigned short)-1);
}
y = (unsigned short)x;
memcpy(p, (char *)&y, sizeof y);
Expand All @@ -624,8 +606,9 @@ np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
int y;
if (get_long(state, v, &x) < 0)
return -1;
if (get_long(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 0, (unsigned int)-1);
}
#if (SIZEOF_LONG > SIZEOF_INT)
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
RANGE_ERROR(state, x, f, 0, -1);
Expand All @@ -640,8 +623,9 @@ np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
unsigned int y;
if (get_ulong(state, v, &x) < 0)
return -1;
if (get_ulong(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 1, (unsigned long)-1);
}
y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT)
if (x > ((unsigned long)UINT_MAX))
Expand All @@ -655,8 +639,9 @@ static int
np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(state, v, &x) < 0)
return -1;
if (get_long(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 0, (unsigned long)-1);
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
Expand All @@ -665,8 +650,9 @@ static int
np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
if (get_ulong(state, v, &x) < 0)
return -1;
if (get_ulong(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 1, (unsigned long)-1);
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
Expand All @@ -675,8 +661,9 @@ 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)
return -1;
if (get_ssize_t(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 0, (unsigned long)-1);
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
Expand All @@ -685,8 +672,9 @@ 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)
return -1;
if (get_size_t(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 1, (size_t)-1);
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
Expand All @@ -695,8 +683,14 @@ 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) {
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;
}
Expand All @@ -705,8 +699,13 @@ 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) {
PyErr_Format(state->StructError,
"'%c' format requires 0 <= number <= %llu",
f->format,
ULLONG_MAX);
return -1;
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
Expand Down Expand Up @@ -911,8 +910,9 @@ 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)
return -1;
if (get_long(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 0, (unsigned int)-1);
}
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
Expand All @@ -935,8 +935,9 @@ 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)
return -1;
if (get_ulong(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 1, (unsigned int)-1);
}
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
Expand Down Expand Up @@ -964,6 +965,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;
}

Expand All @@ -980,6 +989,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;
}

Expand Down Expand Up @@ -1148,8 +1164,9 @@ 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)
return -1;
if (get_long(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 0, (unsigned int)-1);
}
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
Expand All @@ -1172,8 +1189,9 @@ 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)
return -1;
if (get_ulong(state, v, &x) < 0) {
RANGE_ERROR(state, x, f, 1, (unsigned int)-1);
}
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
Expand Down Expand Up @@ -1201,6 +1219,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;
}

Expand All @@ -1217,6 +1243,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;
}

Expand Down