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 = unicodehelper.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 = unicodehelper.encode_error_handler(space) return space.wrap(unicode_encode_utf_8( u, len(u), None, errorhandler=eh, allow_surrogates=True)) 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 oefmt(space.w_TypeError, "encoder did not return an string object (type '%T')", w_retval) return w_retval
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 = unicodehelper.encode_error_handler(space) return space.newbytes( unicode_encode_ascii(u, len(u), None, errorhandler=eh)) if encoding == 'utf-8': u = space.unicode_w(w_object) eh = unicodehelper.encode_error_handler(space) return space.newbytes( unicode_encode_utf_8(u, len(u), None, errorhandler=eh, allow_surrogates=True)) from pypy.module._codecs.interp_codecs import lookup_codec w_encoder = space.getitem(lookup_codec(space, encoding), space.newint(0)) if errors is None: w_errors = space.newtext('strict') else: w_errors = space.newtext(errors) w_restuple = space.call_function(w_encoder, w_object, w_errors) w_retval = space.getitem(w_restuple, space.newint(0)) if not space.isinstance_w(w_retval, space.w_bytes): raise oefmt(space.w_TypeError, "encoder did not return an string object (type '%T')", w_retval) return w_retval
def encode_object(space, w_object, encoding, errors): if errors is None or errors == 'strict': if encoding is None or encoding == 'utf-8': u = space.unicode_w(w_object) eh = unicodehelper.encode_error_handler(space) return space.newbytes( unicode_encode_utf_8(u, len(u), errors, errorhandler=eh)) elif encoding == 'ascii': u = space.unicode_w(w_object) eh = unicodehelper.encode_error_handler(space) return space.newbytes( unicode_encode_ascii(u, len(u), errors, errorhandler=eh)) from pypy.module._codecs.interp_codecs import encode_text if encoding is None: encoding = space.sys.defaultencoding w_retval = encode_text(space, w_object, encoding, errors) if not space.isinstance_w(w_retval, space.w_bytes): raise oefmt( space.w_TypeError, "'%s' encoder returned '%T' instead of 'bytes'; " "use codecs.encode() to encode to arbitrary types", encoding, w_retval) return w_retval