forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysmodule.rs
More file actions
84 lines (76 loc) · 2.71 KB
/
sysmodule.rs
File metadata and controls
84 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use super::obj::objint;
use super::obj::objtype;
use super::pyobject::{DictProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::vm::VirtualMachine;
use num_bigint::ToBigInt;
use num_traits::ToPrimitive;
use std::env;
use std::mem;
use std::rc::Rc;
/*
* The magic sys module.
*/
fn argv(ctx: &PyContext) -> PyObjectRef {
let mut argv: Vec<PyObjectRef> = env::args().map(|x| ctx.new_str(x)).collect();
argv.remove(0);
ctx.new_list(argv)
}
fn getframe(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [],
optional = [(offset, Some(vm.ctx.int_type()))]
);
let idx = match offset {
Some(int) => {
if let Some(offset) = objint::get_value(int).to_usize() {
if offset > vm.frames.len() - 1 {
return Err(vm.new_value_error("call stack is not deep enough".to_string()));
}
offset
} else {
0
}
}
None => 0,
};
let idx = vm.frames.len() - idx - 1;
let frame = &vm.frames[idx];
Ok(frame.clone())
}
fn sys_getrefcount(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(object, None)]);
let size = Rc::strong_count(&object);
Ok(vm.ctx.new_int(size.to_bigint().unwrap()))
}
fn sys_getsizeof(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(object, None)]);
// TODO: implement default optional argument.
let size = mem::size_of_val(&object.borrow());
Ok(vm.ctx.new_int(size.to_bigint().unwrap()))
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let path_list = match env::var_os("PYTHONPATH") {
Some(paths) => env::split_paths(&paths)
.map(|path| ctx.new_str(path.to_str().unwrap().to_string()))
.collect(),
None => vec![],
};
let path = ctx.new_list(path_list);
let modules = ctx.new_dict();
let sys_name = "sys".to_string();
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
modules.set_item(&sys_name, sys_mod.clone());
sys_mod.set_item("modules", modules);
sys_mod.set_item("argv", argv(ctx));
sys_mod.set_item("getrefcount", ctx.new_rustfunc(sys_getrefcount));
sys_mod.set_item("getsizeof", ctx.new_rustfunc(sys_getsizeof));
let maxsize = ctx.new_int(std::usize::MAX.to_bigint().unwrap());
sys_mod.set_item("maxsize", maxsize);
sys_mod.set_item("path", path);
sys_mod.set_item("ps1", ctx.new_str(">>>>> ".to_string()));
sys_mod.set_item("ps2", ctx.new_str("..... ".to_string()));
sys_mod.set_item("_getframe", ctx.new_rustfunc(getframe));
sys_mod
}