示例#1
0
def detect_floatformat():
    from rpython.rtyper.lltypesystem import rffi, lltype
    buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw')
    rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0
    packed = rffi.charpsize2str(buf, 8)
    if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05":
        double_format = 'IEEE, big-endian'
    elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43":
        double_format = 'IEEE, little-endian'
    else:
        double_format = 'unknown'
    lltype.free(buf, flavor='raw')
    #
    buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw')
    rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0)
    packed = rffi.charpsize2str(buf, 4)
    if packed == "\x4b\x7f\x01\x02":
        float_format = 'IEEE, big-endian'
    elif packed == "\x02\x01\x7f\x4b":
        float_format = 'IEEE, little-endian'
    else:
        float_format = 'unknown'
    lltype.free(buf, flavor='raw')

    return double_format, float_format
示例#2
0
def _decode_certificate(space, certificate, verbose=False):
    w_retval = space.newdict()

    w_peer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_subject_name(certificate))
    space.setitem(w_retval, space.wrap("subject"), w_peer)

    if verbose:
        w_issuer = _create_tuple_for_X509_NAME(
            space, libssl_X509_get_issuer_name(certificate))
        space.setitem(w_retval, space.wrap("issuer"), w_issuer)

        space.setitem(w_retval, space.wrap("version"),
                      space.wrap(libssl_X509_get_version(certificate)))

    biobuf = libssl_BIO_new(libssl_BIO_s_mem())
    try:

        if verbose:
            libssl_BIO_reset(biobuf)
            serialNumber = libssl_X509_get_serialNumber(certificate)
            libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
            # should not exceed 20 octets, 160 bits, so buf is big enough
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)

                w_serial = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("serialNumber"), w_serial)

            libssl_BIO_reset(biobuf)
            notBefore = libssl_X509_get_notBefore(certificate)
            libssl_ASN1_TIME_print(biobuf, notBefore)
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)
                w_date = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("notBefore"), w_date)

        libssl_BIO_reset(biobuf)
        notAfter = libssl_X509_get_notAfter(certificate)
        libssl_ASN1_TIME_print(biobuf, notAfter)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notAfter"), w_date)
    finally:
        libssl_BIO_free(biobuf)

    # Now look for subjectAltName
    w_alt_names = _get_peer_alt_names(space, certificate)
    if w_alt_names is not space.w_None:
        space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)

    return w_retval
示例#3
0
def _decode_certificate(space, certificate, verbose=False):
    w_retval = space.newdict()

    w_peer = _create_tuple_for_X509_NAME(
        space, libssl_X509_get_subject_name(certificate))
    space.setitem(w_retval, space.wrap("subject"), w_peer)

    if verbose:
        w_issuer = _create_tuple_for_X509_NAME(
            space, libssl_X509_get_issuer_name(certificate))
        space.setitem(w_retval, space.wrap("issuer"), w_issuer)

        space.setitem(w_retval, space.wrap("version"),
                      space.wrap(libssl_X509_get_version(certificate)))

    biobuf = libssl_BIO_new(libssl_BIO_s_mem())
    try:

        if verbose:
            libssl_BIO_reset(biobuf)
            serialNumber = libssl_X509_get_serialNumber(certificate)
            libssl_i2a_ASN1_INTEGER(biobuf, serialNumber)
            # should not exceed 20 octets, 160 bits, so buf is big enough
            with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
                length = libssl_BIO_gets(biobuf, buf, 99)
                if length < 0:
                    raise _ssl_seterror(space, None, length)

                w_serial = space.wrap(rffi.charpsize2str(buf, length))
            space.setitem(w_retval, space.wrap("serialNumber"), w_serial)

        libssl_BIO_reset(biobuf)
        notBefore = libssl_X509_get_notBefore(certificate)
        libssl_ASN1_TIME_print(biobuf, notBefore)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notBefore"), w_date)

        libssl_BIO_reset(biobuf)
        notAfter = libssl_X509_get_notAfter(certificate)
        libssl_ASN1_TIME_print(biobuf, notAfter)
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as buf:
            length = libssl_BIO_gets(biobuf, buf, 99)
            if length < 0:
                raise _ssl_seterror(space, None, length)
            w_date = space.wrap(rffi.charpsize2str(buf, length))
        space.setitem(w_retval, space.wrap("notAfter"), w_date)
    finally:
        libssl_BIO_free(biobuf)

    # Now look for subjectAltName
    w_alt_names = _get_peer_alt_names(space, certificate)
    if w_alt_names is not space.w_None:
        space.setitem(w_retval, space.wrap("subjectAltName"), w_alt_names)

    return w_retval
示例#4
0
def test_reset():
    db = Sqlite3DB(testdb)
    query = db.execute('select name from contacts;')
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    name = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    query.reset_query()
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    name2 = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    assert name == name2
示例#5
0
    def recv_bytes(self, space, maxlength=PY_SSIZE_T_MAX):
        self._check_readable(space)
        if maxlength < 0:
            raise oefmt(space.w_ValueError, "maxlength < 0")

        res, newbuf = self.do_recv_string(space, self.BUFFER_SIZE, maxlength)
        try:
            if newbuf:
                return space.newbytes(rffi.charpsize2str(newbuf, res))
            else:
                return space.newbytes(rffi.charpsize2str(self.buffer, res))
        finally:
            if newbuf:
                rffi.free_charp(newbuf)
示例#6
0
def test_count_avg_sum():
    db = Sqlite3DB(testdb)
    query = db.execute('select count(*), avg(age), sum(age) from contacts where 2 * age + 2 - age / 1 > 48;')
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    count = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    assert count == "53"
    textlen = query.column_bytes(1)
    avg = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(1)), textlen)
    assert avg == "72.5283018867924"
    textlen = query.column_bytes(2)
    sum = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(2)), textlen)
    assert sum == "3844"
示例#7
0
def _create_tuple_for_attribute(space, name, value):
    with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
        length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_name = space.wrap(rffi.charpsize2str(buf, length))

    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_value = space.wrap(rffi.charpsize2str(buf_ptr[0], length))
        w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))

    return space.newtuple([w_name, w_value])
示例#8
0
    def recv_bytes(self, space, maxlength=PY_SSIZE_T_MAX):
        self._check_readable(space)
        if maxlength < 0:
            raise oefmt(space.w_ValueError, "maxlength < 0")

        res, newbuf = self.do_recv_string(
            space, self.BUFFER_SIZE, maxlength)
        try:
            if newbuf:
                return space.newbytes(rffi.charpsize2str(newbuf, res))
            else:
                return space.newbytes(rffi.charpsize2str(self.buffer, res))
        finally:
            if newbuf:
                rffi.free_charp(newbuf)
示例#9
0
def _create_tuple_for_attribute(space, name, value):
    with lltype.scoped_alloc(rffi.CCHARP.TO, X509_NAME_MAXLEN) as buf:
        length = libssl_OBJ_obj2txt(buf, X509_NAME_MAXLEN, name, 0)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_name = space.wrap(rffi.charpsize2str(buf, length))

    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
        length = libssl_ASN1_STRING_to_UTF8(buf_ptr, value)
        if length < 0:
            raise _ssl_seterror(space, None, 0)
        w_value = space.wrap(rffi.charpsize2str(buf_ptr[0], length))
        w_value = space.call_method(w_value, "decode", space.wrap("utf-8"))

    return space.newtuple([w_name, w_value])
示例#10
0
文件: c_codecs.py 项目: sota/pypy-old
def encodeex(encodebuf,
             unicodedata,
             errors="strict",
             errorcb=None,
             namecb=None,
             ignore_error=0):
    inleft = len(unicodedata)
    with rffi.scoped_nonmoving_unicodebuffer(unicodedata) as inbuf:
        if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0:
            raise MemoryError
        if ignore_error == 0:
            flags = MBENC_FLUSH | MBENC_RESET
        else:
            flags = 0
        while True:
            r = pypy_cjk_enc_chunk(encodebuf, flags)
            if r == 0 or r == ignore_error:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb,
                                    unicodedata)
        while flags & MBENC_RESET:
            r = pypy_cjk_enc_reset(encodebuf)
            if r == 0:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb,
                                    unicodedata)
        src = pypy_cjk_enc_outbuf(encodebuf)
        length = pypy_cjk_enc_outlen(encodebuf)
        return rffi.charpsize2str(src, length)
示例#11
0
 def method_get_bytes(self, space, start, nbytes):
     return space.newstr_fromstr(
         rffi.charpsize2str(
             rffi.cast(rffi.CCHARP, rffi.ptradd(self.ptr, start)),
             nbytes
         )
     )
示例#12
0
    def peer_certificate(self, der=False):
        """peer_certificate([der=False]) -> certificate

        Returns the certificate for the peer.  If no certificate was provided,
        returns None.  If a certificate was provided, but not validated, returns
        an empty dictionary.  Otherwise returns a dict containing information
        about the peer certificate.

        If the optional argument is True, returns a DER-encoded copy of the
        peer certificate, or None if no certificate was provided.  This will
        return the certificate even if it wasn't validated."""
        if not self.peer_cert:
            return self.space.w_None

        if der:
            # return cert in DER-encoded format
            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
                buf_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
                length = libssl_i2d_X509(self.peer_cert, buf_ptr)
                if length < 0:
                    raise _ssl_seterror(self.space, self, length)
                try:
                    # this is actually an immutable bytes sequence
                    return self.space.wrap(rffi.charpsize2str(buf_ptr[0],
                                                              length))
                finally:
                    libssl_OPENSSL_free(buf_ptr[0])
        else:
            verification = libssl_SSL_CTX_get_verify_mode(
                libssl_SSL_get_SSL_CTX(self.ssl))
            if not verification & SSL_VERIFY_PEER:
                return self.space.newdict()
            else:
                return _decode_certificate(self.space, self.peer_cert)
示例#13
0
def encodeex(encodebuf, unicodedata, errors="strict", errorcb=None, namecb=None, ignore_error=0):
    inleft = len(unicodedata)
    inbuf = rffi.get_nonmoving_unicodebuffer(unicodedata)
    try:
        if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0:
            raise MemoryError
        if ignore_error == 0:
            flags = MBENC_FLUSH | MBENC_RESET
        else:
            flags = 0
        while True:
            r = pypy_cjk_enc_chunk(encodebuf, flags)
            if r == 0 or r == ignore_error:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb, unicodedata)
        while flags & MBENC_RESET:
            r = pypy_cjk_enc_reset(encodebuf)
            if r == 0:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb, unicodedata)
        src = pypy_cjk_enc_outbuf(encodebuf)
        length = pypy_cjk_enc_outlen(encodebuf)
        return rffi.charpsize2str(src, length)
    #
    finally:
        rffi.free_nonmoving_unicodebuffer(unicodedata, inbuf)
示例#14
0
 def __init__(self, space, cdata, ctype, w_callable, w_error, w_onerror):
     W_CData.__init__(self, space, cdata, ctype)
     #
     if not space.is_true(space.callable(w_callable)):
         raise oefmt(space.w_TypeError,
                     "expected a callable object, not %T", w_callable)
     self.w_callable = w_callable
     if not space.is_none(w_onerror):
         if not space.is_true(space.callable(w_onerror)):
             raise oefmt(space.w_TypeError,
                         "expected a callable object for 'onerror', not %T",
                         w_onerror)
         self.w_onerror = w_onerror
     #
     fresult = self.getfunctype().ctitem
     size = fresult.size
     if size < 0:
         size = 0
     elif fresult.is_primitive_integer and size < SIZE_OF_FFI_ARG:
         size = SIZE_OF_FFI_ARG
     with lltype.scoped_alloc(rffi.CCHARP.TO, size, zero=True) as ll_error:
         if not space.is_none(w_error):
             convert_from_object_fficallback(fresult, ll_error, w_error,
                                          self.decode_args_from_libffi)
         self.error_string = rffi.charpsize2str(ll_error, size)
     #
     # We must setup the GIL here, in case the callback is invoked in
     # some other non-Pythonic thread.  This is the same as cffi on
     # CPython, or ctypes.
     if space.config.translation.thread:
         from pypy.module.thread.os_thread import setup_threads
         setup_threads(space)
示例#15
0
def ioctl(space, w_fd, op, w_arg, mutate_flag=-1):
    """ioctl(fd, opt[, arg[, mutate_flag]])

    Perform the requested operation on file descriptor fd.  The operation is
    defined by opt and is operating system dependent.  Typically these codes
    are retrieved from the fcntl or termios library modules.
    """
    # removed the largish docstring because it is not in sync with the
    # documentation any more (even in CPython's docstring is out of date)

    # XXX this function's interface is a mess.
    # We try to emulate the behavior of Python >= 2.5 w.r.t. mutate_flag

    fd = space.c_filedescriptor_w(w_fd)
    op = rffi.cast(rffi.INT, op)        # C long => C int

    if mutate_flag != 0:
        try:
            rwbuffer = space.rwbuffer_w(w_arg)
        except OperationError, e:
            if not e.match(space, space.w_TypeError):
                raise
            if mutate_flag > 0:
                raise
        else:
            arg = rwbuffer.as_str()
            ll_arg = rffi.str2charp(arg)
            rv = ioctl_str(fd, op, ll_arg)
            arg = rffi.charpsize2str(ll_arg, len(arg))
            lltype.free(ll_arg, flavor='raw')
            if rv < 0:
                raise _get_error(space, "ioctl")
            rwbuffer.setslice(0, arg)
            return space.wrap(rv)
示例#16
0
def Array_to_str(vm):
    (self, ), _ = vm.decode_args("!", self_of=Array)
    assert isinstance(self, Array)

    data = rffi.charpsize2str(self.data, self.num_entries * self.type_size)
    objectmodel.keepalive_until_here(self)
    return Con_String(vm, data)
示例#17
0
    def peer_certificate(self, space, der=False):
        """peer_certificate([der=False]) -> certificate

        Returns the certificate for the peer.  If no certificate was
        provided, returns None.  If a certificate was provided, but not
        validated, returns an empty dictionary.  Otherwise returns a
        dict containing information about the peer certificate.

        If the optional argument is True, returns a DER-encoded copy of
        the peer certificate, or None if no certificate was provided.
        This will return the certificate even if it wasn't validated.
        """
        if not self.peer_cert:
            return space.w_None

        if der:
            # return cert in DER-encoded format
            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as buf_ptr:
                buf_ptr[0] = lltype.nullptr(rffi.CCHARP.TO)
                length = libssl_i2d_X509(self.peer_cert, buf_ptr)
                if length < 0:
                    raise _ssl_seterror(space, self, length)
                try:
                    # this is actually an immutable bytes sequence
                    return space.wrap(rffi.charpsize2str(buf_ptr[0], length))
                finally:
                    libssl_OPENSSL_free(buf_ptr[0])
        else:
            verification = libssl_SSL_CTX_get_verify_mode(
                libssl_SSL_get_SSL_CTX(self.ssl))
            if not verification & SSL_VERIFY_PEER:
                return space.newdict()
            else:
                return _decode_certificate(space, self.peer_cert)
示例#18
0
def enum_crls_w(space, store_name):
    """enum_crls(store_name) -> []

    Retrieve CRLs from Windows' cert store. store_name may be one of
    'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too.
    The function returns a list of (bytes, encoding_type) tuples. The
    encoding_type flag can be interpreted with X509_ASN_ENCODING or
    PKCS_7_ASN_ENCODING."""
    result_w = []

    pCrlCtx = lltype.nullptr(CRL_CONTEXT)
    hStore = CertOpenSystemStore(None, store_name)
    if not hStore:
        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
    try:
        while True:
            pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)
            if not pCrlCtx:
                break
            w_crl = space.newbytes(
                rffi.charpsize2str(pCrlCtx.c_pbCrlEncoded,
                                   intmask(pCrlCtx.c_cbCrlEncoded)))
            w_enc = w_certEncodingType(space, pCrlCtx.c_dwCertEncodingType)
            result_w.append(space.newtuple([w_crl, w_enc]))
    except:
        raise
    finally:
        if pCrlCtx:
            # loop ended with an error, need to clean up context manually
            CertFreeCRLContext(pCrlCtx)
        if not CertCloseStore(hStore, 0):
            # This error case might shadow another exception.
            raise wrap_windowserror(space, rwin32.lastSavedWindowsError())

    return space.newlist(result_w)
示例#19
0
文件: kernel.py 项目: sn6uv/rmathics
    def msg_recv(socket):
        result = []
        morep = rffi.lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
        morep[0] = rffi.r_int(1)

        more_sizep = rffi.lltype.malloc(rffi.UINTP.TO, 1, flavor='raw')
        more_sizep[0] = rffi.r_uint(rffi.sizeof(rffi.INT))

        while int(morep[0]):
            part = rffi.lltype.malloc(rzmq.msg_t.TO, flavor='raw')
            rc = rzmq.msg_init(part)
            assert rc == 0

            msg_size = rzmq.msg_recv(part, socket, 0)
            assert msg_size != -1

            result.append(rffi.charpsize2str(rzmq.msg_data(part), msg_size))

            rc = rzmq.getsockopt(socket, rzmq.RCVMORE, morep, more_sizep)
            assert rc == 0

            rc = rzmq.msg_close(part)
            assert rc == 0

        return result
示例#20
0
文件: rwin32.py 项目: charred/pypy
    def llimpl_FormatError(code):
        "Return a message corresponding to the given Windows error code."
        buf = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')

        try:
            msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                   FORMAT_MESSAGE_FROM_SYSTEM,
                                   None,
                                   rffi.cast(DWORD, code),
                                   DEFAULT_LANGUAGE,
                                   rffi.cast(rffi.CCHARP, buf),
                                   0, None)

            if msglen <= 2:   # includes the case msglen < 0
                return fake_FormatError(code)

            # FormatMessage always appends \r\n.
            buflen = intmask(msglen - 2)
            assert buflen > 0

            result = rffi.charpsize2str(buf[0], buflen)
            LocalFree(rffi.cast(rffi.VOIDP, buf[0]))
        finally:
            lltype.free(buf, flavor='raw')

        return result
示例#21
0
def enum_crls_w(space, store_name):
    """enum_crls(store_name) -> []

    Retrieve CRLs from Windows' cert store. store_name may be one of
    'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too.
    The function returns a list of (bytes, encoding_type) tuples. The
    encoding_type flag can be interpreted with X509_ASN_ENCODING or
    PKCS_7_ASN_ENCODING."""
    result_w = []

    pCrlCtx = lltype.nullptr(CRL_CONTEXT)
    hStore = CertOpenSystemStore(None, store_name)
    if not hStore:
        raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
    try:
        while True:
            pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx)
            if not pCrlCtx:
                break
            w_crl = space.wrapbytes(
                rffi.charpsize2str(pCrlCtx.c_pbCrlEncoded,
                                   intmask(pCrlCtx.c_cbCrlEncoded)))
            w_enc = w_certEncodingType(space, pCrlCtx.c_dwCertEncodingType)
            result_w.append(space.newtuple([w_crl, w_enc]))
    except:
        raise
    finally:
        if pCrlCtx:
            # loop ended with an error, need to clean up context manually
            CertFreeCRLContext(pCrlCtx)
        if not CertCloseStore(hStore, 0):
            # This error case might shadow another exception.
            raise wrap_windowserror(space, rwin32.lastSavedWindowsError())

    return space.newlist(result_w)
示例#22
0
 def _fetch_one_row(self, space):
     query = jit.promote(self.statement).query
     num_cols = query.data_count()
     jit.promote(num_cols)
     cols = [None] * num_cols
     for i in range(num_cols):
         tid = query.column_type(i)
         if tid == CConfig.SQLITE_TEXT or tid == CConfig.SQLITE_BLOB:
             textlen = query.column_bytes(i)
             result = rffi.charpsize2str(rffi.cast(rffi.CCHARP,
                                                   query.column_text(i)),
                                         textlen)
             w_result = space.wrap_string(result)  # no encoding
         elif tid == CConfig.SQLITE_INTEGER:
             result = query.column_int64(i)
             w_result = space.wrap_int(result)
         elif tid == CConfig.SQLITE_FLOAT:
             result = query.column_double(i)
             w_result = space.wrap_float(result)
         elif tid == CConfig.SQLITE_NULL:
             w_result = space.w_nil
         else:
             raise PrimitiveFailedError('read_row [tid: %s' % tid)
         cols[i] = w_result
     return cols
示例#23
0
def encodeex(encodebuf,
             utf8data,
             length,
             errors="strict",
             errorcb=None,
             namecb=None,
             ignore_error=0):
    inleft = length
    inbuf = rffi.utf82wcharp(utf8data, length)
    try:
        if pypy_cjk_enc_init(encodebuf, inbuf, inleft) < 0:
            raise MemoryError
        if ignore_error == 0:
            flags = MBENC_FLUSH | MBENC_RESET
        else:
            flags = 0
        while True:
            r = pypy_cjk_enc_chunk(encodebuf, flags)
            if r == 0 or r == ignore_error:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb,
                                    utf8data)
        while flags & MBENC_RESET:
            r = pypy_cjk_enc_reset(encodebuf)
            if r == 0:
                break
            multibytecodec_encerror(encodebuf, r, errors, errorcb, namecb,
                                    utf8data)
        src = pypy_cjk_enc_outbuf(encodebuf)
        length = pypy_cjk_enc_outlen(encodebuf)
        return rffi.charpsize2str(src, length)
    finally:
        lltype.free(inbuf, flavor='raw')
示例#24
0
    def recv_bytes_into(self, space, w_buffer, offset=0):
        rwbuffer = space.writebuf_w(w_buffer)
        length = rwbuffer.getlength()

        res, newbuf = self.do_recv_string(space, length - offset,
                                          PY_SSIZE_T_MAX)
        try:
            if newbuf:
                raise BufferTooShort(
                    space, space.wrapbytes(rffi.charpsize2str(newbuf, res)))
            rwbuffer.setslice(offset, rffi.charpsize2str(self.buffer, res))
        finally:
            if newbuf:
                rffi.free_charp(newbuf)

        return space.wrap(res)
示例#25
0
def File_read(vm):
    (self, rsize_o), _ = vm.decode_args(mand="!", opt="I", self_of=File)
    assert isinstance(self, File)
    _check_open(vm, self)

    flockfile(self.filep)
    fsize = os.fstat(fileno(self.filep)).st_size
    if rsize_o is None:
        rsize = fsize
    else:
        assert isinstance(rsize_o, Con_Int)
        rsize = rsize_o.v
        if rsize < 0:
            vm.raise_helper("File_Exception", \
              [Con_String(vm, "Can not read less than 0 bytes from file.")])
        elif rsize > fsize:
            rsize = fsize

    if objectmodel.we_are_translated():
        with lltype.scoped_alloc(rffi.CCHARP.TO, rsize) as buf:
            r = fread(buf, 1, rsize, self.filep)
            if r < rffi.r_size_t(rsize) and ferror(self.filep) != 0:
                vm.raise_helper("File_Exception",
                                [Con_String(vm, "Read error.")])
            s = rffi.charpsize2str(buf, rarithmetic.intmask(r))
    else:
        # rffi.charpsize2str is so slow (taking minutes for big strings) that it's worth bypassing
        # it when things are run untranslated.
        s = os.read(fileno(self.filep), rsize)
    funlockfile(self.filep)

    return Con_String(vm, s)
示例#26
0
def File_read(vm):
    (self, rsize_o),_ = vm.decode_args(mand="!", opt="I", self_of=File)
    assert isinstance(self, File)
    _check_open(vm, self)
    
    flockfile(self.filep)
    fsize = os.fstat(fileno(self.filep)).st_size
    if rsize_o is None:
        rsize = fsize
    else:
        assert isinstance(rsize_o, Con_Int)
        rsize = rsize_o.v
        if rsize < 0:
            vm.raise_helper("File_Exception", \
              [Con_String(vm, "Can not read less than 0 bytes from file.")])
        elif rsize > fsize:
            rsize = fsize

    if objectmodel.we_are_translated():
        with lltype.scoped_alloc(rffi.CCHARP.TO, rsize) as buf:
            r = fread(buf, 1, rsize, self.filep)
            if r < rffi.r_size_t(rsize) and ferror(self.filep) != 0:
                vm.raise_helper("File_Exception", [Con_String(vm, "Read error.")])
            s = rffi.charpsize2str(buf, rarithmetic.intmask(r))
    else:
        # rffi.charpsize2str is so slow (taking minutes for big strings) that it's worth bypassing
        # it when things are run untranslated.
        s = os.read(fileno(self.filep), rsize)
    funlockfile(self.filep)

    return Con_String(vm, s)
示例#27
0
def convert_from_regdata(space, buf, buflen, typ):
    if typ == rwinreg.REG_DWORD:
        if not buflen:
            return space.wrap(0)
        d = rffi.cast(rwin32.LPDWORD, buf)[0]
        return space.wrap(d)

    elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ:
        if not buflen:
            return space.wrap("")
        s = rffi.charp2strn(rffi.cast(rffi.CCHARP, buf), buflen)
        return space.wrap(s)

    elif typ == rwinreg.REG_MULTI_SZ:
        if not buflen:
            return space.newlist([])
        i = 0
        l = []
        while i < buflen and buf[i]:
            s = []
            while i < buflen and buf[i] != '\0':
                s.append(buf[i])
                i += 1
            if len(s) == 0:
                break
            s = ''.join(s)
            l.append(space.wrap(s))
            i += 1
        return space.newlist(l)

    else:  # REG_BINARY and all other types
        return space.wrap(rffi.charpsize2str(buf, buflen))
示例#28
0
 def from_memory(self, space, w_obj, w_pycppclass, offset):
     address = self._get_raw_address(space, w_obj, offset)
     charpptr = rffi.cast(rffi.CCHARP, address)
     strsize = self.size
     if charpptr[self.size - 1] == '\0':
         strsize = self.size - 1  # rffi will add \0 back
     return space.newbytes(rffi.charpsize2str(charpptr, strsize))
示例#29
0
    def msg_recv(socket):
        result = []
        morep = rffi.lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
        morep[0] = rffi.r_int(1)

        more_sizep = rffi.lltype.malloc(rffi.UINTP.TO, 1, flavor='raw')
        more_sizep[0] = rffi.r_uint(rffi.sizeof(rffi.INT))

        while int(morep[0]):
            part = rffi.lltype.malloc(rzmq.msg_t.TO, flavor='raw')
            rc = rzmq.msg_init(part)
            assert rc == 0

            msg_size = rzmq.msg_recv(part, socket, 0)
            assert msg_size != -1

            result.append(rffi.charpsize2str(rzmq.msg_data(part), msg_size))

            rc = rzmq.getsockopt(socket, rzmq.RCVMORE, morep, more_sizep)
            assert rc == 0

            rc = rzmq.msg_close(part)
            assert rc == 0

        return result
示例#30
0
    def recv_bytes_into(self, space, w_buffer, offset=0):
        rwbuffer = space.rwbuffer_w(w_buffer)
        length = rwbuffer.getlength()

        res, newbuf = self.do_recv_string(
            space, length - offset, PY_SSIZE_T_MAX)
        try:
            if newbuf:
                raise BufferTooShort(space, space.wrap(
                    rffi.charpsize2str(newbuf, res)))
            rwbuffer.setslice(offset, rffi.charpsize2str(self.buffer, res))
        finally:
            if newbuf:
                rffi.free_charp(newbuf)

        return space.wrap(res)
示例#31
0
    def llimpl_FormatError(code):
        "Return a message corresponding to the given Windows error code."
        buf = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
        buf[0] = lltype.nullptr(rffi.CCHARP.TO)
        try:
            msglen = FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
                | FORMAT_MESSAGE_IGNORE_INSERTS, None, rffi.cast(DWORD, code),
                DEFAULT_LANGUAGE, rffi.cast(rffi.CCHARP, buf), 0, None)
            buflen = intmask(msglen)

            # remove trailing cr/lf and dots
            s_buf = buf[0]
            while buflen > 0 and (s_buf[buflen - 1] <= ' '
                                  or s_buf[buflen - 1] == '.'):
                buflen -= 1

            if buflen <= 0:
                result = 'Windows Error %d' % (code, )
            else:
                result = rffi.charpsize2str(s_buf, buflen)
        finally:
            LocalFree(rffi.cast(rffi.VOIDP, buf[0]))
            lltype.free(buf, flavor='raw')

        return result
示例#32
0
文件: pointer.py 项目: mswart/topaz
 def method_get_bytes(self, space, offset, length):
     if offset + length > self.sizeof_memory:
         return space.error(space.w_IndexError,
                            "Address out of bounds of pointer")
     result = rffi.cast(rffi.CCHARP, self.ptr)
     result = rffi.charpsize2str(result, length)
     return space.newstr_fromstr(result)
示例#33
0
 def __init__(self, space, cdata, ctype, w_callable, w_error, w_onerror):
     W_CData.__init__(self, space, cdata, ctype)
     #
     if not space.is_true(space.callable(w_callable)):
         raise oefmt(space.w_TypeError,
                     "expected a callable object, not %T", w_callable)
     self.w_callable = w_callable
     if not space.is_none(w_onerror):
         if not space.is_true(space.callable(w_onerror)):
             raise oefmt(space.w_TypeError,
                         "expected a callable object for 'onerror', not %T",
                         w_onerror)
         self.w_onerror = w_onerror
     #
     fresult = self.getfunctype().ctitem
     size = fresult.size
     if size < 0:
         size = 0
     elif fresult.is_primitive_integer and size < SIZE_OF_FFI_ARG:
         size = SIZE_OF_FFI_ARG
     with lltype.scoped_alloc(rffi.CCHARP.TO, size, zero=True) as ll_error:
         if not space.is_none(w_error):
             convert_from_object_fficallback(fresult, ll_error, w_error,
                                          self.decode_args_from_libffi)
         self.error_string = rffi.charpsize2str(ll_error, size)
     #
     # We must setup the GIL here, in case the callback is invoked in
     # some other non-Pythonic thread.  This is the same as cffi on
     # CPython, or ctypes.
     if space.config.translation.thread:
         from pypy.module.thread.os_thread import setup_threads
         setup_threads(space)
示例#34
0
def convert_from_regdata(space, buf, buflen, typ):
    if typ == rwinreg.REG_DWORD:
        if not buflen:
            return space.wrap(0)
        d = rffi.cast(rwin32.LPDWORD, buf)[0]
        return space.wrap(d)

    elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ:
        if not buflen:
            return space.wrap("")
        s = rffi.charp2strn(rffi.cast(rffi.CCHARP, buf), buflen)
        return space.wrap(s)

    elif typ == rwinreg.REG_MULTI_SZ:
        if not buflen:
            return space.newlist([])
        i = 0
        l = []
        while i < buflen and buf[i]:
            s = []
            while i < buflen and buf[i] != '\0':
                s.append(buf[i])
                i += 1
            if len(s) == 0:
                break
            s = ''.join(s)
            l.append(space.wrap(s))
            i += 1
        return space.newlist(l)

    else: # REG_BINARY and all other types
        return space.wrap(rffi.charpsize2str(buf, buflen))
示例#35
0
def Array_to_str(vm):
    (self,),_ = vm.decode_args("!", self_of=Array)
    assert isinstance(self, Array)

    data = rffi.charpsize2str(self.data, self.num_entries * self.type_size)
    objectmodel.keepalive_until_here(self)
    return Con_String(vm, data)
示例#36
0
def Array_serialize(vm):
    (self,),_ = vm.decode_args("!", self_of=Array)
    assert isinstance(self, Array)

    data = rffi.charpsize2str(self.data, self.num_entries * self.type_size)
    objectmodel.keepalive_until_here(self) # XXX I don't really understand why this is needed
    return Con_String(vm, data)
示例#37
0
文件: rwin32.py 项目: bukzor/pypy
    def llimpl_FormatError(code):
        "Return a message corresponding to the given Windows error code."
        buf = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
        buf[0] = lltype.nullptr(rffi.CCHARP.TO)
        try:
            msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                   FORMAT_MESSAGE_FROM_SYSTEM | 
                                   FORMAT_MESSAGE_IGNORE_INSERTS,
                                   None,
                                   rffi.cast(DWORD, code),
                                   DEFAULT_LANGUAGE,
                                   rffi.cast(rffi.CCHARP, buf),
                                   0, None)
            buflen = intmask(msglen)

            # remove trailing cr/lf and dots
            s_buf = buf[0]
            while buflen > 0 and (s_buf[buflen - 1] <= ' ' or
                                  s_buf[buflen - 1] == '.'):
                buflen -= 1

            if buflen <= 0:
                result = fake_FormatError(code)
            else:
                result = rffi.charpsize2str(s_buf, buflen)
        finally:
            LocalFree(rffi.cast(rffi.VOIDP, buf[0]))
            lltype.free(buf, flavor='raw')

        return result
示例#38
0
def test_query_17():
    db = Sqlite3DB(tpchdb)
    queryStr = ("select "
                    "sum(l.extendedprice) / 7.0 as avg_yearly "
                "from "
                    "lineitem l, "
                    "part p "
                "where "
                    "p.partkey = l.partkey "
                    "and p.brand = 'Brand#45' "
                    "and p.container = 'SM PKG' "
                    "and l.quantity < ( "
                        "select "
                            "0.2 * avg(quantity) "
                        "from "
                            "lineitem "
                        "where "
                            "partkey = p.partkey "
                    ");"
        )
    query = db.execute(queryStr)
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.python_sqlite3_column_bytes(0)
    print textlen
    result = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.python_sqlite3_column_text(0)), textlen)
    print result
    assert float(result) == 3655.76571428571
示例#39
0
def PyUnicode_FromKindAndData(space, kind, data, size):
    if size < 0:
        raise oefmt(space.w_ValueError, "size must be positive")
    data = cts.cast('char *', data)
    kind = widen(kind)
    if kind == _1BYTE_KIND:
        value = rffi.charpsize2str(data, size)
        w_res = latin_1_decode(space, value, w_final=space.w_False)
    elif kind == _2BYTE_KIND:
        value = rffi.charpsize2str(data, 2 * size)
        w_res = utf_16_decode(space, value, w_final=space.w_False)
    elif kind == _4BYTE_KIND:
        value = rffi.charpsize2str(data, 4 * size)
        w_res = utf_32_decode(space, value, w_final=space.w_False)
    else:
        raise oefmt(space.w_SystemError, "invalid kind")
    return space.unpackiterable(w_res)[0]
示例#40
0
 def getslice(self, start, stop, step, size):
     if step == 1:
         data = self.array._charbuf_start()
         try:
             return rffi.charpsize2str(rffi.ptradd(data, start), size)
         finally:
             self.array._charbuf_stop()
     return Buffer.getslice(self, start, stop, step, size)
示例#41
0
def PyUnicode_Decode(space, s, size, encoding, errors):
    """Create a Unicode object by decoding size bytes of the encoded string s.
    encoding and errors have the same meaning as the parameters of the same name
    in the unicode() built-in function.  The codec to be used is looked up
    using the Python codec registry.  Return NULL if an exception was raised by
    the codec."""
    return _pyunicode_decode(space, rffi.charpsize2str(s, size), encoding,
                             errors)
示例#42
0
def c_stdstring2charp(space, cppstr):
    sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
    try:
        cstr = _c_stdstring2charp(cppstr, sz)
        cstr_len = intmask(sz[0])
    finally:
        lltype.free(sz, flavor='raw')
    return rffi.charpsize2str(cstr, cstr_len)
示例#43
0
def test_makerecord():
    db = Sqlite3DB(testdb)
    query = db.execute("select age, name from contacts order by age, age * 0.5;")
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(1)
    name = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(1)), textlen)
    assert name == "Jermaine Mayo"
示例#44
0
def test_disable_cache():
    db = Sqlite3DB(testdb)
    query = db.execute('select count(name) from contacts where age > 20;', use_flag_cache=False)
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    count = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    assert int(count) == 76
示例#45
0
def test_comparison():
    db = Sqlite3DB(testdb)
    query = db.execute('select count(*) from contacts where age > 40 and age < 60;')
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    count = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    assert int(count) == 18
示例#46
0
def Array_serialize(vm):
    (self, ), _ = vm.decode_args("!", self_of=Array)
    assert isinstance(self, Array)

    data = rffi.charpsize2str(self.data, self.num_entries * self.type_size)
    objectmodel.keepalive_until_here(
        self)  # XXX I don't really understand why this is needed
    return Con_String(vm, data)
示例#47
0
def c_stdstring2charp(space, cppstr):
    sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
    try:
        cstr = _c_stdstring2charp(cppstr, sz)
        cstr_len = intmask(sz[0])
    finally:
        lltype.free(sz, flavor='raw')
    return rffi.charpsize2str(cstr, cstr_len)
示例#48
0
def mk_mod(vm, bc, mod_off):
    mod_size = read_word(bc, mod_off + BC_MOD_SIZE)
    assert mod_off >= 0 and mod_size >= 0

    mod_bc = rffi.ptradd(bc, mod_off)

    name = _extract_sstr(mod_bc, BC_MOD_NAME, BC_MOD_NAME_SIZE)
    id_ = _extract_sstr(mod_bc, BC_MOD_ID, BC_MOD_ID_SIZE)
    src_path = _extract_sstr(mod_bc, BC_MOD_SRC_PATH, BC_MOD_SRC_PATH_SIZE)

    imps = []
    j = read_word(mod_bc, BC_MOD_IMPORTS)
    for k in range(read_word(mod_bc, BC_MOD_NUM_IMPORTS)):
        assert j > 0
        imp_size = read_word(mod_bc, j)
        assert imp_size > 0
        j += INTSIZE
        imps.append(rffi.charpsize2str(rffi.ptradd(mod_bc, j), imp_size))
        j += align(imp_size)
        j += INTSIZE + align(read_word(mod_bc, j))

    num_vars = read_word(mod_bc, BC_MOD_NUM_TL_VARS_MAP)
    tlvars_map = {}
    j = read_word(mod_bc, BC_MOD_TL_VARS_MAP)
    for k in range(num_vars):
        assert j > 0
        var_num = read_word(mod_bc, j)
        j += INTSIZE
        tlvar_size = read_word(mod_bc, j)
        assert tlvar_size > 0
        j += INTSIZE
        n = rffi.charpsize2str(rffi.ptradd(mod_bc, j), tlvar_size)
        tlvars_map[n] = var_num
        j += align(tlvar_size)

    num_consts = read_word(mod_bc, BC_MOD_NUM_CONSTANTS)

    mod = Builtins.new_bc_con_module(vm, mod_bc, name, id_, src_path, imps,
                                     tlvars_map, num_consts)
    init_func_off = read_word(mod_bc, BC_MOD_INSTRUCTIONS)
    pc = BC_PC(mod, init_func_off)
    max_stack_size = 512  # XXX!
    mod.init_func = Builtins.Con_Func(vm, Builtins.Con_String(vm, "$$init$$"), False, pc, \
      max_stack_size, 0, num_vars, mod, None)

    return mod
示例#49
0
def test_string_comparison():
    db = Sqlite3DB(testdb)
    query = db.execute("select count(*) from contacts where name = 'Raphael Paul';")
    rc = query.mainloop()
    assert rc == CConfig.SQLITE_ROW
    textlen = query.column_bytes(0)
    count = rffi.charpsize2str(rffi.cast(rffi.CCHARP, query.column_text(0)), textlen)
    assert int(count) == 1    
示例#50
0
def mk_mod(vm, bc, mod_off):
    mod_size = read_word(bc, mod_off + BC_MOD_SIZE)
    assert mod_off >= 0 and mod_size >= 0

    mod_bc = rffi.ptradd(bc, mod_off)

    name = _extract_sstr(mod_bc, BC_MOD_NAME, BC_MOD_NAME_SIZE)
    id_ = _extract_sstr(mod_bc, BC_MOD_ID, BC_MOD_ID_SIZE)
    src_path = _extract_sstr(mod_bc, BC_MOD_SRC_PATH, BC_MOD_SRC_PATH_SIZE)

    imps = []
    j = read_word(mod_bc, BC_MOD_IMPORTS)
    for k in range(read_word(mod_bc, BC_MOD_NUM_IMPORTS)):
        assert j > 0
        imp_size = read_word(mod_bc, j)
        assert imp_size > 0
        j += INTSIZE
        imps.append(rffi.charpsize2str(rffi.ptradd(mod_bc, j), imp_size))
        j += align(imp_size)
        j += INTSIZE + align(read_word(mod_bc, j))

    num_vars = read_word(mod_bc, BC_MOD_NUM_TL_VARS_MAP)
    tlvars_map = {}
    j = read_word(mod_bc, BC_MOD_TL_VARS_MAP)
    for k in range(num_vars):
        assert j > 0
        var_num = read_word(mod_bc, j)
        j += INTSIZE
        tlvar_size = read_word(mod_bc, j)
        assert tlvar_size > 0
        j += INTSIZE
        n = rffi.charpsize2str(rffi.ptradd(mod_bc, j), tlvar_size)
        tlvars_map[n] = var_num
        j += align(tlvar_size)

    num_consts = read_word(mod_bc, BC_MOD_NUM_CONSTANTS)

    mod = Builtins.new_bc_con_module(vm, mod_bc, name, id_, src_path, imps, tlvars_map, num_consts)
    init_func_off = read_word(mod_bc, BC_MOD_INSTRUCTIONS)
    pc = BC_PC(mod, init_func_off)
    max_stack_size = 512 # XXX!
    mod.init_func = Builtins.Con_Func(vm, Builtins.Con_String(vm, "$$init$$"), False, pc, \
      max_stack_size, 0, num_vars, mod, None)
    
    return mod
示例#51
0
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
             ropenssl.EVP_DigestFinal(ctx, digest, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return rffi.charpsize2str(digest, digest_size)
示例#52
0
文件: converter.py 项目: xen0n/pypy
 def from_memory(self, space, w_obj, offset):
     address = self._get_raw_address(space, w_obj, offset)
     charpptr = rffi.cast(rffi.CCHARP, address)
     if 0 <= self.size and self.size != 2**31-1:   # cling's code for "unknown" (?)
         strsize = self.size
         if charpptr[self.size-1] == '\0':
             strsize = self.size-1  # rffi will add \0 back
         return space.newtext(rffi.charpsize2str(charpptr, strsize))
     return space.newtext(rffi.charp2str(charpptr))
示例#53
0
 def getslice(self, start, step, size):
     if size == 0:
         return ''
     assert step == 1
     data = self.w_array._charbuf_start()
     try:
         return rffi.charpsize2str(rffi.ptradd(data, start), size)
     finally:
         self.w_array._charbuf_stop()
def PyByteArray_FromStringAndSize(space, char_p, length):
    """Create a new bytearray object from string and its length, len.  On
    failure, NULL is returned."""
    if char_p:
        w_s = space.newbytes(rffi.charpsize2str(char_p, length))
    else:
        w_s = space.newint(length)
    w_buffer = space.call_function(space.w_bytearray, w_s)
    return make_ref(space, w_buffer)
示例#55
0
def c_stdstring2charp(space, cppstr):
    sz = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw')
    try:
        w_cstr = call_capi(space, 'stdstring2charp',
            [_ArgH(cppstr), _ArgP(rffi.cast(rffi.VOIDP, sz))])
        cstr_len = int(intmask(sz[0]))
    finally:
        lltype.free(sz, flavor='raw')
    return rffi.charpsize2str(_cdata_to_ccharp(space, w_cstr), cstr_len)
示例#56
0
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with lltype.scoped_alloc(rffi.CCHARP.TO, digest_size) as digest:
             ropenssl.EVP_DigestFinal(ctx, digest, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return rffi.charpsize2str(digest, digest_size)
示例#57
0
def PyUnicode_DecodeUTF32(space, s, size, llerrors, pbyteorder):
    """Decode length bytes from a UTF-32 encoded buffer string and
    return the corresponding Unicode object.  errors (if non-NULL)
    defines the error handling. It defaults to "strict".

    If byteorder is non-NULL, the decoder starts decoding using the
    given byte order:
    *byteorder == -1: little endian
    *byteorder == 0:  native order
    *byteorder == 1:  big endian

    If *byteorder is zero, and the first four bytes of the input data
    are a byte order mark (BOM), the decoder switches to this byte
    order and the BOM is not copied into the resulting Unicode string.
    If *byteorder is -1 or 1, any byte order mark is copied to the
    output.

    After completion, *byteorder is set to the current byte order at
    the end of input data.

    In a narrow build codepoints outside the BMP will be decoded as
    surrogate pairs.

    If byteorder is NULL, the codec starts in native order mode.

    Return NULL if an exception was raised by the codec.
    """
    string = rffi.charpsize2str(s, size)

    if pbyteorder:
        llbyteorder = rffi.cast(lltype.Signed, pbyteorder[0])
        if llbyteorder < 0:
            byteorder = "little"
        elif llbyteorder > 0:
            byteorder = "big"
        else:
            byteorder = "native"
    else:
        byteorder = "native"

    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = None

    result, length, byteorder = runicode.str_decode_utf_32_helper(
        string,
        size,
        errors,
        True,  # final ? false for multiple passes?
        None,  # errorhandler
        byteorder)
    if pbyteorder is not None:
        pbyteorder[0] = rffi.cast(rffi.INT, byteorder)

    return space.newunicode(result)
示例#58
0
def string_realize(space, py_obj):
    """
    Creates the string in the interpreter. The PyStringObject buffer must not
    be modified after this call.
    """
    py_str = rffi.cast(PyStringObject, py_obj)
    s = rffi.charpsize2str(py_str.c_buffer, py_str.c_size)
    w_obj = space.wrap(s)
    track_reference(space, py_obj, w_obj)
    return w_obj
示例#59
0
 def getslice(self, start, stop, step, size):
     if size == 0:
         return ''
     if step == 1:
         data = self.array._charbuf_start()
         try:
             return rffi.charpsize2str(rffi.ptradd(data, start), size)
         finally:
             self.array._charbuf_stop()
     return Buffer.getslice(self, start, stop, step, size)