Skip to content

Commit 551ae17

Browse files
Update error message
1 parent dfdd4b7 commit 551ae17

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

Doc/library/struct.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ The module defines the following exception and functions:
5555
Exception raised on various occasions; argument is a string describing what
5656
is wrong.
5757

58-
.. versionchanged:: next
59-
Out of range errors are more descriptive specifying which argument resulted
60-
in the exception.
61-
6258

6359
.. function:: pack(format, v1, v2, ...)
6460

Lib/test/test_struct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def test_error_msg(prefix, int_type, is_unsigned):
742742
else:
743743
max_ = 2 ** (size * 8 - 1) - 1
744744
min_ = -2 ** (size * 8 - 1)
745-
error_msg = f"'{int_type}' format requires {min_} <= arg 1 <= {max_}"
745+
error_msg = f"'{int_type}' format requires {min_} <= number <= {max_} (at item 1)"
746746
for number in [int(-1e50), min_ - 1, max_ + 1, int(1e50)]:
747747
with self.subTest(format_str=fmt_str, number=number):
748748
with self.assertRaisesRegex(struct.error, error_msg):

Modules/_struct.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,18 @@ _range_error(_structmodulestate *state, const formatdef *f, int is_unsigned, Py_
362362
assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
363363
if (is_unsigned)
364364
PyErr_Format(state->StructError,
365-
"'%c' format requires 0 <= arg %zd <= %zu",
365+
"'%c' format requires 0 <= number <= %zu (at item %zd)",
366366
f->format,
367-
loc,
368-
ulargest);
367+
ulargest,
368+
loc);
369369
else {
370370
const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
371371
PyErr_Format(state->StructError,
372-
"'%c' format requires %zd <= arg %zd <= %zd",
372+
"'%c' format requires %zd <= number <= %zd (at item %zd)",
373373
f->format,
374374
~ largest,
375-
loc,
376-
largest);
375+
largest,
376+
loc);
377377
}
378378

379379
return -1;
@@ -747,11 +747,11 @@ np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
747747
if (get_longlong(state, v, &x) < 0) {
748748
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
749749
PyErr_Format(state->StructError,
750-
"'%c' format requires %lld <= arg %zd <= %lld",
750+
"'%c' format requires %lld <= number <= %lld (at item %zd)",
751751
f->format,
752752
LLONG_MIN,
753-
state->current_arg,
754-
LLONG_MAX);
753+
LLONG_MAX,
754+
state->current_arg);
755755
}
756756
return -1;
757757
}
@@ -766,10 +766,10 @@ np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
766766
if (get_ulonglong(state, v, &x) < 0) {
767767
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
768768
PyErr_Format(state->StructError,
769-
"'%c' format requires 0 <= arg %zd <= %llu",
769+
"'%c' format requires 0 <= number <= %llu (at item %zd)",
770770
f->format,
771-
state->current_arg,
772-
ULLONG_MAX);
771+
ULLONG_MAX,
772+
state->current_arg);
773773
}
774774
return -1;
775775
}
@@ -1132,11 +1132,11 @@ bp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
11321132
Py_DECREF(v);
11331133
if (res < 0) {
11341134
PyErr_Format(state->StructError,
1135-
"'%c' format requires %lld <= arg %zd <= %lld",
1135+
"'%c' format requires %lld <= number <= %lld (at item %zd)",
11361136
f->format,
11371137
LLONG_MIN,
1138-
state->current_arg,
1139-
LLONG_MAX);
1138+
LLONG_MAX,
1139+
state->current_arg);
11401140
return -1;
11411141
}
11421142
return res;
@@ -1158,10 +1158,10 @@ bp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
11581158
Py_DECREF(v);
11591159
if (res < 0) {
11601160
PyErr_Format(state->StructError,
1161-
"'%c' format requires 0 <= arg %zd <= %llu",
1161+
"'%c' format requires 0 <= number <= %llu (at item %zd)",
11621162
f->format,
1163-
state->current_arg,
1164-
ULLONG_MAX);
1163+
ULLONG_MAX,
1164+
state->current_arg);
11651165
return -1;
11661166
}
11671167
return res;
@@ -1467,11 +1467,11 @@ lp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
14671467
Py_DECREF(v);
14681468
if (res < 0) {
14691469
PyErr_Format(state->StructError,
1470-
"'%c' format requires %lld <= arg %zd <= %lld",
1470+
"'%c' format requires %lld <= number <= %lld (at item %zd)",
14711471
f->format,
14721472
LLONG_MIN,
1473-
state->current_arg,
1474-
LLONG_MAX);
1473+
LLONG_MAX,
1474+
state->current_arg);
14751475
return -1;
14761476
}
14771477
return res;
@@ -1493,9 +1493,10 @@ lp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
14931493
Py_DECREF(v);
14941494
if (res < 0) {
14951495
PyErr_Format(state->StructError,
1496-
"'%c' format requires 0 <= number <= %llu",
1496+
"'%c' format requires 0 <= number <= %llu (at item %zd)",
14971497
f->format,
1498-
ULLONG_MAX);
1498+
ULLONG_MAX,
1499+
state->current_arg);
14991500
return -1;
15001501
}
15011502
return res;

0 commit comments

Comments
 (0)