|
1 | 1 | use num_cpus;
|
2 | 2 | use std::cell::RefCell;
|
| 3 | +use std::convert::TryInto; |
3 | 4 | use std::ffi::CStr;
|
4 | 5 | use std::fs::File;
|
5 | 6 | use std::fs::OpenOptions;
|
6 | 7 | use std::io::{self, Error, ErrorKind, Read, Write};
|
7 |
| -use std::convert::TryInto; |
8 | 8 | #[cfg(unix)]
|
9 | 9 | use std::os::unix::fs::OpenOptionsExt;
|
10 | 10 | #[cfg(windows)]
|
@@ -122,22 +122,26 @@ pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
122 | 122 | let fname = &make_path(vm, name, &dir_fd).value;
|
123 | 123 |
|
124 | 124 | 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); |
134 | 126 | let handle = options
|
135 | 127 | .open(&fname)
|
136 | 128 | .map_err(|err| convert_io_error(vm, err))?;
|
137 | 129 |
|
138 | 130 | Ok(vm.ctx.new_int(raw_file_number(handle)))
|
139 | 131 | }
|
140 | 132 |
|
| 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 | + |
141 | 145 | #[cfg(all(not(unix), not(windows)))]
|
142 | 146 | pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
143 | 147 | unimplemented!()
|
|
0 commit comments