[pypy-commit] pypy default: reenable + fix bitfields, also fixes issue820

RonnyPfannschmidt noreply at buildbot.pypy.org
Wed Aug 10 02:54:45 CEST 2011


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: 
Changeset: r46402:420c6c4b14b8
Date: 2011-08-10 02:20 +0200
http://bitbucket.org/pypy/pypy/changeset/420c6c4b14b8/

Log:	reenable + fix bitfields, also fixes issue820

diff --git a/lib_pypy/_ctypes/structure.py b/lib_pypy/_ctypes/structure.py
--- a/lib_pypy/_ctypes/structure.py
+++ b/lib_pypy/_ctypes/structure.py
@@ -14,6 +14,15 @@
             raise TypeError("Expected CData subclass, got %s" % (tp,))
         if isinstance(tp, StructOrUnionMeta):
             tp._make_final()
+        if len(f) == 3:
+            if (not hasattr(tp, '_type_')
+                or not isinstance(tp._type_, str)
+                or tp._type_ not in "iIhHbBlL"):
+                #XXX: are those all types?
+                #     we just dont get the type name
+                #     in the interp levle thrown TypeError
+                #     from rawffi if there are more
+                raise TypeError('bit fields not allowed for type ' + tp.__name__)
 
     all_fields = []
     for cls in reversed(inspect.getmro(superclass)):
@@ -91,7 +100,7 @@
     if name == '_fields_':
         if self.__dict__.get('_fields_', None) is not None:
             raise AttributeError("_fields_ is final")
-        if self in [v for k, v in value]:
+        if self in [f[1] for f in value]:
             raise AttributeError("Structure or union cannot contain itself")
         names_and_fields(
             self,
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py b/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py
@@ -5,8 +5,6 @@
 
 import ctypes
 
-py.test.skip("bitfields not supported")
-
 def setup_module(mod):
     import conftest
     _ctypes_test = str(conftest.sofile)
@@ -14,7 +12,7 @@
     func.argtypes = POINTER(BITS), c_char
     mod.func = func
 
-class BITS(BaseCTypesTestChecker):
+class BITS(Structure):
     _fields_ = [("A", c_int, 1),
                 ("B", c_int, 2),
                 ("C", c_int, 3),
@@ -197,6 +195,8 @@
         try:
             func(*args, **kw)
         except Exception, detail:
+            import traceback
+            traceback.print_exc()
             return detail.__class__, str(detail)
 
     def test_mixed_1(self):
@@ -228,3 +228,24 @@
         class Y(Structure):
             _anonymous_ = ["_"]
             _fields_ = [("_", X)]
+
+    def test_set_fields_attr(self):
+        class A(Structure):
+            pass
+        A._fields_ = [("a", c_byte),
+                      ("b", c_ubyte)]
+
+    def test_set_fields_attr_bitfields(self):
+        class A(Structure):
+            pass
+        A._fields_ = [("a", POINTER(A)),
+                      ("b", c_ubyte, 4)]
+
+
+    def test_set_fields_cycle_fails(self):
+        class A(Structure):
+            pass
+        import pytest
+        pytest.raises(AttributeError, """
+            A._fields_ = [("a", A)]
+                      """)


More information about the pypy-commit mailing list