Skip to content

Commit dda3d7f

Browse files
committed
Bug fix for sequential Writes, New File Writes
1 parent 3845e23 commit dda3d7f

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

tests/snippets/buffered_reader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from io import BufferedReader, FileIO
2+
3+
fi = FileIO('Cargo.toml')
4+
bb = BufferedReader(fi)
5+
6+
result = bb.read()
7+
8+
assert len(result) <= 8*1024
9+
assert len(result) >= 0
10+

vm/src/stdlib/io.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
111111

112112
let os_mode = match mode.as_ref() {
113113
"r" => 0.to_bigint(),
114-
_ => 1.to_bigint()
114+
_ => 512.to_bigint()
115115
};
116116
let args = vec![name.clone(), vm.ctx.new_int(os_mode.unwrap())];
117117
let fileno = os_open(vm, PyFuncArgs::new(args, vec![]));
@@ -211,7 +211,14 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
211211
match obj.borrow_mut().kind {
212212
PyObjectKind::Bytes { ref mut value } => {
213213
match handle.write(&value[..]) {
214-
Ok(len) => Ok(vm.ctx.new_int(len.to_bigint().unwrap())),
214+
Ok(len) => {
215+
//reset raw fd on the FileIO object
216+
let new_handle = handle.into_raw_fd().to_bigint();
217+
vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
218+
219+
//return number of bytes written
220+
Ok(vm.ctx.new_int(len.to_bigint().unwrap()))
221+
}
215222
Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string()))
216223
}
217224
},
@@ -267,7 +274,6 @@ fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
267274
}
268275
}
269276

270-
271277
pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
272278
arg_check!(
273279
vm,

vm/src/stdlib/os.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ use super::super::pyobject::{
1212
PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol
1313
};
1414

15-
1615
use super::super::vm::VirtualMachine;
1716

18-
1917
pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2018
arg_check!(
2119
vm,
@@ -42,7 +40,7 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4240
if let Ok(f) = handle {
4341
Ok(vm.ctx.new_int(f.into_raw_fd().to_bigint().unwrap()))
4442
} else {
45-
Err(vm.get_none())
43+
Err(vm.new_value_error("Bad file descriptor".to_string()))
4644
}
4745
}
4846

@@ -53,6 +51,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
5351
ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1.to_bigint().unwrap()));
5452
ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2.to_bigint().unwrap()));
5553
ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(3.to_bigint().unwrap()));
56-
54+
ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512.to_bigint().unwrap()));
5755
py_mod
5856
}

0 commit comments

Comments
 (0)