@@ -13,15 +13,15 @@ use super::compile;
1313use super :: pyobject:: { PyObject , PyObjectKind , PyResult , DictProtocol } ;
1414use super :: vm:: VirtualMachine ;
1515
16- pub fn import ( vm : & mut VirtualMachine , name : & String ) -> PyResult {
16+ fn import_module ( vm : & mut VirtualMachine , module : & String ) -> PyResult {
1717 // First, see if we already loaded the module:
1818 let sys_modules = vm. sys_module . get_item ( & "modules" . to_string ( ) ) ;
19- if sys_modules. contains_key ( name ) {
20- return Ok ( sys_modules. get_item ( name ) )
19+ if sys_modules. contains_key ( module ) {
20+ return Ok ( sys_modules. get_item ( module ) ) ;
2121 }
2222
2323 // Time to search for module in any place:
24- let filepath = find_source ( name ) . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
24+ let filepath = find_source ( module ) . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
2525 let source = parser:: read_file ( filepath. as_path ( ) )
2626 . map_err ( |e| vm. new_exception ( format ! ( "Error: {:?}" , e) ) ) ?;
2727
@@ -42,14 +42,23 @@ pub fn import(vm: &mut VirtualMachine, name: &String) -> PyResult {
4242 Ok ( _) => { }
4343 Err ( value) => return Err ( value) ,
4444 }
45+ Ok ( scope)
46+ }
4547
46- let obj = PyObject :: new (
47- PyObjectKind :: Module {
48- name : name. clone ( ) ,
49- dict : scope. clone ( ) ,
50- } ,
51- vm. get_type ( ) ,
52- ) ;
48+ pub fn import ( vm : & mut VirtualMachine , module : & String , symbol : & Option < String > ) -> PyResult {
49+ let scope = import_module ( vm, module) ?;
50+ // If we're importing a symbol, look it up and use it, otherwise construct a module and return
51+ // that
52+ let obj = match symbol {
53+ Some ( symbol) => scope. get_item ( symbol) ,
54+ None => PyObject :: new (
55+ PyObjectKind :: Module {
56+ name : module. clone ( ) ,
57+ dict : scope. clone ( ) ,
58+ } ,
59+ vm. get_type ( ) ,
60+ ) ,
61+ } ;
5362 Ok ( obj)
5463}
5564
0 commit comments