Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Commit 69280ee

Browse files
committed
OsPath internal to OsString
1 parent 5d6ce6c commit 69280ee

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

vm/src/stdlib/io.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,7 +3703,7 @@ mod fileio {
37033703
builtins::{PyStr, PyStrRef},
37043704
common::crt_fd::Fd,
37053705
convert::ToPyException,
3706-
function::{ArgBytesLike, ArgMemoryBuffer, FsPath, OptionalArg, OptionalOption},
3706+
function::{ArgBytesLike, ArgMemoryBuffer, OptionalArg, OptionalOption},
37073707
stdlib::os,
37083708
types::{DefaultConstructor, Initializer},
37093709
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
@@ -3870,19 +3870,13 @@ mod fileio {
38703870
} else if let Some(i) = name.payload::<crate::builtins::PyInt>() {
38713871
i.try_to_primitive(vm)?
38723872
} else {
3873-
let path = FsPath::try_from_object(vm, name.clone())?;
3873+
let path = os::OsPath::try_from_object(vm, name.clone())?;
38743874
if !args.closefd {
38753875
return Err(
38763876
vm.new_value_error("Cannot use closefd=False with file name".to_owned())
38773877
);
38783878
}
3879-
os::open(
3880-
path.to_pathlike(vm)?,
3881-
flags as _,
3882-
None,
3883-
Default::default(),
3884-
vm,
3885-
)?
3879+
os::open(path, flags as _, None, Default::default(), vm)?
38863880
};
38873881

38883882
if mode.contains(Mode::APPENDING) {

vm/src/stdlib/nt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) mod module {
6060
let dir = args.target_is_directory.target_is_directory
6161
|| args
6262
.dst
63-
.path
63+
.as_path()
6464
.parent()
6565
.and_then(|dst_parent| dst_parent.join(&args.src).symlink_metadata().ok())
6666
.map_or(false, |meta| meta.is_dir());
@@ -297,7 +297,7 @@ pub(crate) mod module {
297297

298298
#[pyfunction]
299299
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
300-
let orig: Vec<_> = path.path.into_os_string().encode_wide().collect();
300+
let orig: Vec<_> = path.path.encode_wide().collect();
301301
if orig.is_empty() {
302302
return Ok(("".to_owned(), "".to_owned()));
303303
}

vm/src/stdlib/os.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
builtins::{PyBaseExceptionRef, PyInt, PySet},
2+
builtins::{PyBaseExceptionRef, PySet},
33
common::crt_fd::Fd,
44
convert::IntoPyException,
55
function::{ArgumentError, FromArgs, FsPath, FuncArgs},
@@ -48,22 +48,35 @@ impl OutputMode {
4848
// path_ without allow_fd in CPython
4949
#[derive(Clone)]
5050
pub struct OsPath {
51-
pub path: PathBuf,
51+
pub path: ffi::OsString,
5252
pub(super) mode: OutputMode,
5353
}
5454

5555
impl OsPath {
56-
pub fn new_str(path: impl Into<PathBuf>) -> Self {
56+
pub fn new_str(path: impl Into<ffi::OsString>) -> Self {
5757
Self {
5858
path: path.into(),
5959
mode: OutputMode::String,
6060
}
6161
}
6262

63+
pub(crate) fn from_fspath(fspath: FsPath, vm: &VirtualMachine) -> PyResult<OsPath> {
64+
let path = fspath.as_os_str(vm)?.to_owned();
65+
let mode = match fspath {
66+
FsPath::Str(_) => OutputMode::String,
67+
FsPath::Bytes(_) => OutputMode::Bytes,
68+
};
69+
Ok(OsPath { path, mode })
70+
}
71+
72+
pub fn as_path(&self) -> &Path {
73+
Path::new(&self.path)
74+
}
75+
6376
#[cfg(any(unix, target_os = "wasi"))]
6477
pub fn into_bytes(self) -> Vec<u8> {
65-
use rustpython_common::os::ffi::OsStringExt;
66-
self.path.into_os_string().into_vec()
78+
use rustpython_common::os::ffi::OsStrExt;
79+
self.path.as_bytes().to_vec()
6780
}
6881

6982
#[cfg(windows)]
@@ -98,26 +111,15 @@ pub(super) fn fs_metadata<P: AsRef<Path>>(
98111

99112
impl AsRef<Path> for OsPath {
100113
fn as_ref(&self) -> &Path {
101-
&self.path
102-
}
103-
}
104-
105-
impl FsPath {
106-
pub(crate) fn to_pathlike(&self, vm: &VirtualMachine) -> PyResult<OsPath> {
107-
let path = self.as_os_str(vm)?.to_owned().into();
108-
let mode = match self {
109-
Self::Str(_) => OutputMode::String,
110-
Self::Bytes(_) => OutputMode::Bytes,
111-
};
112-
Ok(OsPath { path, mode })
114+
self.as_path()
113115
}
114116
}
115117

116118
impl TryFromObject for OsPath {
117-
// TODO: path_converter in CPython
119+
// TODO: path_converter with allow_fd=0 in CPython
118120
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
119-
let fs_path = FsPath::try_from(obj, true, vm)?;
120-
fs_path.to_pathlike(vm)
121+
let fspath = FsPath::try_from(obj, true, vm)?;
122+
Self::from_fspath(fspath, vm)
121123
}
122124
}
123125

@@ -684,10 +686,11 @@ pub(super) mod _os {
684686
) -> PyResult {
685687
let do_stat = |follow_symlinks| {
686688
stat(
687-
OsPathOrFd::Path(OsPath {
688-
path: self.pathval.clone(),
689+
OsPath {
690+
path: self.pathval.as_os_str().to_owned(),
689691
mode: OutputMode::String,
690-
}),
692+
}
693+
.into(),
691694
dir_fd,
692695
FollowSymlinks(follow_symlinks),
693696
vm,
@@ -716,10 +719,11 @@ pub(super) mod _os {
716719
Some(ino) => Ok(ino),
717720
None => {
718721
let stat = stat_inner(
719-
OsPathOrFd::Path(OsPath {
720-
path: self.pathval.clone(),
722+
OsPath {
723+
path: self.pathval.as_os_str().to_owned(),
721724
mode: OutputMode::String,
722-
}),
725+
}
726+
.into(),
723727
DirFd::default(),
724728
FollowSymlinks(false),
725729
)

0 commit comments

Comments
 (0)