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

Method try_path_inner

crates/vm/src/ospath.rs:106–136  ·  view source on GitHub ↗

Convert to OsPath only (no fd support)

(
        &self,
        obj: PyObjectRef,
        allow_fd: bool,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

104
105 /// Convert to OsPath only (no fd support)
106 fn try_path_inner(
107 &self,
108 obj: PyObjectRef,
109 allow_fd: bool,
110 vm: &VirtualMachine,
111 ) -> PyResult<OsPath> {
112 // Try direct str/bytes match
113 let obj = match self.try_match_str_bytes(obj.clone(), vm)? {
114 Ok(path) => return Ok(path),
115 Err(obj) => obj,
116 };
117
118 // Call __fspath__
119 let type_error_msg = || self.type_error_msg(&obj.class().name(), allow_fd);
120 let method =
121 vm.get_method_or_type_error(obj.clone(), identifier!(vm, __fspath__), type_error_msg)?;
122 if vm.is_none(&method) {
123 return Err(vm.new_type_error(type_error_msg()));
124 }
125 let result = method.call((), vm)?;
126
127 // Match __fspath__ result
128 self.try_match_str_bytes(result.clone(), vm)?.map_err(|_| {
129 vm.new_type_error(format!(
130 "{}expected {}.__fspath__() to return str or bytes, not {}",
131 self.error_prefix(),
132 obj.class().name(),
133 result.class().name(),
134 ))
135 })
136 }
137
138 /// Try to match str or bytes, returns Err(obj) if neither
139 fn try_match_str_bytes(

Callers 2

try_path_or_fdMethod · 0.80
try_pathMethod · 0.80

Calls 9

try_match_str_bytesMethod · 0.80
type_error_msgMethod · 0.80
ErrClass · 0.50
cloneMethod · 0.45
nameMethod · 0.45
classMethod · 0.45
is_noneMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected