def newp(self, w_init): from pypy.module._cffi_backend.ctypestruct import W_CTypeStructOrUnion space = self.space ctitem = self.ctitem datasize = ctitem.size if datasize < 0: raise oefmt(space.w_TypeError, "cannot instantiate ctype '%s' of unknown size", self.name) if isinstance(ctitem, W_CTypeStructOrUnion): # 'newp' on a struct-or-union pointer: in this case, we return # a W_CDataPtrToStruct object which has a strong reference # to a W_CDataNewOwning that really contains the structure. # if ctitem.with_var_array and not space.is_w(w_init, space.w_None): datasize = ctitem.convert_struct_from_object( lltype.nullptr(rffi.CCHARP.TO), w_init, datasize) # cdatastruct = cdataobj.W_CDataNewOwning(space, datasize, ctitem) cdata = cdataobj.W_CDataPtrToStructOrUnion(space, cdatastruct._cdata, self, cdatastruct) else: if self.is_char_or_unichar_ptr_or_array(): datasize *= 2 # forcefully add a null character cdata = cdataobj.W_CDataNewOwning(space, datasize, self) # if not space.is_w(w_init, space.w_None): ctitem.convert_from_object(cdata._cdata, w_init) keepalive_until_here(cdata) return cdata
def newp(self, w_init, allocator): from pypy.module._cffi_backend.ctypestruct import W_CTypeStructOrUnion space = self.space ctitem = self.ctitem datasize = ctitem.size if datasize < 0: raise oefmt(space.w_TypeError, "cannot instantiate ctype '%s' of unknown size", self.name) if isinstance(ctitem, W_CTypeStructOrUnion): # 'newp' on a struct-or-union pointer: in this case, we return # a W_CDataPtrToStruct object which has a strong reference # to a W_CDataNewOwning that really contains the structure. # varsize_length = -1 ctitem.force_lazy_struct() if ctitem._with_var_array: if not space.is_w(w_init, space.w_None): datasize = ctitem.convert_struct_from_object( lltype.nullptr(rffi.CCHARP.TO), w_init, datasize) varsize_length = datasize # cdatastruct = allocator.allocate(space, datasize, ctitem, length=varsize_length) ptr = cdatastruct.unsafe_escaping_ptr() cdata = cdataobj.W_CDataPtrToStructOrUnion(space, ptr, self, cdatastruct) else: if self.is_char_or_unichar_ptr_or_array(): datasize *= 2 # forcefully add a null character cdata = allocator.allocate(space, datasize, self) # if not space.is_w(w_init, space.w_None): with cdata as ptr: ctitem.convert_from_object(ptr, w_init) return cdata