|
1 | 1 | use crate::{ |
2 | | - builtins::{PyBaseExceptionRef, PyInt, PySet}, |
| 2 | + builtins::{PyBaseExceptionRef, PySet}, |
3 | 3 | common::crt_fd::Fd, |
4 | 4 | convert::IntoPyException, |
5 | 5 | function::{ArgumentError, FromArgs, FsPath, FuncArgs}, |
@@ -48,22 +48,35 @@ impl OutputMode { |
48 | 48 | // path_ without allow_fd in CPython |
49 | 49 | #[derive(Clone)] |
50 | 50 | pub struct OsPath { |
51 | | - pub path: PathBuf, |
| 51 | + pub path: ffi::OsString, |
52 | 52 | pub(super) mode: OutputMode, |
53 | 53 | } |
54 | 54 |
|
55 | 55 | impl OsPath { |
56 | | - pub fn new_str(path: impl Into<PathBuf>) -> Self { |
| 56 | + pub fn new_str(path: impl Into<ffi::OsString>) -> Self { |
57 | 57 | Self { |
58 | 58 | path: path.into(), |
59 | 59 | mode: OutputMode::String, |
60 | 60 | } |
61 | 61 | } |
62 | 62 |
|
| 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 | + |
63 | 76 | #[cfg(any(unix, target_os = "wasi"))] |
64 | 77 | 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() |
67 | 80 | } |
68 | 81 |
|
69 | 82 | #[cfg(windows)] |
@@ -98,26 +111,15 @@ pub(super) fn fs_metadata<P: AsRef<Path>>( |
98 | 111 |
|
99 | 112 | impl AsRef<Path> for OsPath { |
100 | 113 | 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() |
113 | 115 | } |
114 | 116 | } |
115 | 117 |
|
116 | 118 | impl TryFromObject for OsPath { |
117 | | - // TODO: path_converter in CPython |
| 119 | + // TODO: path_converter with allow_fd=0 in CPython |
118 | 120 | 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) |
121 | 123 | } |
122 | 124 | } |
123 | 125 |
|
@@ -684,10 +686,11 @@ pub(super) mod _os { |
684 | 686 | ) -> PyResult { |
685 | 687 | let do_stat = |follow_symlinks| { |
686 | 688 | stat( |
687 | | - OsPathOrFd::Path(OsPath { |
688 | | - path: self.pathval.clone(), |
| 689 | + OsPath { |
| 690 | + path: self.pathval.as_os_str().to_owned(), |
689 | 691 | mode: OutputMode::String, |
690 | | - }), |
| 692 | + } |
| 693 | + .into(), |
691 | 694 | dir_fd, |
692 | 695 | FollowSymlinks(follow_symlinks), |
693 | 696 | vm, |
@@ -716,10 +719,11 @@ pub(super) mod _os { |
716 | 719 | Some(ino) => Ok(ino), |
717 | 720 | None => { |
718 | 721 | let stat = stat_inner( |
719 | | - OsPathOrFd::Path(OsPath { |
720 | | - path: self.pathval.clone(), |
| 722 | + OsPath { |
| 723 | + path: self.pathval.as_os_str().to_owned(), |
721 | 724 | mode: OutputMode::String, |
722 | | - }), |
| 725 | + } |
| 726 | + .into(), |
723 | 727 | DirFd::default(), |
724 | 728 | FollowSymlinks(false), |
725 | 729 | ) |
|
0 commit comments