@@ -15,6 +15,7 @@ use crate::bytecode;
15
15
use crate :: frame:: { ExecutionResult , Frame , FrameRef , Scope } ;
16
16
use crate :: frozen;
17
17
use crate :: function:: PyFuncArgs ;
18
+ use crate :: import;
18
19
use crate :: obj:: objbool;
19
20
use crate :: obj:: objbuiltinfunc:: PyBuiltinFunction ;
20
21
use crate :: obj:: objcode:: { PyCode , PyCodeRef } ;
@@ -305,30 +306,33 @@ impl VirtualMachine {
305
306
306
307
pub fn import ( & self , module : & str , from_list : & PyObjectRef , level : usize ) -> PyResult {
307
308
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
+ ] ,
317
333
)
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) )
332
336
}
333
337
334
338
/// Determines if `obj` is an instance of `cls`, either directly, indirectly or virtually via
0 commit comments