Skip to content

Commit fdf27bf

Browse files
committed
Convert os methods to support path-like objects
1 parent e5e0f5c commit fdf27bf

2 files changed

Lines changed: 32 additions & 41 deletions

File tree

vm/src/stdlib/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ mod fileio {
550550
(
551551
name.clone().into_object(),
552552
os::os_open(
553-
name,
553+
os::PyPathLike::new_str(name.as_str().to_owned()),
554554
mode as _,
555555
OptionalArg::Missing,
556556
OptionalArg::Missing,

vm/src/stdlib/os.rs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::obj::objiter;
3838
use crate::obj::objset::PySet;
3939
use crate::obj::objstr::{self, PyString, PyStringRef};
4040
use crate::obj::objtuple::PyTupleRef;
41-
use crate::obj::objtype::{self, PyClassRef};
41+
use crate::obj::objtype::PyClassRef;
4242
use crate::pyobject::{
4343
Either, ItemProtocol, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
4444
TypeProtocol,
@@ -109,13 +109,13 @@ fn output_by_mode(val: String, mode: OutputMode, vm: &VirtualMachine) -> PyObjec
109109
}
110110
}
111111

112-
struct PyPathLike {
112+
pub struct PyPathLike {
113113
path: String,
114114
mode: OutputMode,
115115
}
116116

117117
impl PyPathLike {
118-
fn new_str(path: String) -> Self {
118+
pub fn new_str(path: String) -> Self {
119119
PyPathLike {
120120
path,
121121
mode: OutputMode::String,
@@ -198,7 +198,7 @@ type OpenFlags = u32;
198198

199199
#[cfg(any(unix, windows))]
200200
pub fn os_open(
201-
name: PyStringRef,
201+
name: PyPathLike,
202202
flags: OpenFlags,
203203
_mode: OptionalArg<PyIntRef>,
204204
dir_fd: OptionalArg<PyIntRef>,
@@ -207,7 +207,7 @@ pub fn os_open(
207207
let dir_fd = DirFd {
208208
dir_fd: dir_fd.into_option(),
209209
};
210-
let fname = make_path(vm, name.as_str(), &dir_fd);
210+
let fname = make_path(vm, &name.path, &dir_fd);
211211

212212
let mut options = OpenOptions::new();
213213

@@ -471,27 +471,27 @@ fn os_write(fd: i64, data: PyBytesLike, vm: &VirtualMachine) -> PyResult {
471471
Ok(vm.ctx.new_int(written))
472472
}
473473

474-
fn os_remove(path: PyStringRef, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<()> {
475-
let path = make_path(vm, path.as_str(), &dir_fd);
474+
fn os_remove(path: PyPathLike, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<()> {
475+
let path = make_path(vm, &path.path, &dir_fd);
476476
fs::remove_file(path).map_err(|err| convert_io_error(vm, err))
477477
}
478478

479479
fn os_mkdir(
480-
path: PyStringRef,
480+
path: PyPathLike,
481481
_mode: OptionalArg<PyIntRef>,
482482
dir_fd: DirFd,
483483
vm: &VirtualMachine,
484484
) -> PyResult<()> {
485-
let path = make_path(vm, path.as_str(), &dir_fd);
485+
let path = make_path(vm, &path.path, &dir_fd);
486486
fs::create_dir(path).map_err(|err| convert_io_error(vm, err))
487487
}
488488

489489
fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
490490
fs::create_dir_all(path.as_str()).map_err(|err| convert_io_error(vm, err))
491491
}
492492

493-
fn os_rmdir(path: PyStringRef, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<()> {
494-
let path = make_path(vm, path.as_str(), &dir_fd);
493+
fn os_rmdir(path: PyPathLike, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<()> {
494+
let path = make_path(vm, &path.path, &dir_fd);
495495
fs::remove_dir(path).map_err(|err| convert_io_error(vm, err))
496496
}
497497

@@ -936,29 +936,29 @@ fn os_lstat(file: Either<PyPathLike, i64>, dir_fd: DirFd, vm: &VirtualMachine) -
936936

937937
#[cfg(unix)]
938938
fn os_symlink(
939-
src: PyStringRef,
940-
dst: PyStringRef,
939+
src: PyPathLike,
940+
dst: PyPathLike,
941941
dir_fd: DirFd,
942942
vm: &VirtualMachine,
943943
) -> PyResult<()> {
944944
use std::os::unix::fs as unix_fs;
945-
let dst = make_path(vm, dst.as_str(), &dir_fd);
946-
unix_fs::symlink(src.as_str(), dst).map_err(|err| convert_io_error(vm, err))
945+
let dst = make_path(vm, &dst.path, &dir_fd);
946+
unix_fs::symlink(src.path, dst).map_err(|err| convert_io_error(vm, err))
947947
}
948948

949949
#[cfg(windows)]
950950
fn os_symlink(
951-
src: PyStringRef,
952-
dst: PyStringRef,
951+
src: PyPathLike,
952+
dst: PyPathLike,
953953
_dir_fd: DirFd,
954954
vm: &VirtualMachine,
955955
) -> PyResult<()> {
956956
use std::os::windows::fs as win_fs;
957-
let meta = fs::metadata(src.as_str()).map_err(|err| convert_io_error(vm, err))?;
957+
let meta = fs::metadata(src.path).map_err(|err| convert_io_error(vm, err))?;
958958
let ret = if meta.is_file() {
959-
win_fs::symlink_file(src.as_str(), dst.as_str())
959+
win_fs::symlink_file(src.path, dst.path)
960960
} else if meta.is_dir() {
961-
win_fs::symlink_dir(src.as_str(), dst.as_str())
961+
win_fs::symlink_dir(src.path, dst.path)
962962
} else {
963963
panic!("Uknown file type");
964964
};
@@ -967,8 +967,8 @@ fn os_symlink(
967967

968968
#[cfg(all(not(unix), not(windows)))]
969969
fn os_symlink(
970-
src: PyStringRef,
971-
dst: PyStringRef,
970+
src: PyPathLike,
971+
dst: PyPathLike,
972972
dir_fd: DirFd,
973973
vm: &VirtualMachine,
974974
) -> PyResult<()> {
@@ -1116,14 +1116,14 @@ fn os_system(command: PyStringRef) -> PyResult<i32> {
11161116

11171117
#[cfg(unix)]
11181118
fn os_chmod(
1119-
path: PyStringRef,
1119+
path: PyPathLike,
11201120
dir_fd: DirFd,
11211121
mode: u32,
11221122
follow_symlinks: FollowSymlinks,
11231123
vm: &VirtualMachine,
11241124
) -> PyResult<()> {
11251125
use std::os::unix::fs::PermissionsExt;
1126-
let path = make_path(vm, path.as_str(), &dir_fd);
1126+
let path = make_path(vm, &path.path, &dir_fd);
11271127
let metadata = if follow_symlinks.follow_symlinks {
11281128
fs::metadata(path)
11291129
} else {
@@ -1136,21 +1136,12 @@ fn os_chmod(
11361136
Ok(())
11371137
}
11381138

1139-
fn os_fspath(path: PyObjectRef, vm: &VirtualMachine) -> PyResult {
1140-
if objtype::issubclass(&path.class(), &vm.ctx.str_type())
1141-
|| objtype::issubclass(&path.class(), &vm.ctx.bytes_type())
1142-
{
1143-
Ok(path)
1144-
} else {
1145-
Err(vm.new_type_error(format!(
1146-
"expected str or bytes object, not {}",
1147-
path.class()
1148-
)))
1149-
}
1139+
fn os_fspath(path: PyPathLike, vm: &VirtualMachine) -> PyResult {
1140+
Ok(output_by_mode(path.path, path.mode, vm))
11501141
}
11511142

1152-
fn os_rename(src: PyStringRef, dst: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
1153-
fs::rename(src.as_str(), dst.as_str()).map_err(|err| convert_io_error(vm, err))
1143+
fn os_rename(src: PyPathLike, dst: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
1144+
fs::rename(src.path, dst.path).map_err(|err| convert_io_error(vm, err))
11541145
}
11551146

11561147
fn os_getpid(vm: &VirtualMachine) -> PyObjectRef {
@@ -1397,12 +1388,12 @@ fn os_lseek(fd: i32, position: Offset, how: i32, vm: &VirtualMachine) -> PyResul
13971388
}
13981389
}
13991390

1400-
fn os_link(src: PyStringRef, dst: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
1401-
fs::hard_link(src.as_str(), dst.as_str()).map_err(|err| convert_io_error(vm, err))
1391+
fn os_link(src: PyPathLike, dst: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
1392+
fs::hard_link(src.path, dst.path).map_err(|err| convert_io_error(vm, err))
14021393
}
14031394

14041395
fn os_utime(
1405-
_path: PyStringRef,
1396+
_path: PyPathLike,
14061397
_time: OptionalArg<PyTupleRef>,
14071398
_vm: &VirtualMachine,
14081399
) -> PyResult<()> {

0 commit comments

Comments
 (0)