Author: Wim Lavrijsen <[email protected]>
Branch: reflex-support
Changeset: r55839:0211a574720a
Date: 2012-06-25 17:21 -0700
http://bitbucket.org/pypy/pypy/changeset/0211a574720a/

Log:    o) fix memory overwrite in test o) remove spurious include of
        iostream o) add tests and arg passing for arrays

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
@@ -135,6 +135,21 @@
     def __init__(self, space, array_size):
         self.size = sys.maxint
 
+    def convert_argument(self, space, w_obj, address, call_local):
+        tc = space.str_w(space.getattr(w_obj, space.wrap('typecode')))
+        if self.typecode != tc:
+            msg = "expected %s pointer type, but received %s" % 
(self.typecode, tc)
+            raise OperationError(space.w_TypeError, space.wrap(msg))
+        x = rffi.cast(rffi.LONGP, address)
+        buf = space.buffer_w(w_obj)
+        try:
+            x[0] = rffi.cast(rffi.LONG, buf.get_raw_address())
+        except ValueError:
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("raw buffer interface not 
supported"))
+        ba = rffi.cast(rffi.CCHARP, address)
+        ba[capi.c_function_arg_typeoffset()] = 'o'
+
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         # read access, so no copy needed
         address_value = self._get_raw_address(space, w_obj, offset)
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
@@ -1,7 +1,5 @@
 #include "datatypes.h"
 
-#include <iostream>
-
 
 //===========================================================================
 cppyy_test_data::cppyy_test_data() : m_owns_arrays(false)
@@ -151,8 +149,19 @@
 void cppyy_test_data::set_pod_ref(const cppyy_test_pod& rp)    { m_pod = rp; }
 void cppyy_test_data::set_pod_ptrptr_in(cppyy_test_pod** ppp)  { m_pod = 
**ppp; }
 void cppyy_test_data::set_pod_void_ptrptr_in(void** pp)        { m_pod = 
**((cppyy_test_pod**)pp); }
-void cppyy_test_data::set_pod_ptrptr_out(cppyy_test_pod** ppp) { *ppp = 
&m_pod; }
-void cppyy_test_data::set_pod_void_ptrptr_out(void** pp)       { 
*((cppyy_test_pod**)pp) = &m_pod; }
+void cppyy_test_data::set_pod_ptrptr_out(cppyy_test_pod** ppp) { delete *ppp; 
*ppp = new cppyy_test_pod(m_pod); }
+void cppyy_test_data::set_pod_void_ptrptr_out(void** pp)       { delete 
*((cppyy_test_pod**)pp);
+                                                                 
*((cppyy_test_pod**)pp) = new cppyy_test_pod(m_pod); }
+
+//- passers -----------------------------------------------------------------
+short*          cppyy_test_data::pass_array(short* a)          { return a; }
+unsigned short* cppyy_test_data::pass_array(unsigned short* a) { return a; }
+int*            cppyy_test_data::pass_array(int* a)            { return a; }
+unsigned int*   cppyy_test_data::pass_array(unsigned int* a)   { return a; }
+long*           cppyy_test_data::pass_array(long* a)           { return a; }
+unsigned long*  cppyy_test_data::pass_array(unsigned long* a)  { return a; }
+float*          cppyy_test_data::pass_array(float* a)          { return a; }
+double*         cppyy_test_data::pass_array(double* a)         { return a; }
 
 char                cppyy_test_data::s_char   = 's';
 unsigned char       cppyy_test_data::s_uchar  = 'u';
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
@@ -94,6 +94,16 @@
     void set_pod_ptrptr_out(cppyy_test_pod**);
     void set_pod_void_ptrptr_out(void**);
 
+// passers
+    short*          pass_array(short*);
+    unsigned short* pass_array(unsigned short*);
+    int*            pass_array(int*);
+    unsigned int*   pass_array(unsigned int*);
+    long*           pass_array(long*);
+    unsigned long*  pass_array(unsigned long*);
+    float*          pass_array(float*);
+    double*         pass_array(double*);
+
 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
@@ -194,16 +194,24 @@
 
         c.destruct()
 
-    def test04_respect_privacy(self):
-        """Test that privacy settings are respected"""
+    def test04_array_passing(self):
+        """Test passing of array arguments"""
 
-        import cppyy
+        import cppyy, array, sys
         cppyy_test_data = cppyy.gbl.cppyy_test_data
 
         c = cppyy_test_data()
         assert isinstance(c, cppyy_test_data)
 
-        raises(AttributeError, getattr, c, 'm_owns_arrays')
+        a = range(self.N)
+        # test arrays in mixed order, to give overload resolution a workout
+        for t in ['d', 'i', 'f', 'H', 'I', 'h', 'L', 'l' ]:
+            b = array.array(t, a)
+            ca = c.pass_array(b)
+            assert type(ca[0]) == type(b[0])
+            assert len(b) == self.N
+            for i in range(self.N):
+                assert ca[i] == b[i]
 
         c.destruct()
 
@@ -524,3 +532,16 @@
         assert c.m_pod.m_double == 3.14
         assert p.m_int == 888
         assert p.m_double == 3.14
+
+    def test14_respect_privacy(self):
+        """Test that privacy settings are respected"""
+
+        import cppyy
+        cppyy_test_data = cppyy.gbl.cppyy_test_data
+
+        c = cppyy_test_data()
+        assert isinstance(c, cppyy_test_data)
+
+        raises(AttributeError, getattr, c, 'm_owns_arrays')
+
+        c.destruct()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to