]> granicus.if.org Git - python/commitdiff
Issue #4228: Pack negative values the same way as 2.4
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 13 Dec 2008 14:34:06 +0000 (14:34 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 13 Dec 2008 14:34:06 +0000 (14:34 +0000)
in struct's L format.

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

index 7ecb6ac9a04dd9dde5642cef41d084c4863f41fb..f13515000f8a69b723ed4ec83d1ddd4d663cfdba 100644 (file)
@@ -3,6 +3,8 @@ import test.test_support
 import struct
 import array
 import warnings
+warnings.filterwarnings("ignore", "struct integer overflow masking is deprecated",
+                        DeprecationWarning)
 
 import sys
 ISBIGENDIAN = sys.byteorder == "big"
@@ -535,6 +537,17 @@ def test_1530559():
 
 test_1530559()
 
+## Issue 4228. Packing a negative unsigned long warns,
+# but then still should give a value with the
+# topmost bit set.
+
+def test_issue4228():
+    # Packing a long may yield either 32 or 64 bits
+    x = struct.pack('L', -1)[:4]
+    vereq(x, '\xff'*4)
+
+test_issue4228()
+
 ###########################################################################
 # Packing and unpacking to/from buffers.
 
index 97efe211e579f810349d22506b30a3139e30e4d7..a98ae2c223ac63de75d2b2fff71c61e7f58790d3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -217,6 +217,8 @@ Library
 Extension Modules
 -----------------
 
+- Issue #4228: Pack negative values the same way as 2.4 in struct's L format.
+
 - Security Issue #2: imageop did not validate arguments correctly and could
   segfault as a result.
 
index 41183fa26b5bcb0db970c223fb981b10500f78a2..e2a883de830141ac4a49529e58d80da1b1db7457 100644 (file)
@@ -645,7 +645,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);
@@ -657,12 +657,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;
@@ -682,8 +682,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;
 }