def convert_to_object(self, cdata): if self.length < 0: # we can't return a <cdata 'int[]'> here, because we don't # know the length to give it. As a compromize, returns # <cdata 'int *'> in this case. self = self.ctptr # return cdataobj.W_CData(self.space, cdata, self)
def read(self, cdata): cdata = rffi.ptradd(cdata, self.offset) if self.bitshift == self.BS_REGULAR: return self.ctype.convert_to_object(cdata) elif self.bitshift == self.BS_EMPTY_ARRAY: from pypy.module._cffi_backend import ctypearray ctype = self.ctype assert isinstance(ctype, ctypearray.W_CTypeArray) return cdataobj.W_CData(ctype.space, cdata, ctype.ctptr) else: return self.convert_bitfield_to_object(cdata)
def rawaddressof(self, cdata, offset): from pypy.module._cffi_backend.ctypestruct import W_CTypeStructOrUnion space = self.space ctype2 = cdata.ctype if (isinstance(ctype2, W_CTypeStructOrUnion) or isinstance(ctype2, W_CTypePtrOrArray)): ptr = cdata.unsafe_escaping_ptr() ptr = rffi.ptradd(ptr, offset) return cdataobj.W_CData(space, ptr, self) else: raise oefmt(space.w_TypeError, "expected a cdata struct/union/array/pointer object")
def rawaddressof(self, cdata, offset): from pypy.module._cffi_backend.ctypestruct import W_CTypeStructOrUnion space = self.space ctype2 = cdata.ctype if (isinstance(ctype2, W_CTypeStructOrUnion) or isinstance(ctype2, W_CTypePtrOrArray)): ptrdata = rffi.ptradd(cdata._cdata, offset) return cdataobj.W_CData(space, ptrdata, self) else: raise OperationError(space.w_TypeError, space.wrap("expected a cdata struct/union/array/pointer" " object"))
def add(self, cdata, i): space = self.space ctitem = self.ctitem itemsize = ctitem.size if ctitem.size < 0: if self.is_void_ptr: itemsize = 1 else: raise oefmt(space.w_TypeError, "ctype '%s' points to items of unknown size", self.name) p = rffi.ptradd(cdata, i * itemsize) return cdataobj.W_CData(space, p, self)
def cast(self, w_ob): # cast to a pointer, to a funcptr, or to an array. # Note that casting to an array is an extension to the C language, # which seems to be necessary in order to sanely get a # <cdata 'int[3]'> at some address. if self.size < 0: return W_CType.cast(self, w_ob) space = self.space if (isinstance(w_ob, cdataobj.W_CData) and isinstance(w_ob.ctype, W_CTypePtrOrArray)): value = w_ob.unsafe_escaping_ptr() else: value = misc.as_unsigned_long(space, w_ob, strict=False) value = rffi.cast(rffi.CCHARP, value) return cdataobj.W_CData(space, value, self)
def read(self, cdata, w_cdata): cdata = rffi.ptradd(cdata, self.offset) if self.bitshift == self.BS_REGULAR: return self.ctype.convert_to_object(cdata) elif self.bitshift == self.BS_EMPTY_ARRAY: from pypy.module._cffi_backend import ctypearray ctype = self.ctype assert isinstance(ctype, ctypearray.W_CTypeArray) structobj = w_cdata.get_structobj() if structobj is not None: # variable-length array size = structobj.allocated_length - self.offset if size >= 0: arraylen = size // ctype.ctitem.size return cdataobj.W_CDataSliced(ctype.space, cdata, ctype, arraylen) return cdataobj.W_CData(ctype.space, cdata, ctype.ctptr) else: return self.convert_bitfield_to_object(cdata)
def cast(self, w_ob): if self.is_file: value = self.prepare_file(w_ob) if value: return cdataobj.W_CData(self.space, value, self) return W_CTypePtrBase.cast(self, w_ob)
def convert_to_object(self, cdata): ptrdata = rffi.cast(rffi.CCHARPP, cdata)[0] return cdataobj.W_CData(self.space, ptrdata, self)
def convert_to_object(self, cdata): space = self.space self.check_complete() return cdataobj.W_CData(space, cdata, self)
def add(self, cdata, i): p = rffi.ptradd(cdata, i * self.ctitem.size) return cdataobj.W_CData(self.space, p, self.ctptr)
def ttree_getattr(space, w_self, args_w): """Specialized __getattr__ for TTree's that allows switching on/off the reading of individual branchs.""" from pypy.module.cppyy import interp_cppyy tree = space.interp_w(interp_cppyy.W_CPPInstance, w_self) space = tree.space # holds the class cache in State # prevent recursion attr = space.str_w(args_w[0]) if attr and attr[0] == '_': raise OperationError(space.w_AttributeError, args_w[0]) # try the saved cdata (for builtin types) try: w_cdata = space.getattr(w_self, space.wrap('_' + attr)) from pypy.module._cffi_backend import cdataobj cdata = space.interp_w(cdataobj.W_CData, w_cdata, can_be_None=False) return cdata.convert_to_object() except OperationError: pass # setup branch as a data member and enable it for reading w_branch = space.call_method(w_self, "GetBranch", args_w[0]) if not space.is_true(w_branch): raise OperationError(space.w_AttributeError, args_w[0]) activate_branch(space, w_branch) # figure out from where we're reading entry = space.r_longlong_w(space.call_method(w_self, "GetReadEntry")) if entry == -1: entry = 0 # setup cache structure w_klassname = space.call_method(w_branch, "GetClassName") if space.is_true(w_klassname): # some instance klass = interp_cppyy.scope_byname(space, space.str_w(w_klassname)) w_obj = klass.construct() # 0x10000 = kDeleteObject; reset because we own the object space.call_method(w_branch, "ResetBit", space.wrap(0x10000)) space.call_method(w_branch, "SetObject", w_obj) space.call_method(w_branch, "GetEntry", space.wrap(entry)) space.setattr(w_self, args_w[0], w_obj) return w_obj else: # builtin data w_leaf = space.call_method(w_self, "GetLeaf", args_w[0]) space.call_method(w_branch, "GetEntry", space.wrap(entry)) # location w_address = space.call_method(w_leaf, "GetValuePointer") buf = space.getarg_w('s*', w_address) from pypy.module._rawffi import buffer assert isinstance(buf, buffer.RawFFIBuffer) address = rffi.cast(rffi.CCHARP, buf.datainstance.ll_buffer) # placeholder w_typename = space.call_method(w_leaf, "GetTypeName") from pypy.module.cppyy import capi typename = capi.c_resolve_name(space, space.str_w(w_typename)) if typename == 'bool': typename = '_Bool' w_address = space.call_method(w_leaf, "GetValuePointer") from pypy.module._cffi_backend import cdataobj, newtype cdata = cdataobj.W_CData(space, address, newtype.new_primitive_type(space, typename)) # cache result space.setattr(w_self, space.wrap('_' + attr), space.wrap(cdata)) return space.getattr(w_self, args_w[0])