Author: Wim Lavrijsen <[email protected]>
Branch: cling-support
Changeset: r86234:8d97e46d038c
Date: 2016-08-16 15:40 -0700
http://bitbucket.org/pypy/pypy/changeset/8d97e46d038c/

Log:    refactored changs from Aditi: all string tests now pass

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
@@ -158,14 +158,16 @@
     return _c_call_r(cppmethod, cppobject, nargs, args)
 _c_call_s = rffi.llexternal(
     "cppyy_call_s",
-    [C_METHOD, C_OBJECT, rffi.INT, rffi.VOIDP, rffi.INTP], rffi.CCHARP,
+    [C_METHOD, C_OBJECT, rffi.INT, rffi.VOIDP, rffi.SIZE_TP], rffi.CCHARP,
     releasegil=ts_call,
     compilation_info=backend.eci)
 def c_call_s(space, cppmethod, cppobject, nargs, args):
-    length = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
-    cstr = _c_call_s(cppmethod, cppobject, nargs, args, length)
-    cstr_len = int(length[0])
-    lltype.free(length, flavor='raw')
+    length = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
+    try:
+        cstr = _c_call_s(cppmethod, cppobject, nargs, args, length)
+        cstr_len = int(length[0])
+    finally:
+        lltype.free(length, flavor='raw')
     return cstr, cstr_len
 
 _c_constructor = rffi.llexternal(
@@ -548,11 +550,11 @@
     [rffi.CCHARP, rffi.SIZE_T], C_OBJECT,
     releasegil=ts_helper,
     compilation_info=backend.eci)
-def c_charp2stdstring(space, svalue, sz):
-    charp = rffi.str2charp(svalue)
-    result = _c_charp2stdstring(charp, sz)
-    rffi.free_charp(charp)
-    return result
+def c_charp2stdstring(space, pystr, sz):
+    cstr = rffi.str2charp(pystr)
+    cppstr = _c_charp2stdstring(cstr, sz)
+    rffi.free_charp(cstr)
+    return cppstr
 _c_stdstring2stdstring = rffi.llexternal(
     "cppyy_stdstring2stdstring",
     [C_OBJECT], C_OBJECT,
diff --git a/pypy/module/cppyy/capi/cling_capi.py 
b/pypy/module/cppyy/capi/cling_capi.py
--- a/pypy/module/cppyy/capi/cling_capi.py
+++ b/pypy/module/cppyy/capi/cling_capi.py
@@ -1,9 +1,13 @@
 import py, os
 
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
-from rpython.rtyper.lltypesystem import rffi
+from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rlib import libffi, rdynload
 
+from pypy.module.cppyy.capi.capi_types import C_OBJECT
+
 __all__ = ['identify', 'std_string_name', 'eci', 'c_load_dictionary']
 
 pkgpath = py.path.local(__file__).dirpath().join(os.pardir)
@@ -68,11 +72,42 @@
     pch = _c_load_dictionary(name)
     return pch
 
+_c_stdstring2charp = rffi.llexternal(
+    "cppyy_stdstring2charp",
+    [C_OBJECT, rffi.SIZE_TP], rffi.CCHARP,
+    releasegil=ts_helper,
+    compilation_info=eci)
+def c_stdstring2charp(space, cppstr):
+    sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
+    try:
+        cstr = _c_stdstring2charp(cppstr, sz)
+        cstr_len = int(sz[0])
+    finally:
+        lltype.free(sz, flavor='raw')
+    return rffi.charpsize2str(cstr, cstr_len)
 
-# Cling-specific pythonizations
+# pythonizations
+def stdstring_c_str(space, w_self):
+    """Return a python string taking into account \0"""
+
+    from pypy.module.cppyy import interp_cppyy
+    cppstr = space.interp_w(interp_cppyy.W_CPPInstance, w_self, 
can_be_None=False)
+    return space.wrap(c_stdstring2charp(space, cppstr._rawobject))
+
+# setup pythonizations for later use at run-time
+_pythonizations = {}
 def register_pythonizations(space):
     "NOT_RPYTHON"
-    pass
+
+    allfuncs = [
+
+        ### std::string
+        stdstring_c_str,
+
+    ]
+
+    for f in allfuncs:
+        _pythonizations[f.__name__] = space.wrap(interp2app(f))
 
 def _method_alias(space, w_pycppclass, m1, m2):
     space.setattr(w_pycppclass, space.wrap(m1),
@@ -80,4 +115,6 @@
 
 def pythonize(space, name, w_pycppclass):
     if name == "string":
+        space.setattr(w_pycppclass, space.wrap("c_str"), 
_pythonizations["stdstring_c_str"])
         _method_alias(space, w_pycppclass, "_cppyy_as_builtin", "c_str")
+        _method_alias(space, w_pycppclass, "__str__",           "c_str")
diff --git a/pypy/module/cppyy/include/capi.h b/pypy/module/cppyy/include/capi.h
--- a/pypy/module/cppyy/include/capi.h
+++ b/pypy/module/cppyy/include/capi.h
@@ -59,7 +59,7 @@
     RPY_EXTERN
     void*  cppyy_call_r(cppyy_method_t method, cppyy_object_t self, int nargs, 
void* args);
     RPY_EXTERN
-    char*  cppyy_call_s(cppyy_method_t method, cppyy_object_t self, int nargs, 
void* args, int* length);
+    char*  cppyy_call_s(cppyy_method_t method, cppyy_object_t self, int nargs, 
void* args, size_t* length);
 
     RPY_EXTERN
     cppyy_object_t cppyy_constructor(cppyy_method_t method, cppyy_type_t 
klass, int nargs, void* args);
@@ -179,6 +179,8 @@
     RPY_EXTERN
     cppyy_object_t cppyy_charp2stdstring(const char* str, size_t sz);
     RPY_EXTERN
+    char* cppyy_stdstring2charp(cppyy_object_t ptr, size_t* lsz);
+    RPY_EXTERN
     cppyy_object_t cppyy_stdstring2stdstring(cppyy_object_t ptr);
 
 #ifdef __cplusplus
diff --git a/pypy/module/cppyy/include/cpp_cppyy.h 
b/pypy/module/cppyy/include/cpp_cppyy.h
--- a/pypy/module/cppyy/include/cpp_cppyy.h
+++ b/pypy/module/cppyy/include/cpp_cppyy.h
@@ -62,7 +62,7 @@
    Double_t     CallD( TCppMethod_t method, TCppObject_t self, void* args );
    LongDouble_t CallLD( TCppMethod_t method, TCppObject_t self, void* args );
    void*        CallR( TCppMethod_t method, TCppObject_t self, void* args );
-   Char_t*      CallS( TCppMethod_t method, TCppObject_t self, void* args, 
int* length );
+   Char_t*      CallS( TCppMethod_t method, TCppObject_t self, void* args, 
size_t* length );
    TCppObject_t CallConstructor( TCppMethod_t method, TCppType_t type, void* 
args );
    void         CallDestructor( TCppType_t type, TCppObject_t self );
    TCppObject_t CallO( TCppMethod_t method, TCppObject_t self, void* args, 
TCppType_t result_type );
diff --git a/pypy/module/cppyy/src/clingcwrapper.cxx 
b/pypy/module/cppyy/src/clingcwrapper.cxx
--- a/pypy/module/cppyy/src/clingcwrapper.cxx
+++ b/pypy/module/cppyy/src/clingcwrapper.cxx
@@ -479,7 +479,7 @@
 }
 
 Char_t* Cppyy::CallS(
-      TCppMethod_t method, TCppObject_t self, void* args, int* length )
+      TCppMethod_t method, TCppObject_t self, void* args, size_t* length )
 {
    char* cstr = nullptr;
    TClassRef cr("std::string");
@@ -1202,9 +1202,9 @@
 }
 
 char* cppyy_call_s(
-        cppyy_method_t method, cppyy_object_t self, int nargs, void* args, 
int* length) {
+        cppyy_method_t method, cppyy_object_t self, int nargs, void* args, 
size_t* lsz) {
     std::vector<TParameter> parvec = vsargs_to_parvec(args, nargs);
-    return Cppyy::CallS(method, (void*)self, &parvec, length);
+    return Cppyy::CallS(method, (void*)self, &parvec, lsz);
 }
 
 cppyy_object_t cppyy_constructor(cppyy_method_t method, cppyy_type_t klass, 
int nargs, void* args) {
@@ -1463,10 +1463,15 @@
     free(ptr);
 }
 
-cppyy_object_t cppyy_charp2stdstring(const char* str, size_t sz){
+cppyy_object_t cppyy_charp2stdstring(const char* str, size_t sz) {
     return (cppyy_object_t)new std::string(str, sz);
 }
 
+char* cppyy_stdstring2charp(cppyy_object_t ptr, size_t* lsz) {
+    *lsz = ((std::string*)ptr)->size();
+    return (char*)((std::string*)ptr)->data();
+}
+
 cppyy_object_t cppyy_stdstring2stdstring(cppyy_object_t ptr){
     return (cppyy_object_t)new std::string(*(std::string*)ptr);
 }
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to