[pypy-commit] pypy reflex-support: o) fleshed out mixins for unsigned integer types

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


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r52156:b637b18b01b8
Date: 2012-02-06 15:32 -0800
http://bitbucket.org/pypy/pypy/changeset/b637b18b01b8/

Log:	o) fleshed out mixins for unsigned integer types o) default args
	supported for all integer types

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
@@ -57,6 +57,10 @@
         from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
         raise FastCallNotPossible
 
+    def default_argument_libffi(self, space, argchain):
+        from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
+        raise FastCallNotPossible
+
     def from_memory(self, space, w_obj, w_type, offset):
         self._is_abstract(space)
 
@@ -140,9 +144,6 @@
 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)
@@ -156,13 +157,13 @@
 
     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])
+        rffiptr = rffi.cast(self.rffiptype, address)
+        return space.wrap(rffiptr[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)
+        rffiptr = rffi.cast(self.rffiptype, address)
+        rffiptr[0] = self._unwrap_object(space, w_value)
 
 
 class VoidConverter(TypeConverter):
@@ -245,96 +246,73 @@
         address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset))
         address[0] = self._unwrap_object(space, w_value)
 
+
+class ShortConverter(IntTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    libffitype = libffi.types.sshort
+    rffiptype = rffi.SHORTP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.SHORT, capi.c_strtoll(default))
+
+    def _unwrap_object(self, space, w_obj):
+        return rffi.cast(rffi.SHORT, space.int_w(w_obj))
+
+class UnsignedShortConverter(IntTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    libffitype = libffi.types.sshort
+    rffiptype = rffi.USHORTP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.USHORT, capi.c_strtoull(default))
+
+    def _unwrap_object(self, space, w_obj):
+        return rffi.cast(rffi.USHORT, space.int_w(w_obj))
+
 class IntConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.sint
-    rffitype = rffi.INT
     rffiptype = rffi.INTP
 
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.INT, capi.c_strtoll(default))
+
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(rffi.INT, space.c_int_w(w_obj))
 
-class UnsignedIntConverter(TypeConverter):
+class UnsignedIntConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.uint
+    rffiptype = rffi.UINTP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.UINT, capi.c_strtoull(default))
 
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(rffi.UINT, space.uint_w(w_obj))
 
-    def convert_argument(self, space, w_obj, address):
-        x = rffi.cast(rffi.UINTP, 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)
-        ulongptr = rffi.cast(rffi.UINTP, address)
-        return space.wrap(ulongptr[0])
-
-    def to_memory(self, space, w_obj, w_value, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        ulongptr = rffi.cast(rffi.UINTP, address)
-        ulongptr[0] = self._unwrap_object(space, w_value)
-
 class LongConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.slong
-    rffitype = rffi.LONG
     rffiptype = rffi.LONGP
 
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.LONG, capi.c_strtoll(default))
+
     def _unwrap_object(self, space, w_obj):
         return space.int_w(w_obj)
 
-class UnsignedLongConverter(TypeConverter):
+class UnsignedLongConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.ulong
+    rffiptype = rffi.ULONGP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(rffi.ULONG, capi.c_strtoull(default))
 
     def _unwrap_object(self, space, w_obj):
         return space.uint_w(w_obj)
 
-    def convert_argument(self, space, w_obj, address):
-        x = rffi.cast(rffi.ULONGP, 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)
-        ulongptr = rffi.cast(rffi.ULONGP, address)
-        return space.wrap(ulongptr[0])
-
-    def to_memory(self, space, w_obj, w_value, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        ulongptr = rffi.cast(rffi.ULONGP, address)
-        ulongptr[0] = self._unwrap_object(space, w_value)
-
-class ShortConverter(TypeConverter):
-    _immutable_ = True
-    libffitype = libffi.types.sshort
-
-    def _unwrap_object(self, space, w_obj):
-        return rffi.cast(rffi.SHORT, space.int_w(w_obj))
-
-    def convert_argument(self, space, w_obj, address):
-        x = rffi.cast(rffi.SHORTP, 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)
-        shortptr = rffi.cast(rffi.SHORTP, address)
-        return space.wrap(shortptr[0])
-
-    def to_memory(self, space, w_obj, w_value, offset):
-        address = self._get_raw_address(space, w_obj, offset)
-        shortptr = rffi.cast(rffi.SHORTP, address)
-        shortptr[0] = self._unwrap_object(space, w_value)
-
 class FloatConverter(TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.float
@@ -454,6 +432,11 @@
     typecode = 'h'
     typesize = rffi.sizeof(rffi.SHORT)
 
+class UnsignedShortArrayConverter(ArrayTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    typecode = 'H'
+    typesize = rffi.sizeof(rffi.USHORT)
+
 class IntArrayConverter(ArrayTypeConverterMixin, TypeConverter):
     _immutable_ = True
     typecode = 'i'
@@ -469,6 +452,11 @@
     typecode = 'l'
     typesize = rffi.sizeof(rffi.LONG)
 
+class UnsignedLongArrayConverter(ArrayTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    typecode = 'L'
+    typesize = rffi.sizeof(rffi.ULONG)
+
 class FloatArrayConverter(ArrayTypeConverterMixin, TypeConverter):
     _immutable_ = True
     typecode = 'f'
@@ -485,6 +473,11 @@
     typecode = 'h'
     typesize = rffi.sizeof(rffi.SHORT)
 
+class UnsignedShortPtrConverter(PtrTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    typecode = 'H'
+    typesize = rffi.sizeof(rffi.USHORT)
+
 class IntPtrConverter(PtrTypeConverterMixin, TypeConverter):
     _immutable_ = True
     typecode = 'i'
@@ -500,6 +493,11 @@
     typecode = 'l'
     typesize = rffi.sizeof(rffi.LONG)
 
+class UnsignedLongPtrConverter(PtrTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    typecode = 'L'
+    typesize = rffi.sizeof(rffi.ULONG)
+
 class FloatPtrConverter(PtrTypeConverterMixin, TypeConverter):
     _immutable_ = True
     typecode = 'f'
@@ -623,7 +621,7 @@
 _converters["unsigned char"]            = CharConverter
 _converters["short int"]                = ShortConverter
 _converters["short"]                    = _converters["short int"]
-_converters["unsigned short int"]       = ShortConverter
+_converters["unsigned short int"]       = UnsignedShortConverter
 _converters["unsigned short"]           = _converters["unsigned short int"]
 _converters["int"]                      = IntConverter
 _converters["unsigned int"]             = UnsignedIntConverter
@@ -644,9 +642,9 @@
 _a_converters["short*"]                   = _a_converters["short int*"]
 _a_converters["short int[]"]              = ShortArrayConverter
 _a_converters["short[]"]                  = _a_converters["short int[]"]
-_a_converters["unsigned short int*"]      = ShortPtrConverter
+_a_converters["unsigned short int*"]      = UnsignedShortPtrConverter
 _a_converters["unsigned short*"]          = _a_converters["unsigned short int*"]
-_a_converters["unsigned short int[]"]     = ShortArrayConverter
+_a_converters["unsigned short int[]"]     = UnsignedShortArrayConverter
 _a_converters["unsigned short[]"]         = _a_converters["unsigned short int[]"]
 _a_converters["int*"]                     = IntPtrConverter
 _a_converters["int[]"]                    = IntArrayConverter
@@ -656,9 +654,9 @@
 _a_converters["long*"]                    = _a_converters["long int*"]
 _a_converters["long int[]"]               = LongArrayConverter
 _a_converters["long[]"]                   = _a_converters["long int[]"]
-_a_converters["unsigned long int*"]       = LongPtrConverter
+_a_converters["unsigned long int*"]       = UnsignedLongPtrConverter
 _a_converters["unsigned long*"]           = _a_converters["unsigned long int*"]
-_a_converters["unsigned long int[]"]      = LongArrayConverter
+_a_converters["unsigned long int[]"]      = UnsignedLongArrayConverter
 _a_converters["unsigned long[]"]          = _a_converters["unsigned long int[]"]
 _a_converters["float*"]                   = FloatPtrConverter
 _a_converters["float[]"]                  = FloatArrayConverter
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
@@ -399,6 +399,11 @@
     return type_cppstring_to_cstring(arg->GetFullTypeName());
 }
 
+char* cppyy_method_arg_default(cppyy_typehandle_t, int, int) {
+/* unused: libffi does not work with CINT back-end */
+   return cppstring_to_cstring("");
+}
+
 
 int cppyy_is_constructor(cppyy_typehandle_t handle, int method_index) {
     TClassRef cr = type_from_handle(handle);
@@ -466,7 +471,7 @@
     return strtoll(str, NULL, 0);
 }
 
-unsigned long long cppyy_strtoull(const char* str) {
+extern "C" unsigned long long cppyy_strtoull(const char* str) {
     return strtoull(str, NULL, 0);
 }
 
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
@@ -364,7 +364,7 @@
     return strtoll(str, NULL, 0);
 }
 
-unsigned long long cppyy_strtoull(const char* str) {
+extern "C" unsigned long long cppyy_strtoull(const char* str) {
     return strtoull(str, NULL, 0);
 }
 
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,8 +138,8 @@
 
 
 // argument passing
-#define typeValueImp(itype)                                                   \
-itype ArgPasser::itype##Value(itype arg0, int argn, itype arg1, itype arg2)   \
+#define typeValueImp(itype, tname)                                            \
+itype ArgPasser::tname##Value(itype arg0, int argn, itype arg1, itype arg2)   \
 {                                                                             \
    switch (argn) {                                                            \
    case 0:                                                                    \
@@ -152,11 +152,15 @@
       break;                                                                  \
    }                                                                          \
                                                                               \
-   return itype(-1);                                                          \
+   return (itype)-1;                                                          \
 }
 
-typeValueImp(int)
-typeValueImp(long)
+typeValueImp(short, short)
+typeValueImp(unsigned short, ushort)
+typeValueImp(int, int)
+typeValueImp(unsigned int, uint)
+typeValueImp(long, long)
+typeValueImp(unsigned long, ulong)
 
 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,14 +61,18 @@
     int globalAddOneToInt(int a);
 }
 
-#define typeValue(itype)\
-   itype itype##Value(itype arg0, int argn=0, itype arg1=itype(1), itype arg2=itype(2))
+#define typeValue(itype, tname) \
+   itype tname##Value(itype arg0, int argn=0, itype arg1=1, itype arg2=2)
 
 // argument passing
 class ArgPasser {        // use a class for now as methptrgetter not
 public:                  // implemented for global functions
-   typeValue(int);
-   typeValue(long);
+   typeValue(short, short);
+   typeValue(unsigned short, ushort);
+   typeValue(int, int);
+   typeValue(unsigned int, uint);
+   typeValue(long, long);
+   typeValue(unsigned long, ulong);
 
    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,7 +257,7 @@
         assert f(s("noot"), 1).c_str() == "default"
         assert f(s("mies")).c_str() == "mies"
 
-        for itype in ['int', 'long']:
+        for itype in ['short', 'ushort', 'int', 'uint', 'long', 'ulong']:
             g = getattr(a, '%sValue' % itype)
             raises(TypeError, 'g(1, 2, 3, 4, 6)')
             assert g(11, 0, 12, 13) == 11


More information about the pypy-commit mailing list