Files
RustPython/vm/src/sysmodule.rs
Daniel Watkins da34b86a9c Implement PYTHONPATH
This modifies the import logic to use sys.path, and uses PYTHONPATH to
generate the initial contents of sys.path.
2018-08-24 22:55:30 -04:00

27 lines
868 B
Rust

// use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
/*
* The magic sys module.
*/
use std::env;
use super::pyobject::{DictProtocol, PyContext, PyObjectRef};
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let mut 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![],
};
path_list.insert(0, ctx.new_str("".to_string()));
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(&"path".to_string(), path);
sys_mod
}