22 * Import mechanics
33 */
44
5- use std:: path:: PathBuf ;
6-
75use crate :: bytecode:: CodeObject ;
86use crate :: frame:: Scope ;
9- use crate :: obj:: { objcode, objsequence , objstr } ;
7+ use crate :: obj:: objcode;
108use crate :: pyobject:: { ItemProtocol , PyResult , PyValue } ;
11- use crate :: util;
129use crate :: vm:: VirtualMachine ;
1310#[ cfg( feature ="rustpython-compiler" ) ]
1411use rustpython_compiler:: compile;
@@ -20,7 +17,7 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
2017 vm. invoke ( install, vec ! [ vm. sys_module. clone( ) , impmod] ) ?;
2118 vm. import_func
2219. replace ( vm. get_attribute ( importlib. clone ( ) , "__import__" ) ?) ;
23- if external{
20+ if external&& cfg ! ( feature = "rustpython-compiler" ) {
2421let install_external =
2522 vm. get_attribute ( importlib. clone ( ) , "_install_external_importers" ) ?;
2623 vm. invoke ( install_external, vec ! [ ] ) ?;
@@ -49,39 +46,6 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
4946} )
5047}
5148
52- pub fn import_module ( vm : & VirtualMachine , current_path : PathBuf , module_name : & str ) ->PyResult {
53- // Cached modules:
54- let sys_modules = vm. get_attribute ( vm. sys_module . clone ( ) , "modules" ) . unwrap ( ) ;
55-
56- // First, see if we already loaded the module:
57- if let Ok ( module) = sys_modules. get_item ( module_name. to_string ( ) , vm) {
58- Ok ( module)
59- } else if vm. frozen . borrow ( ) . contains_key ( module_name) {
60- import_frozen ( vm, module_name)
61- } else if vm. stdlib_inits . borrow ( ) . contains_key ( module_name) {
62- import_builtin ( vm, module_name)
63- } else if cfg ! ( feature ="rustpython-compiler" ) {
64- let notfound_error =& vm. ctx . exceptions . module_not_found_error ;
65- let import_error =& vm. ctx . exceptions . import_error ;
66-
67- // Time to search for module in any place:
68- let file_path =find_source ( vm, current_path, module_name)
69- . map_err ( |e| vm. new_exception ( notfound_error. clone ( ) , e) ) ?;
70- let source = util:: read_file ( file_path. as_path ( ) )
71- . map_err ( |e| vm. new_exception ( import_error. clone ( ) , e. to_string ( ) ) ) ?;
72-
73- import_file (
74- vm,
75- module_name,
76- file_path. to_str ( ) . unwrap ( ) . to_string ( ) ,
77- source,
78- )
79- } else {
80- let notfound_error =& vm. ctx . exceptions . module_not_found_error ;
81- Err ( vm. new_exception ( notfound_error. clone ( ) , module_name. to_string ( ) ) )
82- }
83- }
84-
8549#[ cfg( feature ="rustpython-compiler" ) ]
8650pub fn import_file (
8751vm : & VirtualMachine ,
@@ -118,29 +82,3 @@ pub fn import_codeobj(
11882) ?;
11983Ok ( module)
12084}
121-
122- fn find_source ( vm : & VirtualMachine , current_path : PathBuf , name : & str ) ->Result < PathBuf , String > {
123- let sys_path = vm. get_attribute ( vm. sys_module . clone ( ) , "path" ) . unwrap ( ) ;
124- let mut paths: Vec < PathBuf > = objsequence:: get_elements_list ( & sys_path)
125- . iter ( )
126- . map ( |item|PathBuf :: from ( objstr:: get_value ( item) ) )
127- . collect ( ) ;
128-
129- paths. insert ( 0 , current_path) ;
130-
131- let rel_name = name. replace ( '.' , "/" ) ;
132- let suffixes =[ ".py" , "/__init__.py" ] ;
133- let mut file_paths =vec ! [ ] ;
134- for pathin paths{
135- for suffixin suffixes. iter ( ) {
136- let mut file_path = path. clone ( ) ;
137- file_path. push ( format ! ( "{}{}" , rel_name, suffix) ) ;
138- file_paths. push ( file_path) ;
139- }
140- }
141-
142- match file_paths. iter ( ) . find ( |p| p. exists ( ) ) {
143- Some ( path) =>Ok ( path. to_path_buf ( ) ) ,
144- None =>Err ( format ! ( "No module named '{}'" , name) ) ,
145- }
146- }