Author: Wim Lavrijsen <wlavrij...@lbl.gov> Branch: cppyy-packaging Changeset: r94774:df5f9ec89c17 Date: 2018-06-20 19:26 -0700 http://bitbucket.org/pypy/pypy/changeset/df5f9ec89c17/
Log: simplify use of converters 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 @@ -108,7 +108,7 @@ from pypy.module._cppyy.interp_cppyy import FastCallNotPossible raise FastCallNotPossible - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): self._is_abstract(space) def convert_argument_libffi(self, space, w_obj, address, call_local): @@ -125,10 +125,10 @@ def to_memory(self, space, w_obj, w_value, offset): self._is_abstract(space) - def finalize_call(self, space, w_obj, call_local): + def finalize_call(self, space, w_obj): pass - def free_argument(self, space, arg, call_local): + def free_argument(self, space, arg): pass @@ -172,7 +172,7 @@ state = space.fromcache(ffitypes.State) return state.c_voidp - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): w_tc = space.findattr(w_obj, space.newtext('typecode')) if w_tc is not None and space.text_w(w_tc) != self.typecode: raise oefmt(space.w_TypeError, @@ -247,7 +247,7 @@ class IntTypeConverterMixin(NumericTypeConverterMixin): _mixin_ = True - def convert_argument(self, space, w_obj, address, call_local): + 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) @@ -256,7 +256,7 @@ class FloatTypeConverterMixin(NumericTypeConverterMixin): _mixin_ = True - def convert_argument(self, space, w_obj, address, call_local): + 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) @@ -273,12 +273,12 @@ state = space.fromcache(ffitypes.State) return state.c_void - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): self._is_abstract(space) class BoolConverter(ffitypes.typeid(bool), TypeConverter): - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.LONGP, address) x[0] = self._unwrap_object(space, w_obj) ba = rffi.cast(rffi.CCHARP, address) @@ -303,7 +303,7 @@ address[0] = '\x00' class CharConverter(ffitypes.typeid(rffi.CHAR), TypeConverter): - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.CCHARP, address) x[0] = self._unwrap_object(space, w_obj) ba = rffi.cast(rffi.CCHARP, address) @@ -381,7 +381,7 @@ class CStringConverter(TypeConverter): - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.LONGP, address) arg = space.text_w(w_obj) x[0] = rffi.cast(rffi.LONG, rffi.str2charp(arg)) @@ -393,7 +393,7 @@ charpptr = rffi.cast(rffi.CCHARPP, address) return space.newtext(rffi.charp2str(charpptr[0])) - def free_argument(self, space, arg, call_local): + def free_argument(self, space, arg): lltype.free(rffi.cast(rffi.CCHARPP, arg)[0], flavor='raw') class CStringConverterWithSize(CStringConverter): @@ -423,7 +423,7 @@ state = space.fromcache(ffitypes.State) return state.c_voidp - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.VOIDPP, address) x[0] = self._unwrap_object(space, w_obj) ba = rffi.cast(rffi.CCHARP, address) @@ -452,37 +452,39 @@ address[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_value)) class VoidPtrPtrConverter(TypeConverter): - _immutable_fields_ = ['uses_local', 'typecode'] + typecode = 'p' - uses_local = True - typecode = 'a' + def __init__(self, space, extra): + self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.VOIDPP, address) - ba = rffi.cast(rffi.CCHARP, address) try: x[0] = get_rawbuffer(space, w_obj) except TypeError: - r = rffi.cast(rffi.VOIDPP, call_local) - r[0] = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj)) - x[0] = rffi.cast(rffi.VOIDP, call_local) + ptr = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj)) + self.ref_buffer = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') + self.ref_buffer[0] = ptr + x[0] = self.ref_buffer + ba = rffi.cast(rffi.CCHARP, address) ba[capi.c_function_arg_typeoffset(space)] = self.typecode - def finalize_call(self, space, w_obj, call_local): - r = rffi.cast(rffi.VOIDPP, call_local) - try: - set_rawobject(space, w_obj, r[0]) - except OperationError: - pass # no set on buffer/array/None + def finalize_call(self, space, w_obj): + if self.ref_buffer: + set_rawobject(space, w_obj, self.ref_buffer[0]) + + def free_argument(self, space, arg): + if self.ref_buffer: + lltype.free(self.ref_buffer, flavor='raw') + self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) class VoidPtrRefConverter(VoidPtrPtrConverter): - _immutable_fields_ = ['uses_local', 'typecode'] - uses_local = True + _immutable_fields_ = ['typecode'] typecode = 'V' class InstanceRefConverter(TypeConverter): _immutable_fields_ = ['typecode', 'clsdecl'] - typecode = 'V' + typecode = 'V' def __init__(self, space, clsdecl): from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl @@ -508,7 +510,7 @@ state = space.fromcache(ffitypes.State) return state.c_voidp - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.VOIDPP, address) x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) address = rffi.cast(capi.C_OBJECT, address) @@ -551,9 +553,8 @@ def to_memory(self, space, w_obj, w_value, offset): self._is_abstract(space) - class InstancePtrConverter(InstanceRefConverter): - typecode = 'o' + typecode = 'o' def _unwrap_object(self, space, w_obj): try: @@ -574,36 +575,41 @@ address[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_value)) class InstancePtrPtrConverter(InstancePtrConverter): - _immutable_fields_ = ['uses_local'] + typecode = 'o' - uses_local = True + def __init__(self, space, extra): + InstancePtrConverter.__init__(self, space, extra) + self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) - def convert_argument(self, space, w_obj, address, call_local): - r = rffi.cast(rffi.VOIDPP, call_local) - r[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.VOIDPP, address) - x[0] = rffi.cast(rffi.VOIDP, call_local) - address = rffi.cast(capi.C_OBJECT, address) + ptr = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) + self.ref_buffer = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') + self.ref_buffer[0] = ptr + x[0] = self.ref_buffer ba = rffi.cast(rffi.CCHARP, address) - ba[capi.c_function_arg_typeoffset(space)] = 'o' + ba[capi.c_function_arg_typeoffset(space)] = self.typecode def convert_argument_libffi(self, space, w_obj, address, call_local): # TODO: finalize_call not yet called for fast call (see interp_cppyy.py) from pypy.module._cppyy.interp_cppyy import FastCallNotPossible raise FastCallNotPossible - def finalize_call(self, space, w_obj, call_local): - from pypy.module._cppyy.interp_cppyy import W_CPPInstance - assert isinstance(w_obj, W_CPPInstance) - r = rffi.cast(rffi.VOIDPP, call_local) - w_obj._rawobject = rffi.cast(capi.C_OBJECT, r[0]) - def from_memory(self, space, w_obj, w_pycppclass, offset): address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) from pypy.module._cppyy import interp_cppyy return interp_cppyy.wrap_cppinstance( space, address, self.clsdecl, do_cast=False, is_ref=True) + def finalize_call(self, space, w_obj): + if self.ref_buffer: + set_rawobject(space, w_obj, self.ref_buffer[0]) + + def free_argument(self, space, arg): + if self.ref_buffer: + lltype.free(self.ref_buffer, flavor='raw') + self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) + class StdStringConverter(InstanceConverter): def __init__(self, space, extra): @@ -628,7 +634,7 @@ except Exception: InstanceConverter.to_memory(self, space, w_obj, w_value, offset) - def free_argument(self, space, arg, call_local): + def free_argument(self, space, arg): capi.c_destruct(space, self.clsdecl, rffi.cast(capi.C_OBJECT, rffi.cast(rffi.VOIDPP, arg)[0])) class StdStringRefConverter(InstancePtrConverter): @@ -646,7 +652,7 @@ state = space.fromcache(ffitypes.State) return state.c_voidp - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): if hasattr(space, "fake"): raise NotImplementedError space.getbuiltinmodule("cpyext") @@ -671,7 +677,7 @@ x = rffi.cast(rffi.VOIDPP, address) x[0] = rffi.cast(rffi.VOIDP, ref)""" - def free_argument(self, space, arg, call_local): + def free_argument(self, space, arg): if hasattr(space, "fake"): raise NotImplementedError space.getbuiltinmodule("cpyext") @@ -685,7 +691,7 @@ def __init__(self, space, signature): self.signature = signature - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): # TODO: atm, does not actually get an overload, but a staticmethod from pypy.module._cppyy.interp_cppyy import W_CPPOverload cppol = space.interp_w(W_CPPOverload, w_obj) @@ -740,7 +746,7 @@ raise oefmt(space.w_TypeError, "cannot pass %T instance as %s", w_obj, self.rawdecl.name) - def convert_argument(self, space, w_obj, address, call_local): + def convert_argument(self, space, w_obj, address): x = rffi.cast(rffi.VOIDPP, address) x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) address = rffi.cast(capi.C_OBJECT, address) 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 @@ -185,7 +185,7 @@ _attrs_ = ['space', 'scope', 'cppmethod', 'arg_defs', 'args_required', 'converters', 'executor', '_funcaddr', 'cif_descr', 'uses_local'] _immutable_fields_ = ['scope', 'cppmethod', 'arg_defs', 'args_required', - 'converters', 'executor', 'uses_local'] + 'converters', 'executor', '_funcaddr', 'cif_descr', 'uses_local'] def __init__(self, space, declaring_scope, cppmethod, arg_defs, args_required): self.space = space @@ -249,7 +249,7 @@ pass # can happen if converters or executor does not implement ffi # ffi chain must have failed; using stub functions instead - args, stat = self.prepare_arguments(args_w, call_local) + args, stat = self.prepare_arguments(args_w) try: result = self.executor.execute( self.space, self.cppmethod, cppthis, len(args_w), args) @@ -260,7 +260,7 @@ raise OperationError(self.space.w_Exception, self.space.newtext(pywhat)) return result finally: - self.finalize_call(args, args_w, call_local) + self.finalize_call(args, args_w) finally: if call_local: lltype.free(call_local, flavor='raw') @@ -386,7 +386,7 @@ self._funcaddr = funcaddr @jit.unroll_safe - def prepare_arguments(self, args_w, call_local): + def prepare_arguments(self, args_w): args = capi.c_allocate_function_args(self.space, len(args_w)) stride = capi.c_function_arg_sizeof(self.space) for i in range(len(args_w)): @@ -394,15 +394,13 @@ w_arg = args_w[i] try: arg_i = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), i*stride) - loc_i = self._address_from_local_buffer(call_local, i) - conv.convert_argument(self.space, w_arg, rffi.cast(capi.C_OBJECT, arg_i), loc_i) + conv.convert_argument(self.space, w_arg, rffi.cast(capi.C_OBJECT, arg_i)) except: # fun :-( for j in range(i): conv = self.converters[j] arg_j = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), j*stride) - loc_j = self._address_from_local_buffer(call_local, j) - conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_j), loc_j) + conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_j)) capi.c_deallocate_function_args(self.space, args) raise stat = rffi.cast(rffi.ULONGP, @@ -411,14 +409,13 @@ return args, stat @jit.unroll_safe - def finalize_call(self, args, args_w, call_local): + def finalize_call(self, args, args_w): stride = capi.c_function_arg_sizeof(self.space) for i in range(len(args_w)): conv = self.converters[i] arg_i = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), i*stride) - loc_i = self._address_from_local_buffer(call_local, i) - conv.finalize_call(self.space, args_w[i], loc_i) - conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_i), loc_i) + conv.finalize_call(self.space, args_w[i]) + conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_i)) capi.c_deallocate_function_args(self.space, args) def signature(self, show_formalargs=True): _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit