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

Function call_native_translate_error

crates/vm/src/codecs.rs:1065–1102  ·  view source on GitHub ↗

this is a hack, for now

(
    handler: E,
    err: PyObjectRef,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

1063
1064// this is a hack, for now
1065fn call_native_translate_error<E>(
1066 handler: E,
1067 err: PyObjectRef,
1068 vm: &VirtualMachine,
1069) -> PyResult<(PyObjectRef, usize)>
1070where
1071 for<'a> E: EncodeErrorHandler<PyEncodeContext<'a>>,
1072{
1073 // let err = err.
1074 let range = extract_unicode_error_range(&err, vm)?;
1075 let s = PyStrRef::try_from_object(vm, err.get_attr("object", vm)?)?;
1076 let mut ctx = PyEncodeContext {
1077 vm,
1078 encoding: "",
1079 data: &s,
1080 pos: StrSize::default(),
1081 exception: OnceCell::from(err.downcast().unwrap()),
1082 };
1083 let mut iter = s.as_wtf8().code_point_indices();
1084 let start = StrSize {
1085 chars: range.start,
1086 bytes: iter.nth(range.start).unwrap().0,
1087 };
1088 let end = StrSize {
1089 chars: range.end,
1090 bytes: if let Some(n) = range.len().checked_sub(1) {
1091 iter.nth(n).map_or(s.byte_len(), |(i, _)| i)
1092 } else {
1093 start.bytes
1094 },
1095 };
1096 let (replace, restart) = handler.handle_encode_error(&mut ctx, start..end, None)?;
1097 let replace = match replace {
1098 EncodeReplace::Str(s) => s.into(),
1099 EncodeReplace::Bytes(b) => b.into(),
1100 };
1101 Ok((replace, restart.chars))
1102}
1103
1104// TODO: exceptions with custom payloads
1105fn extract_unicode_error_range(err: &PyObject, vm: &VirtualMachine) -> PyResult<Range<usize>> {

Callers 1

backslashreplace_errorsFunction · 0.85

Calls 9

downcastMethod · 0.80
code_point_indicesMethod · 0.80
get_attrMethod · 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