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

simonb at codespeak.net simonb at codespeak.net
Fri Jul 13 19:26:41 CEST 2007


Author: simonb
Date: Fri Jul 13 19:26:40 2007
New Revision: 45038

Modified:
   pypy/dist/pypy/rpython/tool/mkrffi.py
   pypy/dist/pypy/rpython/tool/test/test_mkrffi.py
Log:
put structs in with rest of the code (they dont need to be all at the top for any reason), rename some stuff, add more types (manual maping for now)

Modified: pypy/dist/pypy/rpython/tool/mkrffi.py
==============================================================================
--- pypy/dist/pypy/rpython/tool/mkrffi.py	(original)
+++ pypy/dist/pypy/rpython/tool/mkrffi.py	Fri Jul 13 19:26:40 2007
@@ -9,48 +9,68 @@
 
 # XXX any automatic stuff here?
 SIMPLE_TYPE_MAPPING = {
-    ctypes.c_int   : 'rffi.INT',
-    ctypes.c_uint  : 'rffi.UINT',
-    ctypes.c_voidp : 'rffi.VOIDP',
+    ctypes.c_ubyte     : 'rffi.UCHAR',
+    ctypes.c_byte      : 'rffi.CHAR',
+    ctypes.c_char      : 'rffi.CHAR',
+    ctypes.c_int8      : 'rffi.CHAR',
+    ctypes.c_ushort    : 'rffi.USHORT',
+    ctypes.c_short     : 'rffi.SHORT',
+    ctypes.c_uint16    : 'rffi.USHORT',
+    ctypes.c_int16     : 'rffi.SHORT',
+    ctypes.c_int       : 'rffi.INT',
+    ctypes.c_uint      : 'rffi.UINT',
+    ctypes.c_int32     : 'rffi.INT',
+    ctypes.c_uint32    : 'rffi.UINT',
+    #ctypes.c_long      : 'rffi.LONG', # same as c_int..
+    #ctypes.c_ulong     : 'rffi.ULONG',
+    ctypes.c_longlong  : 'rffi.LONGLONG',
+    ctypes.c_ulonglong : 'rffi.ULONGLONG',
+    ctypes.c_int64     : 'rffi.LONGLONG',
+    ctypes.c_uint64    : 'rffi.ULONGLONG',
+    ctypes.c_voidp     : 'rffi.VOIDP',
+    None               : 'rffi.lltype.Void', # XXX make a type in rffi
+    ctypes.c_char_p    : 'rffi.CCHARP',
+    ctypes.c_double    : 'rffi.lltype.Float', # XXX make a type in rffi
 }
 
 class RffiSource(object):
-    def __init__(self, structures=None, code=None):
-        # ctypes structure -> code mapping
-        if structures is None:
-            self.structures = {}
+    def __init__(self, structs=None, source=None):
+        # set of ctypes structs
+        if structs is None:
+            self.structs = set()
         else:
-            self.structures = structures
-        # rest of the code
-        if code is None:
-            self.code = py.code.Source()
+            self.structs = structs
+        if source is None:
+            self.source = py.code.Source()
         else:
-            self.code = code
+            self.source = source
+
+    def __str__(self):
+        return str(self.source)
 
     def __add__(self, other):
-        structs = self.structures.copy()
-        structs.update(other.structures)
-        code = py.code.Source(self.code, other.code)
-        return RffiSource(structs, code)
+        structs = self.structs.copy()
+        structs.update(other.structs)
+        source = py.code.Source(self.source, other.source)
+        return RffiSource(structs, source)
 
     def __iadd__(self, other):
-        self.structures.update(other.structures)
-        self.code = py.code.Source(self.code, other.code)
+        self.structs.update(other.structs)
+        self.source = py.code.Source(self.source, other.source)
         return self
 
     def proc_struct(self, tp):
         name = tp.__name__
-        real_name = 'c_' + name
-        real_name = real_name.rstrip('_structure')
-        real_name += '_structure'
-        if tp not in self.structures:
-            fields = ["('%s', %s)" % (name_, self.proc_tp(field_tp))
+        if tp not in self.structs:
+            fields = ["('%s', %s), " % (name_, self.proc_tp(field_tp))
                       for name_, field_tp in tp._fields_]
-            fields_repr = ", ".join(fields)
-            self.structures[tp] = py.code.Source(
-                "%s = rffi.CStruct('%s', %s)"%(real_name, name,
-                                              fields_repr))
-        return real_name
+            fields_repr = ''.join(fields)
+            self.structs.add(tp)
+            src = py.code.Source(
+                "%s = lltype.Struct('%s', %s hints={'external':'C'})"%(
+                    name, name, fields_repr))
+            self.source = py.code.Source(self.source, src)
+        return name
 
     def proc_tp(self, tp):
         try:
@@ -67,10 +87,29 @@
         raise NotImplementedError("Not implemented mapping for %s" % tp)
 
     def proc_func(self, func):
+        print "proc_func", func
         name = func.__name__
         src = py.code.Source("""
-        c_%s = rffi.llexternal('%s', [%s], %s)
+        %s = rffi.llexternal('%s', [%s], %s)
         """%(name, name, ", ".join([self.proc_tp(arg) for
                                    arg in func.argtypes]),
              self.proc_tp(func.restype)))
-        self.code = py.code.Source(self.code, src)
+        self.source = py.code.Source(self.source, src)
+
+    def proc_namespace(self, ns):
+        exempt = set(id(value) for value in ctypes.__dict__.values())
+        for key, value in ns.items():
+            if id(value) in exempt: 
+                continue
+            if isinstance(value, ctypes._CFuncPtr):
+                print "func", key, value
+                try:    
+                    self.proc_func(value)
+                except NotImplementedError:
+                    print "skipped:", value
+            #print value, value.__class__.__name__
+
+
+
+
+

Modified: pypy/dist/pypy/rpython/tool/test/test_mkrffi.py
==============================================================================
--- pypy/dist/pypy/rpython/tool/test/test_mkrffi.py	(original)
+++ pypy/dist/pypy/rpython/tool/test/test_mkrffi.py	Fri Jul 13 19:26:40 2007
@@ -10,10 +10,10 @@
 
 def test_rffisource():
     res = RffiSource({1:2, 3:4}, "ab") + RffiSource(None, "c")
-    assert res.structures == {1:2, 3:4}
-    assert str(res.code) == "ab\nc"
+    assert res.structs == {1:2, 3:4}
+    assert str(res.source) == "ab\nc"
     res += RffiSource({5:6})
-    assert 5 in res.structures.keys()
+    assert 5 in res.structs.keys()
 
 def test_proc_tp_simple():
     rffi_source = RffiSource()
@@ -26,9 +26,9 @@
            "lltype.Ptr(lltype.Array(rffi.UINT, hints={'nolength': True}))"
     rffi_source.proc_tp(random_structure)
     _src = py.code.Source("""
-    c_random_structure = rffi.CStruct('random_structure', ('one', rffi.INT), ('two', lltype.Ptr(lltype.Array(rffi.INT, hints={'nolength': True}))))
+    random_structure = lltype.Struct('random_structure', ('one', rffi.INT), ('two', lltype.Ptr(lltype.Array(rffi.INT, hints={'nolength': True}))),  hints={'external':'C'})
     """)
-    src = rffi_source.structures[random_structure]
+    src = rffi_source.source
     assert src.strip() == _src.strip(), str(src) + "\n" + str(_src)
 
 class TestMkrffi(TestBasic):
@@ -40,10 +40,10 @@
         src = RffiSource()
         src.proc_func(func)
         _src = py.code.Source("""
-        c_int_to_void_p = rffi.llexternal('int_to_void_p', [rffi.INT], rffi.VOIDP)
+        int_to_void_p = rffi.llexternal('int_to_void_p', [rffi.INT], rffi.VOIDP)
         """)
 
-        assert src.code == _src, str(src) + "\n" + str(_src)
+        assert src.source == _src, str(src) + "\n" + str(_src)
 
     def test_struct_return(self):
         func = self.lib.int_int_to_struct_p
@@ -51,9 +51,11 @@
         func.restype = ctypes.POINTER(random_structure)
         rffi_source = RffiSource()
         rffi_source.proc_func(func)
-        assert random_structure in rffi_source.structures
+        assert random_structure in rffi_source.structs
         _src = py.code.Source("""
-        c_int_int_to_struct_p = rffi.llexternal('int_int_to_struct_p', [rffi.INT, rffi.INT], lltype.Ptr(c_random_structure))
+        random_structure = lltype.Struct('random_structure', ('one', rffi.INT), ('two', lltype.Ptr(lltype.Array(rffi.INT, hints={'nolength': True}))),  hints={'external':'C'})
+
+        int_int_to_struct_p = rffi.llexternal('int_int_to_struct_p', [rffi.INT, rffi.INT], lltype.Ptr(random_structure))
         """)
-        src = rffi_source.code
+        src = rffi_source.source
         assert src.strip() == _src.strip(), str(src) + "\n" + str(_src)



More information about the Pypy-commit mailing list