MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_importer

Function get_importer

src/lib.rs:195–224  ·  view source on GitHub ↗
(path: &str, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

193}
194
195fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
196 use rustpython_vm::builtins::PyDictRef;
197 use rustpython_vm::convert::TryFromObject;
198
199 let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?;
200 let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
201 if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
202 return Ok(Some(importer));
203 }
204 let path_obj = vm.ctx.new_str(path);
205 let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
206 let mut importer = None;
207 let path_hooks: Vec<PyObjectRef> = path_hooks.try_into_value(vm)?;
208 for path_hook in path_hooks {
209 match path_hook.call((path_obj.clone(),), vm) {
210 Ok(imp) => {
211 importer = Some(imp);
212 break;
213 }
214 Err(e) if e.fast_isinstance(vm.ctx.exceptions.import_error) => continue,
215 Err(e) => return Err(e),
216 }
217 }
218 Ok(if let Some(imp) = importer {
219 let imp = path_importer_cache.get_or_insert(vm, path_obj.into(), || imp.clone())?;
220 Some(imp)
221 } else {
222 None
223 })
224}
225
226// pymain_run_python
227fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {

Callers 1

run_fileFunction · 0.70

Calls 10

get_item_optMethod · 0.80
try_into_valueMethod · 0.80
fast_isinstanceMethod · 0.80
get_or_insertMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
get_attrMethod · 0.45
new_strMethod · 0.45
callMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected