Skip to content

bpo-45034: Fixes how upper limit is formatted for struct.pack("H", ...) #28178

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 6 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,24 @@ def test_issue35714(self):
'embedded null character'):
struct.calcsize(s)

@support.cpython_only
def test_issue45034_unsigned(self):
from _testcapi import USHRT_MAX
error_msg = f'ushort format requires 0 <= number <= {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):
from _testcapi import SHRT_MIN, SHRT_MAX
error_msg = f'short format requires {SHRT_MIN} <= number <= {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


class UnpackIteratorTest(unittest.TestCase):
"""
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@ Ryan Smith-Roberts
Rafal Smotrzyk
Josh Snider
Eric Snow
Nikita Sobolev
Dirk Soede
Nir Soffer
Paul Sokolovsky
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changes how error is formatted for ``struct.pack`` with ``'H'`` and ``'h'`` modes and
too large / small numbers. Now it shows the actual numeric limits, while previously it was showing arithmetic expressions.
12 changes: 6 additions & 6 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
if (get_long(state, v, &x) < 0)
return -1;
if (x < SHRT_MIN || x > SHRT_MAX) {
PyErr_SetString(state->StructError,
"short format requires " Py_STRINGIFY(SHRT_MIN)
" <= number <= " Py_STRINGIFY(SHRT_MAX));
PyErr_Format(state->StructError,
"short format requires %d <= number <= %d",
(int)SHRT_MIN, (int)SHRT_MAX);
return -1;
}
y = (short)x;
Expand All @@ -607,9 +607,9 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
if (get_long(state, v, &x) < 0)
return -1;
if (x < 0 || x > USHRT_MAX) {
PyErr_SetString(state->StructError,
"ushort format requires 0 <= number <= "
Py_STRINGIFY(USHRT_MAX));
PyErr_Format(state->StructError,
"ushort format requires 0 <= number <= %u",
(unsigned int)USHRT_MAX);
return -1;
}
y = (unsigned short)x;
Expand Down