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
30 lines (27 loc) · 962 Bytes
/
sysmodule.rs
File metadata and controls
30 lines (27 loc) · 962 Bytes
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
use super::pyobject::{DictProtocol, PyContext, PyObjectRef};
use std::env;
/*
* 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)
}
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".to_string(), modules);
sys_mod.set_item(&"argv".to_string(), argv(ctx));
sys_mod.set_item(&"path".to_string(), path);
sys_mod
}