]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45034: Fix how upper limit is formatted for `struct.pack("H", ...)` (GH-28178)
authorNikita Sobolev <mail@sobolevn.me>
Tue, 7 Sep 2021 12:18:46 +0000 (15:18 +0300)
committerGitHub <noreply@github.com>
Tue, 7 Sep 2021 12:18:46 +0000 (13:18 +0100)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_struct.py
Misc/ACKS
Misc/NEWS.d/next/Library/2021-09-05-20-33-25.bpo-45034.62NLD5.rst [new file with mode: 0644]
Modules/_struct.c

index b3f21ea7db49ef3d2aed55489cba5c985e3a2db9..49decacb132f87fe81359dd46e004b4b3b6c17ac 100644 (file)
@@ -678,6 +678,24 @@ class StructTest(unittest.TestCase):
                                         '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):
     """
index df0b92c72df86232167a8f5a09a650674c18902d..481e46d4c173200e54c57a42000b9b24c7a0244a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1663,6 +1663,7 @@ Ryan Smith-Roberts
 Rafal Smotrzyk
 Josh Snider
 Eric Snow
+Nikita Sobolev
 Dirk Soede
 Nir Soffer
 Paul Sokolovsky
diff --git a/Misc/NEWS.d/next/Library/2021-09-05-20-33-25.bpo-45034.62NLD5.rst b/Misc/NEWS.d/next/Library/2021-09-05-20-33-25.bpo-45034.62NLD5.rst
new file mode 100644 (file)
index 0000000..8d94821
--- /dev/null
@@ -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.
index 30ad9f2b79d8f3fdc378ade6c8a66bb9c2bec882..872c30d659d824e08fba07aba7ef0eacbb5be09f 100644 (file)
@@ -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;
@@ -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;