[pypy-commit] pypy reflex-support: allow const char* returns to be NULL (-> empty string)

wlav noreply at buildbot.pypy.org
Wed Mar 27 00:43:02 CET 2013


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r62820:c74ba167a532
Date: 2013-03-26 13:30 -0700
http://bitbucket.org/pypy/pypy/changeset/c74ba167a532/

Log:	allow const char* returns to be NULL (-> empty string)

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
@@ -133,8 +133,10 @@
     def execute(self, space, cppmethod, cppthis, num_args, args):
         lresult = capi.c_call_l(cppmethod, cppthis, num_args, args)
         ccpresult = rffi.cast(rffi.CCHARP, lresult)
-        result = rffi.charp2str(ccpresult)  # TODO: make it a choice to free
-        return space.wrap(result)
+        if ccpresult:
+            result = rffi.charp2str(ccpresult)  # TODO: make it a choice to free
+            return space.wrap(result)
+        return space.wrap('')
 
 
 class ConstructorExecutor(FunctionExecutor):
diff --git a/pypy/module/cppyy/test/datatypes.cxx b/pypy/module/cppyy/test/datatypes.cxx
--- a/pypy/module/cppyy/test/datatypes.cxx
+++ b/pypy/module/cppyy/test/datatypes.cxx
@@ -183,6 +183,10 @@
 double              cppyy_test_data::s_double = -707.;
 cppyy_test_data::what  cppyy_test_data::s_enum = cppyy_test_data::kNothing;
 
+//- strings -----------------------------------------------------------------
+const char* cppyy_test_data::get_valid_string(const char* in) { return in; }
+const char* cppyy_test_data::get_invalid_string() { return (const char*)0; }
+
 
 //= global functions ========================================================
 long get_pod_address(cppyy_test_data& c)
diff --git a/pypy/module/cppyy/test/datatypes.h b/pypy/module/cppyy/test/datatypes.h
--- a/pypy/module/cppyy/test/datatypes.h
+++ b/pypy/module/cppyy/test/datatypes.h
@@ -148,6 +148,10 @@
     float*          pass_void_array_f(void* a) { return pass_array((float*)a); }
     double*         pass_void_array_d(void* a) { return pass_array((double*)a); }
 
+// strings
+    const char* get_valid_string(const char* in);
+    const char* get_invalid_string();
+
 public:
 // basic types
     bool                 m_bool;
diff --git a/pypy/module/cppyy/test/test_datatypes.py b/pypy/module/cppyy/test/test_datatypes.py
--- a/pypy/module/cppyy/test/test_datatypes.py
+++ b/pypy/module/cppyy/test/test_datatypes.py
@@ -459,7 +459,17 @@
         assert gbl.kBanana == 29
         assert gbl.kCitrus == 34
 
-    def test12_copy_contructor(self):
+    def test12_string_passing(self):
+        """Test passing/returning of a const char*"""
+
+        import cppyy
+        cppyy_test_data = cppyy.gbl.cppyy_test_data
+
+        c = cppyy_test_data()
+        assert c.get_valid_string('aap') == 'aap'
+        assert c.get_invalid_string() == ''
+
+    def test13_copy_contructor(self):
         """Test copy constructor"""
 
         import cppyy
@@ -475,7 +485,7 @@
         for i in range(4):
             assert t1[i] == t3[i]
 
-    def test13_object_returns(self):
+    def test14_object_returns(self):
         """Test access to and return of PODs"""
 
         import cppyy
@@ -502,7 +512,7 @@
         assert c.get_pod_ptrref().m_int == 666
         assert c.get_pod_ptrref().m_double == 3.14
 
-    def test14_object_arguments(self):
+    def test15_object_arguments(self):
         """Test setting and returning of a POD through arguments"""
 
         import cppyy
@@ -570,7 +580,7 @@
         assert p.m_int == 888
         assert p.m_double == 3.14
 
-    def test15_respect_privacy(self):
+    def test16_respect_privacy(self):
         """Test that privacy settings are respected"""
 
         import cppyy
@@ -583,7 +593,7 @@
 
         c.destruct()
 
-    def test16_object_and_pointer_comparisons(self):
+    def test17_object_and_pointer_comparisons(self):
         """Verify object and pointer comparisons"""
     
         import cppyy 
@@ -620,7 +630,7 @@
         assert l3 != l5
         assert l5 != l3
 
-    def test17_object_validity(self):
+    def test18_object_validity(self):
         """Test object validity checking"""
         
         from cppyy import gbl
@@ -634,7 +644,7 @@
 
         assert not d2
 
-    def test18_buffer_reshaping(self):
+    def test19_buffer_reshaping(self):
         """Test usage of buffer sizing"""
 
         import cppyy
@@ -654,4 +664,3 @@
             l = list(arr)
             for i in range(self.N):
                 assert arr[i] == l[i]
-


More information about the pypy-commit mailing list