@@ -25,24 +25,24 @@ pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
25
25
}
26
26
27
27
pub fn import_frozen ( vm : & VirtualMachine , module_name : & str ) -> PyResult {
28
- if let Some ( frozen) = vm. frozen . borrow ( ) . get ( module_name) {
29
- let mut frozen = frozen. clone ( ) ;
30
- frozen. source_path = format ! ( "frozen {}" , module_name) ;
31
- import_codeobj ( vm, module_name, frozen)
32
- } else {
33
- Err ( vm. new_import_error ( format ! ( "Cannot import frozen module {}" , module_name) ) )
34
- }
28
+ vm. frozen
29
+ . borrow ( )
30
+ . get ( module_name)
31
+ . ok_or_else ( || vm. new_import_error ( format ! ( "Cannot import frozen module {}" , module_name) ) )
32
+ . and_then ( |frozen| import_codeobj ( vm, module_name, frozen. clone ( ) , false ) )
35
33
}
36
34
37
35
pub fn import_builtin ( vm : & VirtualMachine , module_name : & str ) -> PyResult {
38
- let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
39
- if let Some ( make_module_func) = vm. stdlib_inits . borrow ( ) . get ( module_name) {
40
- let module = make_module_func ( vm) ;
41
- sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
42
- Ok ( module)
43
- } else {
44
- Err ( vm. new_import_error ( format ! ( "Cannot import bultin module {}" , module_name) ) )
45
- }
36
+ vm. stdlib_inits
37
+ . borrow ( )
38
+ . get ( module_name)
39
+ . ok_or_else ( || vm. new_import_error ( format ! ( "Cannot import bultin module {}" , module_name) ) )
40
+ . and_then ( |make_module_func| {
41
+ let module = make_module_func ( vm) ;
42
+ let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) ?;
43
+ sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
44
+ Ok ( module)
45
+ } )
46
46
}
47
47
48
48
pub fn import_module ( vm : & VirtualMachine , current_path : PathBuf , module_name : & str ) -> PyResult {
@@ -57,8 +57,8 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
57
57
} else if vm. stdlib_inits . borrow ( ) . contains_key ( module_name) {
58
58
import_builtin ( vm, module_name)
59
59
} else {
60
- let notfound_error = vm. context ( ) . exceptions . module_not_found_error . clone ( ) ;
61
- let import_error = vm. context ( ) . exceptions . import_error . clone ( ) ;
60
+ let notfound_error = & vm. ctx . exceptions . module_not_found_error ;
61
+ let import_error = & vm. ctx . exceptions . import_error ;
62
62
63
63
// Time to search for module in any place:
64
64
let file_path = find_source ( vm, current_path, module_name)
@@ -83,21 +83,24 @@ pub fn import_file(
83
83
) -> PyResult {
84
84
let code_obj = compile:: compile ( & content, & compile:: Mode :: Exec , file_path)
85
85
. map_err ( |err| vm. new_syntax_error ( & err) ) ?;
86
- import_codeobj ( vm, module_name, code_obj)
86
+ import_codeobj ( vm, module_name, code_obj, true )
87
87
}
88
88
89
- pub fn import_codeobj ( vm : & VirtualMachine , module_name : & str , code_obj : CodeObject ) -> PyResult {
89
+ pub fn import_codeobj (
90
+ vm : & VirtualMachine ,
91
+ module_name : & str ,
92
+ code_obj : CodeObject ,
93
+ set_file_attr : bool ,
94
+ ) -> PyResult {
90
95
let attrs = vm. ctx . new_dict ( ) ;
91
96
attrs. set_item ( "__name__" , vm. new_str ( module_name. to_string ( ) ) , vm) ?;
92
- let file_path = & code_obj. source_path ;
93
- if !file_path. starts_with ( "frozen" ) {
94
- // TODO: Should be less hacky, not depend on source_path
95
- attrs. set_item ( "__file__" , vm. new_str ( file_path. to_owned ( ) ) , vm) ?;
97
+ if set_file_attr {
98
+ attrs. set_item ( "__file__" , vm. new_str ( code_obj. source_path . to_owned ( ) ) , vm) ?;
96
99
}
97
100
let module = vm. ctx . new_module ( module_name, attrs. clone ( ) ) ;
98
101
99
102
// Store module in cache to prevent infinite loop with mutual importing libs:
100
- let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
103
+ let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) ? ;
101
104
sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
102
105
103
106
// Execute main code in module:
0 commit comments