def _get_raw_address(self, space, w_obj, offset): rawobject = get_rawobject_nonnull(space, w_obj) assert lltype.typeOf(rawobject) == capi.C_OBJECT if rawobject: fieldptr = capi.direct_ptradd(rawobject, offset) else: fieldptr = rffi.cast(capi.C_OBJECT, offset) return fieldptr
def wrap_cppobject(space, w_pycppclass, cppclass, rawobject, isref, python_owns): if rawobject: actual = capi.c_actual_class(cppclass, rawobject) if actual != cppclass.handle: offset = capi._c_base_offset(actual, cppclass.handle, rawobject, -1) rawobject = capi.direct_ptradd(rawobject, offset) w_pycppclass = get_pythonized_cppclass(space, actual) w_cppclass = space.findattr(w_pycppclass, space.wrap("_cpp_proxy")) cppclass = space.interp_w(W_CPPClass, w_cppclass, can_be_None=False) return wrap_cppobject_nocast(space, w_pycppclass, cppclass, rawobject, isref, python_owns)
def _unwrap_object(self, space, w_obj): from pypy.module.cppyy.interp_cppyy import W_CPPInstance if isinstance(w_obj, W_CPPInstance): if capi.c_is_subtype(space, w_obj.cppclass, self.cppclass): rawobject = w_obj.get_rawobject() offset = capi.c_base_offset(space, w_obj.cppclass, self.cppclass, rawobject, 1) obj_address = capi.direct_ptradd(rawobject, offset) return rffi.cast(capi.C_OBJECT, obj_address) raise oefmt(space.w_TypeError, "cannot pass %T as %s", w_obj, self.cppclass.name)
def to_memory(self, space, w_obj, w_value, offset): # copy only the pointer value rawobject = get_rawobject_nonnull(space, w_obj) byteptr = rffi.cast(rffi.CCHARPP, capi.direct_ptradd(rawobject, offset)) buf = space.getarg_w('s*', w_value) try: byteptr[0] = buf.get_raw_address() except ValueError: raise OperationError(space.w_TypeError, space.wrap("raw buffer interface not supported"))
def to_memory(self, space, w_obj, w_value, offset): # copy only the pointer value rawobject = get_rawobject_nonnull(space, w_obj) byteptr = rffi.cast(rffi.CCHARPP, capi.direct_ptradd(rawobject, offset)) buf = space.getarg_w('s*', w_value) try: byteptr[0] = buf.get_raw_address() except ValueError: raise oefmt(space.w_TypeError, "raw buffer interface not supported")
def _unwrap_object(self, space, w_obj): from pypy.module.cppyy.interp_cppyy import W_CPPInstance obj = space.interpclass_w(w_obj) if isinstance(obj, W_CPPInstance): if capi.c_is_subtype(obj.cppclass, self.cppclass): rawobject = obj.get_rawobject() offset = capi.c_base_offset(obj.cppclass, self.cppclass, rawobject, 1) obj_address = capi.direct_ptradd(rawobject, offset) return rffi.cast(capi.C_OBJECT, obj_address) raise OperationError(space.w_TypeError, space.wrap("cannot pass %s as %s" % (space.type(w_obj).getname(space, "?"), self.cppclass.name)))
def descr_next(self, space): if self.w_seq is None: raise OperationError(space.w_StopIteration, space.w_None) if self.len <= self.index: self.w_seq = None raise OperationError(space.w_StopIteration, space.w_None) try: from pypy.module.cppyy import capi # TODO: refector offset = capi.direct_ptradd(rffi.cast(C_OBJECT, self.data), self.index*self.stride) w_item = self.converter.from_memory(space, space.w_None, space.w_None, offset) except OperationError as e: self.w_seq = None if not e.match(space, space.w_IndexError): raise raise OperationError(space.w_StopIteration, space.w_None) self.index += 1 return w_item
def descr_next(self, space): if self.w_seq is None: raise OperationError(space.w_StopIteration, space.w_None) if self.len <= self.index: self.w_seq = None raise OperationError(space.w_StopIteration, space.w_None) try: from pypy.module.cppyy import capi # TODO: refector offset = capi.direct_ptradd(rffi.cast(C_OBJECT, self.data), self.index * self.stride) w_item = self.converter.from_memory(space, space.w_None, space.w_None, offset) except OperationError as e: self.w_seq = None if not e.match(space, space.w_IndexError): raise raise OperationError(space.w_StopIteration, space.w_None) self.index += 1 return w_item
def wrap_cppobject(space, rawobject, cppclass, do_cast=True, python_owns=False, is_ref=False, fresh=False): rawobject = rffi.cast(capi.C_OBJECT, rawobject) # cast to actual cast if requested and possible w_pycppclass = space.w_None if do_cast and rawobject: actual = capi.c_actual_class(space, cppclass, rawobject) if actual != cppclass.handle: try: w_pycppclass = get_pythonized_cppclass(space, actual) offset = capi.c_base_offset1(space, actual, cppclass, rawobject, -1) rawobject = capi.direct_ptradd(rawobject, offset) w_cppclass = space.findattr(w_pycppclass, space.wrap("_cpp_proxy")) cppclass = space.interp_w(W_CPPClass, w_cppclass, can_be_None=False) except Exception: # failed to locate/build the derived class, so stick to the base (note # that only get_pythonized_cppclass is expected to raise, so none of # the variables are re-assigned yet) pass if space.is_w(w_pycppclass, space.w_None): w_pycppclass = get_pythonized_cppclass(space, cppclass.handle) # try to recycle existing object if this one is not newly created if not fresh: obj = memory_regulator.retrieve(rawobject) if obj is not None and obj.cppclass is cppclass: return obj # fresh creation w_cppinstance = space.allocate_instance(W_CPPInstance, w_pycppclass) cppinstance = space.interp_w(W_CPPInstance, w_cppinstance, can_be_None=False) cppinstance.__init__(space, cppclass, rawobject, is_ref, python_owns) memory_regulator.register(cppinstance) return w_cppinstance
def get_cppthis(self, cppinstance, calling_scope): assert self == cppinstance.cppclass offset = capi.c_base_offset(self, calling_scope, cppinstance.get_rawobject(), 1) return capi.direct_ptradd(cppinstance.get_rawobject(), offset)