def encode_unicode(space, w_unistr, encoding, errors): from pypy.objspace.std.unicodetype import getdefaultencoding, \ _get_encoding_and_errors, encode_object from pypy.objspace.std.ropeobject import W_RopeObject if errors is None or errors == "strict": node = w_unistr._node if encoding == 'ascii': result = rope.unicode_encode_ascii(node) if result is not None: return W_RopeObject(result) elif encoding == 'latin-1': result = rope.unicode_encode_latin1(node) if result is not None: return W_RopeObject(result) elif encoding == "utf-8": result = rope.unicode_encode_utf8(node) if result is not None: return W_RopeObject(result) return encode_object(space, w_unistr, encoding, errors)