[Python-checkins] r67733 - in python/branches/release25-maint: Lib/test/test_struct.py Misc/NEWS Modules/_struct.c

martin.v.loewis python-checkins at python.org
Sat Dec 13 15:34:08 CET 2008


Author: martin.v.loewis
Date: Sat Dec 13 15:34:06 2008
New Revision: 67733

Log:
Issue #4228: Pack negative values the same way as 2.4 
in struct's L format.


Modified:
   python/branches/release25-maint/Lib/test/test_struct.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_struct.c

Modified: python/branches/release25-maint/Lib/test/test_struct.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_struct.py	(original)
+++ python/branches/release25-maint/Lib/test/test_struct.py	Sat Dec 13 15:34:06 2008
@@ -3,6 +3,8 @@
 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 @@
 
 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.
 

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Sat Dec 13 15:34:06 2008
@@ -217,6 +217,8 @@
 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.
 

Modified: python/branches/release25-maint/Modules/_struct.c
==============================================================================
--- python/branches/release25-maint/Modules/_struct.c	(original)
+++ python/branches/release25-maint/Modules/_struct.c	Sat Dec 13 15:34:06 2008
@@ -645,7 +645,7 @@
 		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 @@
 {
 	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 @@
 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;
 }


More information about the Python-checkins mailing list