[pypy-commit] pypy reflex-support: assignment from str to std::string and __str__ for std::string

wlav noreply at buildbot.pypy.org
Wed Apr 25 00:44:30 CEST 2012


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r54740:ad83dba9b24b
Date: 2012-04-24 13:33 -0700
http://bitbucket.org/pypy/pypy/changeset/ad83dba9b24b/

Log:	assignment from str to std::string and __str__ for std::string

diff --git a/pypy/module/cppyy/capi/__init__.py b/pypy/module/cppyy/capi/__init__.py
--- a/pypy/module/cppyy/capi/__init__.py
+++ b/pypy/module/cppyy/capi/__init__.py
@@ -430,6 +430,11 @@
     [C_OBJECT], C_OBJECT,
     threadsafe=threadsafe,
     compilation_info=backend.eci)
+c_assign2stdstring = rffi.llexternal(
+    "cppyy_assign2stdstring",
+    [C_OBJECT, rffi.CCHARP], lltype.Void,
+    threadsafe=threadsafe,
+    compilation_info=backend.eci)
 c_free_stdstring = rffi.llexternal(
     "cppyy_free_stdstring",
     [C_OBJECT], lltype.Void,
diff --git a/pypy/module/cppyy/converter.py b/pypy/module/cppyy/converter.py
--- a/pypy/module/cppyy/converter.py
+++ b/pypy/module/cppyy/converter.py
@@ -643,6 +643,17 @@
            arg = InstanceConverter._unwrap_object(self, space, w_obj)
            return capi.c_stdstring2stdstring(arg)
 
+    def to_memory(self, space, w_obj, w_value, offset):
+        try:
+            address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset))
+            charp = rffi.str2charp(space.str_w(w_value))
+            capi.c_assign2stdstring(address, charp)
+            rffi.free_charp(charp)
+            return
+        except Exception:
+            pass
+        return InstanceConverter.to_memory(self, space, w_obj, w_value, offset)
+
     def free_argument(self, arg):
         capi.c_free_stdstring(rffi.cast(capi.C_OBJECT, rffi.cast(rffi.VOIDPP, arg)[0]))
 
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
@@ -100,6 +100,7 @@
 
     cppyy_object_t cppyy_charp2stdstring(const char* str);
     cppyy_object_t cppyy_stdstring2stdstring(cppyy_object_t ptr);
+    void cppyy_assign2stdstring(cppyy_object_t ptr, const char* str);
     void cppyy_free_stdstring(cppyy_object_t ptr);
 
 #ifdef __cplusplus
diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -354,6 +354,7 @@
             else:
                 return self.c_str() == other
         pyclass.__eq__ = eq
+        pyclass.__str__ = pyclass.c_str
 
     # TODO: clean this up
     # fixup lack of __getitem__ if no const return
diff --git a/pypy/module/cppyy/src/cintcwrapper.cxx b/pypy/module/cppyy/src/cintcwrapper.cxx
--- a/pypy/module/cppyy/src/cintcwrapper.cxx
+++ b/pypy/module/cppyy/src/cintcwrapper.cxx
@@ -761,6 +761,10 @@
     delete (std::string*)ptr;
 }
 
+void cppyy_assign2stdstring(cppyy_object_t ptr, const char* str) {
+   *((std::string*)ptr) = str;
+}
+
 void* cppyy_load_dictionary(const char* lib_name) {
     if (0 <= gSystem->Load(lib_name))
         return (void*)1;
diff --git a/pypy/module/cppyy/src/reflexcwrapper.cxx b/pypy/module/cppyy/src/reflexcwrapper.cxx
--- a/pypy/module/cppyy/src/reflexcwrapper.cxx
+++ b/pypy/module/cppyy/src/reflexcwrapper.cxx
@@ -520,6 +520,10 @@
     return (cppyy_object_t)new std::string(*(std::string*)ptr);
 }
 
+void cppyy_assign2stdstring(cppyy_object_t ptr, const char* str) {
+   *((std::string*)ptr) = str;
+}
+
 void cppyy_free_stdstring(cppyy_object_t ptr) {
     delete (std::string*)ptr;
 }
diff --git a/pypy/module/cppyy/test/test_stltypes.py b/pypy/module/cppyy/test/test_stltypes.py
--- a/pypy/module/cppyy/test/test_stltypes.py
+++ b/pypy/module/cppyy/test/test_stltypes.py
@@ -244,25 +244,27 @@
 
         raises(TypeError, c.get_string2, "temp string")
 
-    def test02_string_data_ccess(self):
+    def test02_string_data_access(self):
         """Test access to std::string object data members"""
 
         import cppyy
         std = cppyy.gbl.std
         stringy_class = cppyy.gbl.stringy_class
 
-        return
-
         c, s = stringy_class(""), std.string("test string")
 
-        c.m_string = s
-        assert c.m_string == s
-        assert c.get_string1() == s
-
         c.m_string = "another test"
         assert c.m_string == "another test"
+        assert str(c.m_string) == c.m_string
         assert c.get_string1() == "another test"
 
+        return
+        # TODO: assignment from object
+        c.m_string = s
+        assert str(c.m_string) == s
+        assert c.m_string == s
+        assert c.get_string1() == s
+
     def test03_string_with_null_character(self):
         """Test that strings with NULL do not get truncated"""
 


More information about the pypy-commit mailing list