Skip to content

Commit 8ca6b61

Browse files
sobolevnmdickinsonserhiy-storchaka
authored
bpo-45034: Fix how upper limit is formatted for struct.pack("H", ...) (GH-28178)
Co-authored-by: Mark Dickinson <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 97b754d commit 8ca6b61

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

Lib/test/test_struct.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,24 @@ def test_issue35714(self):
678678
'embedded null character'):
679679
struct.calcsize(s)
680680

681+
@support.cpython_only
682+
def test_issue45034_unsigned(self):
683+
from _testcapi import USHRT_MAX
684+
error_msg = f'ushort format requires 0 <= number <= {USHRT_MAX}'
685+
with self.assertRaisesRegex(struct.error, error_msg):
686+
struct.pack('H', 70000) # too large
687+
with self.assertRaisesRegex(struct.error, error_msg):
688+
struct.pack('H', -1) # too small
689+
690+
@support.cpython_only
691+
def test_issue45034_signed(self):
692+
from _testcapi import SHRT_MIN, SHRT_MAX
693+
error_msg = f'short format requires {SHRT_MIN} <= number <= {SHRT_MAX}'
694+
with self.assertRaisesRegex(struct.error, error_msg):
695+
struct.pack('h', 70000) # too large
696+
with self.assertRaisesRegex(struct.error, error_msg):
697+
struct.pack('h', -70000) # too small
698+
681699

682700
class UnpackIteratorTest(unittest.TestCase):
683701
"""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,7 @@ Ryan Smith-Roberts
16631663
Rafal Smotrzyk
16641664
Josh Snider
16651665
Eric Snow
1666+
Nikita Sobolev
16661667
Dirk Soede
16671668
Nir Soffer
16681669
Paul Sokolovsky
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changes how error is formatted for ``struct.pack`` with ``'H'`` and ``'h'`` modes and
2+
too large / small numbers. Now it shows the actual numeric limits, while previously it was showing arithmetic expressions.

Modules/_struct.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,9 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
589589
if (get_long(state, v, &x) < 0)
590590
return -1;
591591
if (x < SHRT_MIN || x > SHRT_MAX) {
592-
PyErr_SetString(state->StructError,
593-
"short format requires " Py_STRINGIFY(SHRT_MIN)
594-
" <= number <= " Py_STRINGIFY(SHRT_MAX));
592+
PyErr_Format(state->StructError,
593+
"short format requires %d <= number <= %d",
594+
(int)SHRT_MIN, (int)SHRT_MAX);
595595
return -1;
596596
}
597597
y = (short)x;
@@ -607,9 +607,9 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
607607
if (get_long(state, v, &x) < 0)
608608
return -1;
609609
if (x < 0 || x > USHRT_MAX) {
610-
PyErr_SetString(state->StructError,
611-
"ushort format requires 0 <= number <= "
612-
Py_STRINGIFY(USHRT_MAX));
610+
PyErr_Format(state->StructError,
611+
"ushort format requires 0 <= number <= %u",
612+
(unsigned int)USHRT_MAX);
613613
return -1;
614614
}
615615
y = (unsigned short)x;

0 commit comments

Comments
 (0)