]> granicus.if.org Git - python/commitdiff
fix ctypes test alignment assumptions (closes #20946)
authorBenjamin Peterson <benjamin@python.org>
Sun, 16 Mar 2014 09:07:26 +0000 (10:07 +0100)
committerBenjamin Peterson <benjamin@python.org>
Sun, 16 Mar 2014 09:07:26 +0000 (10:07 +0100)
Patch by Andreas Schwab.

Lib/ctypes/test/test_bitfields.py
Lib/ctypes/test/test_structures.py
Misc/NEWS
Modules/_ctypes/cfield.c

index c89ee345ee8f2142b27632dca791245aa4e5fbde..77de6063b6ff138d89ac40e4ea015e0b57e4a244 100644 (file)
@@ -207,7 +207,7 @@ class BitFieldTest(unittest.TestCase):
         class X(Structure):
             _fields_ = [("a", c_byte, 4),
                         ("b", c_int, 32)]
-        self.assertEqual(sizeof(X), sizeof(c_int)*2)
+        self.assertEqual(sizeof(X), alignment(c_int)+sizeof(c_int))
 
     def test_mixed_3(self):
         class X(Structure):
index 61b9fe7c18131815f0ec4e81cf2e10c58b4b21bf..87613ad1ef79f6c8e9bf6cb998b0842967315d54 100644 (file)
@@ -83,7 +83,7 @@ class StructureTestCase(unittest.TestCase):
         class Y(Structure):
             _fields_ = [("x", c_char * 3),
                         ("y", c_int)]
-        self.assertEqual(alignment(Y), calcsize("i"))
+        self.assertEqual(alignment(Y), alignment(c_int))
         self.assertEqual(sizeof(Y), calcsize("3si"))
 
         class SI(Structure):
@@ -175,23 +175,23 @@ class StructureTestCase(unittest.TestCase):
         self.assertEqual(sizeof(X), 10)
         self.assertEqual(X.b.offset, 2)
 
+        import struct
+        longlong_size = struct.calcsize("q")
+        longlong_align = struct.calcsize("bq") - longlong_size
+
         class X(Structure):
             _fields_ = [("a", c_byte),
                         ("b", c_longlong)]
             _pack_ = 4
-        self.assertEqual(sizeof(X), 12)
-        self.assertEqual(X.b.offset, 4)
-
-        import struct
-        longlong_size = struct.calcsize("q")
-        longlong_align = struct.calcsize("bq") - longlong_size
+        self.assertEqual(sizeof(X), min(4, longlong_align) + longlong_size)
+        self.assertEqual(X.b.offset, min(4, longlong_align))
 
         class X(Structure):
             _fields_ = [("a", c_byte),
                         ("b", c_longlong)]
             _pack_ = 8
 
-        self.assertEqual(sizeof(X), longlong_align + longlong_size)
+        self.assertEqual(sizeof(X), min(8, longlong_align) + longlong_size)
         self.assertEqual(X.b.offset, min(8, longlong_align))
 
 
index 2b455e79f8be90a8308316bd00ca90e632f6ebe2..f44427204f8c85c14d48863af90508a94847e760 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,8 @@ Library
 Tests
 -----
 
+- Issue #20946: Correct alignment assumptions of some ctypes tests.
+
 - Issue #20939: Fix test_geturl failure in test_urllibnet due to
   new redirect of http://www.python.org/ to https://www.python.org.
 
index 65772cfa45a4c5a12b99b24ce9d687e2bde17d60..79d60f3728d1973b444687225ad6037bdf01960e 100644 (file)
@@ -1640,9 +1640,9 @@ typedef struct { char c; void *x; } s_void_p;
 /*
 #define CHAR_ALIGN (sizeof(s_char) - sizeof(char))
 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
-#define INT_ALIGN (sizeof(s_int) - sizeof(int))
 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
 */
+#define INT_ALIGN (sizeof(s_int) - sizeof(int))
 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
 #define LONGDOUBLE_ALIGN (sizeof(s_long_double) - sizeof(long double))
@@ -1684,8 +1684,8 @@ ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 };
 ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 };
 ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 };
 
-ffi_type ffi_type_uint32 = { 4, 4, FFI_TYPE_UINT32 };
-ffi_type ffi_type_sint32 = { 4, 4, FFI_TYPE_SINT32 };
+ffi_type ffi_type_uint32 = { 4, INT_ALIGN, FFI_TYPE_UINT32 };
+ffi_type ffi_type_sint32 = { 4, INT_ALIGN, FFI_TYPE_SINT32 };
 
 ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 };
 ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 };