[pypy-svn] r76194 - in pypy/branch/reflex-support/pypy/module/cppyy: . test

wlav at codespeak.net wlav at codespeak.net
Wed Jul 14 11:09:14 CEST 2010


Author: wlav
Date: Wed Jul 14 11:09:13 2010
New Revision: 76194

Modified:
   pypy/branch/reflex-support/pypy/module/cppyy/pythonify.py
   pypy/branch/reflex-support/pypy/module/cppyy/test/example01.cxx
   pypy/branch/reflex-support/pypy/module/cppyy/test/example01.h
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_pythonify.py
Log:
Enable returning objects from static classes and some code cleanup/cutification.


Modified: pypy/branch/reflex-support/pypy/module/cppyy/pythonify.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/pythonify.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/pythonify.py	Wed Jul 14 11:09:13 2010
@@ -7,16 +7,26 @@
 
 class CppyyObject(object):
     def __init__(self, *args):
-        print '__init__ called', args
         self._cppinstance = self._cppyyclass.construct(*args)
         
     def destruct(self):
         self._cppinstance.destruct()
 
-
-def make_static_function(cpptype, name):
-    def method(*args):
-        return cpptype.invoke(name, *args)
+def bind_object(cppobj, cppclass):
+    if cppobj is None:
+        return None
+    bound_obj = object.__new__(cppclass)
+    bound_obj._cppinstance = cppobj
+    return bound_obj
+
+def make_static_function(cpptype, name, rettype):
+    if rettype is None:
+        def method(*args):
+            return cpptype.invoke(name, *args)
+    else:
+        cppclass = get_cppclass(rettype)
+        def method(*args):
+            return bind_object(cpptype.invoke(name, *args), cppclass)
     method.__name__ = name
     return staticmethod(method)
 
@@ -24,17 +34,12 @@
     if rettype is None:                          # return builtin type
         def method(self, *args):
             return self._cppinstance.invoke(name, *args)
-        method.__name__ = name
-        return method
     else:                                        # return instance
+        cppclass = get_cppclass(rettype)
         def method(self, *args):
-            result = self._cppinstance.invoke(name, *args)
-            if not result is None:
-                bound_result = object.__new__(get_cppclass(rettype))
-                bound_result._cppinstance = result
-            return bound_result
-        method.__name__ = name
-        return method
+            return bind_object(self._cppinstance.invoke(name, *args), cppclass)
+    method.__name__ = name
+    return method
 
 
 _existing_classes = {}
@@ -52,7 +57,7 @@
     for f in cpptype.get_function_members():
         cppol = cpptype.get_overload(f)
         if cppol.is_static():
-            d[f] = make_static_function(cpptype, f)
+            d[f] = make_static_function(cpptype, f, cppol.get_returntype())
         else:
             d[f] = make_method(f, cppol.get_returntype())
 

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/example01.cxx
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/example01.cxx	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/example01.cxx	Wed Jul 14 11:09:13 2010
@@ -15,20 +15,20 @@
 
 
 //===========================================================================
-example01::example01() : somedata(-99) {
+example01::example01() : m_somedata(-99) {
     count++;
 }
-example01::example01(int a) : somedata(a) {
+example01::example01(int a) : m_somedata(a) {
     count++;
     std::cout << "constructor called" << std::endl;
 }
-example01::example01(const example01& e) : somedata(e.somedata) {
+example01::example01(const example01& e) : m_somedata(e.m_somedata) {
     count++;
     std::cout << "copy constructor called" << std::endl;
 }
 example01& example01::operator=(const example01& e) {
     if (this != &e) {
-        somedata = e.somedata;
+        m_somedata = e.m_somedata;
     }
     return *this;
 }
@@ -58,6 +58,11 @@
     p->setData(d);
 }
 
+payload* example01::staticCyclePayload(payload* p, double d) {
+    staticSetPayload(p, d);
+    return p;
+}
+
 int example01::getCount() {
     std::cout << "getcount called" << std::endl;
     return count;
@@ -65,19 +70,19 @@
 
 // instance methods
 int example01::addDataToInt(int a) {
-    return somedata + a;
+    return m_somedata + a;
 }
 
 double example01::addDataToDouble(double a) {
-    return somedata + a;
+    return m_somedata + a;
 }
 
 int example01::addDataToAtoi(const char* str) {
-    return ::atoi(str) + somedata;
+    return ::atoi(str) + m_somedata;
 }   
 
 char* example01::addToStringValue(const char* str) {
-    int out = ::atoi(str) + somedata;
+    int out = ::atoi(str) + m_somedata;
     std::ostringstream ss;
     ss << out << std::ends;
     std::string result = ss.str();
@@ -87,7 +92,7 @@
 }
 
 void example01::setPayload(payload* p) {
-    p->setData(somedata);
+    p->setData(m_somedata);
 }
 
 payload* example01::cyclePayload(payload* p) {

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/example01.h
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/example01.h	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/example01.h	Wed Jul 14 11:09:13 2010
@@ -14,7 +14,7 @@
 class example01 {
 public:
     static int count;
-    int somedata;
+    int m_somedata;
 
     example01();
     example01(int a);
@@ -29,6 +29,7 @@
     static int staticAtoi(const char* str);
     static char* staticStrcpy(const char* strin);
     static void staticSetPayload(payload* p, double d);
+    static payload* staticCyclePayload(payload* p, double d);
     static int getCount();
 
 // instance methods

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py	Wed Jul 14 11:09:13 2010
@@ -153,19 +153,20 @@
         assert t.invoke("getCount") == 0
 
     def testReturningOfAnObjectByPointer(self):
-        """Test passing of an instance as an argument."""
+        """Test returing of an instance as an argument."""
 
         t = self.example01
         
         pl = self.payload.construct(3.14)
         assert round(pl.invoke("getData")-3.14, 8) == 0
 
+        pl2 = t.invoke("staticCyclePayload", pl, 38.)
+        assert pl2.invoke("getData") == 38.
+
         e = t.construct(50)
 
-        e.invoke("setPayload", pl);
-        assert round(pl.invoke("getData")-50., 8) == 0
-        pl = e.invoke("cyclePayload", pl);
-        assert round(pl.invoke("getData")-50., 8) == 0
+        pl2 = e.invoke("cyclePayload", pl);
+        assert round(pl2.invoke("getData")-50., 8) == 0
 
         e.destruct()
         pl.destruct() 

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/test_pythonify.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/test_pythonify.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/test_pythonify.py	Wed Jul 14 11:09:13 2010
@@ -139,9 +139,27 @@
 
         e.setPayload(pl)
         assert round(pl.getData()-14., 8) == 0
-        pl = e.cyclePayload(pl)
-        assert round(pl.getData()-14., 8) == 0
 
         pl.destruct()
         e.destruct()
         assert example01_class.getCount() == 0
+
+    def testReturningOfAnObjectByPointer(self):
+        import cppyy
+        example01_class = cppyy.gbl.example01
+        payload_class = cppyy.gbl.payload
+
+        pl = payload_class(3.14)
+        assert round(pl.getData()-3.14, 8) == 0
+        
+        pl2 = example01_class.staticCyclePayload(pl, 38.)
+        assert pl2.getData() == 38.
+
+        e = example01_class(14)
+
+        pl2 = e.cyclePayload(pl)
+        assert round(pl2.getData()-14., 8) == 0
+        
+        pl.destruct()
+        e.destruct()
+        assert example01_class.getCount() == 0



More information about the Pypy-commit mailing list