[pypy-svn] r76333 - in pypy/branch/reflex-support/pypy/module/cppyy: . include src test

wlav at codespeak.net wlav at codespeak.net
Sat Jul 24 01:36:37 CEST 2010


Author: wlav
Date: Sat Jul 24 01:36:35 2010
New Revision: 76333

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/include/reflexcwrapper.h
   pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py
Log:
Allow passing of C-floats.


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	Sat Jul 24 01:36:35 2010
@@ -41,6 +41,7 @@
     "cppyy_deallocate",
     [C_TYPEHANDLE, C_OBJECT], lltype.Void,
     compilation_info=eci)
+
 c_call_v = rffi.llexternal(
     "cppyy_call_v",
     [C_TYPEHANDLE, rffi.INT, C_OBJECT, rffi.INT, rffi.VOIDPP], lltype.Void,
@@ -57,10 +58,15 @@
     "cppyy_call_l",
     [C_TYPEHANDLE, rffi.INT, C_OBJECT, rffi.INT, rffi.VOIDPP], rffi.LONG,
     compilation_info=eci)
+c_call_f = rffi.llexternal(
+    "cppyy_call_f",
+    [C_TYPEHANDLE, rffi.INT, C_OBJECT, rffi.INT, rffi.VOIDPP], rffi.DOUBLE,
+    compilation_info=eci)
 c_call_d = rffi.llexternal(
     "cppyy_call_d",
     [C_TYPEHANDLE, rffi.INT, C_OBJECT, rffi.INT, rffi.VOIDPP], rffi.DOUBLE,
     compilation_info=eci)
+
 c_destruct = rffi.llexternal(
     "cppyy_destruct",
     [C_TYPEHANDLE, C_OBJECT], lltype.Void,

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	Sat Jul 24 01:36:35 2010
@@ -1,5 +1,6 @@
 from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.rlib.rarithmetic import r_singlefloat
 
 from pypy.module.cppyy import helper, capi
 
@@ -47,6 +48,13 @@
         x[0] = arg
         return rffi.cast(rffi.VOIDP, x)
 
+class FloatConverter(TypeConverter):
+    def convert_argument(self, space, w_obj):
+        arg = space.float_w(w_obj)
+        x = lltype.malloc(rffi.FLOATP.TO, 1, flavor='raw')
+        x[0] = r_singlefloat(arg)
+        return rffi.cast(rffi.VOIDP, x)        
+
 class DoubleConverter(TypeConverter):
     def convert_argument(self, space, w_obj):
         arg = space.float_w(w_obj)
@@ -112,5 +120,6 @@
 _converters["char"]                = CharConverter()
 _converters["unsigned char"]       = CharConverter()
 _converters["int"]                 = IntConverter()
+_converters["float"]               = FloatConverter()
 _converters["double"]              = DoubleConverter()
 _converters["const char*"]         = CStringConverter()

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	Sat Jul 24 01:36:35 2010
@@ -30,6 +30,11 @@
         result = capi.c_call_l(func.cpptype.handle, func.method_index, cppthis, num_args, args)
         return space.wrap(result)
 
+class FloatExecutor(FunctionExecutor):
+    def execute(self, space, func, cppthis, num_args, args):
+        result = capi.c_call_f(func.cpptype.handle, func.method_index, cppthis, num_args, args)
+        return space.wrap(result)
+
 class DoubleExecutor(FunctionExecutor):
     def execute(self, space, func, cppthis, num_args, args):
         result = capi.c_call_d(func.cpptype.handle, func.method_index, cppthis, num_args, args)
@@ -78,5 +83,6 @@
 _executors["unsigned char"]       = CharExecutor()
 _executors["int"]                 = LongExecutor()
 _executors["long int"]            = LongExecutor()
+_executors["float"]               = FloatExecutor()
 _executors["double"]              = DoubleExecutor()
 _executors["char*"]               = CStringExecutor()

Modified: pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h	Sat Jul 24 01:36:35 2010
@@ -18,7 +18,9 @@
     int    cppyy_call_b(cppyy_typehandle_t handle, int method_index, cppyy_object_t self, int numargs, void* args[]);
     char   cppyy_call_c(cppyy_typehandle_t handle, int method_index, cppyy_object_t self, int numargs, void* args[]);
     long   cppyy_call_l(cppyy_typehandle_t handle, int method_index, cppyy_object_t self, int numargs, void* args[]);
+    double cppyy_call_f(cppyy_typehandle_t handle, int method_index, cppyy_object_t self, int numargs, void* args[]);
     double cppyy_call_d(cppyy_typehandle_t handle, int method_index, cppyy_object_t self, int numargs, void* args[]);
+
     void cppyy_destruct(cppyy_typehandle_t handle, cppyy_object_t self);
     cppyy_methptrgetter_t cppyy_get_methptr_getter(cppyy_typehandle_t handle, int method_index);
 

Modified: pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx	Sat Jul 24 01:36:35 2010
@@ -62,6 +62,11 @@
     return cppyy_call_T<long>(handle, method_index, self, numargs, args);
 }
 
+double cppyy_call_f(cppyy_typehandle_t handle, int method_index,
+                    cppyy_object_t self, int numargs, void* args[]) {
+    return cppyy_call_T<float>(handle, method_index, self, numargs, args);
+}
+
 double cppyy_call_d(cppyy_typehandle_t handle, int method_index,
                     cppyy_object_t self, int numargs, void* args[]) {
     return cppyy_call_T<double>(handle, method_index, self, numargs, args);

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/test_datatypes.py	Sat Jul 24 01:36:35 2010
@@ -81,8 +81,12 @@
         for i in range(len(names)):
             exec 'c.set_%s = %d' % (names[i],2*i)
             assert eval('c.m_%s' % names[i]) == i
+        """
 
         # float types
+        c.set_float( 0.123 );  assert round(c.get_float()  - 0.123, 5) == 0
+        c.set_double( 0.456 ); assert round(c.get_double() - 0.456, 8) == 0
+        """
         c.m_float = 0.123;     assert round(c.get_float()  - 0.123, 5) == 0
         c.set_float( 0.234 );  assert round(c.m_float      - 0.234, 5) == 0
         c.m_double = 0.456;    assert round(c.get_double() - 0.456, 8) == 0



More information about the Pypy-commit mailing list