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