[pypy-commit] pypy reflex-support: refactoring and another attempt at ccharp vs voidp consistency

wlav noreply at buildbot.pypy.org
Thu Jul 14 11:35:44 CEST 2011


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r45587:281a4df60741
Date: 2011-07-14 02:35 -0700
http://bitbucket.org/pypy/pypy/changeset/281a4df60741/

Log:	refactoring and another attempt at ccharp vs voidp consistency

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
@@ -12,21 +12,24 @@
 from pypy.module.cppyy import helper, capi
 
 
-NULL = lltype.nullptr(clibffi.FFI_TYPE_P.TO)
-
 def get_rawobject(space, w_obj):
     if not space.eq_w(w_obj, space.w_None):
         from pypy.module.cppyy.interp_cppyy import W_CPPInstance
         w_cpp_instance = space.findattr(w_obj, space.wrap("_cppinstance"))
         cpp_instance = space.interp_w(W_CPPInstance, w_cpp_instance, can_be_None=True)
         if cpp_instance:
+            assert lltype.typeOf(cpp_instance.rawobject) == rffi.VOIDP
             return cpp_instance.rawobject
-    return lltype.nullptr(rffi.CCHARP.TO)
+    return lltype.nullptr(rffi.VOIDP.TO)
+
+def _direct_ptradd(ptr, offset):
+    address = rffi.cast(rffi.CCHARP, ptr)
+    return rffi.cast(rffi.CCHARP, lltype.direct_ptradd(address, offset))
 
 
 class TypeConverter(object):
     _immutable = True
-    libffitype = NULL
+    libffitype = lltype.nullptr(clibffi.FFI_TYPE_P.TO)
 
     def __init__(self, space, array_size):
         pass
@@ -35,9 +38,7 @@
     def _get_raw_address(self, space, w_obj, offset):
         rawobject = get_rawobject(space, w_obj)
         if rawobject:
-            assert lltype.typeOf(rawobject) == rffi.CCHARP
-            field_address = lltype.direct_ptradd(rawobject, offset)
-            fieldptr = rffi.cast(rffi.CCHARP, field_address)
+            fieldptr = _direct_ptradd(rawobject, offset)
         else:
             fieldptr = rffi.cast(rffi.CCHARP, offset)
         return fieldptr
@@ -124,9 +125,7 @@
     def to_memory(self, space, w_obj, w_value, offset):
         # copy only the pointer value
         rawobject = get_rawobject(space, w_obj)
-        assert lltype.typeOf(rawobject) == rffi.CCHARP
-        field_address = lltype.direct_ptradd(rawobject, offset)
-        byteptr = rffi.cast(rffi.CCHARPP, field_address)
+        byteptr = rffi.cast(rffi.CCHARPP, _direct_ptradd(rawobject, offset))
         buf = space.buffer_w(w_value)
         try:
             byteptr[0] = buf.get_raw_address()
@@ -492,10 +491,8 @@
         if isinstance(obj, W_CPPInstance):
             if capi.c_is_subtype(obj.cppclass.handle, self.cpptype.handle):
                 offset = capi.c_base_offset(obj.cppclass.handle, self.cpptype.handle)
-                assert lltype.typeOf(obj.rawobject) == rffi.CCHARP
-                obj_address = lltype.direct_ptradd(obj.rawobject, offset)
-                objptr = rffi.cast(rffi.VOIDP, obj_address)
-                return objptr
+                obj_address = _direct_ptradd(obj.rawobject, offset)
+                return rffi.cast(rffi.VOIDP, obj_address)
         raise OperationError(space.w_TypeError,
                              space.wrap("cannot pass %s as %s" % (
                                  space.type(w_obj).getname(space, "?"),
@@ -514,7 +511,7 @@
     _immutable_ = True
 
     def from_memory(self, space, w_obj, offset):
-        address = self._get_raw_address(space, w_obj, offset)
+        address = rffi.cast(rffi.VOIDP, self._get_raw_address(space, w_obj, offset))
         from pypy.module.cppyy import interp_cppyy
         return interp_cppyy.W_CPPInstance(space, self.cpptype, address, False)
 
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
@@ -243,14 +243,15 @@
 
     def execute(self, space, func, cppthis, num_args, args):
         from pypy.module.cppyy import interp_cppyy
-        long_result = capi.c_call_l(func.cpptype.handle, func.method_index, cppthis, num_args, args)
-        ptr_result = rffi.cast(rffi.CCHARP, long_result)
+        long_result = capi.c_call_l(
+            func.cpptype.handle, func.method_index, cppthis, num_args, args)
+        ptr_result = rffi.cast(rffi.VOIDP, long_result)
         return interp_cppyy.W_CPPInstance(space, self.cpptype, ptr_result, False)
 
     def execute_libffi(self, space, libffifunc, argchain):
         from pypy.module.cppyy import interp_cppyy
-        result = libffifunc.call(argchain, rffi.VOIDP)
-        return interp_cppyy.W_CPPInstance(space, self.cpptype, result, False)
+        ptr_result = rffi.cast(rffi.VOIDP, libffifunc.call(argchain, rffi.VOIDP))
+        return interp_cppyy.W_CPPInstance(space, self.cpptype, ptr_result, False)
 
 
 class InstanceExecutor(InstancePtrExecutor):
@@ -260,7 +261,7 @@
         from pypy.module.cppyy import interp_cppyy
         long_result = capi.c_call_o(
             func.cpptype.handle, func.method_index, cppthis, num_args, args, self.cpptype.handle)
-        ptr_result = rffi.cast(rffi.CCHARP, long_result)
+        ptr_result = rffi.cast(rffi.VOIDP, long_result)
         return interp_cppyy.W_CPPInstance(space, self.cpptype, ptr_result, True)
 
 
diff --git a/pypy/module/cppyy/interp_cppyy.py b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -15,7 +15,6 @@
 class FastCallNotPossible(Exception):
     pass
 
-NULL_CCHARP = lltype.nullptr(rffi.CCHARP.TO)
 NULL_VOIDP  = lltype.nullptr(rffi.VOIDP.TO)
 
 def load_lib(space, name):
@@ -103,8 +102,10 @@
         self._libffifunc_cache = {}
 
     def call(self, cppthis, args_w):
+        assert lltype.typeOf(cppthis) == rffi.VOIDP
         if self.executor is None:
-            raise OperationError(self.space.w_TypeError, self.space.wrap("return type not handled"))
+            raise OperationError(self.space.w_TypeError,
+                                 self.space.wrap("return type not handled"))
 
         if self.methgetter and cppthis: # only for methods
             try:
@@ -205,13 +206,15 @@
     _immutable_ = True
 
     def call(self, cppthis, args_w):
+        assert lltype.typeOf(cppthis) == rffi.VOIDP
         if self.executor is None:
-            raise OperationError(self.space.w_TypeError, self.space.wrap("return type not handled"))
+            raise OperationError(self.space.w_TypeError,
+                                 self.space.wrap("return type not handled"))
 
         assert not cppthis
         args = self.prepare_arguments(args_w)
         try:
-            return self.executor.execute(self.space, self, NULL_CCHARP,
+            return self.executor.execute(self.space, self, NULL_VOIDP,
                                          len(args_w), args)
         finally:
             self.free_arguments(args, len(args_w))
@@ -223,13 +226,13 @@
     def call(self, cppthis, args_w):
         assert not cppthis
         newthis = capi.c_allocate(self.cpptype.handle)
-        ccharp_this = rffi.cast(rffi.CCHARP, newthis)
+        assert lltype.typeOf(newthis) == rffi.VOIDP
         try:
-            CPPMethod.call(self, ccharp_this, args_w)
+            CPPMethod.call(self, newthis, args_w)
         except Exception, e:
             capi.c_deallocate(self.cpptype.handle, newthis)
             raise
-        return W_CPPInstance(self.space, self.cpptype, ccharp_this, True)
+        return W_CPPInstance(self.space, self.cpptype, newthis, True)
 
 
 class W_CPPOverload(Wrappable):
@@ -252,7 +255,8 @@
         if cppinstance:
             cppthis = cppinstance.rawobject
         else:
-            cppthis = NULL_CCHARP
+            cppthis = NULL_VOIDP
+        assert lltype.typeOf(cppthis) == rffi.VOIDP
 
         space = self.space
         errmsg = 'None of the overloads matched:'
@@ -536,13 +540,14 @@
     def __init__(self, space, cppclass, rawobject, python_owns):
         self.space = space
         self.cppclass = cppclass
-        assert lltype.typeOf(rawobject) == rffi.CCHARP
+        assert lltype.typeOf(rawobject) == rffi.VOIDP
         self.rawobject = rawobject
         self.python_owns = python_owns
 
     def _nullcheck(self):
         if not self.rawobject:
-            raise OperationError(self.space.w_ReferenceError, self.space.wrap("trying to access a NULL pointer"))
+            raise OperationError(self.space.w_ReferenceError,
+                                 self.space.wrap("trying to access a NULL pointer"))
 
     def invoke(self, overload, args_w):
         self._nullcheck()
@@ -551,7 +556,7 @@
     def destruct(self):
         if self.rawobject:
             capi.c_destruct(self.cppclass.handle, self.rawobject)
-            self.rawobject = NULL_CCHARP
+            self.rawobject = NULL_VOIDP
 
     def __del__(self):
         if self.python_owns:


More information about the pypy-commit mailing list