[pypy-svn] r33669 - in pypy/dist/pypy/rpython/rctypes/tool: . test

ac at codespeak.net ac at codespeak.net
Tue Oct 24 18:44:27 CEST 2006


Author: ac
Date: Tue Oct 24 18:44:26 2006
New Revision: 33669

Modified:
   pypy/dist/pypy/rpython/rctypes/tool/ctypes_platform.py
   pypy/dist/pypy/rpython/rctypes/tool/test/test_ctypes_platform.py
Log:
Add support for conditionally discovered types.

Modified: pypy/dist/pypy/rpython/rctypes/tool/ctypes_platform.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/tool/ctypes_platform.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/tool/ctypes_platform.py	Tue Oct 24 18:44:26 2006
@@ -121,11 +121,15 @@
     """An entry in a CConfig class that stands for an externally
     defined structure.
     """
-    def __init__(self, name, interesting_fields):
+    def __init__(self, name, interesting_fields, ifdef=None):
         self.name = name
         self.interesting_fields = interesting_fields
+        self.ifdef = ifdef
 
     def prepare_code(self):
+        if self.ifdef is not None:
+            yield '#ifdef %s' % (self.ifdef,)
+            yield 'dump("defined", 1);'
         yield 'typedef %s ctypesplatcheck_t;' % (self.name,)
         yield 'typedef struct {'
         yield '    char c;'
@@ -146,8 +150,15 @@
                                                    fieldname)
                 yield 'dump("fldunsigned %s", s.%s > 0);' % (fieldname,
                                                              fieldname)
+        if self.ifdef is not None:
+            yield '#else'
+            yield 'dump("defined", 0);'
+            yield '#endif'
 
     def build_result(self, info, config_result):
+        if self.ifdef is not None:
+            if not info['defined']:
+                return None
         alignment = 1
         layout = [None] * info['size']
         for fieldname, fieldtype in self.interesting_fields:
@@ -212,11 +223,15 @@
     """An entry in a CConfig class that stands for an externally
     defined simple numeric type.
     """
-    def __init__(self, name, ctype_hint=ctypes.c_int):
+    def __init__(self, name, ctype_hint=ctypes.c_int, ifdef=None):
         self.name = name
         self.ctype_hint = ctype_hint
-
+        self.ifdef = ifdef
+        
     def prepare_code(self):
+        if self.ifdef is not None:
+            yield '#ifdef %s' % (self.ifdef,)
+            yield 'dump("defined", 1);'
         yield 'typedef %s ctypesplatcheck_t;' % (self.name,)
         yield ''
         yield 'ctypesplatcheck_t x;'
@@ -224,8 +239,14 @@
         if self.ctype_hint in integer_class:
             yield 'x = 0; x = ~x;'
             yield 'dump("unsigned", x > 0);'
+        if self.ifdef is not None:
+            yield '#else'
+            yield 'dump("defined", 0);'
+            yield '#endif'
 
     def build_result(self, info, config_result):
+        if self.ifdef is not None and not info['defined']:
+            return None
         size = info['size']
         sign = info.get('unsigned', False)
         ctype = self.ctype_hint

Modified: pypy/dist/pypy/rpython/rctypes/tool/test/test_ctypes_platform.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/tool/test/test_ctypes_platform.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/tool/test/test_ctypes_platform.py	Tue Oct 24 18:44:26 2006
@@ -125,6 +125,31 @@
                    'ushort': ctypes.c_ushort,
                    'XYZZY': 42}
 
+def test_ifdef():
+    class CConfig:
+        _header_ = """ /* a C comment */
+#define XYZZY 42
+typedef int foo;
+struct s {
+    int i;
+    double f;
+};
+"""
+
+        s = ctypes_platform.Struct('struct s', [('i', ctypes.c_int)],
+                                   ifdef='XYZZY')
+        z = ctypes_platform.Struct('struct z', [('i', ctypes.c_int)],
+                                   ifdef='FOOBAR')
+
+        foo = ctypes_platform.SimpleType('foo', ifdef='XYZZY')
+        bar = ctypes_platform.SimpleType('bar', ifdef='FOOBAR')
+
+    res = ctypes_platform.configure(CConfig)
+    assert res['s'] is not None
+    assert res['z'] is None
+    assert res['foo'] is not None
+    assert res['bar'] is None
+
 def test_nested_structs():
     class CConfig:
         _header_ = """



More information about the Pypy-commit mailing list