1
1
use std:: cell:: RefCell ;
2
2
use std:: fmt;
3
- use std:: path:: PathBuf ;
4
3
use std:: rc:: Rc ;
5
4
6
5
use num_bigint:: BigInt ;
@@ -9,7 +8,6 @@ use rustpython_parser::ast;
9
8
10
9
use crate :: builtins;
11
10
use crate :: bytecode;
12
- use crate :: import:: { import, import_module} ;
13
11
use crate :: obj:: objbool;
14
12
use crate :: obj:: objbuiltinfunc:: PyBuiltinFunction ;
15
13
use crate :: obj:: objcode;
@@ -22,8 +20,8 @@ use crate::obj::objslice::PySlice;
22
20
use crate :: obj:: objstr;
23
21
use crate :: obj:: objtype;
24
22
use crate :: pyobject:: {
25
- DictProtocol , IdProtocol , PyContext , PyFuncArgs , PyObject , PyObjectRef , PyResult , PyValue ,
26
- TryFromObject , TypeProtocol ,
23
+ AttributeProtocol , DictProtocol , IdProtocol , PyContext , PyFuncArgs , PyObject , PyObjectRef ,
24
+ PyResult , PyValue , TryFromObject , TypeProtocol ,
27
25
} ;
28
26
use crate :: vm:: VirtualMachine ;
29
27
@@ -806,30 +804,31 @@ impl Frame {
806
804
module : & str ,
807
805
symbol : & Option < String > ,
808
806
) -> FrameResult {
809
- let current_path = {
810
- let mut source_pathbuf = PathBuf :: from ( & self . code . source_path ) ;
811
- source_pathbuf. pop ( ) ;
812
- source_pathbuf
807
+ let module = vm. import ( module) ?;
808
+
809
+ // If we're importing a symbol, look it up and use it, otherwise construct a module and return
810
+ // that
811
+ let obj = match symbol {
812
+ Some ( symbol) => module. get_attr ( symbol) . map_or_else (
813
+ || {
814
+ let import_error = vm. context ( ) . exceptions . import_error . clone ( ) ;
815
+ Err ( vm. new_exception ( import_error, format ! ( "cannot import name '{}'" , symbol) ) )
816
+ } ,
817
+ Ok ,
818
+ ) ,
819
+ None => Ok ( module) ,
813
820
} ;
814
821
815
- let obj = import ( vm, current_path, module, symbol) ?;
816
-
817
822
// Push module on stack:
818
- self . push_value ( obj) ;
823
+ self . push_value ( obj? ) ;
819
824
Ok ( None )
820
825
}
821
826
822
827
fn import_star ( & self , vm : & mut VirtualMachine , module : & str ) -> FrameResult {
823
- let current_path = {
824
- let mut source_pathbuf = PathBuf :: from ( & self . code . source_path ) ;
825
- source_pathbuf. pop ( ) ;
826
- source_pathbuf
827
- } ;
828
+ let module = vm. import ( module) ?;
828
829
829
830
// Grab all the names from the module and put them in the context
830
- let obj = import_module ( vm, current_path, module) ?;
831
-
832
- for ( k, v) in obj. get_key_value_pairs ( ) . iter ( ) {
831
+ for ( k, v) in module. get_key_value_pairs ( ) . iter ( ) {
833
832
self . scope . store_name ( & vm, & objstr:: get_value ( k) , v. clone ( ) ) ;
834
833
}
835
834
Ok ( None )
0 commit comments