Repository: RustPython/RustPython (language: rust)

Issue:
Add _imp._override_frozen_modules_for_tests
https://github.com/RustPython/RustPython/pull/4537/files#diff-9164b29482098900098a8f331d75f919a81d6e8df1f3861eaacdb10967dd7257R105

CPython source code: https://github.com/python/cpython/blob/v3.11.2/Python/import.c#L2291-L2310

We need to investigate what `override_frozen_modules` does in CPython and make something similar.


Discussion / hints:

Fix patch:
diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs
index f5a1eddf2e..b1b2dbcb82 100644
--- a/vm/src/stdlib/imp.rs
+++ b/vm/src/stdlib/imp.rs
@@ -116,6 +116,11 @@ mod _imp {
             .ok_or_else(|| vm.new_import_error(format!("No such frozen object named {name}"), name))
     }
 
+    #[pyfunction]
+    fn _override_frozen_modules_for_tests(value: isize, vm: &VirtualMachine) {
+        vm.state.override_frozen_modules.store(value);
+    }
+
     #[pyfunction]
     fn _fix_co_filename(_code: PyObjectRef, _path: PyStrRef) {
         // TODO:
diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs
index b8a25512cd..2b32b6db04 100644
--- a/vm/src/vm/mod.rs
+++ b/vm/src/vm/mod.rs
@@ -96,6 +96,7 @@ pub struct PyGlobalState {
     pub codec_registry: CodecsRegistry,
     pub finalizing: AtomicBool,
     pub warnings: WarningsState,
+    pub override_frozen_modules: AtomicCell<isize>,
 }
 
 pub fn process_hash_secret_seed() -> u32 {
@@ -172,6 +173,7 @@ impl VirtualMachine {
                 codec_registry,
                 finalizing: AtomicBool::new(false),
                 warnings,
+                override_frozen_modules: AtomicCell::new(0),
             }),
             initialized: false,
             recursion_depth: Cell::new(0),


Test patch: