Author: Wim Lavrijsen <[email protected]>
Branch: cling-support
Changeset: r86295:4831e0f5b4d5
Date: 2016-08-18 13:41 -0700
http://bitbucket.org/pypy/pypy/changeset/4831e0f5b4d5/

Log:    fix string handling for the loadable capi

diff --git a/pypy/module/cppyy/capi/builtin_capi.py 
b/pypy/module/cppyy/capi/builtin_capi.py
--- a/pypy/module/cppyy/capi/builtin_capi.py
+++ b/pypy/module/cppyy/capi/builtin_capi.py
@@ -354,7 +354,7 @@
         i += 1
         py_indices.append(index)
         index = indices[i]
-    c_free(rffi.cast(rffi.VOIDP, indices))   # c_free defined below
+    c_free(space, rffi.cast(rffi.VOIDP, indices))      # c_free defined below
     return py_indices
 
 _c_method_name = rffi.llexternal(
@@ -533,16 +533,18 @@
     compilation_info=backend.eci)
 def c_strtoull(space, svalue):
     return _c_strtoull(svalue)
-c_free = rffi.llexternal(
+_c_free = rffi.llexternal(
     "cppyy_free",
     [rffi.VOIDP], lltype.Void,
     releasegil=ts_memory,
     compilation_info=backend.eci)
+def c_free(space, voidp):
+    return _c_free(voidp)
 
 def charp2str_free(space, charp):
     string = rffi.charp2str(charp)
     voidp = rffi.cast(rffi.VOIDP, charp)
-    c_free(voidp)
+    _c_free(voidp)
     return string
 
 _c_charp2stdstring = rffi.llexternal(
diff --git a/pypy/module/cppyy/capi/loadable_capi.py 
b/pypy/module/cppyy/capi/loadable_capi.py
--- a/pypy/module/cppyy/capi/loadable_capi.py
+++ b/pypy/module/cppyy/capi/loadable_capi.py
@@ -220,7 +220,7 @@
 
             'charp2stdstring'          : ([c_ccharp, c_size_t],       
c_object),
             #stdstring2charp  actually takes an size_t* as last parameter, but 
this will do
-            'stdstring2charp'          : ([c_ccharp, c_voidp],        
c_ccharp),
+            'stdstring2charp'          : ([c_object, c_voidp],        
c_ccharp),
             'stdstring2stdstring'      : ([c_object],                 
c_object),
         }
 
@@ -281,6 +281,10 @@
     ptr = w_cdata.unsafe_escaping_ptr()
     return rffi.cast(rffi.VOIDP, ptr)
 
+def _cdata_to_ccharp(space, w_cdata):
+    ptr = _cdata_to_ptr(space, w_cdata)      # see above ... something better?
+    return rffi.cast(rffi.CCHARP, ptr)
+
 def c_load_dictionary(name):
     return libffi.CDLL(name)
 
@@ -341,11 +345,14 @@
     return _cdata_to_ptr(space, call_capi(space, 'call_r', args))
 def c_call_s(space, cppmethod, cppobject, nargs, cargs):
     length = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
-    args = [_Arg(h=cppmethod), _Arg(h=cppobject), _Arg(l=nargs), 
_Arg(vp=cargs), _Arg(vp=length)]
-    cstr = call_capi(space, 'call_s', args)
-    cstr_len = int(length[0])
-    lltype.free(length, flavor='raw')
-    return cstr, cstr_len
+    try:
+        w_cstr = call_capi(space, 'call_s',
+            [_Arg(h=cppmethod), _Arg(h=cppobject), _Arg(l=nargs), 
_Arg(vp=cargs),
+             _Arg(vp=rffi.cast(rffi.VOIDP, length))])
+        cstr_len = int(length[0])
+    finally:
+        lltype.free(length, flavor='raw')
+    return _cdata_to_ccharp(space, w_cstr), cstr_len
 
 def c_constructor(space, cppmethod, cppobject, nargs, cargs):
     args = [_Arg(h=cppmethod), _Arg(h=cppobject), _Arg(l=nargs), 
_Arg(vp=cargs)]
@@ -527,15 +534,16 @@
 
 def c_charp2stdstring(space, svalue, sz):
     return _cdata_to_cobject(
-        space, call_capi(space, 'charp2stdstring', [_Arg(s=svalue), 
_Arg(l=sz)]))
+        space, call_capi(space, 'charp2stdstring', [_Arg(s=svalue), 
_Arg(h=sz)]))
 def c_stdstring2charp(space, cppstr):
     sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
     try:
-        cstr = call_capi(space, 'stdstring2charp', [_Arg(h=cppstr), 
_Arg(vp=sz)])
+        w_cstr = call_capi(space, 'stdstring2charp',
+            [_Arg(h=cppstr), _Arg(vp=rffi.cast(rffi.VOIDP, sz))])
         cstr_len = int(sz[0])
     finally:
         lltype.free(sz, flavor='raw')
-    return rffi.charpsize2str(cstr, cstr_len)
+    return rffi.charpsize2str(_cdata_to_ccharp(space, w_cstr), cstr_len)
 def c_stdstring2stdstring(space, cppobject):
     return _cdata_to_cobject(space, call_capi(space, 'stdstring2stdstring', 
[_Arg(h=cppobject)]))
 
diff --git a/pypy/module/cppyy/executor.py b/pypy/module/cppyy/executor.py
--- a/pypy/module/cppyy/executor.py
+++ b/pypy/module/cppyy/executor.py
@@ -196,9 +196,9 @@
 
     def execute(self, space, cppmethod, cppthis, num_args, args):
         cstr, cstr_len = capi.c_call_s(space, cppmethod, cppthis, num_args, 
args)
-        string = rffi.charpsize2str(cstr, cstr_len)
-        capi.c_free(rffi.cast(rffi.VOIDP, cstr))
-        return space.wrap(string)
+        pystr = rffi.charpsize2str(cstr, cstr_len)
+        capi.c_free(space, rffi.cast(rffi.VOIDP, cstr))
+        return space.wrap(pystr)
 
     def execute_libffi(self, space, cif_descr, funcaddr, buffer):
         from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to