Commits

Armin Rigo committed 174265c

Update to cffi/9cdae12e006f

Comments (0)

Files changed (2)

pypy/module/_cffi_backend/newtype.py

 
 # ____________________________________________________________
 
-SF_MSVC_BITFIELDS = 1
-SF_GCC_ARM_BITFIELDS = 2
-SF_GCC_BIG_ENDIAN = 4
-SF_PACKED = 8
+
+SF_MSVC_BITFIELDS     = 0x01
+SF_GCC_ARM_BITFIELDS  = 0x02
+SF_GCC_X86_BITFIELDS  = 0x10
+
+SF_GCC_BIG_ENDIAN     = 0x04
+SF_GCC_LITTLE_ENDIAN  = 0x40
+
+SF_PACKED             = 0x08
+
 
 if sys.platform == 'win32':
-    DEFAULT_SFLAGS = SF_MSVC_BITFIELDS
+    DEFAULT_SFLAGS_PLATFORM = SF_MSVC_BITFIELDS
 else:
     if rffi_platform.getdefined('__arm__', ''):
-        DEFAULT_SFLAGS = SF_GCC_ARM_BITFIELDS
+        DEFAULT_SFLAGS_PLATFORM = SF_GCC_ARM_BITFIELDS
     else:
-        DEFAULT_SFLAGS = 0
-    if sys.byteorder == 'big':
-        DEFAULT_SFLAGS |= SF_GCC_BIG_ENDIAN
+        DEFAULT_SFLAGS_PLATFORM = SF_GCC_X86_BITFIELDS
+
+if sys.byteorder == 'big':
+    DEFAULT_SFLAGS_ENDIAN = SF_GCC_BIG_ENDIAN
+else:
+    DEFAULT_SFLAGS_ENDIAN = SF_GCC_LITTLE_ENDIAN
+
+
+def complete_sflags(sflags):
+    # add one of the SF_xxx_BITFIELDS flags if none is specified
+    if not (sflags & (SF_MSVC_BITFIELDS | SF_GCC_ARM_BITFIELDS |
+                      SF_GCC_X86_BITFIELDS)):
+        sflags |= DEFAULT_SFLAGS_PLATFORM
+    # add one of SF_GCC_xx_ENDIAN if none is specified
+    if not (sflags & (SF_GCC_BIG_ENDIAN | SF_GCC_LITTLE_ENDIAN)):
+        sflags |= DEFAULT_SFLAGS_ENDIAN
+    return sflags
+
+# ____________________________________________________________
+
 
 @unwrap_spec(name=str)
 def new_struct_type(space, name):
 @unwrap_spec(w_ctype=ctypeobj.W_CType, totalsize=int, totalalignment=int,
              sflags=int)
 def complete_struct_or_union(space, w_ctype, w_fields, w_ignored=None,
-                             totalsize=-1, totalalignment=-1,
-                             sflags=DEFAULT_SFLAGS):
+                             totalsize=-1, totalalignment=-1, sflags=0):
+    sflags = complete_sflags(sflags)
     if (not isinstance(w_ctype, ctypestruct.W_CTypeStructOrUnion)
             or w_ctype.size >= 0):
         raise OperationError(space.w_TypeError,

pypy/module/_cffi_backend/test/_backend_test_c.py

                                        ('b1', BInt, 9),
                                        ('b2', BUInt, 7),
                                        ('c', BChar, -1)], -1, -1, -1, flag)
-    if flag % 2 == 0:   # gcc, any variant
+    if not (flag & SF_MSVC_BITFIELDS):   # gcc, any variant
         assert typeoffsetof(BStruct, 'c') == (BChar, 3)
         assert sizeof(BStruct) == 4
     else:               # msvc
     p.c = b'\x9D'
     raw = buffer(p)[:]
     if sys.byteorder == 'little':
-        if flag == 0 or flag == 2:  # gcc, little endian
+        if flag & SF_MSVC_BITFIELDS:
+            assert raw == b'A\x00\x00\x007\xC7\x00\x00\x9D\x00\x00\x00'
+        elif flag & SF_GCC_LITTLE_ENDIAN:
             assert raw == b'A7\xC7\x9D'
-        elif flag == 1: # msvc
-            assert raw == b'A\x00\x00\x007\xC7\x00\x00\x9D\x00\x00\x00'
-        elif flag == 4: # gcc, big endian
+        elif flag & SF_GCC_BIG_ENDIAN:
             assert raw == b'A\xE3\x9B\x9D'
         else:
             raise AssertionError("bad flag")
     else:
-        if flag == 0 or flag == 2:  # gcc
+        if flag & SF_MSVC_BITFIELDS:
+            assert raw == b'A\x00\x00\x00\x00\x00\xC77\x9D\x00\x00\x00'
+        elif flag & SF_GCC_LITTLE_ENDIAN:
             assert raw == b'A\xC77\x9D'
-        elif flag == 1: # msvc
-            assert raw == b'A\x00\x00\x00\x00\x00\xC77\x9D\x00\x00\x00'
-        elif flag == 4: # gcc, big endian
+        elif flag & SF_GCC_BIG_ENDIAN:
             assert raw == b'A\x9B\xE3\x9D'
         else:
             raise AssertionError("bad flag")
                                        ('',  BShort, 9),
                                        ('c', BChar, -1)], -1, -1, -1, flag)
     assert typeoffsetof(BStruct, 'c') == (BChar, 4)
-    if flag == 0:   # gcc
+    if flag & SF_MSVC_BITFIELDS:
+        assert sizeof(BStruct) == 6
+        assert alignof(BStruct) == 2
+    elif flag & SF_GCC_X86_BITFIELDS:
         assert sizeof(BStruct) == 5
         assert alignof(BStruct) == 1
-    elif flag == 1: # msvc
+    elif flag & SF_GCC_ARM_BITFIELDS:
         assert sizeof(BStruct) == 6
         assert alignof(BStruct) == 2
-    elif flag == 2: # gcc ARM
-        assert sizeof(BStruct) == 6
-        assert alignof(BStruct) == 2
-    elif flag == 4: # gcc, big endian
-        assert sizeof(BStruct) == 5
-        assert alignof(BStruct) == 1
     else:
         raise AssertionError("bad flag")
     #
                                        ('',  BInt, 0),
                                        ('',  BInt, 0),
                                        ('c', BChar, -1)], -1, -1, -1, flag)
-    if flag == 0:    # gcc
+    if flag & SF_MSVC_BITFIELDS:
+        assert typeoffsetof(BStruct, 'c') == (BChar, 1)
+        assert sizeof(BStruct) == 2
+        assert alignof(BStruct) == 1
+    elif flag & SF_GCC_X86_BITFIELDS:
         assert typeoffsetof(BStruct, 'c') == (BChar, 4)
         assert sizeof(BStruct) == 5
         assert alignof(BStruct) == 1
-    elif flag == 1:  # msvc
-        assert typeoffsetof(BStruct, 'c') == (BChar, 1)
-        assert sizeof(BStruct) == 2
-        assert alignof(BStruct) == 1
-    elif flag == 2:  # gcc ARM
+    elif flag & SF_GCC_ARM_BITFIELDS:
         assert typeoffsetof(BStruct, 'c') == (BChar, 4)
         assert sizeof(BStruct) == 8
         assert alignof(BStruct) == 4
-    elif flag == 4:  # gcc, big endian
-        assert typeoffsetof(BStruct, 'c') == (BChar, 4)
-        assert sizeof(BStruct) == 5
-        assert alignof(BStruct) == 1
     else:
         raise AssertionError("bad flag")
 
 
-def test_bitfield_as_gcc():
-    _test_bitfield_details(flag=0)
+SF_MSVC_BITFIELDS     = 0x01
+SF_GCC_ARM_BITFIELDS  = 0x02
+SF_GCC_X86_BITFIELDS  = 0x10
+
+SF_GCC_BIG_ENDIAN     = 0x04
+SF_GCC_LITTLE_ENDIAN  = 0x40
+
+SF_PACKED             = 0x08
+
+def test_bitfield_as_x86_gcc():
+    _test_bitfield_details(flag=SF_GCC_X86_BITFIELDS|SF_GCC_LITTLE_ENDIAN)
 
 def test_bitfield_as_msvc():
-    _test_bitfield_details(flag=1)
+    _test_bitfield_details(flag=SF_MSVC_BITFIELDS|SF_GCC_LITTLE_ENDIAN)
 
 def test_bitfield_as_arm_gcc():
-    _test_bitfield_details(flag=2)
+    _test_bitfield_details(flag=SF_GCC_ARM_BITFIELDS|SF_GCC_LITTLE_ENDIAN)
 
-def test_bitfield_as_big_endian():
-    _test_bitfield_details(flag=4)
+def test_bitfield_as_ppc_gcc():
+    # PowerPC uses the same format as X86, but is big-endian
+    _test_bitfield_details(flag=SF_GCC_X86_BITFIELDS|SF_GCC_BIG_ENDIAN)
 
 
 def test_struct_array_no_length():
     complete_struct_or_union(BStruct, [('a1', BLong, -1),
                                        ('a2', BChar, -1),
                                        ('a3', BShort, -1)],
-                             None, -1, -1, 8)   # SF_PACKED==8
+                             None, -1, -1, SF_PACKED)
     d = BStruct.fields
     assert len(d) == 3
     assert d[0][0] == 'a1'
                    complete_struct_or_union,
                    BStruct, [('a1', BLong, 30),
                              ('a2', BChar, 5)],
-                   None, -1, -1, 8)   # SF_PACKED==8
+                   None, -1, -1, SF_PACKED)
 
 def test_version():
     # this test is here mostly for PyPy