Skip to content

Commit 6deb71d

Browse files
committed
os.open impl
1 parent dc6238b commit 6deb71d

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

vm/src/stdlib/os.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::fs::File;
2+
use std::os::unix::io::IntoRawFd;
3+
4+
use num_bigint::{BigInt, ToBigInt};
5+
6+
use super::super::obj::objstr;
7+
use super::super::obj::objint;
8+
use super::super::obj::objtype;
9+
10+
use super::super::pyobject::{
11+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
12+
};
13+
14+
15+
use super::super::vm::VirtualMachine;
16+
17+
18+
fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
19+
arg_check!(
20+
vm,
21+
args,
22+
required = [(name, Some(vm.ctx.str_type()))]
23+
);
24+
match File::open(objstr::get_value(&name)) {
25+
Ok(v) => Ok(vm.ctx.new_int(v.into_raw_fd().to_bigint().unwrap())),
26+
Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
27+
}
28+
}
29+
30+
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
31+
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
32+
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(os_open));
33+
py_mod
34+
}

0 commit comments

Comments
 (0)