[pypy-commit] pypy space-newtext: stab at disambiguating newint/newlong

wlav pypy.commits at gmail.com
Mon Nov 21 19:58:56 EST 2016


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: space-newtext
Changeset: r88537:be088827c1c4
Date: 2016-11-21 16:57 -0800
http://bitbucket.org/pypy/pypy/changeset/be088827c1c4/

Log:	stab at disambiguating newint/newlong

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
@@ -217,6 +217,9 @@
 class NumericTypeConverterMixin(object):
     _mixin_ = True
 
+    def _wrap_object(self, space, obj):
+        return getattr(space, self.wrapper)(obj)
+
     def convert_argument_libffi(self, space, w_obj, address, call_local):
         x = rffi.cast(self.c_ptrtype, address)
         x[0] = self._unwrap_object(space, w_obj)
@@ -228,7 +231,7 @@
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = self._get_raw_address(space, w_obj, offset)
         rffiptr = rffi.cast(self.c_ptrtype, address)
-        return space.wrap(rffiptr[0])
+        return self._wrap_object(space, rffiptr[0])
 
     def to_memory(self, space, w_obj, w_value, offset):
         address = self._get_raw_address(space, w_obj, offset)
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
@@ -79,7 +79,7 @@
     _mixin_ = True
 
     def _wrap_object(self, space, obj):
-        return space.wrap(obj)
+        return getattr(space, self.wrapper)(obj)
 
     def execute(self, space, cppmethod, cppthis, num_args, args):
         result = self.c_stubcall(space, cppmethod, cppthis, num_args, args)
@@ -103,7 +103,7 @@
         self.do_assign = True
 
     def _wrap_object(self, space, obj):
-        return space.wrap(rffi.cast(self.c_type, obj))
+        return getattr(space, self.wrapper)(rffi.cast(self.c_type, obj))
 
     def _wrap_reference(self, space, rffiptr):
         if self.do_assign:
diff --git a/pypy/module/cppyy/ffitypes.py b/pypy/module/cppyy/ffitypes.py
--- a/pypy/module/cppyy/ffitypes.py
+++ b/pypy/module/cppyy/ffitypes.py
@@ -9,6 +9,17 @@
 # sets of jit_libffi, rffi, and different space unwrapping calls. To get the
 # right mixin, a non-RPython function typeid() is used.
 
+class BaseIntTypeMixin(object):
+    _mixin_     = True
+    _immutable_fields_ = ['wrapper']
+
+    wrapper     = 'newint'
+
+class BaseLongTypeMixin(object):
+    _mixin_     = True
+    _immutable_fields_ = ['wrapper']
+
+    wrapper     = 'newlong'
 
 class BoolTypeMixin(object):
     _mixin_     = True
@@ -35,6 +46,7 @@
     libffitype  = jit_libffi.types.schar
     c_type      = rffi.CHAR
     c_ptrtype   = rffi.CCHARP           # there's no such thing as rffi.CHARP
+    wrapper     = 'newbytes'
 
     def _unwrap_object(self, space, w_value):
         # allow int to pass to char and make sure that str is of length 1
@@ -52,7 +64,7 @@
                         "char expected, got string of size %d", len(value))
         return value[0] # turn it into a "char" to the annotator
 
-class ShortTypeMixin(object):
+class ShortTypeMixin(BaseIntTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -63,7 +75,7 @@
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(rffi.SHORT, space.int_w(w_obj))
 
-class UShortTypeMixin(object):
+class UShortTypeMixin(BaseIntTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -74,7 +86,7 @@
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(self.c_type, space.int_w(w_obj))
 
-class IntTypeMixin(object):
+class IntTypeMixin(BaseIntTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -85,7 +97,7 @@
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(self.c_type, space.c_int_w(w_obj))
 
-class UIntTypeMixin(object):
+class UIntTypeMixin(BaseLongTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -96,7 +108,7 @@
     def _unwrap_object(self, space, w_obj):
         return rffi.cast(self.c_type, space.uint_w(w_obj))
 
-class LongTypeMixin(object):
+class LongTypeMixin(BaseLongTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -107,7 +119,9 @@
     def _unwrap_object(self, space, w_obj):
         return space.int_w(w_obj)
 
-class ULongTypeMixin(object):
+# TODO: check ULong limits; actually, they fail if this is
+#  an BaseLongTypeMixin (i.e. use of space.ewlong) ... why??
+class ULongTypeMixin(BaseIntTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -118,7 +132,7 @@
     def _unwrap_object(self, space, w_obj):
         return space.uint_w(w_obj)
 
-class LongLongTypeMixin(object):
+class LongLongTypeMixin(BaseLongTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -129,7 +143,7 @@
     def _unwrap_object(self, space, w_obj):
         return space.r_longlong_w(w_obj)
 
-class ULongLongTypeMixin(object):
+class ULongLongTypeMixin(BaseLongTypeMixin):
     _mixin_     = True
     _immutable_fields_ = ['libffitype', 'c_type', 'c_ptrtype']
 
@@ -162,6 +176,7 @@
     libffitype  = jit_libffi.types.double
     c_type      = rffi.DOUBLE
     c_ptrtype   = rffi.DOUBLEP
+    wrapper     = 'newfloat'
     typecode    = 'd'
 
     def _unwrap_object(self, space, w_obj):
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
@@ -71,6 +71,10 @@
     typename = "int"
     def __init__(self, val):
         self.val = val
+class FakeLong(FakeBase):
+    typename = "long"
+    def __init__(self, val):
+        self.val = val
 class FakeFloat(FakeBase):
     typename = "float"
     def __init__(self, val):
@@ -145,16 +149,7 @@
     def issequence_w(self, w_obj):
         return True
 
-    @specialize.argtype(1)
     def wrap(self, obj):
-        if isinstance(obj, int):
-            return FakeInt(obj)
-        if isinstance(obj, float):
-            return FakeFloat(obj)
-        if isinstance(obj, str):
-            return FakeString(obj)
-        if isinstance(obj, rffi.r_int):
-            return FakeInt(int(obj))
         assert 0
 
     @specialize.argtype(1)
@@ -163,11 +158,11 @@
 
     @specialize.argtype(1)
     def newint(self, obj):
-        return FakeInt(obj)
+        return FakeInt(rarithmetic.intmask(obj))
 
     @specialize.argtype(1)
     def newlong(self, obj):
-        return FakeInt(obj)
+        return FakeLong(rarithmetic.intmask(obj))
 
     @specialize.argtype(1)
     def newfloat(self, obj):
@@ -207,7 +202,9 @@
         return w_obj.val
 
     def uint_w(self, w_obj):
-        assert isinstance(w_obj, FakeInt)
+        if isinstance(w_obj, FakeInt):
+            return rarithmetic.r_uint(w_obj.val)
+        assert isinstance(w_obj, FakeLong)
         return rarithmetic.r_uint(w_obj.val)
 
     def str_w(self, w_obj):


More information about the pypy-commit mailing list