(
vm: &VirtualMachine,
module_name: &str,
code_obj: PyRef<PyCode>,
set_file_attr: bool,
)
| 175 | } |
| 176 | |
| 177 | pub 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 | |
| 208 | fn remove_importlib_frames_inner( |
| 209 | vm: &VirtualMachine, |
no test coverage detected