def convert_argument_from_object(self, cdata, w_ob): from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag result = (not isinstance(w_ob, cdataobj.W_CData) and self._prepare_pointer_call_argument(w_ob, cdata)) if result == 0: self.convert_from_object(cdata, w_ob) set_mustfree_flag(cdata, result) return result
def convert_argument_from_object(self, cdata, w_ob, keepalives, i): # writes the pointer to cdata[0], writes the must-free flag in # the byte just before cdata[0], and returns True if something # must be done later to free. from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag if isinstance(w_ob, cdataobj.W_CData): result = 0 else: space = self.space if self.accept_str and space.isinstance_w(w_ob, space.w_bytes): # special case to optimize strings passed to a "char *" argument value = space.bytes_w(w_ob) if isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveBool): self._must_be_string_of_zero_or_one(value) keepalives[i] = misc.write_string_as_charp(cdata, value) return True result = self._prepare_pointer_call_argument(w_ob, cdata) if result == 0: self.convert_from_object(cdata, w_ob) set_mustfree_flag(cdata, result) return result == 1 # 0 or 2 => False, nothing to do later
def write_string_as_charp(target, string): from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag buf, llobj, buf_flag = rffi.get_nonmovingbuffer_ll_final_null(string) set_mustfree_flag(target, ord(buf_flag)) # 4, 5 or 6 rffi.cast(rffi.CCHARPP, target)[0] = buf return llobj