1
+ use crate :: compile;
2
+ use crate :: import:: import_file;
3
+ use crate :: obj:: objcode:: PyCodeRef ;
4
+ use crate :: obj:: objmodule:: PyModuleRef ;
5
+ use crate :: obj:: objstr;
1
6
use crate :: obj:: objstr:: PyStringRef ;
2
- use crate :: pyobject:: { PyObjectRef , PyResult } ;
7
+ use crate :: pyobject:: { ItemProtocol , PyObjectRef , PyResult } ;
3
8
use crate :: vm:: VirtualMachine ;
4
9
5
10
fn imp_extension_suffixes ( vm : & VirtualMachine ) -> PyResult {
@@ -25,6 +30,53 @@ fn imp_is_builtin(name: PyStringRef, vm: &VirtualMachine) -> bool {
25
30
vm. stdlib_inits . borrow ( ) . contains_key ( name. as_str ( ) )
26
31
}
27
32
33
+ fn imp_is_frozen ( name : PyStringRef , vm : & VirtualMachine ) -> bool {
34
+ vm. frozen . borrow ( ) . contains_key ( name. as_str ( ) )
35
+ }
36
+
37
+ fn imp_create_builtin ( spec : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
38
+ let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
39
+
40
+ let name = & objstr:: get_value ( & vm. get_attribute ( spec. clone ( ) , "name" ) ?) ;
41
+
42
+ if let Ok ( module) = sys_modules. get_item ( name, vm) {
43
+ Ok ( module)
44
+ } else {
45
+ if let Some ( make_module_func) = vm. stdlib_inits . borrow ( ) . get ( name) {
46
+ Ok ( make_module_func ( vm) )
47
+ } else {
48
+ Ok ( vm. get_none ( ) )
49
+ }
50
+ }
51
+ }
52
+
53
+ fn imp_exec_builtin ( _mod : PyModuleRef , _vm : & VirtualMachine ) -> i32 {
54
+ // TOOD: Should we do something here?
55
+ 0
56
+ }
57
+
58
+ fn imp_get_frozen_object ( name : PyStringRef , vm : & VirtualMachine ) -> PyResult < PyCodeRef > {
59
+ if let Some ( frozen) = vm. frozen . borrow ( ) . get ( name. as_str ( ) ) {
60
+ compile:: compile ( vm, frozen, & compile:: Mode :: Exec , "frozen" . to_string ( ) )
61
+ . map_err ( |err| vm. new_syntax_error ( & err) )
62
+ } else {
63
+ Err ( vm. new_import_error ( format ! ( "No such frozen object named {}" , name. as_str( ) ) ) )
64
+ }
65
+ }
66
+
67
+ fn imp_init_frozen ( name : PyStringRef , vm : & VirtualMachine ) -> PyResult {
68
+ if let Some ( frozen) = vm. frozen . borrow ( ) . get ( name. as_str ( ) ) {
69
+ import_file ( vm, name. as_str ( ) , "frozen" . to_string ( ) , frozen. to_string ( ) )
70
+ } else {
71
+ Err ( vm. new_import_error ( format ! ( "No such frozen object named {}" , name. as_str( ) ) ) )
72
+ }
73
+ }
74
+
75
+ fn imp_is_frozen_package ( _name : PyStringRef , _vm : & VirtualMachine ) -> bool {
76
+ // TODO: Support frozen package.
77
+ false
78
+ }
79
+
28
80
pub fn make_module ( vm : & VirtualMachine ) -> PyObjectRef {
29
81
let ctx = & vm. ctx ;
30
82
let module = py_module ! ( vm, "_imp" , {
@@ -33,6 +85,12 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
33
85
"release_lock" => ctx. new_rustfunc( imp_release_lock) ,
34
86
"lock_held" => ctx. new_rustfunc( imp_lock_held) ,
35
87
"is_builtin" => ctx. new_rustfunc( imp_is_builtin) ,
88
+ "is_frozen" => ctx. new_rustfunc( imp_is_frozen) ,
89
+ "create_builtin" => ctx. new_rustfunc( imp_create_builtin) ,
90
+ "exec_builtin" => ctx. new_rustfunc( imp_exec_builtin) ,
91
+ "get_frozen_object" => ctx. new_rustfunc( imp_get_frozen_object) ,
92
+ "init_frozen" => ctx. new_rustfunc( imp_init_frozen) ,
93
+ "is_frozen_package" => ctx. new_rustfunc( imp_is_frozen_package) ,
36
94
} ) ;
37
95
38
96
module
0 commit comments