|
2 | 2 | * Import mechanics |
3 | 3 | */ |
4 | 4 |
|
5 | | -use std::path::PathBuf; |
6 | | - |
7 | 5 | usecrate::bytecode::CodeObject; |
8 | 6 | usecrate::frame::Scope; |
9 | | -usecrate::obj::{objcode, objsequence, objstr}; |
| 7 | +usecrate::obj::objcode; |
10 | 8 | usecrate::pyobject::{ItemProtocol,PyResult,PyValue}; |
11 | | -usecrate::util; |
12 | 9 | usecrate::vm::VirtualMachine; |
13 | 10 | #[cfg(feature ="rustpython-compiler")] |
14 | 11 | use rustpython_compiler::compile; |
@@ -49,39 +46,6 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult { |
49 | 46 | }) |
50 | 47 | } |
51 | 48 |
|
52 | | -pubfnimport_module(vm:&VirtualMachine,current_path:PathBuf,module_name:&str) ->PyResult{ |
53 | | -// Cached modules: |
54 | | -let sys_modules = vm.get_attribute(vm.sys_module.clone(),"modules").unwrap(); |
55 | | - |
56 | | -// First, see if we already loaded the module: |
57 | | -ifletOk(module) = sys_modules.get_item(module_name.to_string(), vm){ |
58 | | -Ok(module) |
59 | | -}elseif vm.frozen.borrow().contains_key(module_name){ |
60 | | -import_frozen(vm, module_name) |
61 | | -}elseif vm.stdlib_inits.borrow().contains_key(module_name){ |
62 | | -import_builtin(vm, module_name) |
63 | | -}elseifcfg!(feature ="rustpython-compiler"){ |
64 | | -let notfound_error =&vm.ctx.exceptions.module_not_found_error; |
65 | | -let import_error =&vm.ctx.exceptions.import_error; |
66 | | - |
67 | | -// Time to search for module in any place: |
68 | | -let file_path =find_source(vm, current_path, module_name) |
69 | | -.map_err(|e| vm.new_exception(notfound_error.clone(), e))?; |
70 | | -let source = util::read_file(file_path.as_path()) |
71 | | -.map_err(|e| vm.new_exception(import_error.clone(), e.to_string()))?; |
72 | | - |
73 | | -import_file( |
74 | | - vm, |
75 | | - module_name, |
76 | | - file_path.to_str().unwrap().to_string(), |
77 | | - source, |
78 | | -) |
79 | | -}else{ |
80 | | -let notfound_error =&vm.ctx.exceptions.module_not_found_error; |
81 | | -Err(vm.new_exception(notfound_error.clone(), module_name.to_string())) |
82 | | -} |
83 | | -} |
84 | | - |
85 | 49 | #[cfg(feature ="rustpython-compiler")] |
86 | 50 | pubfnimport_file( |
87 | 51 | vm:&VirtualMachine, |
@@ -118,29 +82,3 @@ pub fn import_codeobj( |
118 | 82 | )?; |
119 | 83 | Ok(module) |
120 | 84 | } |
121 | | - |
122 | | -fnfind_source(vm:&VirtualMachine,current_path:PathBuf,name:&str) ->Result<PathBuf,String>{ |
123 | | -let sys_path = vm.get_attribute(vm.sys_module.clone(),"path").unwrap(); |
124 | | -letmut paths:Vec<PathBuf> = objsequence::get_elements_list(&sys_path) |
125 | | -.iter() |
126 | | -.map(|item|PathBuf::from(objstr::get_value(item))) |
127 | | -.collect(); |
128 | | - |
129 | | - paths.insert(0, current_path); |
130 | | - |
131 | | -let rel_name = name.replace('.',"/"); |
132 | | -let suffixes =[".py","/__init__.py"]; |
133 | | -letmut file_paths =vec![]; |
134 | | -for pathin paths{ |
135 | | -for suffixin suffixes.iter(){ |
136 | | -letmut file_path = path.clone(); |
137 | | - file_path.push(format!("{}{}", rel_name, suffix)); |
138 | | - file_paths.push(file_path); |
139 | | -} |
140 | | -} |
141 | | - |
142 | | -match file_paths.iter().find(|p| p.exists()){ |
143 | | -Some(path) =>Ok(path.to_path_buf()), |
144 | | -None =>Err(format!("No module named '{}'", name)), |
145 | | -} |
146 | | -} |