Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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():
                 cdata = rffi.cast(rffi.CWCHARP, ptr)
                 if length < 0:
                     u = rffi.wcharp2unicode(cdata)
                 else:
                     u = rffi.wcharp2unicoden(cdata, length)
                 return space.newunicode(u)
     #
     return W_CType.string(self, cdataobj, maxlen)
Ejemplo n.º 4
0
def wcharp2unicode(space, address, maxlength=-1):
    if address == 0:
        return space.w_None
    wcharp_addr = rffi.cast(rffi.CWCHARP, address)
    if maxlength == -1:
        s = rffi.wcharp2unicode(wcharp_addr)
    else:
        s = rffi.wcharp2unicoden(wcharp_addr, maxlength)
    return space.wrap(s)
Ejemplo n.º 5
0
def wcharp2unicode(space, address, maxlength=-1):
    if address == 0:
        return space.w_None
    wcharp_addr = rffi.cast(rffi.CWCHARP, address)
    if maxlength == -1:
        s = rffi.wcharp2unicode(wcharp_addr)
    else:
        s = rffi.wcharp2unicoden(wcharp_addr, maxlength)
    return space.newunicode(s)
Ejemplo n.º 6
0
def QueryValue(space, w_hkey, w_subkey):
    """string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.

key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that holds the name of the subkey with which the value
 is associated.  If this parameter is None or empty, the function retrieves
 the value set by the SetValue() method for the key identified by key.

Values in the registry have name, type, and data components. This method
retrieves the data for a key's first value that has a NULL name.
But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!"""
    hkey = hkey_w(w_hkey, space)
    if space.is_w(w_subkey, space.w_None):
        subkey = None
    else:
        subkey = space.unicode_w(w_subkey)
    with rffi.scoped_unicode2wcharp(subkey) as wide_subkey:
        c_subkey = rffi.cast(rffi.CCHARP, wide_subkey)
        with lltype.scoped_alloc(rwin32.PLONG.TO, 1) as bufsize_p:
            ret = rwinreg.RegQueryValueW(hkey, c_subkey, None, bufsize_p)
            bufSize = intmask(bufsize_p[0])
            if ret == rwinreg.ERROR_MORE_DATA:
                bufSize = 256
            elif ret != 0:
                raiseWindowsError(space, ret, 'RegQueryValue')

            while True:
                with lltype.scoped_alloc(rffi.CCHARP.TO, bufSize) as buf:
                    ret = rwinreg.RegQueryValueW(hkey, c_subkey, buf,
                                                 bufsize_p)
                    if ret == rwinreg.ERROR_MORE_DATA:
                        print 'bufSize was %d, too small' % bufSize
                        # Resize and retry
                        bufSize *= 2
                        bufsize_p[0] = bufSize
                        continue

                    if ret != 0:
                        raiseWindowsError(space, ret, 'RegQueryValue')
                    length = intmask(bufsize_p[0] - 1) / 2
                    wide_buf = rffi.cast(rffi.CWCHARP, buf)
                    return space.newunicode(
                        rffi.wcharp2unicoden(wide_buf, length))
Ejemplo n.º 7
0
def convert_from_regdata(space, buf, buflen, typ):
    if typ == rwinreg.REG_DWORD:
        if not buflen:
            return space.newint(0)
        d = rffi.cast(rwin32.LPDWORD, buf)[0]
        return space.newint(d)

    elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ:
        if not buflen:
            s = u""
        else:
            # may or may not have a trailing NULL in the buffer.
            buf = rffi.cast(rffi.CWCHARP, buf)
            buflen /= 2
            if buf[buflen - 1] == '\x00':
                buflen -= 1
            s = rffi.wcharp2unicoden(buf, buflen)
        w_s = space.newunicode(s)
        return w_s

    elif typ == rwinreg.REG_MULTI_SZ:
        if not buflen:
            return space.newlist([])
        buf = rffi.cast(rffi.CWCHARP, buf)
        buflen /= 2
        i = 0
        l = []
        while i < buflen and buf[i]:
            s = []
            while i < buflen and buf[i] != u'\0':
                s.append(buf[i])
                i += 1
            if len(s) == 0:
                break
            s = u''.join(s)
            l.append(space.newunicode(s))
            i += 1
        return space.newlist(l)

    else:  # REG_BINARY and all other types
        return space.newbytes(rffi.charpsize2str(buf, buflen))