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

Function _path_normpath

crates/vm/src/stdlib/nt.rs:1700–1742  ·  view source on GitHub ↗
(path: crate::PyObjectRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1698
1699 #[pyfunction]
1700 fn _path_normpath(path: crate::PyObjectRef, vm: &VirtualMachine) -> PyResult {
1701 // Handle path-like objects via os.fspath
1702 let path = if let Some(fspath) = vm.get_method(path.clone(), identifier!(vm, __fspath__)) {
1703 fspath?.call((), vm)?
1704 } else {
1705 path
1706 };
1707
1708 let (wide, is_bytes): (Vec<u16>, bool) = if let Some(s) = path.downcast_ref::<PyStr>() {
1709 let wide: Vec<u16> = s.as_wtf8().encode_wide().collect();
1710 (wide, false)
1711 } else if let Some(b) = path.downcast_ref::<PyBytes>() {
1712 let s = core::str::from_utf8(b.as_bytes()).map_err(|e| {
1713 vm.new_exception_msg(
1714 vm.ctx.exceptions.unicode_decode_error.to_owned(),
1715 format!(
1716 "'utf-8' codec can't decode byte {:#x} in position {}: invalid start byte",
1717 b.as_bytes().get(e.valid_up_to()).copied().unwrap_or(0),
1718 e.valid_up_to()
1719 )
1720 .into(),
1721 )
1722 })?;
1723 let wide: Vec<u16> = s.encode_utf16().collect();
1724 (wide, true)
1725 } else {
1726 return Err(vm.new_type_error(format!(
1727 "expected str or bytes, not {}",
1728 path.class().name()
1729 )));
1730 };
1731
1732 let normalized = normpath_wide(&wide);
1733
1734 if is_bytes {
1735 let s = String::from_utf16(&normalized)
1736 .map_err(|e| vm.new_unicode_decode_error(e.to_string()))?;
1737 Ok(vm.ctx.new_bytes(s.into_bytes()).into())
1738 } else {
1739 let s = Wtf8Buf::from_wide(&normalized);
1740 Ok(vm.ctx.new_str(s).into())
1741 }
1742 }
1743
1744 #[pyfunction]
1745 fn _getdiskusage(path: OsPath, vm: &VirtualMachine) -> PyResult<(u64, u64)> {

Callers

nothing calls this directly

Calls 15

normpath_wideFunction · 0.85
collectMethod · 0.80
encode_wideMethod · 0.80
new_exception_msgMethod · 0.80
to_stringMethod · 0.80
ErrClass · 0.50
get_methodMethod · 0.45
cloneMethod · 0.45
callMethod · 0.45
as_wtf8Method · 0.45
as_bytesMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected