Author: Carl Friedrich Bolz <[email protected]>
Branch: reflex-support
Changeset: r45600:6413ad84ad2f
Date: 2011-07-14 18:24 +0200
http://bitbucket.org/pypy/pypy/changeset/6413ad84ad2f/

Log:    (arigo, cfbolz): refactor away the invokes and replace them with
        Overload.call with a slightly strange set of arguments:
         - first argument is this or None
         - second argument is the type of the result or None

diff --git a/pypy/module/cppyy/interp_cppyy.py 
b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -3,7 +3,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import ObjSpace, interp2app
 from pypy.interpreter.typedef import TypeDef, interp_attrproperty
-from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.baseobjspace import Wrappable, W_Root
 
 from pypy.rpython.lltypesystem import rffi, lltype
 
@@ -101,7 +101,7 @@
         self.methgetter = methgetter
         self._libffifunc_cache = {}
 
-    def call(self, cppthis, args_w):
+    def call(self, cppthis, w_type, args_w):
         assert lltype.typeOf(cppthis) == rffi.VOIDP
         if self.executor is None:
             raise OperationError(self.space.w_TypeError,
@@ -209,7 +209,7 @@
 class CPPFunction(CPPMethod):
     _immutable_ = True
 
-    def call(self, cppthis, args_w):
+    def call(self, cppthis, w_type, args_w):
         assert lltype.typeOf(cppthis) == rffi.VOIDP
         if self.executor is None:
             raise OperationError(self.space.w_TypeError,
@@ -227,16 +227,18 @@
 class CPPConstructor(CPPMethod):
     _immutable_=True
 
-    def call(self, cppthis, args_w):
-        assert not cppthis
+    def call(self, cppthis, w_type, args_w):
         newthis = capi.c_allocate(self.cpptype.handle)
         assert lltype.typeOf(newthis) == rffi.VOIDP
         try:
-            CPPMethod.call(self, newthis, args_w)
+            CPPMethod.call(self, newthis, None, args_w)
         except Exception, e:
             capi.c_deallocate(self.cpptype.handle, newthis)
             raise
-        return W_CPPInstance(self.space, self.cpptype, newthis, True)
+        w_instance = self.space.allocate_instance(W_CPPInstance, w_type)
+        instance = self.space.interp_w(W_CPPInstance, w_instance)
+        W_CPPInstance.__init__(instance, self.space, self.cpptype, newthis, 
True)
+        return w_instance
 
 
 class W_CPPOverload(Wrappable):
@@ -255,8 +257,10 @@
         return self.space.wrap(self.functions[0].executor.name)
 
     @jit.unroll_safe
-    def call(self, cppinstance, args_w):
-        if cppinstance:
+    def call(self, w_cppinstance, w_type, args_w):
+        cppinstance = self.space.interp_w(W_CPPInstance, w_cppinstance, 
can_be_None=True)
+        if cppinstance is not None:
+            cppinstance._nullcheck()
             cppthis = cppinstance.rawobject
         else:
             cppthis = NULL_VOIDP
@@ -268,7 +272,7 @@
         for i in range(len(self.functions)):
             cppyyfunc = self.functions[i]
             try:
-                cppresult = cppyyfunc.call(cppthis, args_w)
+                cppresult = cppyyfunc.call(cppthis, w_type, args_w)
                 if cppinstance and isinstance(cppresult, W_CPPInstance):
                     if cppresult.rawobject == cppinstance.rawobject:
                         return cppinstance  # recycle object to preserve 
identity
@@ -289,6 +293,7 @@
     'CPPOverload',
     is_static = interp2app(W_CPPOverload.is_static, unwrap_spec=['self']),
     get_returntype = interp2app(W_CPPOverload.get_returntype, 
unwrap_spec=['self']),
+    call = interp2app(W_CPPOverload.call, unwrap_spec=['self', W_Root, W_Root, 
'args_w']),
 )
 
 
@@ -396,16 +401,12 @@
                 self.space.w_AttributeError,
                 self.space.wrap(str("class %s has no attribute %s" % 
(self.name, name))))
 
-    def invoke(self, overload, args_w):
-        return overload.call(None, args_w)
-
 W_CPPScope.typedef = TypeDef(
     'CPPScope',
     get_method_names = interp2app(W_CPPScope.get_method_names, 
unwrap_spec=['self']),
     get_overload = interp2app(W_CPPScope.get_overload, unwrap_spec=['self', 
str]),
     get_data_member_names = interp2app(W_CPPScope.get_data_member_names, 
unwrap_spec=['self']),
     get_data_member = interp2app(W_CPPScope.get_data_member, 
unwrap_spec=['self', str]),
-    invoke = interp2app(W_CPPScope.invoke, unwrap_spec=['self', W_CPPOverload, 
'args_w']),
 )
 
 
@@ -450,7 +451,6 @@
     get_data_member_names = interp2app(W_CPPNamespace.get_data_member_names, 
unwrap_spec=['self']),
     get_data_member = interp2app(W_CPPNamespace.get_data_member, 
unwrap_spec=['self', str]),
     is_namespace = interp2app(W_CPPNamespace.is_namespace, 
unwrap_spec=['self']),
-    invoke = interp2app(W_CPPNamespace.invoke, unwrap_spec=['self', 
W_CPPOverload, 'args_w']),
 )
 
 
@@ -495,7 +495,8 @@
             bases.append(self.space.wrap(base_name))
         return self.space.newlist(bases)
 
-    def construct(self, args_w):
+    def construct(self, w_type, args_w):
+        # XXX make pure python
         try:
             overload = self.get_overload(self.name)
         except OperationError, e:
@@ -503,8 +504,7 @@
                 raise OperationError(self.space.w_TypeError,
                                      self.space.wrap("%s is abstract" % 
self.name))
             raise
-
-        return overload.call(None, args_w)
+        return overload.call(self.space.w_None, w_type, args_w)
 
 W_CPPType.typedef = TypeDef(
     'CPPType',
@@ -515,8 +515,7 @@
     get_data_member_names = interp2app(W_CPPType.get_data_member_names, 
unwrap_spec=['self']),
     get_data_member = interp2app(W_CPPType.get_data_member, 
unwrap_spec=['self', str]),
     is_namespace = interp2app(W_CPPType.is_namespace, unwrap_spec=['self']),
-    invoke = interp2app(W_CPPType.invoke, unwrap_spec=['self', W_CPPOverload, 
'args_w']),
-    construct = interp2app(W_CPPType.construct, unwrap_spec=['self', 
'args_w']),
+    construct = interp2app(W_CPPType.construct, unwrap_spec=['self', W_Root, 
'args_w']),
 )
 
 
@@ -549,15 +548,12 @@
         self.rawobject = rawobject
         self.python_owns = python_owns
 
+
     def _nullcheck(self):
         if not self.rawobject:
             raise OperationError(self.space.w_ReferenceError,
                                  self.space.wrap("trying to access a NULL 
pointer"))
 
-    def invoke(self, overload, args_w):
-        self._nullcheck()
-        return overload.call(self, args_w)
-
     def destruct(self):
         if self.rawobject:
             capi.c_destruct(self.cppclass.handle, self.rawobject)
@@ -567,9 +563,9 @@
         if self.python_owns:
             self.destruct()
 
+
 W_CPPInstance.typedef = TypeDef(
     'CPPInstance',
     cppclass = interp_attrproperty('cppclass', W_CPPInstance),
-    invoke = interp2app(W_CPPInstance.invoke, unwrap_spec=['self', 
W_CPPOverload, 'args_w']),
     destruct = interp2app(W_CPPInstance.destruct, unwrap_spec=['self']),
 )
diff --git a/pypy/module/cppyy/test/test_cppyy.py 
b/pypy/module/cppyy/test/test_cppyy.py
--- a/pypy/module/cppyy/test/test_cppyy.py
+++ b/pypy/module/cppyy/test/test_cppyy.py
@@ -43,30 +43,30 @@
         import sys, math
         t = self.example01
 
-        res = t.invoke(t.get_overload("staticAddOneToInt"), 1)
+        res = t.get_overload("staticAddOneToInt").call(None, None, 1)
         assert res == 2
-        res = t.invoke(t.get_overload("staticAddOneToInt"), 1L)
+        res = t.get_overload("staticAddOneToInt").call(None, None, 1L)
         assert res == 2
-        res = t.invoke(t.get_overload("staticAddOneToInt"), 1, 2)
+        res = t.get_overload("staticAddOneToInt").call(None, None, 1, 2)
         assert res == 4
-        res = t.invoke(t.get_overload("staticAddOneToInt"), -1)
+        res = t.get_overload("staticAddOneToInt").call(None, None, -1)
         assert res == 0
         maxint32 = int(2 ** 31 - 1)
-        res = t.invoke(t.get_overload("staticAddOneToInt"), maxint32-1)
+        res = t.get_overload("staticAddOneToInt").call(None, None, maxint32-1)
         assert res == maxint32
-        res = t.invoke(t.get_overload("staticAddOneToInt"), maxint32)
+        res = t.get_overload("staticAddOneToInt").call(None, None, maxint32)
         assert res == -maxint32-1
 
-        raises(TypeError, 't.invoke(t.get_overload("staticAddOneToInt"), 1, 
[])')
-        raises(TypeError, 't.invoke(t.get_overload("staticAddOneToInt"), 1.)')
-        raises(OverflowError, 't.invoke(t.get_overload("staticAddOneToInt"), 
maxint32+1)')
+        raises(TypeError, 't.get_overload("staticAddOneToInt").call(None, 
None, 1, [])')
+        raises(TypeError, 't.get_overload("staticAddOneToInt").call(None, 
None, 1.)')
+        raises(OverflowError, 't.get_overload("staticAddOneToInt").call(None, 
None, maxint32+1)')
 
     def test_example01static_double(self):
         """Test passing of a double and returning of a double on a static 
function."""
 
         t = self.example01
 
-        res = t.invoke(t.get_overload("staticAddToDouble"), 0.09)
+        res = t.get_overload("staticAddToDouble").call(None, None, 0.09)
         assert res == 0.09 + 0.01
 
     def test_example01static_constcharp(self):
@@ -75,141 +75,147 @@
 
         t = self.example01
 
-        res = t.invoke(t.get_overload("staticAtoi"), "1")
+        res = t.get_overload("staticAtoi").call(None, None, "1")
         assert res == 1
-        res = t.invoke(t.get_overload("staticStrcpy"), "aap")   # TODO: this 
leaks
+        res = t.get_overload("staticStrcpy").call(None, None, "aap")   # TODO: 
this leaks
         assert res == "aap"
-        res = t.invoke(t.get_overload("staticStrcpy"), u"aap")  # TODO: this 
leaks
+        res = t.get_overload("staticStrcpy").call(None, None, u"aap")  # TODO: 
this leaks
         assert res == "aap"
 
-        raises(TypeError, 't.invoke(t.get_overload("staticStrcpy"), 1.)') # 
TODO: this leaks
+        raises(TypeError, 't.get_overload("staticStrcpy").call(None, None, 
1.)') # TODO: this leaks
 
     def test_example01method_int(self):
         """Test passing of a int, returning of a int, and memory cleanup, on
             a method."""
+        import cppyy
 
         t = self.example01
 
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
-        e1 = t.construct(7)
-        assert t.invoke(t.get_overload("getCount")) == 1
-        res = e1.invoke(t.get_overload("addDataToInt"), 4)
+        e1 = t.construct(cppyy.CPPInstance, 7)
+        assert t.get_overload("getCount").call(None, None) == 1
+        res = t.get_overload("addDataToInt").call(e1, None, 4)
         assert res == 11
-        res = e1.invoke(t.get_overload("addDataToInt"), -4)
+        res = t.get_overload("addDataToInt").call(e1, None, -4)
         assert res == 3
         e1.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
-        raises(ReferenceError, 'e1.invoke(t.get_overload("addDataToInt"), 4)')
+        assert t.get_overload("getCount").call(None, None) == 0
+        raises(ReferenceError, 't.get_overload("addDataToInt").call(e1, None, 
4)')
 
-        e1 = t.construct(7)
-        e2 = t.construct(8)
-        assert t.invoke(t.get_overload("getCount")) == 2
+        e1 = t.construct(cppyy.CPPInstance, 7)
+        e2 = t.construct(cppyy.CPPInstance, 8)
+        assert t.get_overload("getCount").call(None, None) == 2
         e1.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 1
+        assert t.get_overload("getCount").call(None, None) == 1
         e2.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
         e2.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
     def test_example01memory(self):
         """Test memory destruction and integrity."""
 
         import gc
+        import cppyy
 
         t = self.example01
 
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
-        e1 = t.construct(7)
-        assert t.invoke(t.get_overload("getCount")) == 1
-        res = e1.invoke(t.get_overload("addDataToInt"), 4)
+        e1 = t.construct(cppyy.CPPInstance, 7)
+        assert t.get_overload("getCount").call(None, None) == 1
+        res = t.get_overload("addDataToInt").call(e1, None, 4)
         assert res == 11
-        res = e1.invoke(t.get_overload("addDataToInt"), -4)
+        res = t.get_overload("addDataToInt").call(e1, None, -4)
         assert res == 3
         e1 = None
         gc.collect()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
-        e1 = t.construct(7)
-        e2 = t.construct(8)
-        assert t.invoke(t.get_overload("getCount")) == 2
+        e1 = t.construct(cppyy.CPPInstance, 7)
+        e2 = t.construct(cppyy.CPPInstance, 8)
+        assert t.get_overload("getCount").call(None, None) == 2
         e1 = None
         gc.collect()
-        assert t.invoke(t.get_overload("getCount")) == 1
+        assert t.get_overload("getCount").call(None, None) == 1
        e2.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
         e2 = None
         gc.collect()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
     def test_example01method_double(self):
         """Test passing of a double and returning of double on a method"""
+        import cppyy
 
         t = self.example01
 
-        e = t.construct(13)
-        res = e.invoke(t.get_overload("addDataToDouble"), 16)
+        e = t.construct(cppyy.CPPInstance, 13)
+        res = t.get_overload("addDataToDouble").call(e, None, 16)
         assert round(res-29, 8) == 0.
         e.destruct()
 
-        e = t.construct(-13)
-        res = e.invoke(t.get_overload("addDataToDouble"), 16)
+        e = t.construct(cppyy.CPPInstance, -13)
+        res = t.get_overload("addDataToDouble").call(e, None, 16)
         assert round(res-3, 8) == 0.
         e.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
     def test_example01method_constcharp(self):
         """Test passing of a C string and returning of a C string on a
             method."""
+        import cppyy
 
         t = self.example01
 
-        e = t.construct(42)
-        res = e.invoke(t.get_overload("addDataToAtoi"), "13")
+        e = t.construct(cppyy.CPPInstance, 42)
+        res = t.get_overload("addDataToAtoi").call(e, None, "13")
         assert res == 55
-        res = e.invoke(t.get_overload("addToStringValue"), "12")     # TODO: 
this leaks
+        res = t.get_overload("addToStringValue").call(e, None, "12")     # 
TODO: this leaks
         assert res == "54"
-        res = e.invoke(t.get_overload("addToStringValue"), "-12")    # TODO: 
this leaks
+        res = t.get_overload("addToStringValue").call(e, None, "-12")    # 
TODO: this leaks
         assert res == "30"
         e.destruct()
-        assert t.invoke(t.get_overload("getCount")) == 0
+        assert t.get_overload("getCount").call(None, None) == 0
 
     def testPassingOfAnObjectByPointer(self):
         """Test passing of an instance as an argument."""
+        import cppyy
 
         t1 = self.example01
         t2 = self.payload
 
-        pl = t2.construct(3.14)
-        assert round(pl.invoke(t2.get_overload("getData"))-3.14, 8) == 0
-        t1.invoke(t1.get_overload("staticSetPayload"), pl, 41.)  # now pl is a 
CPPInstance
-        assert pl.invoke(t2.get_overload("getData")) == 41.
+        pl = t2.construct(cppyy.CPPInstance, 3.14)
+        assert round(t2.get_overload("getData").call(pl, None)-3.14, 8) == 0
+        t1.get_overload("staticSetPayload").call(None, None, pl, 41.)  # now 
pl is a CPPInstance
+        assert t2.get_overload("getData").call(pl, None) == 41.
 
-        e = t1.construct(50)
-        e.invoke(t1.get_overload("setPayload"), pl);
-        assert round(pl.invoke(t2.get_overload("getData"))-50., 8) == 0
+        e = t1.construct(cppyy.CPPInstance, 50)
+        t1.get_overload("setPayload").call(e, None, pl);
+        assert round(t2.get_overload("getData").call(pl, None)-50., 8) == 0
 
         e.destruct()
         pl.destruct() 
-        assert t1.invoke(t1.get_overload("getCount")) == 0
+        assert t1.get_overload("getCount").call(None, None) == 0
 
     def testReturningOfAnObjectByPointer(self):
         """Test returning of an instance as an argument."""
+        import cppyy
 
         t1 = self.example01
         t2 = self.payload
 
-        pl1 = t2.construct(3.14)
-        assert round(pl1.invoke(t2.get_overload("getData"))-3.14, 8) == 0
-        pl2 = t1.invoke(t1.get_overload("staticCyclePayload"), pl1, 38.)
-        assert pl2.invoke(t2.get_overload("getData")) == 38.
+        pl1 = t2.construct(cppyy.CPPInstance, 3.14)
+        assert round(t2.get_overload("getData").call(pl1, None)-3.14, 8) == 0
+        pl2 = t1.get_overload("staticCyclePayload").call(None, 
cppyy.CPPInstance, pl1, 38.)
+        assert t2.get_overload("getData").call(pl2, None) == 38.
 
-        e = t1.construct(50)
-        pl2 = e.invoke(t1.get_overload("cyclePayload"), pl1);
-        assert round(pl2.invoke(t2.get_overload("getData"))-50., 8) == 0
+        e = t1.construct(cppyy.CPPInstance, 50)
+        pl2 = t1.get_overload("cyclePayload").call(e, cppyy.CPPInstance, pl1);
+        assert round(t2.get_overload("getData").call(pl2, None)-50., 8) == 0
 
         e.destruct()
         pl1.destruct() 
-        assert t1.invoke(t1.get_overload("getCount")) == 0
+        assert t1.get_overload("getCount").call(None, None) == 0
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to