@@ -15,6 +15,7 @@ use crate::bytecode;
1515use crate :: frame:: { ExecutionResult , Frame , FrameRef , Scope } ;
1616use crate :: frozen;
1717use crate :: function:: PyFuncArgs ;
18+ use crate :: import;
1819use crate :: obj:: objbool;
1920use crate :: obj:: objbuiltinfunc:: PyBuiltinFunction ;
2021use crate :: obj:: objcode:: { PyCode , PyCodeRef } ;
@@ -305,30 +306,33 @@ impl VirtualMachine {
305306
306307 pub fn import ( & self , module : & str , from_list : & PyObjectRef , level : usize ) -> PyResult {
307308 let sys_modules = self . get_attribute ( self . sys_module . clone ( ) , "modules" ) ?;
308- sys_modules. get_item ( module. to_string ( ) , self ) . or_else ( |_| {
309- let import_func = self
310- . get_attribute ( self . builtins . clone ( ) , "__import__" )
311- . map_err ( |_| self . new_import_error ( "__import__ not found" . to_string ( ) ) ) ?;
312-
313- let ( locals, globals) = if let Some ( frame) = self . current_frame ( ) {
314- (
315- frame. scope . get_locals ( ) . into_object ( ) ,
316- frame. scope . globals . clone ( ) . into_object ( ) ,
309+ sys_modules
310+ . get_item ( module. to_string ( ) , self )
311+ . or_else ( |_| {
312+ let import_func = self
313+ . get_attribute ( self . builtins . clone ( ) , "__import__" )
314+ . map_err ( |_| self . new_import_error ( "__import__ not found" . to_string ( ) ) ) ?;
315+
316+ let ( locals, globals) = if let Some ( frame) = self . current_frame ( ) {
317+ (
318+ frame. scope . get_locals ( ) . into_object ( ) ,
319+ frame. scope . globals . clone ( ) . into_object ( ) ,
320+ )
321+ } else {
322+ ( self . get_none ( ) , self . get_none ( ) )
323+ } ;
324+ self . invoke (
325+ import_func,
326+ vec ! [
327+ self . ctx. new_str( module. to_string( ) ) ,
328+ globals,
329+ locals,
330+ from_list. clone( ) ,
331+ self . ctx. new_int( level) ,
332+ ] ,
317333 )
318- } else {
319- ( self . get_none ( ) , self . get_none ( ) )
320- } ;
321- self . invoke (
322- import_func,
323- vec ! [
324- self . ctx. new_str( module. to_string( ) ) ,
325- globals,
326- locals,
327- from_list. clone( ) ,
328- self . ctx. new_int( level) ,
329- ] ,
330- )
331- } )
334+ } )
335+ . map_err ( |exc| import:: remove_importlib_frames ( self , & exc) )
332336 }
333337
334338 /// Determines if `obj` is an instance of `cls`, either directly, indirectly or virtually via
0 commit comments