Set SourceFileLoader for script execution in run_simple_file (#6496)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
This commit is contained in:
Copilotandyouknowone authored and GitHub committed 2025-12-25 10:33:22 +09:00
1 parent 5122aed738
commit 6dbc8f0cfa
2 files changed
+11 -3

No files matched your search

-2
View File
@@ -225,8 +225,6 @@ class CmdLineTest(unittest.TestCase):
def test_repl_stderr_flush_separate_stderr(self):
self.check_repl_stderr_flush(True)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_basic_script(self):
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script')
+11 -1
View File
@@ -81,7 +81,7 @@ impl VirtualMachine {
todo!("running pyc is not implemented yet");
} else {
if path != "<stdin>" {
// TODO: set_main_loader(dict, filename, "SourceFileLoader");
set_main_loader(&module_dict, path, self)?;
}
// TODO: replace to something equivalent to py_run_file
match std::fs::read_to_string(path) {
@@ -125,6 +125,16 @@ impl VirtualMachine {
}
}
fn set_main_loader(module_dict: &PyDictRef, filename: &str, vm: &VirtualMachine) -> PyResult<()> {
vm.import("importlib.machinery", 0)?;
let sys_modules = vm.sys_module.get_attr(identifier!(vm, modules), vm)?;
let machinery = sys_modules.get_item("importlib.machinery", vm)?;
let loader_class = machinery.get_attr("SourceFileLoader", vm)?;
let loader = loader_class.call((identifier!(vm, __main__).to_owned(), filename), vm)?;
module_dict.set_item("__loader__", loader, vm)?;
Ok(())
}
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?;
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;