]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#4228: Pack negative values the same way as 2.4
authorGeorg Brandl <georg@python.org>
Thu, 1 Jan 2009 12:15:31 +0000 (12:15 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 1 Jan 2009 12:15:31 +0000 (12:15 +0000)
in struct's L format.

Lib/test/test_struct.py
Misc/NEWS
Modules/_struct.c

index 232bffc275ba74cba95b45b40c7ae3c02980a0fc..7f5f08b4c691523771d329a7628516987220945d 100644 (file)
@@ -2,6 +2,8 @@ import array
 import unittest
 import struct
 import warnings
+warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated",
+                        DeprecationWarning)
 
 from functools import wraps
 from test.test_support import TestFailed, verbose, run_unittest
@@ -461,6 +463,11 @@ class StructTest(unittest.TestCase):
                 self.check_float_coerce(endian + fmt, 1.0)
                 self.check_float_coerce(endian + fmt, 1.5)
 
+    def test_issue4228(self):
+        # Packing a long may yield either 32 or 64 bits
+        x = struct.pack('L', -1)[:4]
+        self.assertEqual(x, '\xff'*4)
+
     def test_unpack_from(self):
         test_string = 'abcd01234'
         fmt = '4s'
index 7eab3ae08e18285da036209f990a9fa546ae05a8..9639b1897f277f1074efc9e4ad57fa017ab6727c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -258,6 +258,8 @@ C-API
 Extension Modules
 -----------------
 
+- Issue #4228: Pack negative values the same way as 2.4 in struct's L format.
+
 - Issue #1040026: Fix os.times result on systems where HZ is incorrect.
 
 - Issues #3167, #3682: Fix test_math failures for log, log10 on Solaris,
index 30feaa6fb1f9b4d8f929d7b31b28e07a64865cae..b8f1525f6c1248db5153bbbe3c7bd5ef018daafe 100644 (file)
@@ -663,7 +663,7 @@ np_int(char *p, PyObject *v, const formatdef *f)
                return -1;
 #if (SIZEOF_LONG > SIZEOF_INT)
        if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
-               return _range_error(f, 0);
+               RANGE_ERROR(x, f, 0, -1);
 #endif
        y = (int)x;
        memcpy(p, (char *)&y, sizeof y);
@@ -675,12 +675,12 @@ np_uint(char *p, PyObject *v, const formatdef *f)
 {
        unsigned long x;
        unsigned int y;
-       if (get_ulong(v, &x) < 0)
-               return _range_error(f, 1);
+       if (get_wrapped_ulong(v, &x) < 0)
+               return -1;
        y = (unsigned int)x;
 #if (SIZEOF_LONG > SIZEOF_INT)
        if (x > ((unsigned long)UINT_MAX))
-               return _range_error(f, 1);
+               RANGE_ERROR(y, f, 1, -1);
 #endif
        memcpy(p, (char *)&y, sizeof y);
        return 0;
@@ -700,8 +700,8 @@ static int
 np_ulong(char *p, PyObject *v, const formatdef *f)
 {
        unsigned long x;
-       if (get_ulong(v, &x) < 0)
-               return _range_error(f, 1);
+       if (get_wrapped_ulong(v, &x) < 0)
+               return -1;
        memcpy(p, (char *)&x, sizeof x);
        return 0;
 }