コード例 #1
0
def wcharp2rawunicode(space, address, maxlength=-1):
    if maxlength == -1:
        return wcharp2unicode(space, address)
    elif maxlength < 0:
        maxlength = 0
    s = wcharpsize2utf8(space, rffi.cast(rffi.CWCHARP, address), maxlength)
    return space.newutf8(s, maxlength)
コード例 #2
0
def PyUnicode_EncodeDecimal(space, s, length, output, llerrors):
    """Takes a Unicode string holding a decimal value and writes it
    into an output buffer using standard ASCII digit codes.

    The output buffer has to provide at least length+1 bytes of
    storage area. The output string is 0-terminated.

    The encoder converts whitespace to ' ', decimal characters to
    their corresponding ASCII digit and all other Latin-1 characters
    except \0 as-is. Characters outside this range (Unicode ordinals
    1-256) are treated as errors. This includes embedded NULL bytes.

    Returns 0 on success, -1 on failure.
    """
    u = wcharpsize2utf8(space, s, length)
    if llerrors:
        errors = rffi.charp2str(llerrors)
    else:
        errors = None
    state = space.fromcache(CodecState)
    result = unicode_encode_decimal(u, errors, state.encode_error_handler)
    i = len(result)
    output[i] = '\0'
    i -= 1
    while i >= 0:
        output[i] = result[i]
        i -= 1
    return 0
コード例 #3
0
ファイル: longobject.py プロジェクト: sczfaker/pypy
def PyLong_FromUnicode(space, u, length, base):
    """Convert a sequence of Unicode digits to a Python long integer value.
    The first parameter, u, points to the first character of the Unicode
    string, length gives the number of characters, and base is the radix
    for the conversion.  The radix must be in the range [2, 36]; if it is
    out of range, ValueError will be raised."""
    w_value = space.newutf8(wcharpsize2utf8(space, u, length), length)
    w_base = space.newint(rffi.cast(lltype.Signed, base))
    return space.call_function(space.w_long, w_value, w_base)
コード例 #4
0
def PyLong_FromUnicode(space, u, length, base):
    """Convert a sequence of Unicode digits to a Python long integer value.
    The first parameter, u, points to the first character of the Unicode
    string, length gives the number of characters, and base is the radix
    for the conversion.  The radix must be in the range [2, 36]; if it is
    out of range, ValueError will be raised."""
    if length < 0:
        length = 0
    w_value = space.newutf8(wcharpsize2utf8(space, u, length), length)
    return PyLong_FromUnicodeObject(space, w_value, base)
コード例 #5
0
 def PyUnicode_EncodeXXX(space, s, size, errors):
     """Encode the Py_UNICODE buffer of the given size and return a
     Python string object.  Return NULL if an exception was raised
     by the codec."""
     u = wcharpsize2utf8(space, s, size)
     w_u = space.newutf8(u, size)
     if errors:
         w_errors = space.newtext(rffi.charp2str(errors))
     else:
         w_errors = None
     return space.call_method(w_u, 'encode', space.newtext(encoding), w_errors)
コード例 #6
0
def PyUnicode_FromUnicode(space, wchar_p, length):
    """Create a Unicode Object from the Py_UNICODE buffer u of the given size. u
    may be NULL which causes the contents to be undefined. It is the user's
    responsibility to fill in the needed data.  The buffer is copied into the new
    object. If the buffer is not NULL, the return value might be a shared object.
    Therefore, modification of the resulting Unicode object is only allowed when u
    is NULL."""
    if wchar_p:
        s = wcharpsize2utf8(space, wchar_p, length)
        return make_ref(space, space.newutf8(s, length))
    else:
        return rffi.cast(PyObject, new_empty_unicode(space, length))
コード例 #7
0
def unicode_realize(space, py_obj):
    """
    Creates the unicode in the interpreter. The PyUnicodeObject buffer must not
    be modified after this call.
    """
    py_uni = rffi.cast(PyUnicodeObject, py_obj)
    length = py_uni.c_length
    s = wcharpsize2utf8(space, py_uni.c_str, length)
    w_type = from_ref(space, rffi.cast(PyObject, py_obj.c_ob_type))
    w_obj = space.allocate_instance(unicodeobject.W_UnicodeObject, w_type)
    w_obj.__init__(s, length)
    py_uni.c_hash = space.hash_w(space.newutf8(s, length))
    track_reference(space, py_obj, w_obj)
    return w_obj
コード例 #8
0
def HPyUnicode_FromWideChar(space, ctx, wchar_p, size):
    # remove the "const", else we can't call wcharpsize2utf8 later
    wchar_p = rffi.cast(rffi.CWCHARP, wchar_p)
    if wchar_p:
        if size == -1:
            size = wcharplen(wchar_p)
        # WRITE TEST: this automatically raises "character not in range", but
        # we don't have any test for it
        s = wcharpsize2utf8(space, wchar_p, size)
        w_obj = space.newutf8(s, size)
        return handles.new(space, w_obj)
    else:
        # cpyext returns an empty string, we need a test
        raise NotImplementedError("WRITE TEST")
コード例 #9
0
    def descr_tounicode(self, space):
        """ tounicode() -> unicode

        Convert the array to a unicode string.  The array must be
        a type 'u' array; otherwise a ValueError is raised.  Use
        array.tostring().decode() to obtain a unicode string from
        an array of some other type.
        """
        if self.typecode == 'u':
            buf = rffi.cast(UNICODE_ARRAY, self._buffer_as_unsigned())
            utf8 = wcharpsize2utf8(space, buf, self.len)
            return space.newutf8(utf8, self.len)
        else:
            raise oefmt(space.w_ValueError,
                        "tounicode() may only be called on type 'u' arrays")