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

Function call_native_encode_error

crates/vm/src/codecs.rs:1001–1039  ·  view source on GitHub ↗
(
    handler: E,
    err: PyObjectRef,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

999}
1000
1001fn call_native_encode_error<E>(
1002 handler: E,
1003 err: PyObjectRef,
1004 vm: &VirtualMachine,
1005) -> PyResult<(PyObjectRef, usize)>
1006where
1007 for<'a> E: EncodeErrorHandler<PyEncodeContext<'a>>,
1008{
1009 // let err = err.
1010 let range = extract_unicode_error_range(&err, vm)?;
1011 let s = PyStrRef::try_from_object(vm, err.get_attr("object", vm)?)?;
1012 let s_encoding = PyUtf8StrRef::try_from_object(vm, err.get_attr("encoding", vm)?)?;
1013 let mut ctx = PyEncodeContext {
1014 vm,
1015 encoding: s_encoding.as_str(),
1016 data: &s,
1017 pos: StrSize::default(),
1018 exception: OnceCell::from(err.downcast().unwrap()),
1019 };
1020 let mut iter = s.as_wtf8().code_point_indices();
1021 let start = StrSize {
1022 chars: range.start,
1023 bytes: iter.nth(range.start).unwrap().0,
1024 };
1025 let end = StrSize {
1026 chars: range.end,
1027 bytes: if let Some(n) = range.len().checked_sub(1) {
1028 iter.nth(n).map_or(s.byte_len(), |(i, _)| i)
1029 } else {
1030 start.bytes
1031 },
1032 };
1033 let (replace, restart) = handler.handle_encode_error(&mut ctx, start..end, None)?;
1034 let replace = match replace {
1035 EncodeReplace::Str(s) => s.into(),
1036 EncodeReplace::Bytes(b) => b.into(),
1037 };
1038 Ok((replace, restart.chars))
1039}
1040
1041fn call_native_decode_error<E>(
1042 handler: E,

Callers 6

replace_errorsFunction · 0.85
xmlcharrefreplace_errorsFunction · 0.85
backslashreplace_errorsFunction · 0.85
namereplace_errorsFunction · 0.85
surrogatepass_errorsFunction · 0.85
surrogateescape_errorsFunction · 0.85

Calls 10

downcastMethod · 0.80
code_point_indicesMethod · 0.80
get_attrMethod · 0.45
as_strMethod · 0.45
unwrapMethod · 0.45
as_wtf8Method · 0.45
lenMethod · 0.45
byte_lenMethod · 0.45
handle_encode_errorMethod · 0.45

Tested by

no test coverage detected