Skip to content

Commit 0f62a47

Browse files
committed
update os.open
1 parent 05adf7f commit 0f62a47

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

vm/src/stdlib/os.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use num_cpus;
22
use std::cell::RefCell;
3+
use std::convert::TryInto;
34
use std::ffi::CStr;
45
use std::fs::File;
56
use std::fs::OpenOptions;
67
use std::io::{self, Error, ErrorKind, Read, Write};
7-
use std::convert::TryInto;
88
#[cfg(unix)]
99
use std::os::unix::fs::OpenOptionsExt;
1010
#[cfg(windows)]
@@ -122,22 +122,26 @@ pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
122122
let fname = &make_path(vm, name, &dir_fd).value;
123123

124124
let mut options = OpenOptions::new();
125-
126-
if cfg!(unix) {
127-
let flags = objint::get_value(flags).to_i32().unwrap();
128-
options.custom_flags(flags);
129-
} else {
130-
let flags = objint::get_value(flags).to_u32().unwrap();
131-
options.custom_flags(flags.try_into().unwrap_or_default());
132-
};
133-
125+
let options = _set_file_model(&mut options, flags);
134126
let handle = options
135127
.open(&fname)
136128
.map_err(|err| convert_io_error(vm, err))?;
137129

138130
Ok(vm.ctx.new_int(raw_file_number(handle)))
139131
}
140132

133+
#[cfg(unix)]
134+
fn _set_file_model<'a>(options: &'a mut OpenOptions, flags: &PyObjectRef) -> &'a mut OpenOptions {
135+
let flags = objint::get_value(flags).to_i32().unwrap();
136+
options.custom_flags(flags)
137+
}
138+
139+
#[cfg(windows)]
140+
fn _set_file_model<'a>(options: &'a mut OpenOptions, flags: &PyObjectRef) -> &'a mut OpenOptions {
141+
let flags = objint::get_value(flags).to_u32().unwrap();
142+
options.custom_flags(flags)
143+
}
144+
141145
#[cfg(all(not(unix), not(windows)))]
142146
pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
143147
unimplemented!()

0 commit comments

Comments
 (0)