@@ -11,19 +11,43 @@ use crate::pyobject::{ItemProtocol, PyResult};
11
11
use crate :: util;
12
12
use crate :: vm:: VirtualMachine ;
13
13
14
+ pub fn init_importlib ( vm : & VirtualMachine ) -> PyResult {
15
+ let importlib = import_frozen ( vm, "_frozen_importlib" ) ?;
16
+ let impmod = import_builtin ( vm, "_imp" ) ?;
17
+ let install = vm. get_attribute ( importlib, "_install" ) ?;
18
+ vm. invoke ( install, vec ! [ vm. sys_module. clone( ) , impmod] )
19
+ }
20
+
21
+ fn import_frozen ( vm : & VirtualMachine , module_name : & str ) -> PyResult {
22
+ if let Some ( frozen) = vm. frozen . borrow ( ) . get ( module_name) {
23
+ import_file ( vm, module_name, "frozen" . to_string ( ) , frozen. to_string ( ) )
24
+ } else {
25
+ Err ( vm. new_import_error ( format ! ( "Cannot import frozen module {}" , module_name) ) )
26
+ }
27
+ }
28
+
29
+ fn import_builtin ( vm : & VirtualMachine , module_name : & str ) -> PyResult {
30
+ let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
31
+ if let Some ( make_module_func) = vm. stdlib_inits . borrow ( ) . get ( module_name) {
32
+ let module = make_module_func ( vm) ;
33
+ sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
34
+ Ok ( module)
35
+ } else {
36
+ Err ( vm. new_import_error ( format ! ( "Cannot import bultin module {}" , module_name) ) )
37
+ }
38
+ }
39
+
14
40
pub fn import_module ( vm : & VirtualMachine , current_path : PathBuf , module_name : & str ) -> PyResult {
15
41
// Cached modules:
16
42
let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
17
43
18
44
// First, see if we already loaded the module:
19
45
if let Ok ( module) = sys_modules. get_item ( module_name. to_string ( ) , vm) {
20
46
Ok ( module)
21
- } else if let Some ( frozen) = vm. frozen . borrow ( ) . get ( module_name) {
22
- import_file ( vm, module_name, "frozen" . to_string ( ) , frozen. to_string ( ) )
23
- } else if let Some ( make_module_func) = vm. stdlib_inits . borrow ( ) . get ( module_name) {
24
- let module = make_module_func ( vm) ;
25
- sys_modules. set_item ( module_name, module. clone ( ) , vm) ?;
26
- Ok ( module)
47
+ } else if vm. frozen . borrow ( ) . contains_key ( module_name) {
48
+ import_frozen ( vm, module_name)
49
+ } else if vm. stdlib_inits . borrow ( ) . contains_key ( module_name) {
50
+ import_builtin ( vm, module_name)
27
51
} else {
28
52
let notfound_error = vm. context ( ) . exceptions . module_not_found_error . clone ( ) ;
29
53
let import_error = vm. context ( ) . exceptions . import_error . clone ( ) ;
0 commit comments