Exemple #1
0
def encode_object(space, w_object, encoding, errors):
    if encoding is None:
        # Get the encoder functions as a wrapped object.
        # This lookup is cached.
        w_encoder = space.sys.get_w_default_encoder()
    else:
        if errors is None or errors == 'strict':
            if encoding == 'ascii':
                u = space.unicode_w(w_object)
                eh = encode_error_handler(space)
                return space.wrap(unicode_encode_ascii(u, len(u), None,
                                                       errorhandler=eh))
            if encoding == 'utf-8':
                u = space.unicode_w(w_object)
                eh = encode_error_handler(space)
                return space.wrap(unicode_encode_utf_8(u, len(u), None,
                                                       errorhandler=eh))
        from pypy.module._codecs.interp_codecs import lookup_codec
        w_encoder = space.getitem(lookup_codec(space, encoding), space.wrap(0))
    if errors is None:
        w_errors = space.wrap('strict')
    else:
        w_errors = space.wrap(errors)
    w_restuple = space.call_function(w_encoder, w_object, w_errors)
    w_retval = space.getitem(w_restuple, space.wrap(0))
    if not space.is_true(space.isinstance(w_retval, space.w_str)):
        raise operationerrfmt(space.w_TypeError,
            "encoder did not return an string object (type '%s')",
            space.type(w_retval).getname(space))
    return w_retval
Exemple #2
0
def encode_object(space, w_object, encoding, errors):
    if encoding is None:
        # Get the encoder functions as a wrapped object.
        # This lookup is cached.
        w_encoder = space.sys.get_w_default_encoder()
    else:
        if errors is None or errors == 'strict':
            if encoding == 'ascii':
                u = space.unicode_w(w_object)
                eh = encode_error_handler(space)
                return space.wrap(unicode_encode_ascii(u, len(u), None,
                                                       errorhandler=eh))
            if encoding == 'utf-8':
                u = space.unicode_w(w_object)
                eh = encode_error_handler(space)
                return space.wrap(unicode_encode_utf_8(u, len(u), None,
                                                       errorhandler=eh))
        from pypy.module._codecs.interp_codecs import lookup_codec
        w_encoder = space.getitem(lookup_codec(space, encoding), space.wrap(0))
    if errors is None:
        w_errors = space.wrap('strict')
    else:
        w_errors = space.wrap(errors)
    w_restuple = space.call_function(w_encoder, w_object, w_errors)
    w_retval = space.getitem(w_restuple, space.wrap(0))
    if not space.isinstance_w(w_retval, space.w_str):
        raise operationerrfmt(space.w_TypeError,
            "encoder did not return an string object (type '%s')",
            space.type(w_retval).getname(space))
    return w_retval
Exemple #3
0
def unicode_encode_utf8(rope):
    from pypy.rlib.runicode import unicode_encode_utf_8

    if rope.is_ascii():
        return rope
    elif isinstance(rope, BinaryConcatNode):
        return BinaryConcatNode(unicode_encode_utf8(rope.left), unicode_encode_utf8(rope.right))
    elif isinstance(rope, LiteralUnicodeNode):
        return LiteralStringNode(unicode_encode_utf_8(rope.u, len(rope.u), "strict"))
    elif isinstance(rope, LiteralStringNode):
        return LiteralStringNode(_str_encode_utf_8(rope.s))
Exemple #4
0
def unicode_encode_utf8(rope):
    from pypy.rlib.runicode import unicode_encode_utf_8
    if rope.is_ascii():
        return rope
    elif isinstance(rope, BinaryConcatNode):
        return BinaryConcatNode(unicode_encode_utf8(rope.left),
                                unicode_encode_utf8(rope.right))
    elif isinstance(rope, LiteralUnicodeNode):
        return LiteralStringNode(
            unicode_encode_utf_8(rope.u, len(rope.u), "strict"))
    elif isinstance(rope, LiteralStringNode):
        return LiteralStringNode(_str_encode_utf_8(rope.s))
Exemple #5
0
        def f(x):

            s1 = "".join(["\xd7\x90\xd6\x96\xeb\x96\x95\xf0\x90\x91\x93"] * x)
            u, consumed = runicode.str_decode_utf_8(s1, len(s1), True)
            s2 = runicode.unicode_encode_utf_8(u, len(u), True)
            return s1 == s2
Exemple #6
0
 def as_bytes(self):
     from pypy.rlib.runicode import unicode_encode_utf_8
     return unicode_encode_utf_8(self.unistr, len(self.unistr),
                                 "strict")
Exemple #7
0
 def as_bytes(self):
     from pypy.rlib.runicode import unicode_encode_utf_8
     return unicode_encode_utf_8(self.unistr, len(self.unistr),
                                 "strict")
Exemple #8
0
        def f(x):

            s1 = "".join(["\xd7\x90\xd6\x96\xeb\x96\x95\xf0\x90\x91\x93"] * x)
            u, consumed = runicode.str_decode_utf_8(s1, len(s1), True)
            s2 = runicode.unicode_encode_utf_8(u, len(u), True)
            return s1 == s2