@@ -21,7 +21,8 @@ fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
2121 }
2222
2323 // Time to search for module in any place:
24- let filepath = find_source ( module) . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
24+ let filepath =
25+ find_source ( vm, module) . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
2526 let source = parser:: read_file ( filepath. as_path ( ) )
2627 . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
2728
@@ -62,14 +63,29 @@ pub fn import(vm: &mut VirtualMachine, module: &String, symbol: &Option<String>)
6263 Ok ( obj)
6364}
6465
65- fn find_source ( name : & String ) -> io:: Result < PathBuf > {
66+ fn find_source ( vm : & VirtualMachine , name : & String ) -> io:: Result < PathBuf > {
67+ let sys_path = vm. sys_module . get_item ( "path" ) . unwrap ( ) ;
68+ let paths: Vec < PathBuf > = match sys_path. borrow ( ) . kind {
69+ PyObjectKind :: List { ref elements } => elements
70+ . iter ( )
71+ . filter_map ( |item| match item. borrow ( ) . kind {
72+ PyObjectKind :: String { ref value } => Some ( PathBuf :: from ( value) ) ,
73+ _ => None ,
74+ } ) . collect ( ) ,
75+ _ => panic ! ( "sys.path unexpectedly not a list" ) ,
76+ } ;
77+
6678 let suffixes = [ ".py" , "/__init__.py" ] ;
67- let filepaths = suffixes
68- . iter ( )
69- . map ( |suffix| format ! ( "{}{}" , name, suffix) )
70- . map ( |filename| PathBuf :: from ( filename) ) ;
79+ let mut filepaths = vec ! [ ] ;
80+ for path in paths {
81+ for suffix in suffixes. iter ( ) {
82+ let mut filepath = path. clone ( ) ;
83+ filepath. push ( format ! ( "{}{}" , name, suffix) ) ;
84+ filepaths. push ( filepath) ;
85+ }
86+ }
7187
72- match filepaths. filter ( |p| p. exists ( ) ) . next ( ) {
88+ match filepaths. iter ( ) . filter ( |p| p. exists ( ) ) . next ( ) {
7389 Some ( path) => Ok ( path. to_path_buf ( ) ) ,
7490 None => Err ( io:: Error :: new (
7591 NotFound ,
0 commit comments