예제 #1
0
파일: ctypeptr.py 프로젝트: juokaz/pypy
 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 copy_and_convert_to_object(self, source):
     space = self.space
     self.check_complete()
     ob = cdataobj.W_CDataNewOwning(space, self.size, self)
     with ob as target:
         misc._raw_memcopy(source, target, self.size)
     return ob
예제 #3
0
 def copy_and_convert_to_object(self, cdata):
     space = self.space
     self.check_complete()
     ob = cdataobj.W_CDataNewOwning(space, self.size, self)
     misc._raw_memcopy(cdata, ob._cdata, self.size)
     keepalive_until_here(ob)
     return ob
예제 #4
0
 def newp(self, w_init):
     space = self.space
     datasize = self.size
     #
     if datasize < 0:
         from pypy.module._cffi_backend import misc
         w_init, length = misc.get_new_array_length(space, w_init)
         try:
             datasize = ovfcheck(length * self.ctitem.size)
         except OverflowError:
             raise OperationError(
                 space.w_OverflowError,
                 space.wrap("array size would overflow a ssize_t"))
         #
         cdata = cdataobj.W_CDataNewOwningLength(space, datasize, self,
                                                 length)
     #
     else:
         cdata = cdataobj.W_CDataNewOwning(space, datasize, self)
     #
     if not space.is_w(w_init, space.w_None):
         self.convert_from_object(cdata._cdata, w_init)
         keepalive_until_here(cdata)
     return cdata