Merge pull request #2594 from deantvv/os-open-args

os: Fix `open` keyword arguments
This commit is contained in:
Noah
2021-04-26 09:06:25 -05:00
committed by GitHub
3 changed files with 22 additions and 12 deletions

View File

@@ -274,8 +274,6 @@ class FileTests(unittest.TestCase):
with open(TESTFN2, 'r') as f:
self.assertEqual(f.read(), "1")
# TODO: RUSTPYTHON (TypeError: Expected at least 2 arguments (0 given))
@unittest.expectedFailure
def test_open_keywords(self):
f = os.open(path=__file__, flags=os.O_RDONLY, mode=0o777,
dir_fd=None)

View File

@@ -2878,13 +2878,7 @@ mod fileio {
vm.new_value_error("Cannot use closefd=False with file name".to_owned())
);
}
os::open(
path,
flags as _,
OptionalArg::Missing,
Default::default(),
vm,
)?
os::open(path, flags as _, None, Default::default(), vm)?
};
if mode.contains(Mode::APPENDING) {

View File

@@ -356,12 +356,30 @@ mod _os {
}
}
#[cfg(any(unix, windows, target_os = "wasi"))]
#[derive(FromArgs)]
struct OpenArgs {
#[pyarg(any)]
path: PyPathLike,
#[pyarg(any)]
flags: OpenFlags,
#[pyarg(any, default)]
mode: Option<PyIntRef>,
#[pyarg(flatten)]
dir_fd: DirFd,
}
#[cfg(any(unix, windows, target_os = "wasi"))]
#[pyfunction]
pub(crate) fn open(
fn open(args: OpenArgs, vm: &VirtualMachine) -> PyResult<i64> {
os_open(args.path, args.flags, args.mode, args.dir_fd, vm)
}
#[cfg(any(unix, windows, target_os = "wasi"))]
pub(crate) fn os_open(
name: PyPathLike,
flags: OpenFlags,
_mode: OptionalArg<PyIntRef>,
_mode: Option<PyIntRef>,
dir_fd: DirFd,
vm: &VirtualMachine,
) -> PyResult<i64> {
@@ -1413,7 +1431,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
module
}
pub(crate) use _os::open;
pub(crate) use _os::os_open as open;
fn to_seconds_from_unix_epoch(sys_time: SystemTime) -> f64 {
match sys_time.duration_since(SystemTime::UNIX_EPOCH) {