[pypy-svn] r43570 - in pypy/branch/kill-ctypes/pypy: rpython/lltypesystem rpython/lltypesystem/test translator/c

fijal at codespeak.net fijal at codespeak.net
Wed May 23 15:02:53 CEST 2007


Author: fijal
Date: Wed May 23 15:02:52 2007
New Revision: 43570

Modified:
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
   pypy/branch/kill-ctypes/pypy/translator/c/funcgen.py
Log:
Add mapping from str -> char*. Kind of works, but requires some
more thinking probably.


Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py	Wed May 23 15:02:52 2007
@@ -1,9 +1,19 @@
 
-from pypy.rpython.lltypesystem.lltype import functionptr, FuncType
+from pypy.rpython.lltypesystem import lltype
 
 def llexternal(name, args, result, sources=[], includes=[]):
-    ext_type = FuncType(args, result)
-    return functionptr(ext_type, name, external='C', sources=tuple(sources),
-                       includes=tuple(includes))
+    ext_type = lltype.FuncType(args, result)
+    return lltype.functionptr(ext_type, name, external='C',
+                              sources=tuple(sources),
+                              includes=tuple(includes))
 
+CCHARP = lltype.Array(lltype.Char, hints={'nolength': True})
 
+# various type mapping
+# str -> char*
+def str2charp(s):
+    array = lltype.malloc(CCHARP, len(s) + 1, flavor='raw')
+    for i in range(len(s)):
+        array[i] = s[i]
+    array[len(s)] = chr(0)
+    return array

Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py	Wed May 23 15:02:52 2007
@@ -1,7 +1,9 @@
 
-from pypy.rpython.lltypesystem.rffi import llexternal
+#import py
+from pypy.rpython.lltypesystem.rffi import llexternal, str2charp, CCHARP
 from pypy.translator.c.test.test_genc import compile
-from pypy.rpython.lltypesystem.lltype import Signed
+from pypy.rpython.lltypesystem.lltype import Signed, Ptr, Char, malloc
+from pypy.rpython.lltypesystem import lltype
 from pypy.tool.udir import udir
 
 def test_basic():
@@ -36,3 +38,46 @@
 
     xf = compile(f, [])
     assert xf() == 8+3
+
+def test_string():
+    z = llexternal('strlen', [Ptr(CCHARP)], Signed, includes=['stdio.h'])
+
+    def f():
+        s = str2charp("xxx")
+        res = z(s)
+        lltype.free(s, flavor='raw')
+        return res
+
+    xf = compile(f, [], backendopt=False)
+    assert xf() == 3
+
+def test_stringstar():
+    import py
+    py.test.skip("completely broken")
+    c_source = """
+    int f(char *args[]) {
+        char **p = args;
+        int l = 0;
+        while (p) {
+            l += strlen(p*);
+            p++;
+        }
+        return (l);
+    }
+    """
+    elem_T = lltype.FixedSizeArray(lltype.Char, 1)
+    T = lltype.FixedSizeArray(Ptr(elem_T), 1) # this is char**
+    z = llexternal('f', [Ptr(T)], Signed, includes=['stdio.h'])
+    alloc_T = lltype.Array(CCHARP)
+
+    def f():
+        ss = malloc(alloc_T, 4, flavor='raw')
+        ref1, ss[0] = str2charp("xxx")
+        ref2, ss[1] = str2charp("x")
+        ref3, ss[2] = str2charp("xxxx")
+        _, ss[3] = lltype.nullptr(elem_T)
+        to_fun = lltype._subarray._makeptr(ss._obj, 0, ss._solid)
+        return z(to_fun)
+
+    xf = compile(f, [])
+    assert xf() == 8

Modified: pypy/branch/kill-ctypes/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/translator/c/funcgen.py	(original)
+++ pypy/branch/kill-ctypes/pypy/translator/c/funcgen.py	Wed May 23 15:02:52 2007
@@ -590,6 +590,8 @@
         else:
             raise NotImplementedError
 
+    OP_FLAVORED_MALLOC_VARSIZE = OP_FLAVORED_MALLOC
+
     def OP_FLAVORED_FREE(self, op):
         flavor = op.args[0].value
         if flavor == "raw":



More information about the Pypy-commit mailing list