[pypy-commit] pypy reflex-support: o) integer class mixin

wlav noreply at buildbot.pypy.org
Tue Feb 7 04:36:28 CET 2012


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r52155:d72d77b4a76e
Date: 2012-02-06 13:35 -0800
http://bitbucket.org/pypy/pypy/changeset/d72d77b4a76e/

Log:	o) integer class mixin o) long integer default parameters for ffi
	call

diff --git a/pypy/module/cppyy/capi/__init__.py b/pypy/module/cppyy/capi/__init__.py
--- a/pypy/module/cppyy/capi/__init__.py
+++ b/pypy/module/cppyy/capi/__init__.py
@@ -208,9 +208,14 @@
     [C_TYPEHANDLE, rffi.INT], rffi.INT,
     compilation_info=backend.eci)
 
-c_atoi = rffi.llexternal(
-    "cppyy_atoi",
-    [rffi.CCHARP], rffi.INT,
+c_strtoll = rffi.llexternal(
+    "cppyy_strtoll",
+    [rffi.CCHARP], rffi.LONGLONG,
+    compilation_info=backend.eci)
+
+c_strtoull = rffi.llexternal(
+    "cppyy_strtoull",
+    [rffi.CCHARP], rffi.ULONGLONG,
     compilation_info=backend.eci)
 
 c_free = rffi.llexternal(
diff --git a/pypy/module/cppyy/converter.py b/pypy/module/cppyy/converter.py
--- a/pypy/module/cppyy/converter.py
+++ b/pypy/module/cppyy/converter.py
@@ -137,6 +137,34 @@
                                  space.wrap("raw buffer interface not supported"))
 
 
+class IntTypeConverterMixin(object):
+    _mixin_ = True
+    _immutable_ = True
+    
+    def __init__(self, space, default):
+        self.default = rffi.cast(self.rffitype, capi.c_strtoll(default))
+
+    def convert_argument(self, space, w_obj, address):
+        x = rffi.cast(self.rffiptype, address)
+        x[0] = self._unwrap_object(space, w_obj)
+
+    def convert_argument_libffi(self, space, w_obj, argchain):
+        argchain.arg(self._unwrap_object(space, w_obj))
+
+    def default_argument_libffi(self, space, argchain):
+        argchain.arg(self.default)
+
+    def from_memory(self, space, w_obj, w_type, offset):
+        address = self._get_raw_address(space, w_obj, offset)
+        intptr = rffi.cast(self.rffiptype, address)
+        return space.wrap(intptr[0])
+
+    def to_memory(self, space, w_obj, w_value, offset):
+        address = self._get_raw_address(space, w_obj, offset)
+        intptr = rffi.cast(self.rffiptype, address)
+        intptr[0] = self._unwrap_object(space, w_value)
+
+
 class VoidConverter(TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.void
@@ -217,36 +245,15 @@
         address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset))
         address[0] = self._unwrap_object(space, w_value)
 
-class IntConverter(TypeConverter):
+class IntConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.sint
-
-    def __init__(self, space, default):
-        self.default = capi.c_atoi(default)
+    rffitype = rffi.INT
+    rffiptype = rffi.INTP
 
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(rffi.INT, space.c_int_w(w_obj))
 
-    def convert_argument(self, space, w_obj, address):
-        x = rffi.cast(rffi.INTP, address)
-        x[0] = self._unwrap_object(space, w_obj)
-
-    def convert_argument_libffi(self, space, w_obj, argchain):
-        argchain.arg(self._unwrap_object(space, w_obj))
-
-    def default_argument_libffi(self, space, argchain):
-        argchain.arg(self.default)
-
-    def from_memory(self, space, w_obj, w_type, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        intptr = rffi.cast(rffi.INTP, address)
-        return space.wrap(intptr[0])
-
-    def to_memory(self, space, w_obj, w_value, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        intptr = rffi.cast(rffi.INTP, address)
-        intptr[0] = self._unwrap_object(space, w_value)
-
 class UnsignedIntConverter(TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.uint
@@ -271,30 +278,15 @@
         ulongptr = rffi.cast(rffi.UINTP, address)
         ulongptr[0] = self._unwrap_object(space, w_value)
 
-class LongConverter(TypeConverter):
+class LongConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.slong
+    rffitype = rffi.LONG
+    rffiptype = rffi.LONGP
 
     def _unwrap_object(self, space, w_obj):
         return space.int_w(w_obj)
 
-    def convert_argument(self, space, w_obj, address):
-        x = rffi.cast(rffi.LONGP, address)
-        x[0] = self._unwrap_object(space, w_obj)
-
-    def convert_argument_libffi(self, space, w_obj, argchain):
-        argchain.arg(self._unwrap_object(space, w_obj))
-
-    def from_memory(self, space, w_obj, w_type, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        longptr = rffi.cast(rffi.LONGP, address)
-        return space.wrap(longptr[0])
-
-    def to_memory(self, space, w_obj, w_value, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        longptr = rffi.cast(rffi.LONGP, address)
-        longptr[0] = self._unwrap_object(space, w_value)
-
 class UnsignedLongConverter(TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.ulong
diff --git a/pypy/module/cppyy/include/capi.h b/pypy/module/cppyy/include/capi.h
--- a/pypy/module/cppyy/include/capi.h
+++ b/pypy/module/cppyy/include/capi.h
@@ -73,7 +73,8 @@
 
     /* misc helpers */
     void cppyy_free(void* ptr);
-    int cppyy_atoi(const char* str);
+    long long cppyy_strtoll(const char* str);
+    unsigned long long cppyy_strtuoll(const char* str);
 
 #ifdef __cplusplus
 }
diff --git a/pypy/module/cppyy/src/cintcwrapper.cxx b/pypy/module/cppyy/src/cintcwrapper.cxx
--- a/pypy/module/cppyy/src/cintcwrapper.cxx
+++ b/pypy/module/cppyy/src/cintcwrapper.cxx
@@ -462,8 +462,12 @@
 
 
 /* misc helpers ----------------------------------------------------------- */
-int cppyy_atoi(const char* str) {
-    return atoi(str);
+long long cppyy_strtoll(const char* str) {
+    return strtoll(str, NULL, 0);
+}
+
+unsigned long long cppyy_strtoull(const char* str) {
+    return strtoull(str, NULL, 0);
 }
 
 void cppyy_free(void* ptr) {
diff --git a/pypy/module/cppyy/src/reflexcwrapper.cxx b/pypy/module/cppyy/src/reflexcwrapper.cxx
--- a/pypy/module/cppyy/src/reflexcwrapper.cxx
+++ b/pypy/module/cppyy/src/reflexcwrapper.cxx
@@ -360,8 +360,12 @@
 
 
 /* misc helpers ----------------------------------------------------------- */
-int cppyy_atoi(const char* str) {
-    return atoi(str);
+long long cppyy_strtoll(const char* str) {
+    return strtoll(str, NULL, 0);
+}
+
+unsigned long long cppyy_strtoull(const char* str) {
+    return strtoull(str, NULL, 0);
 }
 
 void cppyy_free(void* ptr) {
diff --git a/pypy/module/cppyy/test/example01.cxx b/pypy/module/cppyy/test/example01.cxx
--- a/pypy/module/cppyy/test/example01.cxx
+++ b/pypy/module/cppyy/test/example01.cxx
@@ -138,21 +138,25 @@
 
 
 // argument passing
-int ArgPasser::intValue(int arg0, int argn, int arg1, int arg2)
-{
-   switch (argn) {
-   case 0:
-      return arg0;
-   case 1:
-      return arg1;
-   case 2:
-      return arg2;
-   default:
-      break;
-   }
+#define typeValueImp(itype)                                                   \
+itype ArgPasser::itype##Value(itype arg0, int argn, itype arg1, itype arg2)   \
+{                                                                             \
+   switch (argn) {                                                            \
+   case 0:                                                                    \
+      return arg0;                                                            \
+   case 1:                                                                    \
+      return arg1;                                                            \
+   case 2:                                                                    \
+      return arg2;                                                            \
+   default:                                                                   \
+      break;                                                                  \
+   }                                                                          \
+                                                                              \
+   return itype(-1);                                                          \
+}
 
-   return -1;
-}
+typeValueImp(int)
+typeValueImp(long)
 
 std::string ArgPasser::stringValue(std::string arg0, int argn, std::string arg1)
 {
diff --git a/pypy/module/cppyy/test/example01.h b/pypy/module/cppyy/test/example01.h
--- a/pypy/module/cppyy/test/example01.h
+++ b/pypy/module/cppyy/test/example01.h
@@ -61,11 +61,14 @@
     int globalAddOneToInt(int a);
 }
 
+#define typeValue(itype)\
+   itype itype##Value(itype arg0, int argn=0, itype arg1=itype(1), itype arg2=itype(2))
 
 // argument passing
 class ArgPasser {        // use a class for now as methptrgetter not
 public:                  // implemented for global functions
-   int intValue(int arg0, int argn=0, int arg1=1, int arg2=2);
+   typeValue(int);
+   typeValue(long);
 
    std::string stringValue(
       std::string arg0, int argn=0, std::string arg1 = "default");
diff --git a/pypy/module/cppyy/test/test_pythonify.py b/pypy/module/cppyy/test/test_pythonify.py
--- a/pypy/module/cppyy/test/test_pythonify.py
+++ b/pypy/module/cppyy/test/test_pythonify.py
@@ -257,15 +257,16 @@
         assert f(s("noot"), 1).c_str() == "default"
         assert f(s("mies")).c_str() == "mies"
 
-        g = a.intValue
-        raises(TypeError, 'g(1, 2, 3, 4, 6)')
-        assert g(11, 0, 12, 13) == 11
-        assert g(11, 1, 12, 13) == 12
-        assert g(11, 1, 12)     == 12
-        assert g(11, 2, 12)     ==  2
-        assert g(11, 1)         ==  1
-        assert g(11, 2)         ==  2
-        assert g(11)            == 11
+        for itype in ['int', 'long']:
+            g = getattr(a, '%sValue' % itype)
+            raises(TypeError, 'g(1, 2, 3, 4, 6)')
+            assert g(11, 0, 12, 13) == 11
+            assert g(11, 1, 12, 13) == 12
+            assert g(11, 1, 12)     == 12
+            assert g(11, 2, 12)     ==  2
+            assert g(11, 1)         ==  1
+            assert g(11, 2)         ==  2
+            assert g(11)            == 11
 
     def test12_underscore_in_class_name(self):
         """Test recognition of '_' as part of a valid class name"""


More information about the pypy-commit mailing list