Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ def test_issue35714(self):
'embedded null character'):
struct.calcsize(s)

@support.cpython_only
def test_issue45034(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


class UnpackIteratorTest(unittest.TestCase):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changes how max value is formatted for ``struct.pack`` with ``'H'`` mode and
too large / small numbers.
6 changes: 3 additions & 3 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <= %zu",
Comment thread
sobolevn marked this conversation as resolved.
Outdated
USHRT_MAX);
Comment thread
sobolevn marked this conversation as resolved.
Outdated
return -1;
}
y = (unsigned short)x;
Expand Down