forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathospath.rs
More file actions
192 lines (167 loc) · 5.3 KB
/
ospath.rs
File metadata and controls
192 lines (167 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use rustpython_common::crt_fd;
use crate::{
PyObjectRef, PyResult, VirtualMachine,
builtins::PyBaseExceptionRef,
convert::{IntoPyException, ToPyException, ToPyObject, TryFromObject},
function::FsPath,
object::AsObject,
};
use std::path::{Path, PathBuf};
// path_ without allow_fd in CPython
#[derive(Clone)]
pub struct OsPath {
pub path: std::ffi::OsString,
pub(super) mode: OutputMode,
}
#[derive(Debug, Copy, Clone)]
pub(super) enum OutputMode {
String,
Bytes,
}
impl OutputMode {
pub(super) fn process_path(self, path: impl Into<PathBuf>, vm: &VirtualMachine) -> PyObjectRef {
fn inner(mode: OutputMode, path: PathBuf, vm: &VirtualMachine) -> PyObjectRef {
match mode {
OutputMode::String => vm.fsdecode(path).into(),
OutputMode::Bytes => vm
.ctx
.new_bytes(path.into_os_string().into_encoded_bytes())
.into(),
}
}
inner(self, path.into(), vm)
}
}
impl OsPath {
pub fn new_str(path: impl Into<std::ffi::OsString>) -> Self {
let path = path.into();
Self {
path,
mode: OutputMode::String,
}
}
pub(crate) fn from_fspath(fspath: FsPath, vm: &VirtualMachine) -> PyResult<Self> {
let path = fspath.as_os_str(vm)?.into_owned();
let mode = match fspath {
FsPath::Str(_) => OutputMode::String,
FsPath::Bytes(_) => OutputMode::Bytes,
};
Ok(Self { path, mode })
}
pub fn as_path(&self) -> &Path {
Path::new(&self.path)
}
pub fn into_bytes(self) -> Vec<u8> {
self.path.into_encoded_bytes()
}
pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
self.path.to_string_lossy()
}
pub fn into_cstring(self, vm: &VirtualMachine) -> PyResult<std::ffi::CString> {
std::ffi::CString::new(self.into_bytes()).map_err(|err| err.to_pyexception(vm))
}
#[cfg(windows)]
pub fn to_wide_cstring(&self, vm: &VirtualMachine) -> PyResult<widestring::WideCString> {
widestring::WideCString::from_os_str(&self.path).map_err(|err| err.to_pyexception(vm))
}
pub fn filename(&self, vm: &VirtualMachine) -> PyObjectRef {
self.mode.process_path(self.path.clone(), vm)
}
}
impl AsRef<Path> for OsPath {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl TryFromObject for OsPath {
// TODO: path_converter with allow_fd=0 in CPython
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let fspath = FsPath::try_from(obj, true, vm)?;
Self::from_fspath(fspath, vm)
}
}
// path_t with allow_fd in CPython
#[derive(Clone)]
pub(crate) enum OsPathOrFd<'fd> {
Path(OsPath),
Fd(crt_fd::Borrowed<'fd>),
}
impl TryFromObject for OsPathOrFd<'_> {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match obj.try_index_opt(vm) {
Some(int) => {
let fd = int?.try_to_primitive(vm)?;
unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }
.map(Self::Fd)
.map_err(|e| e.into_pyexception(vm))
}
None => obj.try_into_value(vm).map(Self::Path),
}
}
}
impl From<OsPath> for OsPathOrFd<'_> {
fn from(path: OsPath) -> Self {
Self::Path(path)
}
}
impl OsPathOrFd<'_> {
pub fn filename(&self, vm: &VirtualMachine) -> PyObjectRef {
match self {
Self::Path(path) => path.filename(vm),
Self::Fd(fd) => fd.to_pyobject(vm),
}
}
}
// TODO: preserve the input `PyObjectRef` of filename and filename2 (Failing check `self.assertIs(err.filename, name, str(func)`)
pub struct IOErrorBuilder<'a> {
error: &'a std::io::Error,
filename: Option<OsPathOrFd<'a>>,
filename2: Option<OsPathOrFd<'a>>,
}
impl<'a> IOErrorBuilder<'a> {
pub const fn new(error: &'a std::io::Error) -> Self {
Self {
error,
filename: None,
filename2: None,
}
}
pub(crate) fn filename(mut self, filename: impl Into<OsPathOrFd<'a>>) -> Self {
let filename = filename.into();
self.filename.replace(filename);
self
}
pub(crate) fn filename2(mut self, filename: impl Into<OsPathOrFd<'a>>) -> Self {
let filename = filename.into();
self.filename2.replace(filename);
self
}
pub(crate) fn with_filename(
error: &'a std::io::Error,
filename: impl Into<OsPathOrFd<'a>>,
vm: &VirtualMachine,
) -> PyBaseExceptionRef {
let zelf = IOErrorBuilder {
error,
filename: Some(filename.into()),
filename2: None,
};
zelf.to_pyexception(vm)
}
}
impl ToPyException for IOErrorBuilder<'_> {
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
let exc = self.error.to_pyexception(vm);
if let Some(filename) = &self.filename {
exc.as_object()
.set_attr("filename", filename.filename(vm), vm)
.unwrap();
}
if let Some(filename2) = &self.filename2 {
exc.as_object()
.set_attr("filename2", filename2.filename(vm), vm)
.unwrap();
}
exc
}
}