Beispiel #1
0
 def string(self, cdataobj, maxlen):
     space = self.space
     if isinstance(self.ctitem, ctypeprim.W_CTypePrimitive):
         cdata = cdataobj._cdata
         if not cdata:
             raise operationerrfmt(space.w_RuntimeError,
                                   "cannot use string() on %s",
                                   space.str_w(cdataobj.repr()))
         #
         from pypy.module._cffi_backend import ctypearray
         length = maxlen
         if length < 0 and isinstance(self, ctypearray.W_CTypeArray):
             length = cdataobj.get_array_length()
         #
         # pointer to a primitive type of size 1: builds and returns a str
         if self.ctitem.size == rffi.sizeof(lltype.Char):
             if length < 0:
                 s = rffi.charp2str(cdata)
             else:
                 s = rffi.charp2strn(cdata, length)
             keepalive_until_here(cdataobj)
             return space.wrap(s)
         #
         # pointer to a wchar_t: builds and returns a unicode
         if self.is_unichar_ptr_or_array():
             cdata = rffi.cast(rffi.CWCHARP, cdata)
             if length < 0:
                 u = rffi.wcharp2unicode(cdata)
             else:
                 u = rffi.wcharp2unicoden(cdata, length)
             keepalive_until_here(cdataobj)
             return space.wrap(u)
     #
     return W_CType.string(self, cdataobj, maxlen)
Beispiel #2
0
 def string(self, cdataobj, maxlen):
     space = self.space
     if (isinstance(self.ctitem, ctypeprim.W_CTypePrimitive) and
             not isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveBool)):
         with cdataobj as ptr:
             if not ptr:
                 raise oefmt(space.w_RuntimeError,
                             "cannot use string() on %R", cdataobj)
             #
             from pypy.module._cffi_backend import ctypearray
             length = maxlen
             if length < 0 and isinstance(self, ctypearray.W_CTypeArray):
                 length = cdataobj.get_array_length()
             #
             # pointer to a primitive type of size 1: builds and returns a str
             if self.ctitem.size == rffi.sizeof(lltype.Char):
                 if length < 0:
                     s = rffi.charp2str(ptr)
                 else:
                     s = rffi.charp2strn(ptr, length)
                 return space.newbytes(s)
             #
             # pointer to a wchar_t: builds and returns a unicode
             if self.is_unichar_ptr_or_array():
                 from pypy.module._cffi_backend import wchar_helper
                 if self.ctitem.size == 2:
                     length = wchar_helper.measure_length_16(ptr, length)
                 else:
                     length = wchar_helper.measure_length_32(ptr, length)
                 return self.ctitem.unpack_ptr(self, ptr, length)
     #
     return W_CType.string(self, cdataobj, maxlen)
Beispiel #3
0
 def string(self, cdataobj, maxlen):
     space = self.space
     if isinstance(self.ctitem, ctypeprim.W_CTypePrimitive):
         cdata = cdataobj._cdata
         if not cdata:
             raise oefmt(space.w_RuntimeError, "cannot use string() on %s",
                         space.str_w(cdataobj.repr()))
         #
         from pypy.module._cffi_backend import ctypearray
         length = maxlen
         if length < 0 and isinstance(self, ctypearray.W_CTypeArray):
             length = cdataobj.get_array_length()
         #
         # pointer to a primitive type of size 1: builds and returns a str
         if self.ctitem.size == rffi.sizeof(lltype.Char):
             if length < 0:
                 s = rffi.charp2str(cdata)
             else:
                 s = rffi.charp2strn(cdata, length)
             keepalive_until_here(cdataobj)
             return space.wrap(s)
         #
         # pointer to a wchar_t: builds and returns a unicode
         if self.is_unichar_ptr_or_array():
             cdata = rffi.cast(rffi.CWCHARP, cdata)
             if length < 0:
                 u = rffi.wcharp2unicode(cdata)
             else:
                 u = rffi.wcharp2unicoden(cdata, length)
             keepalive_until_here(cdataobj)
             return space.wrap(u)
     #
     return W_CType.string(self, cdataobj, maxlen)