[pypy-svn] r43965 - in pypy/branch/kill-ctypes/pypy/rpython/lltypesystem: . test
fijal at codespeak.net
fijal at codespeak.net
Fri Jun 1 07:48:38 CEST 2007
Author: fijal
Date: Fri Jun 1 07:48:37 2007
New Revision: 43965
Modified:
pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/rffi.py
pypy/branch/kill-ctypes/pypy/rpython/lltypesystem/test/test_rffi.py
Log:
Add a possibility to map backwards from charp to str. There is still open
question what to do with free, since RTyper is sometimes complaining
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 Jun 1 07:48:37 2007
@@ -18,11 +18,12 @@
def lltype(self):
return self.TP
-def llexternal(name, args, result, sources=[], includes=[]):
+def llexternal(name, args, result, sources=[], includes=[], libraries=[]):
ext_type = lltype.FuncType(args, result)
return lltype.functionptr(ext_type, name, external='C',
sources=tuple(sources),
- includes=tuple(includes))
+ includes=tuple(includes),
+ libraries=tuple(libraries))
def setup():
""" creates necessary c-level types
@@ -61,6 +62,7 @@
CCHARP = lltype.Ptr(lltype.Array(lltype.Char, hints={'nolength': True}))
# various type mapping
+# str -> char*
def str2charp(s):
""" str -> char*
"""
@@ -70,6 +72,16 @@
array[len(s)] = '\x00'
return array
+# char* -> str
+# doesn't free char*
+def charp2str(cp):
+ l = []
+ i = 0
+ while cp[i] != '\x00':
+ l.append(cp[i])
+ i += 1
+ return "".join(l)
+
# char**
CCHARPP = lltype.Ptr(lltype.Array(CCHARP, hints={'nolength': True}))
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 Jun 1 07:48:37 2007
@@ -1,5 +1,5 @@
-#import py
+import py
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
@@ -51,6 +51,33 @@
xf = compile(f, [], backendopt=False)
assert xf() == 3
+def test_string_reverse():
+ c_source = py.code.Source("""
+ #include <string.h>
+
+ char *f(char* arg)
+ {
+ char *ret;
+ ret = (char*)malloc(strlen(arg) + 1);
+ strcpy(ret, arg);
+ return ret;
+ }
+ """)
+ c_file = udir.join("stringrev.c")
+ c_file.write(c_source)
+ z = llexternal('f', [CCHARP], CCHARP, sources=[str(c_file)])
+
+ def f():
+ s = str2charp("xxx")
+ l_res = z(s)
+ res = charp2str(l_res)
+ lltype.free(l_res, flavor='raw')
+ lltype.free(s, flavor='raw')
+ return len(res)
+
+ xf = compile(f, [], backendopt=False)
+ assert xf(expected_extra_mallocs=-1) == 3
+
def test_stringstar():
c_source = """
#include <string.h>
More information about the Pypy-commit
mailing list