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

wlav at codespeak.net wlav at codespeak.net
Tue Jul 13 17:26:12 CEST 2010


Author: wlav
Date: Tue Jul 13 17:26:09 2010
New Revision: 76188

Modified:
   pypy/branch/reflex-support/pypy/module/cppyy/converter.py
   pypy/branch/reflex-support/pypy/module/cppyy/executor.py
   pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py
   pypy/branch/reflex-support/pypy/module/cppyy/pythonify.py
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_pythonify.py
Log:
Allow methods to return instances.


Modified: pypy/branch/reflex-support/pypy/module/cppyy/converter.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/converter.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/converter.py	Tue Jul 13 17:26:09 2010
@@ -44,7 +44,7 @@
         if w_cppinstance is not None:
             w_obj = w_cppinstance
         obj = space.interpclass_w(w_obj)
-        if isinstance(obj, interp_cppyy.W_CCPInstance):
+        if isinstance(obj, interp_cppyy.W_CPPInstance):
             if capi.c_is_subtype(obj.cppclass.handle, self.cpptype.handle):
                 return obj.rawobject
         raise OperationError(space.w_TypeError,

Modified: pypy/branch/reflex-support/pypy/module/cppyy/executor.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/executor.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/executor.py	Tue Jul 13 17:26:09 2010
@@ -32,6 +32,7 @@
         result = capi.charp2str_free(ccpresult)
         return space.wrap(result)
 
+
 class InstancePtrExecutor(FunctionExecutor):
     _immutable_ = True
     def __init__(self, space, cpptype):
@@ -41,7 +42,7 @@
         from pypy.module.cppyy import interp_cppyy
         long_result = capi.c_call_l(func.cpptype.handle, func.method_index, cppthis, num_args, args)
         ptr_result = rffi.cast(rffi.VOIDP, long_result)
-        return interp_cppyy.W_CCPInstance(self.cpptype, ptr_result)
+        return interp_cppyy.W_CPPInstance(self.cpptype, ptr_result)
 
 
 def get_executor(space, name):

Modified: pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py	Tue Jul 13 17:26:09 2010
@@ -166,7 +166,7 @@
         except Exception, e:
             capi.c_deallocate(self.cpptype.handle, newthis)
             raise
-        return W_CCPInstance(self.cpptype, newthis)
+        return W_CPPInstance(self.cpptype, newthis)
 
 
 class W_CPPOverload(Wrappable):
@@ -180,6 +180,12 @@
     def is_static(self):
         return self.space.wrap(isinstance(self.functions[0], CPPFunction))
 
+    def get_returntype(self):
+        try:
+            return self.space.wrap(self.functions[0].executor.cpptype.name)
+        except AttributeError:
+            return None
+
     @jit.unroll_safe
     def call(self, cppthis, args_w):
         space = self.space
@@ -201,6 +207,7 @@
 W_CPPOverload.typedef = TypeDef(
     'CPPOverload',
     is_static = interp2app(W_CPPOverload.is_static, unwrap_spec=['self']),
+    get_returntype = interp2app(W_CPPOverload.get_returntype, unwrap_spec=['self']),
 )
 
 
@@ -268,8 +275,7 @@
 )
 
 
-
-class W_CCPInstance(Wrappable):
+class W_CPPInstance(Wrappable):
     _immutable_ = True
     def __init__(self, cppclass, rawobject):
         self.space = cppclass.space
@@ -290,8 +296,8 @@
         capi.c_destruct(self.cppclass.handle, self.rawobject)
         self.rawobject = NULL_VOIDP
 
-W_CCPInstance.typedef = TypeDef(
+W_CPPInstance.typedef = TypeDef(
     'CPPInstance',
-    invoke = interp2app(W_CCPInstance.invoke, unwrap_spec=['self', str, 'args_w']),
-    destruct = interp2app(W_CCPInstance.destruct, unwrap_spec=['self']),
+    invoke = interp2app(W_CPPInstance.invoke, unwrap_spec=['self', str, 'args_w']),
+    destruct = interp2app(W_CPPInstance.destruct, unwrap_spec=['self']),
 )

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	Tue Jul 13 17:26:09 2010
@@ -5,25 +5,37 @@
 class CppyyClass(type):
      pass
 
+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)
     method.__name__ = name
     return staticmethod(method)
 
-def make_method(name):
-    def method(self, *args):
-        return self._cppinstance.invoke(name, *args)
-    method.__name__ = name
-    return method
+def make_method(name, rettype):
+    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
+        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
 
-class CppyyObject(object):
-    def __init__(self, *args):
-        self._cppinstance = self._cppyyclass.construct(*args)
-
-    def destruct(self):
-        self._cppinstance.destruct()
-    
 
 _existing_classes = {}
 def get_cppclass(name):
@@ -42,7 +54,7 @@
         if cppol.is_static():
             d[f] = make_static_function(cpptype, f)
         else:
-            d[f] = make_method(f)
+            d[f] = make_method(f, cppol.get_returntype())
 
     pycpptype = CppyyClass(name, (CppyyObject,), d)
 

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	Tue Jul 13 17:26:09 2010
@@ -164,7 +164,7 @@
 
         e.invoke("setPayload", pl);
         assert round(pl.invoke("getData")-50., 8) == 0
-        e.invoke("cyclePayload", pl);
+        pl = e.invoke("cyclePayload", pl);
         assert round(pl.invoke("getData")-50., 8) == 0
 
         e.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	Tue Jul 13 17:26:09 2010
@@ -137,9 +137,9 @@
         e.staticSetPayload(pl, 45.)
         assert pl.getData() == 45.
 
-        e.setPayload(pl);
+        e.setPayload(pl)
         assert round(pl.getData()-14., 8) == 0
-        e.cyclePayload(pl);
+        pl = e.cyclePayload(pl)
         assert round(pl.getData()-14., 8) == 0
 
         pl.destruct()



More information about the Pypy-commit mailing list