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

Function text_encoding

crates/vm/src/stdlib/_io.rs:5228–5261  ·  view source on GitHub ↗
(
        encoding: PyObjectRef,
        stacklevel: OptionalArg<i32>,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

5226
5227 #[pyfunction]
5228 fn text_encoding(
5229 encoding: PyObjectRef,
5230 stacklevel: OptionalArg<i32>,
5231 vm: &VirtualMachine,
5232 ) -> PyResult<PyStrRef> {
5233 if vm.is_none(&encoding) {
5234 let encoding = if vm.state.config.settings.utf8_mode > 0 {
5235 "utf-8"
5236 } else {
5237 "locale"
5238 };
5239 if vm.state.config.settings.warn_default_encoding {
5240 let mut stacklevel = stacklevel.unwrap_or(2);
5241 if stacklevel > 1
5242 && let Some(frame) = vm.current_frame()
5243 && let Some(stdlib_dir) = vm.state.config.paths.stdlib_dir.as_deref()
5244 {
5245 let path = frame.code.source_path().as_str();
5246 if !path.starts_with(stdlib_dir) {
5247 stacklevel = stacklevel.saturating_sub(1);
5248 }
5249 }
5250 let stacklevel = usize::try_from(stacklevel).unwrap_or(0);
5251 crate::stdlib::_warnings::warn(
5252 vm.ctx.exceptions.encoding_warning,
5253 "'encoding' argument not specified".to_owned(),
5254 stacklevel,
5255 vm,
5256 )?;
5257 }
5258 return Ok(vm.ctx.new_str(encoding));
5259 }
5260 encoding.try_into_value(vm)
5261 }
5262
5263 #[cfg(test)]
5264 mod tests {

Callers 1

io_openFunction · 0.70

Calls 9

current_frameMethod · 0.80
starts_withMethod · 0.80
try_into_valueMethod · 0.80
warnFunction · 0.70
is_noneMethod · 0.45
as_strMethod · 0.45
source_pathMethod · 0.45
to_ownedMethod · 0.45
new_strMethod · 0.45

Tested by

no test coverage detected