Skip to content

Commit d6d689d

Browse files
committed
os.open: make make_path return Cow
1 parent cdcc191 commit d6d689d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

vm/src/stdlib/os.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,14 @@ impl TryFromObject for PyPathLike {
159159
}
160160
}
161161

162-
fn make_path(vm: &VirtualMachine, path: &PyPathLike, dir_fd: &DirFd) -> PyResult<ffi::OsString> {
163-
let path = path.path.to_path_buf();
162+
fn make_path<'a>(
163+
vm: &VirtualMachine,
164+
path: &'a PyPathLike,
165+
dir_fd: &DirFd,
166+
) -> PyResult<std::borrow::Cow<'a, ffi::OsStr>> {
167+
let path = &path.path;
164168
if dir_fd.0.is_none() | path.is_absolute() {
165-
return Ok(path.into_os_string());
169+
return Ok(std::borrow::Cow::Borrowed(path.as_os_str().into()));
166170
}
167171

168172
cfg_if::cfg_if! {
@@ -175,8 +179,8 @@ fn make_path(vm: &VirtualMachine, path: &PyPathLike, dir_fd: &DirFd) -> PyResult
175179
return Err(vm.new_os_error(format!("Cannot determine path of dir_fd: {:?}", dir_fd.0)));
176180
}
177181
};
178-
let p: PathBuf = vec![dir_path, path].iter().collect();
179-
Ok(p.into_os_string())
182+
let p: PathBuf = vec![dir_path, path.to_path_buf()].iter().collect();
183+
Ok(p.into_os_string().into())
180184
}
181185
}
182186
}

0 commit comments

Comments
 (0)