MCPcopy Index your code
hub / github.com/RustPython/RustPython / handle_encode_error

Method handle_encode_error

crates/vm/src/codecs.rs:495–521  ·  view source on GitHub ↗
(
        &self,
        ctx: &mut PyEncodeContext<'a>,
        range: Range<StrSize>,
        reason: Option<&str>,
    )

Source from the content-addressed store, hash-verified

493
494impl<'a> EncodeErrorHandler<PyEncodeContext<'a>> for SurrogatePass {
495 fn handle_encode_error(
496 &self,
497 ctx: &mut PyEncodeContext<'a>,
498 range: Range<StrSize>,
499 reason: Option<&str>,
500 ) -> PyResult<(EncodeReplace<PyEncodeContext<'a>>, StrSize)> {
501 let standard_encoding = StandardEncoding::parse(ctx.encoding)
502 .ok_or_else(|| ctx.error_encoding(range.clone(), reason))?;
503 let err_str = &ctx.full_data()[range.start.bytes..range.end.bytes];
504 let num_chars = range.end.chars - range.start.chars;
505 let mut out: Vec<u8> = Vec::with_capacity(num_chars * 4);
506 for ch in err_str.code_points() {
507 let c = ch.to_u32();
508 let 0xd800..=0xdfff = c else {
509 // Not a surrogate, fail with original exception
510 return Err(ctx.error_encoding(range, reason));
511 };
512 match standard_encoding {
513 StandardEncoding::Utf8 => out.extend(ch.encode_wtf8(&mut [0; 4]).as_bytes()),
514 StandardEncoding::Utf16Le => out.extend((c as u16).to_le_bytes()),
515 StandardEncoding::Utf16Be => out.extend((c as u16).to_be_bytes()),
516 StandardEncoding::Utf32Le => out.extend(c.to_le_bytes()),
517 StandardEncoding::Utf32Be => out.extend(c.to_be_bytes()),
518 }
519 }
520 Ok((EncodeReplace::Bytes(ctx.bytes(out)), range.end))
521 }
522}
523
524impl<'a> DecodeErrorHandler<PyDecodeContext<'a>> for SurrogatePass {

Callers 2

call_native_encode_errorFunction · 0.45

Calls 15

BytesClass · 0.85
ok_or_elseMethod · 0.80
error_encodingMethod · 0.80
full_dataMethod · 0.80
code_pointsMethod · 0.80
encode_wtf8Method · 0.80
code_point_indicesMethod · 0.80
parseFunction · 0.50
ErrClass · 0.50
cloneMethod · 0.45
to_u32Method · 0.45
extendMethod · 0.45

Tested by

no test coverage detected