[pypy-commit] pypy reflex-support: long long and unsigned long long converters and executors

wlav noreply at buildbot.pypy.org
Fri May 4 21:07:21 CEST 2012


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r54892:a0f4fef869dd
Date: 2012-05-04 11:17 -0700
http://bitbucket.org/pypy/pypy/changeset/a0f4fef869dd/

Log:	long long and unsigned long long converters and executors

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
@@ -113,6 +113,11 @@
     [C_METHOD, C_OBJECT, rffi.INT, rffi.VOIDP], rffi.LONG,
     threadsafe=threadsafe,
     compilation_info=backend.eci)
+c_call_ll = rffi.llexternal(
+    "cppyy_call_ll",
+    [C_METHOD, C_OBJECT, rffi.INT, rffi.VOIDP], rffi.LONGLONG,
+    threadsafe=threadsafe,
+    compilation_info=backend.eci)
 c_call_f = rffi.llexternal(
     "cppyy_call_f",
     [C_METHOD, C_OBJECT, rffi.INT, rffi.VOIDP], rffi.DOUBLE,
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
@@ -366,6 +366,29 @@
         ba = rffi.cast(rffi.CCHARP, address)
         ba[capi.c_function_arg_typeoffset()] = self.typecode
 
+class LongLongConverter(IntTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    libffitype = libffi.types.slong
+    c_type     = rffi.LONGLONG
+    c_ptrtype  = rffi.LONGLONGP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(self.c_type, capi.c_strtoll(default))
+
+    def _unwrap_object(self, space, w_obj):
+        return space.r_longlong_w(w_obj)
+
+class ConstLongLongRefConverter(ConstRefNumericTypeConverterMixin, LongLongConverter):
+    _immutable_ = True
+    libffitype = libffi.types.pointer
+    typecode = 'r'
+
+    def convert_argument(self, space, w_obj, address):
+        x = rffi.cast(self.c_ptrtype, address)
+        x[0] = self._unwrap_object(space, w_obj)
+        ba = rffi.cast(rffi.CCHARP, address)
+        ba[capi.c_function_arg_typeoffset()] = self.typecode
+
 class UnsignedLongConverter(IntTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.ulong
@@ -382,6 +405,23 @@
     _immutable_ = True
     libffitype = libffi.types.pointer
 
+class UnsignedLongLongConverter(IntTypeConverterMixin, TypeConverter):
+    _immutable_ = True
+    libffitype = libffi.types.ulong
+    c_type     = rffi.ULONGLONG
+    c_ptrtype  = rffi.ULONGLONGP
+
+    def __init__(self, space, default):
+        self.default = rffi.cast(self.c_type, capi.c_strtoull(default))
+
+    def _unwrap_object(self, space, w_obj):
+        return space.r_ulonglong_w(w_obj)
+
+class ConstUnsignedLongLongRefConverter(ConstRefNumericTypeConverterMixin, UnsignedLongLongConverter):
+    _immutable_ = True
+    libffitype = libffi.types.pointer
+
+
 class FloatConverter(FloatTypeConverterMixin, TypeConverter):
     _immutable_ = True
     libffitype = libffi.types.float
@@ -748,6 +788,14 @@
 _converters["const unsigned long int&"] = ConstUnsignedLongRefConverter
 _converters["unsigned long"]            = _converters["unsigned long int"]
 _converters["const unsigned long&"]     = _converters["const unsigned long int&"]
+_converters["long long int"]            = LongLongConverter
+_converters["const long long int&"]     = ConstLongLongRefConverter
+_converters["long long"]                = _converters["long long int"]
+_converters["const long long&"]         = _converters["const long long int&"]
+_converters["unsigned long long int"]   = UnsignedLongLongConverter
+_converters["const unsigned long long int&"] = ConstUnsignedLongLongRefConverter
+_converters["unsigned long long"]       = _converters["unsigned long long int"]
+_converters["const unsigned long long&"] = _converters["const unsigned long long int&"]
 _converters["float"]                    = FloatConverter
 _converters["const float&"]             = ConstFloatRefConverter
 _converters["double"]                   = DoubleConverter
diff --git a/pypy/module/cppyy/executor.py b/pypy/module/cppyy/executor.py
--- a/pypy/module/cppyy/executor.py
+++ b/pypy/module/cppyy/executor.py
@@ -147,6 +147,32 @@
         result = libffifunc.call(argchain, rffi.ULONG)
         return space.wrap(result)
 
+class LongLongExecutor(FunctionExecutor):
+    _immutable_ = True
+    libffitype = libffi.types.sint64
+
+    def _wrap_result(self, space, result):
+        return space.wrap(result)
+
+    def execute(self, space, cppmethod, cppthis, num_args, args):
+        result = capi.c_call_ll(cppmethod, cppthis, num_args, args)
+        return self._wrap_result(space, result)
+
+    def execute_libffi(self, space, libffifunc, argchain):
+        result = libffifunc.call(argchain, rffi.LONGLONG)
+        return space.wrap(result)
+
+class UnsignedLongLongExecutor(LongLongExecutor):
+    _immutable_ = True
+    libffitype = libffi.types.uint64
+
+    def _wrap_result(self, space, result):
+        return space.wrap(rffi.cast(rffi.ULONGLONG, result))
+
+    def execute_libffi(self, space, libffifunc, argchain):
+        result = libffifunc.call(argchain, rffi.ULONGLONG)
+        return space.wrap(result)
+
 class ConstIntRefExecutor(FunctionExecutor):
     _immutable_ = True
     libffitype = libffi.types.pointer
@@ -396,6 +422,10 @@
 _executors["unsigned long"]       = _executors["unsigned long int"]
 _executors["unsigned long int*"]  = UnsignedLongPtrExecutor
 _executors["unsigned long*"]      = _executors["unsigned long int*"]
+_executors["long long int"]       = LongLongExecutor
+_executors["long long"]           = _executors["long long int"]
+_executors["unsigned long long int"] = UnsignedLongLongExecutor
+_executors["unsigned long long"]  = _executors["unsigned long long int"]
 _executors["float"]               = FloatExecutor
 _executors["float*"]              = FloatPtrExecutor
 _executors["double"]              = DoubleExecutor
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
@@ -31,6 +31,7 @@
     short  cppyy_call_h(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
     int    cppyy_call_i(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
     long   cppyy_call_l(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
+    long long cppyy_call_ll(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
     double cppyy_call_f(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
     double cppyy_call_d(cppyy_method_t method, cppyy_object_t self, int nargs, void* args);
 
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
@@ -136,6 +136,10 @@
     return cppyy_call_T<long>(method, self, nargs, args);
 }
 
+long long cppyy_call_ll(cppyy_method_t method, cppyy_object_t self, int nargs, void* args) {
+    return cppyy_call_T<long long>(method, self, nargs, args);
+}
+
 double cppyy_call_f(cppyy_method_t method, cppyy_object_t self, int nargs, void* args) {
     return cppyy_call_T<float>(method, self, nargs, args);
 }
diff --git a/pypy/module/cppyy/test/datatypes.cxx b/pypy/module/cppyy/test/datatypes.cxx
--- a/pypy/module/cppyy/test/datatypes.cxx
+++ b/pypy/module/cppyy/test/datatypes.cxx
@@ -13,8 +13,10 @@
     m_uint   =  22u;
     m_long   = -33l;
     m_ulong  =  33ul;
-    m_float  = -44.f;
-    m_double = -55.;
+    m_llong  = -44ll;
+    m_ullong =  55ull;
+    m_float  = -66.f;
+    m_double = -77.;
     m_enum   = kNothing;
 
     m_short_array2  = new short[N];
@@ -86,6 +88,8 @@
 unsigned int   cppyy_test_data::get_uint()   { return m_uint; }
 long           cppyy_test_data::get_long()   { return m_long; }
 unsigned long  cppyy_test_data::get_ulong()  { return m_ulong; }
+long long      cppyy_test_data::get_llong()  { return m_llong; }
+unsigned long long cppyy_test_data::get_ullong()  { return m_ullong; }
 float          cppyy_test_data::get_float()  { return m_float; }
 double         cppyy_test_data::get_double() { return m_double; }
 cppyy_test_data::what cppyy_test_data::get_enum() { return m_enum; }
@@ -129,22 +133,28 @@
 void cppyy_test_data::set_long_c(const long& l)              { m_long   = l; }
 void cppyy_test_data::set_ulong(unsigned long ul)            { m_ulong  = ul; }
 void cppyy_test_data::set_ulong_c(const unsigned long& ul)   { m_ulong  = ul; }
+void cppyy_test_data::set_llong(long long ll)                { m_llong  = ll; }
+void cppyy_test_data::set_llong_c(const long long& ll)       { m_llong  = ll; }
+void cppyy_test_data::set_ullong(unsigned long long ull)     { m_ullong  = ull; }
+void cppyy_test_data::set_ullong_c(const unsigned long long& ull) { m_ullong  = ull; }
 void cppyy_test_data::set_float(float f)                     { m_float  = f; }
 void cppyy_test_data::set_float_c(const float& f)            { m_float  = f; }
 void cppyy_test_data::set_double(double d)                   { m_double = d; }
 void cppyy_test_data::set_double_c(const double& d)          { m_double = d; }
 void cppyy_test_data::set_enum(what w)                       { m_enum   = w; }
 
-char           cppyy_test_data::s_char   = 's';
-unsigned char  cppyy_test_data::s_uchar  = 'u';
-short          cppyy_test_data::s_short  = -101;
-unsigned short cppyy_test_data::s_ushort =  255u;
-int            cppyy_test_data::s_int    = -202;
-unsigned int   cppyy_test_data::s_uint   =  202u;
-long           cppyy_test_data::s_long   = -303l;
-unsigned long  cppyy_test_data::s_ulong  =  303ul;
-float          cppyy_test_data::s_float  = -404.f;
-double         cppyy_test_data::s_double = -505.;
+char                cppyy_test_data::s_char   = 's';
+unsigned char       cppyy_test_data::s_uchar  = 'u';
+short               cppyy_test_data::s_short  = -101;
+unsigned short      cppyy_test_data::s_ushort =  255u;
+int                 cppyy_test_data::s_int    = -202;
+unsigned int        cppyy_test_data::s_uint   =  202u;
+long                cppyy_test_data::s_long   = -303l;
+unsigned long       cppyy_test_data::s_ulong  =  303ul;
+long long           cppyy_test_data::s_llong  = -404ll;
+unsigned long long  cppyy_test_data::s_ullong =  505ull;
+float               cppyy_test_data::s_float  = -606.f;
+double              cppyy_test_data::s_double = -707.;
 cppyy_test_data::what  cppyy_test_data::s_enum = cppyy_test_data::kNothing;
 
 
diff --git a/pypy/module/cppyy/test/datatypes.h b/pypy/module/cppyy/test/datatypes.h
--- a/pypy/module/cppyy/test/datatypes.h
+++ b/pypy/module/cppyy/test/datatypes.h
@@ -21,18 +21,20 @@
     void destroy_arrays();
 
 // getters
-    bool           get_bool();
-    char           get_char();
-    unsigned char  get_uchar();
-    short          get_short();
-    unsigned short get_ushort();
-    int            get_int();
-    unsigned int   get_uint();
-    long           get_long();
-    unsigned long  get_ulong();
-    float          get_float();
-    double         get_double();
-    what           get_enum();
+    bool                 get_bool();
+    char                 get_char();
+    unsigned char        get_uchar();
+    short                get_short();
+    unsigned short       get_ushort();
+    int                  get_int();
+    unsigned int         get_uint();
+    long                 get_long();
+    unsigned long        get_ulong();
+    long long            get_llong();
+    unsigned long long   get_ullong();
+    float                get_float();
+    double               get_double();
+    what                 get_enum();
 
     short*          get_short_array();
     short*          get_short_array2();
@@ -71,8 +73,12 @@
     void set_uint_c(const unsigned int& ui);
     void set_long(long l);
     void set_long_c(const long& l);
+    void set_llong(long long ll);
+    void set_llong_c(const long long& ll);
     void set_ulong(unsigned long ul);
     void set_ulong_c(const unsigned long& ul);
+    void set_ullong(unsigned long long ll);
+    void set_ullong_c(const unsigned long long& ll);
     void set_float(float f);
     void set_float_c(const float& f);
     void set_double(double d);
@@ -81,18 +87,20 @@
 
 public:
 // basic types
-    bool           m_bool;
-    char           m_char;
-    unsigned char  m_uchar;
-    short          m_short;
-    unsigned short m_ushort;
-    int            m_int;
-    unsigned int   m_uint;
-    long           m_long;
-    unsigned long  m_ulong;
-    float          m_float;
-    double         m_double;
-    what           m_enum;
+    bool                 m_bool;
+    char                 m_char;
+    unsigned char        m_uchar;
+    short                m_short;
+    unsigned short       m_ushort;
+    int                  m_int;
+    unsigned int         m_uint;
+    long                 m_long;
+    unsigned long        m_ulong;
+    long long            m_llong;
+    unsigned long long   m_ullong;
+    float                m_float;
+    double               m_double;
+    what                 m_enum;
 
 // array types
     short           m_short_array[N];
@@ -118,17 +126,19 @@
     cppyy_test_pod* m_ppod;
 
 public:
-    static char           s_char;
-    static unsigned char  s_uchar;
-    static short          s_short;
-    static unsigned short s_ushort;
-    static int            s_int;
-    static unsigned int   s_uint;
-    static long           s_long;
-    static unsigned long  s_ulong;
-    static float          s_float;
-    static double         s_double;
-    static what           s_enum;
+    static char                    s_char;
+    static unsigned char           s_uchar;
+    static short                   s_short;
+    static unsigned short          s_ushort;
+    static int                     s_int;
+    static unsigned int            s_uint;
+    static long                    s_long;
+    static unsigned long           s_ulong;
+    static long long               s_llong;
+    static unsigned long long      s_ullong;
+    static float                   s_float;
+    static double                  s_double;
+    static what                    s_enum;
 
 private:
     bool m_owns_arrays;
diff --git a/pypy/module/cppyy/test/test_datatypes.py b/pypy/module/cppyy/test/test_datatypes.py
--- a/pypy/module/cppyy/test/test_datatypes.py
+++ b/pypy/module/cppyy/test/test_datatypes.py
@@ -53,10 +53,12 @@
         assert c.m_uint   ==  22
         assert c.m_long   == -33
         assert c.m_ulong  ==  33
+        assert c.m_llong  == -44
+        assert c.m_ullong ==  55
 
         # reading floating point types
-        assert round(c.m_float  + 44., 5) == 0
-        assert round(c.m_double + 55., 8) == 0
+        assert round(c.m_float  + 66., 5) == 0
+        assert round(c.m_double + 77., 8) == 0
 
         # reding of array types
         for i in range(self.N):
@@ -146,7 +148,7 @@
 # TODO: raises(TypeError, 'c.set_uchar(-1)')
 
         # integer types
-        names = ['short', 'ushort', 'int', 'uint', 'long', 'ulong']
+        names = ['short', 'ushort', 'int', 'uint', 'long', 'ulong', 'llong', 'ullong']
         for i in range(len(names)):
             exec 'c.m_%s = %d' % (names[i],i)
             assert eval('c.get_%s()' % names[i]) == i
@@ -175,6 +177,7 @@
         c.destroy_arrays()
 
         # integer arrays
+        names = ['short', 'ushort', 'int', 'uint', 'long', 'ulong']
         import array
         a = range(self.N)
         atypes = ['h', 'H', 'i', 'I', 'l', 'L' ]
@@ -232,12 +235,16 @@
         assert c.s_long                 == -303L
         assert c.s_ulong                ==  303L
         assert cppyy_test_data.s_ulong  ==  303L
+        assert cppyy_test_data.s_llong  == -404L
+        assert c.s_llong                == -404L
+        assert c.s_ullong               ==  505L
+        assert cppyy_test_data.s_ullong ==  505L
 
         # floating point types
-        assert round(cppyy_test_data.s_float  + 404., 5) == 0
-        assert round(c.s_float                + 404., 5) == 0
-        assert round(cppyy_test_data.s_double + 505., 8) == 0
-        assert round(c.s_double               + 505., 8) == 0
+        assert round(cppyy_test_data.s_float  + 606., 5) == 0
+        assert round(c.s_float                + 606., 5) == 0
+        assert round(cppyy_test_data.s_double + 707., 8) == 0
+        assert round(c.s_double               + 707., 8) == 0
 
         c.destruct()
 
diff --git a/pypy/module/cppyy/test/test_zjit.py b/pypy/module/cppyy/test/test_zjit.py
--- a/pypy/module/cppyy/test/test_zjit.py
+++ b/pypy/module/cppyy/test/test_zjit.py
@@ -137,6 +137,8 @@
         return obj
 
     c_int_w = int_w
+    r_longlong_w = int_w
+    r_ulonglong_w = uint_w
 
     def isinstance_w(self, w_obj, w_type):
         assert isinstance(w_obj, FakeBase)


More information about the pypy-commit mailing list