@@ -735,7 +735,7 @@ fn builtin_zip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
735
735
736
736
// builtin___import__
737
737
738
- pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
738
+ pub fn make_module ( ctx : & PyContext , builtin_overrides : HashMap < String , PyObjectRef > ) -> PyObjectRef {
739
739
// scope[String::from("print")] = print;
740
740
let mut dict = HashMap :: new ( ) ;
741
741
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
@@ -840,6 +840,10 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
840
840
ctx. exceptions . value_error . clone ( ) ,
841
841
) ;
842
842
843
+ for ( name, py_object) in builtin_overrides {
844
+ dict. insert ( name, py_object) ;
845
+ }
846
+
843
847
let d2 = PyObject :: new ( PyObjectKind :: Dict { elements : dict } , ctx. type_type ( ) ) ;
844
848
let scope = PyObject :: new (
845
849
PyObjectKind :: Scope {
@@ -897,3 +901,26 @@ pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> Py
897
901
898
902
vm. call_method ( & metaclass, "__call__" , vec ! [ name_arg, bases, namespace] )
899
903
}
904
+
905
+ #[ cfg( test) ]
906
+ mod tests {
907
+ use super :: * ;
908
+ use super :: super :: pyobject:: PyContext ;
909
+
910
+ #[ test]
911
+ fn test_make_module ( ) {
912
+ let ctx = PyContext :: new ( ) ;
913
+ let builtin_module = make_module ( & ctx, HashMap :: new ( ) ) ;
914
+ assert ! ( builtin_module. get_attr( "print" ) . is_some( ) ) ;
915
+ }
916
+
917
+ #[ test]
918
+ fn test_make_module_override ( ) {
919
+ let ctx = PyContext :: new ( ) ;
920
+ let mut overrides = HashMap :: new ( ) ;
921
+ overrides. insert ( String :: from ( "test_function" ) , ctx. object ( ) ) ;
922
+ let builtin_module = make_module ( & ctx, overrides) ;
923
+ assert ! ( builtin_module. get_attr( "test_function" ) . is_some( ) ) ;
924
+ }
925
+
926
+ }
0 commit comments