@@ -11,54 +11,47 @@ use crate::pyobject::{ItemProtocol, PyResult};
11
11
use crate :: util;
12
12
use crate :: vm:: VirtualMachine ;
13
13
14
- fn import_uncached_module (
15
- vm : & VirtualMachine ,
16
- current_path : PathBuf ,
17
- module_name : & str ,
18
- ) -> PyResult {
19
- // Check for Rust-native modules
20
- if let Some ( module) = vm. stdlib_inits . borrow ( ) . get ( module_name) {
21
- return Ok ( module ( vm) . clone ( ) ) ;
22
- }
14
+ pub fn import_module ( vm : & VirtualMachine , current_path : PathBuf , module_name : & str ) -> PyResult {
15
+ // Cached modules:
16
+ let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
23
17
24
- let notfound_error = vm. context ( ) . exceptions . module_not_found_error . clone ( ) ;
25
- let import_error = vm. context ( ) . exceptions . import_error . clone ( ) ;
18
+ // First, see if we already loaded the module:
19
+ if let Ok ( module) = sys_modules. get_item ( module_name. to_string ( ) , vm) {
20
+ Ok ( module)
21
+ } else if let Some ( make_module_func) = vm. stdlib_inits . borrow ( ) . get ( module_name) {
22
+ let module = make_module_func ( vm) ;
23
+ sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
24
+ Ok ( module)
25
+ } else {
26
+ let notfound_error = vm. context ( ) . exceptions . module_not_found_error . clone ( ) ;
27
+ let import_error = vm. context ( ) . exceptions . import_error . clone ( ) ;
26
28
27
- // Time to search for module in any place:
28
- let file_path = find_source ( vm, current_path, module_name)
29
- . map_err ( |e| vm. new_exception ( notfound_error. clone ( ) , e) ) ?;
30
- let source = util:: read_file ( file_path. as_path ( ) )
31
- . map_err ( |e| vm. new_exception ( import_error. clone ( ) , e. to_string ( ) ) ) ?;
32
- let code_obj = compile:: compile (
33
- vm,
34
- & source,
35
- & compile:: Mode :: Exec ,
36
- file_path. to_str ( ) . unwrap ( ) . to_string ( ) ,
37
- )
38
- . map_err ( |err| vm. new_syntax_error ( & err) ) ?;
39
- // trace!("Code object: {:?}", code_obj);
29
+ // Time to search for module in any place:
30
+ let file_path = find_source ( vm, current_path, module_name)
31
+ . map_err ( |e| vm. new_exception ( notfound_error. clone ( ) , e) ) ?;
32
+ let source = util:: read_file ( file_path. as_path ( ) )
33
+ . map_err ( |e| vm. new_exception ( import_error. clone ( ) , e. to_string ( ) ) ) ?;
34
+ let code_obj = compile:: compile (
35
+ vm,
36
+ & source,
37
+ & compile:: Mode :: Exec ,
38
+ file_path. to_str ( ) . unwrap ( ) . to_string ( ) ,
39
+ )
40
+ . map_err ( |err| vm. new_syntax_error ( & err) ) ?;
41
+ // trace!("Code object: {:?}", code_obj);
40
42
41
- let attrs = vm. ctx . new_dict ( ) ;
42
- attrs. set_item ( "__name__" , vm. new_str ( module_name. to_string ( ) ) , vm) ?;
43
- let module = vm. ctx . new_module ( module_name, attrs. clone ( ) ) ;
43
+ let attrs = vm. ctx . new_dict ( ) ;
44
+ attrs. set_item ( "__name__" , vm. new_str ( module_name. to_string ( ) ) , vm) ?;
45
+ let module = vm. ctx . new_module ( module_name, attrs. clone ( ) ) ;
44
46
45
- // Store module in cache to prevent infinite loop with mutual importing libs:
46
- let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
47
- sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
47
+ // Store module in cache to prevent infinite loop with mutual importing libs:
48
+ sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
48
49
49
- // Execute main code in module:
50
- vm. run_code_obj ( code_obj, Scope :: new ( None , attrs) ) ?;
51
- Ok ( module)
52
- }
50
+ // Execute main code in module:
51
+ vm. run_code_obj ( code_obj, Scope :: new ( None , attrs) ) ?;
53
52
54
- pub fn import_module ( vm : & VirtualMachine , current_path : PathBuf , module_name : & str ) -> PyResult {
55
- // First, see if we already loaded the module:
56
- let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
57
- if let Ok ( module) = sys_modules. get_item ( module_name. to_string ( ) , vm) {
58
- return Ok ( module) ;
53
+ Ok ( module)
59
54
}
60
- let module = import_uncached_module ( vm, current_path, module_name) ?;
61
- Ok ( module)
62
55
}
63
56
64
57
fn find_source ( vm : & VirtualMachine , current_path : PathBuf , name : & str ) -> Result < PathBuf , String > {
0 commit comments