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

wlav at codespeak.net wlav at codespeak.net
Tue Jul 6 18:45:12 CEST 2010


Author: wlav
Date: Tue Jul  6 18:45:10 2010
New Revision: 75932

Modified:
   pypy/branch/reflex-support/pypy/module/cppyy/converter.py
   pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py
   pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx
   pypy/branch/reflex-support/pypy/module/cppyy/test/example01.cxx
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
Log:
(wlav, cfbolz) Add const char* argument passing and refactor converter lookup.


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  6 18:45:10 2010
@@ -1,9 +1,14 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 
+_converters = {}
+
 class TypeConverter(object):
     def convert_argument(self, space, w_obj):
         raise NotImplementedError("abstract base class")
 
+    def free_argument(self, arg):
+        lltype.free(arg, flavor='raw')
+
 
 class IntConverter(TypeConverter):
     def convert_argument(self, space, w_obj):
@@ -19,9 +24,20 @@
         x[0] = arg
         return rffi.cast(rffi.VOIDP, x)        
 
+class CStringConverter(TypeConverter):
+    def convert_argument(self, space, w_obj):
+        arg = space.str_w(w_obj)
+        x = rffi.str2charp(arg)
+        return rffi.cast(rffi.VOIDP, x)
+
 def get_converter(name):
-    if name == "int":
-        return IntConverter()
-    if name == "double":
-        return DoubleConverter()
+    try:
+        return _converters[name]
+    except KeyError:
+        pass
+    
     raise TypeError("no clue what %s is" % name)
+
+_converters["int"]                 = IntConverter()
+_converters["double"]              = DoubleConverter()
+_converters["const char*"]         = CStringConverter()

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  6 18:45:10 2010
@@ -145,14 +145,16 @@
         except:
             # fun :-(
             for j in range(i):
-                lltype.free(args[j], flavor='raw')
+                conv = self.arg_converters[j]
+                conv.free_argument(args[j])
             lltype.free(args, flavor='raw')
             raise
         return args
 
     def free_arguments(self, args):
         for i in range(len(self.arg_types)):
-            lltype.free(args[i], flavor='raw')
+            conv = self.arg_converters[i]
+            conv.free_argument(args[i])
         lltype.free(args, flavor='raw')
 
     def __repr__(self):

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	Tue Jul  6 18:45:10 2010
@@ -52,10 +52,10 @@
 
 int num_methods(const char* class_name) {
     Reflex::Type t = Reflex::Type::ByName(class_name);
-    for (int i = 0; i < t.FunctionMemberSize(); i++) {
+    for (int i = 0; i < (int)t.FunctionMemberSize(); i++) {
         Reflex::Member m = t.FunctionMemberAt(i);
         std::cout << i << " " << m.Name() << std::endl;
-        for (int j = 0; j < m.FunctionParameterSize(); j++) {
+        for (int j = 0; j < (int)m.FunctionParameterSize(); j++) {
             Reflex::Type at = m.TypeOf().FunctionParameterAt(j);
             std::cout << "    " << j << " " << at.Name() << std::endl;
         }
@@ -76,7 +76,7 @@
     Reflex::Type t = Reflex::Type::ByName(class_name);
     Reflex::Member m = t.FunctionMemberAt(method_index);
     Reflex::Type rt = m.TypeOf().ReturnType();
-    std::string name = rt.Name(Reflex::FINAL);
+    std::string name = rt.Name(Reflex::FINAL|Reflex::SCOPED|Reflex::QUALIFIED);
     char* name_char = (char*)malloc(name.size() + 1);
     strcpy(name_char, name.c_str());
     return name_char;
@@ -93,7 +93,7 @@
     Reflex::Type t = Reflex::Type::ByName(class_name);
     Reflex::Member m = t.FunctionMemberAt(method_index);
     Reflex::Type at = m.TypeOf().FunctionParameterAt(arg_index);
-    std::string name = at.Name(Reflex::FINAL);
+    std::string name = at.Name(Reflex::FINAL|Reflex::SCOPED|Reflex::QUALIFIED);
     char* name_char = (char*)malloc(name.size() + 1);
     strcpy(name_char, name.c_str());
     return name_char;

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	Tue Jul  6 18:45:10 2010
@@ -1,4 +1,5 @@
 #include <iostream>
+#include <stdlib.h>
 
 class example01 {
 public:
@@ -35,6 +36,9 @@
     static double adddouble(double a) {
         return a + 0.01;
     }
+    static int atoi(const char* str) {
+        return ::atoi(str);
+    }
     static int getcount() {
         std::cout << "getcount called" << std::endl;
         return count;

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  6 18:45:10 2010
@@ -44,6 +44,11 @@
         res = t.invoke("adddouble", 0.09)
         assert res == 0.09 + 0.01
 
+    def test_example01static_constcharp(self):
+        t = self.example01.type_byname("example01")
+        res = t.invoke("atoi", "1")
+        assert res == 1
+
     def test_example01method(self):
         t = self.example01.type_byname("example01")
         count = t.invoke("getcount")
@@ -57,3 +62,4 @@
         count = t.invoke("getcount")
         assert count == 0
 
+



More information about the Pypy-commit mailing list