Exemplo n.º 1
0
W_UnicodeObject.EMPTY = W_UnicodeObject(u'')

# Helper for converting int/long this is called only from
# {int,long,float}type.descr__new__: in the default branch this is implemented
# using the same logic as PyUnicode_EncodeDecimal, as CPython 2.7 does.
#
# In CPython3 the call to PyUnicode_EncodeDecimal has been replaced to a call
# to PyUnicode_TransformDecimalToASCII, which is much simpler. Here, we do the
# equivalent plus the final step of encoding the result to utf-8.
def unicode_to_decimal_w(space, w_unistr):
    if not isinstance(w_unistr, W_UnicodeObject):
        raise oefmt(space.w_TypeError, "expected unicode, got '%T'", w_unistr)
    unistr = w_unistr._value
    result = [u'\0'] * len(unistr)
    for i in xrange(len(unistr)):
        uchr = ord(unistr[i])
        if uchr > 127:
            if unicodedb.isspace(uchr):
                result[i] = ' '
                continue
            try:
                uchr = ord(u'0') + unicodedb.decimal(uchr)
            except KeyError:
                pass
        result[i] = unichr(uchr)
    return unicodehelper.encode_utf8(space, u''.join(result))


_repr_function, _ = make_unicode_escape_function(
    pass_printable=True, unicode_output=True, quotes=True, prefix='')
Exemplo n.º 2
0
        raise oefmt(space.w_TypeError, "expected unicode, got '%T'", w_unistr)
    unistr = w_unistr._value
    result = ['\0'] * len(unistr)
    digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    for i in xrange(len(unistr)):
        uchr = ord(unistr[i])
        if unicodedb.isspace(uchr):
            result[i] = ' '
            continue
        try:
            result[i] = digits[unicodedb.decimal(uchr)]
        except KeyError:
            if 0 < uchr < 256:
                result[i] = chr(uchr)
            else:
                w_encoding = space.newtext('decimal')
                w_start = space.newint(i)
                w_end = space.newint(i + 1)
                w_reason = space.newtext('invalid decimal Unicode string')
                raise OperationError(
                    space.w_UnicodeEncodeError,
                    space.newtuple(
                        [w_encoding, w_unistr, w_start, w_end, w_reason]))
    return ''.join(result)


_repr_function, _ = make_unicode_escape_function(pass_printable=False,
                                                 unicode_output=False,
                                                 quotes=True,
                                                 prefix='u')