Skip to content

Commit 4f8ef16

Browse files
committed
Re-organize vm.run_script inner functions
1 parent 8715ae7 commit 4f8ef16

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

crates/vm/src/vm/compile.rs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ impl VirtualMachine {
2626
compiler::compile(source, mode, &source_path, opts).map(|code| self.ctx.new_code(code))
2727
}
2828

29+
// pymain_run_file_obj
2930
pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> {
31+
// when pymain_run_module?
3032
if get_importer(path, self)?.is_some() {
3133
self.insert_sys_path(self.new_pyobj(path))?;
3234
let runpy = self.import("runpy", 0)?;
@@ -35,6 +37,7 @@ impl VirtualMachine {
3537
return Ok(());
3638
}
3739

40+
// TODO: check if this is proper place
3841
if !self.state.settings.safe_path {
3942
let dir = std::path::Path::new(path)
4043
.parent()
@@ -44,19 +47,61 @@ impl VirtualMachine {
4447
self.insert_sys_path(self.new_pyobj(dir))?;
4548
}
4649

47-
match std::fs::read_to_string(path) {
48-
Ok(source) => {
49-
self.run_code_string(scope, &source, path.to_owned())?;
50+
self.run_any_file(scope, path)
51+
}
52+
53+
// = _PyRun_AnyFileObject
54+
fn run_any_file(&self, scope: Scope, path: &str) -> PyResult<()> {
55+
let path = if path.is_empty() { "???" } else { path };
56+
57+
self.run_simple_file(scope, path)
58+
}
59+
60+
// = _PyRun_SimpleFileObject
61+
fn run_simple_file(&self, scope: Scope, path: &str) -> PyResult<()> {
62+
// __main__ is given by scope
63+
let sys_modules = self.sys_module.get_attr(identifier!(self, modules), self)?;
64+
let main_module = sys_modules.get_item(identifier!(self, __main__), self)?;
65+
let module_dict = main_module.dict().expect("main module must have __dict__");
66+
if !module_dict.contains_key(identifier!(self, __file__), self) {
67+
module_dict.set_item(
68+
identifier!(self, __file__),
69+
self.ctx.new_str(path).into(),
70+
self,
71+
)?;
72+
}
73+
74+
// Consider to use enum to distinguish `path`
75+
// https://github.com/RustPython/RustPython/pull/6276#discussion_r2529849479
76+
77+
// TODO: check .pyc here
78+
let pyc = false;
79+
if pyc {
80+
todo!("running pyc is not implemented yet");
81+
} else {
82+
if path != "<stdin>" {
83+
// TODO: set_main_loader(dict, filename, "SourceFileLoader");
5084
}
51-
Err(err) => {
52-
error!("Failed reading file '{path}': {err}");
53-
// TODO: Need to change to ExitCode or Termination
54-
std::process::exit(1);
85+
// TODO: replace to something equivalent to py_run_file
86+
match std::fs::read_to_string(path) {
87+
Ok(source) => {
88+
let code_obj = self
89+
.compile(&source, compiler::Mode::Exec, path.to_owned())
90+
.map_err(|err| self.new_syntax_error(&err, Some(&source)))?;
91+
// trace!("Code object: {:?}", code_obj.borrow());
92+
self.run_code_obj(code_obj, scope)?;
93+
}
94+
Err(err) => {
95+
error!("Failed reading file '{path}': {err}");
96+
// TODO: Need to change to ExitCode or Termination
97+
std::process::exit(1);
98+
}
5599
}
56100
}
57101
Ok(())
58102
}
59103

104+
// TODO: deprecate or reimplement using other primitive functions
60105
pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult {
61106
let code_obj = self
62107
.compile(source, compiler::Mode::Exec, source_path.clone())

crates/vm/src/vm/context.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ declare_const_name! {
102102
__ceil__,
103103
__cformat__,
104104
__class__,
105-
__classcell__,
106105
__class_getitem__,
106+
__classcell__,
107107
__complex__,
108108
__contains__,
109109
__copy__,
@@ -234,26 +234,27 @@ declare_const_name! {
234234
_attributes,
235235
_fields,
236236
_showwarnmsg,
237+
backslashreplace,
238+
close,
239+
copy,
237240
decode,
238241
encode,
239-
keys,
240-
items,
241-
values,
242-
version,
243-
update,
244-
copy,
245242
flush,
246-
close,
247-
WarningMessage,
248-
strict,
249243
ignore,
250-
replace,
251-
xmlcharrefreplace,
252-
backslashreplace,
244+
items,
245+
keys,
246+
modules,
253247
namereplace,
254-
surrogatepass,
248+
replace,
249+
strict,
255250
surrogateescape,
251+
surrogatepass,
252+
update,
256253
utf_8: "utf-8",
254+
values,
255+
version,
256+
WarningMessage,
257+
xmlcharrefreplace,
257258
}
258259

259260
// Basic objects:

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn install_pip(installer: InstallPipMode, scope: Scope, vm: &VirtualMachine) ->
154154
}
155155
}
156156

157+
// pymain_run_python
157158
fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
158159
#[cfg(feature = "flame-it")]
159160
let main_guard = flame::start_guard("RustPython main");
@@ -206,9 +207,10 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
206207
vm.run_module(&module)
207208
}
208209
RunMode::InstallPip(installer) => install_pip(installer, scope.clone(), vm),
209-
RunMode::Script(script) => {
210-
debug!("Running script {}", &script);
211-
vm.run_script(scope.clone(), &script)
210+
RunMode::Script(script_path) => {
211+
// pymain_run_file
212+
debug!("Running script {}", &script_path);
213+
vm.run_script(scope.clone(), &script_path)
212214
}
213215
RunMode::Repl => Ok(()),
214216
};

0 commit comments

Comments
 (0)