[pypy-svn] r76163 - in pypy/branch/reflex-support/pypy/module/cppyy: . test
wlav at codespeak.net
wlav at codespeak.net
Mon Jul 12 22:26:28 CEST 2010
Author: wlav
Date: Mon Jul 12 22:26:26 2010
New Revision: 76163
Modified:
pypy/branch/reflex-support/pypy/module/cppyy/capi.py
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/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:
Allow executor calls returning objects to succeed (the resulting return object is not yet usuable, though).
Modified: pypy/branch/reflex-support/pypy/module/cppyy/capi.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/capi.py (original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/capi.py Mon Jul 12 22:26:26 2010
@@ -39,7 +39,7 @@
compilation_info=eci)
c_deallocate = rffi.llexternal(
"cppyy_deallocate",
- [C_TYPEHANDLE, C_OBJECT], rffi.VOID,
+ [C_TYPEHANDLE, C_OBJECT], lltype.Void,
compilation_info=eci)
c_call_v = rffi.llexternal(
"cppyy_call_v",
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 Mon Jul 12 22:26:26 2010
@@ -68,13 +68,12 @@
return _converters[name]
except KeyError:
pass
+
compound = helper.compound(name)
cpptype = interp_cppyy.type_byname(space, helper.clean_type(name))
if compound == "*":
return InstancePtrConverter(space, cpptype)
- print name
-
raise OperationError(space.w_TypeError, space.wrap("no clue what %s is" % name))
_converters["int"] = IntConverter()
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 Mon Jul 12 22:26:26 2010
@@ -1,9 +1,9 @@
-import pypy.module.cppyy.capi as capi
-
from pypy.rpython.lltypesystem import rffi, lltype
-_executors = {}
+from pypy.module.cppyy import helper, capi
+
+_executors = {}
class FunctionExecutor(object):
def execute(self, space, func, cppthis, num_args, args):
@@ -32,12 +32,30 @@
result = capi.charp2str_free(ccpresult)
return space.wrap(result)
+class InstancePtrExecutor(FunctionExecutor):
+ _immutable_ = True
+ def __init__(self, space, cpptype):
+ self.cpptype = cpptype
+
+ def execute(self, space, func, cppthis, num_args, args):
+ from pypy.module.cppyy import interp_cppyy
+ result = capi.c_call_l(func.cpptype.handle, func.method_index, cppthis, num_args, args)
+ return interp_cppyy.W_CCPInstance(self.cpptype, result)
+
+
def get_executor(space, name):
+ from pypy.module.cppyy import interp_cppyy
+
try:
return _executors[name]
except KeyError:
pass
+ compound = helper.compound(name)
+ cpptype = interp_cppyy.type_byname(space, helper.clean_type(name))
+ if compound == "*":
+ return InstancePtrExecutor(space, cpptype)
+
return None # currently used until proper lazy instantiation available in interp_cppyy
# raise TypeError("no clue what %s is" % 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 Mon Jul 12 22:26:26 2010
@@ -35,6 +35,7 @@
if handle:
cpptype = W_CPPType(space, name, handle)
state.cpptype_cache[name] = cpptype
+ cpptype._find_func_members()
return cpptype
raise OperationError(space.w_TypeError, space.wrap("no such C++ class %s" % name))
@@ -211,7 +212,10 @@
self.name = name
self.handle = handle
self.function_members = {}
- self._find_func_members()
+ # Do not call "self._find_func_members()" here, so that a distinction can be
+ # made between testing for existence (i.e. existence in the cache of classes)
+ # and actual use. Point being that a class can use itself, e.g. as a return
+ # type or an argument to one of its methods.
def _find_func_members(self):
num_func_members = capi.c_num_methods(self.handle)
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 Mon Jul 12 22:26:26 2010
@@ -54,7 +54,7 @@
::strcpy(strout, strin);
return strout;
}
-void example01::setPayload( payload* p, double d ) {
+void example01::staticSetPayload(payload* p, double d) {
p->setData(d);
}
@@ -86,4 +86,13 @@
return cresult;
}
+void example01::setPayload(payload* p) {
+ p->setData(somedata);
+}
+
+payload* example01::cyclePayload(payload* p) {
+ setPayload(p);
+ return p;
+}
+
int example01::count = 0;
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 Mon Jul 12 22:26:26 2010
@@ -28,7 +28,7 @@
static double staticAddToDouble(double a);
static int staticAtoi(const char* str);
static char* staticStrcpy(const char* strin);
- static void setPayload( payload* p, double d );
+ static void staticSetPayload(payload* p, double d);
static int getCount();
// instance methods
@@ -36,4 +36,7 @@
double addDataToDouble(double a);
int addDataToAtoi(const char* str);
char* addToStringValue(const char* str);
+
+ void setPayload(payload* p);
+ payload* cyclePayload(payload* p);
};
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 Mon Jul 12 22:26:26 2010
@@ -141,8 +141,33 @@
pl = self.payload.construct(3.14)
assert round(pl.invoke("getData")-3.14, 8) == 0
- t.invoke("setPayload", pl, 41.) # now pl is a CPPInstance
+ t.invoke("staticSetPayload", pl, 41.) # now pl is a CPPInstance
assert pl.invoke("getData") == 41.
+
+ e = t.construct(50)
+ e.invoke("setPayload", pl);
+ assert round(pl.invoke("getData")-50., 8) == 0
+
+ e.destruct()
+ pl.destruct()
+ assert t.invoke("getCount") == 0
+
+ def testReturningOfAnObjectByPointer(self):
+ """Test passing of an instance as an argument."""
+
+ t = self.example01
+ pl = self.payload.construct(3.14)
+ assert round(pl.invoke("getData")-3.14, 8) == 0
+
+ e = t.construct(50)
+
+ e.invoke("setPayload", pl);
+ assert round(pl.invoke("getData")-50., 8) == 0
+ e.invoke("cyclePayload", pl);
+ assert round(pl.invoke("getData")-50., 8) == 0
+
+ e.destruct()
pl.destruct()
assert t.invoke("getCount") == 0
+
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 Mon Jul 12 22:26:26 2010
@@ -130,13 +130,18 @@
pl = payload_class(3.14)
assert round(pl.getData()-3.14, 8) == 0
- example01_class.setPayload(pl._cppinstance, 41.)
+ example01_class.staticSetPayload(pl._cppinstance, 41.)
assert pl.getData() == 41.
- example01_class.setPayload(pl, 43.)
+ example01_class.staticSetPayload(pl, 43.)
assert pl.getData() == 43.
- e.setPayload(pl, 45.)
+ e.staticSetPayload(pl, 45.)
assert pl.getData() == 45.
+ e.setPayload(pl);
+ assert round(pl.getData()-14., 8) == 0
+ e.cyclePayload(pl);
+ assert round(pl.getData()-14., 8) == 0
+
pl.destruct()
e.destruct()
assert example01_class.getCount() == 0
More information about the Pypy-commit
mailing list