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

Function import_code_obj

crates/vm/src/import.rs:177–206  ·  view source on GitHub ↗
(
    vm: &VirtualMachine,
    module_name: &str,
    code_obj: PyRef<PyCode>,
    set_file_attr: bool,
)

Source from the content-addressed store, hash-verified

175}
176
177pub fn import_code_obj(
178 vm: &VirtualMachine,
179 module_name: &str,
180 code_obj: PyRef<PyCode>,
181 set_file_attr: bool,
182) -> PyResult {
183 let attrs = vm.ctx.new_dict();
184 attrs.set_item(
185 identifier!(vm, __name__),
186 vm.ctx.new_utf8_str(module_name).into(),
187 vm,
188 )?;
189 if set_file_attr {
190 attrs.set_item(
191 identifier!(vm, __file__),
192 code_obj.source_path().to_object(),
193 vm,
194 )?;
195 }
196 let module = vm.new_module(module_name, attrs.clone(), None);
197
198 // Store module in cache to prevent infinite loop with mutual importing libs:
199 let sys_modules = vm.sys_module.get_attr("modules", vm)?;
200 sys_modules.set_item(module_name, module.clone().into(), vm)?;
201
202 // Execute main code in module:
203 let scope = Scope::with_builtins(None, attrs, vm);
204 vm.run_code_obj(code_obj, scope)?;
205 Ok(module.into())
206}
207
208fn remove_importlib_frames_inner(
209 vm: &VirtualMachine,

Callers 3

import_frozenFunction · 0.85
import_fileFunction · 0.85
import_sourceFunction · 0.85

Calls 9

new_dictMethod · 0.80
new_utf8_strMethod · 0.80
run_code_objMethod · 0.80
set_itemMethod · 0.45
to_objectMethod · 0.45
source_pathMethod · 0.45
new_moduleMethod · 0.45
cloneMethod · 0.45
get_attrMethod · 0.45

Tested by

no test coverage detected