[pypy-svn] r43626 - in pypy/branch/kill-ctypes/pypy/rpython/lltypesystem: . test

fijal at codespeak.net fijal at codespeak.net
Fri May 25 14:40:28 CEST 2007


Author: fijal
Date: Fri May 25 14:40:27 2007
New Revision: 43626

Modified:
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/lloperation.py
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
   pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
Log:
Next test (involving char**, null terminated) passes.
This is just enough to write down execv


Modified: pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/lloperation.py	Fri May 25 14:40:27 2007
@@ -314,6 +314,7 @@
     'zero_malloc_varsize':  LLOp(canraise=(MemoryError,), canunwindgc=True),
     'zero_gc_pointers_inside': LLOp(),
     'flavored_malloc':      LLOp(canraise=(MemoryError,)),
+    'flavored_malloc_varsize' : LLOp(canraise=(MemoryError,)),
     'flavored_free':        LLOp(),
     'getfield':             LLOp(sideeffects=False, canrun=True),
     'getarrayitem':         LLOp(sideeffects=False, canrun=True),

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	Fri May 25 14:40:27 2007
@@ -1,5 +1,6 @@
 
 from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem.lloperation import llop
 
 def llexternal(name, args, result, sources=[], includes=[]):
     ext_type = lltype.FuncType(args, result)
@@ -15,5 +16,24 @@
     array = lltype.malloc(CCHARP, len(s) + 1, flavor='raw')
     for i in range(len(s)):
         array[i] = s[i]
-    array[len(s)] = chr(0)
+    array[len(s)] = '\x00'
     return array
+
+CCHARPP = lltype.Array(lltype.Ptr(CCHARP), hints={'nolength': True})
+# list[str] -> char**, NULL terminated
+def liststr2charpp(l):
+    array = lltype.malloc(CCHARPP, len(l) + 1, flavor='raw')
+    for i in range(len(l)):
+        array[i] = str2charp(l[i])
+    array[len(l)] = lltype.nullptr(CCHARP)
+    return array
+
+# frees list of char**
+def free_charpp(ref):
+    next = ref
+    i = 0
+    while next[i]:
+        lltype.free(next[i], flavor='raw')
+        i += 1
+    lltype.free(ref, flavor='raw')
+

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	Fri May 25 14:40:27 2007
@@ -1,6 +1,6 @@
 
 #import py
-from pypy.rpython.lltypesystem.rffi import llexternal, str2charp, CCHARP
+from pypy.rpython.lltypesystem.rffi import *
 from pypy.translator.c.test.test_genc import compile
 from pypy.rpython.lltypesystem.lltype import Signed, Ptr, Char, malloc
 from pypy.rpython.lltypesystem import lltype
@@ -40,7 +40,7 @@
     assert xf() == 8+3
 
 def test_string():
-    z = llexternal('strlen', [Ptr(CCHARP)], Signed, includes=['stdio.h'])
+    z = llexternal('strlen', [Ptr(CCHARP)], Signed, includes=['string.h'])
 
     def f():
         s = str2charp("xxx")
@@ -50,3 +50,31 @@
 
     xf = compile(f, [], backendopt=False)
     assert xf() == 3
+
+def test_stringstar():
+    c_source = """
+    #include <string.h>
+    
+    int f(char *args[]) {
+        char **p = args;
+        int l = 0;
+        while (*p) {
+            l += strlen(*p);
+            p++;
+        }
+        return (l);
+    }
+    """
+    c_file = udir.join("stringstar.c")
+    c_file.write(c_source)
+    z = llexternal('f', [Ptr(CCHARPP)], Signed, sources=[str(c_file)])
+
+    def f():
+        l = ["xxx", "x", "xxxx"]
+        ss = liststr2charpp(l)
+        result = z(ss)
+        free_charpp(ss)
+        return result
+
+    xf = compile(f, [], backendopt=False)
+    assert xf() == 8



More information about the Pypy-commit mailing list