Visitar URL original
Merge remote-tracking branch 'upstream/main' into move-stdlib · RustPython/RustPython@1586f29 · GitHub
Skip to content

Commit 1586f29

Browse files
committed
Merge remote-tracking branch 'upstream/main' into move-stdlib
2 parents 7908f4f + 6991a80 commit 1586f29

7 files changed

Lines changed: 80 additions & 26 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ template = "installer-config/installer.wxs"
122122
resolver = "2"
123123
members = [
124124
".",
125-
"derive",
126125
"wasm/lib",
127126
"crates/*",
128127
]
@@ -140,7 +139,7 @@ rustpython-compiler-core = { path = "crates/compiler-core", version = "0.4.0" }
140139
rustpython-compiler = { path = "crates/compiler", version = "0.4.0" }
141140
rustpython-codegen = { path = "crates/codegen", version = "0.4.0" }
142141
rustpython-common = { path = "crates/common", version = "0.4.0" }
143-
rustpython-derive = { path = "derive", version = "0.4.0" }
142+
rustpython-derive = { path = "crates/derive", version = "0.4.0" }
144143
rustpython-derive-impl = { path = "crates/derive-impl", version = "0.4.0" }
145144
rustpython-jit = { path = "crates/jit", version = "0.4.0" }
146145
rustpython-literal = { path = "crates/literal", version = "0.4.0" }

‎crates/vm/src/vm/compile.rs‎

Lines changed: 53 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,62 @@ 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+
module_dict.set_item(identifier!(self, __cached__), self.ctx.none(), self)?;
73+
}
74+
75+
// Consider to use enum to distinguish `path`
76+
// https://github.com/RustPython/RustPython/pull/6276#discussion_r2529849479
77+
78+
// TODO: check .pyc here
79+
let pyc = false;
80+
if pyc {
81+
todo!("running pyc is not implemented yet");
82+
} else {
83+
if path != "<stdin>" {
84+
// TODO: set_main_loader(dict, filename, "SourceFileLoader");
5085
}
51-
Err(err) => {
52-
error!("Failed reading file '{path}': {err}");
53-
// TODO: Need to change to ExitCode or Termination
54-
std::process::exit(1);
86+
// TODO: replace to something equivalent to py_run_file
87+
match std::fs::read_to_string(path) {
88+
Ok(source) => {
89+
let code_obj = self
90+
.compile(&source, compiler::Mode::Exec, path.to_owned())
91+
.map_err(|err| self.new_syntax_error(&err, Some(&source)))?;
92+
// trace!("Code object: {:?}", code_obj.borrow());
93+
self.run_code_obj(code_obj, scope)?;
94+
}
95+
Err(err) => {
96+
error!("Failed reading file '{path}': {err}");
97+
// TODO: Need to change to ExitCode or Termination
98+
std::process::exit(1);
99+
}
55100
}
56101
}
57102
Ok(())
58103
}
59104

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

‎crates/vm/src/vm/context.rs‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ declare_const_name! {
9898
__build_class__,
9999
__builtins__,
100100
__bytes__,
101+
__cached__,
101102
__call__,
102103
__ceil__,
103104
__cformat__,
104105
__class__,
105-
__classcell__,
106106
__class_getitem__,
107+
__classcell__,
107108
__complex__,
108109
__contains__,
109110
__copy__,
@@ -234,26 +235,27 @@ declare_const_name! {
234235
_attributes,
235236
_fields,
236237
_showwarnmsg,
238+
backslashreplace,
239+
close,
240+
copy,
237241
decode,
238242
encode,
239-
keys,
240-
items,
241-
values,
242-
version,
243-
update,
244-
copy,
245243
flush,
246-
close,
247-
WarningMessage,
248-
strict,
249244
ignore,
250-
replace,
251-
xmlcharrefreplace,
252-
backslashreplace,
245+
items,
246+
keys,
247+
modules,
253248
namereplace,
254-
surrogatepass,
249+
replace,
250+
strict,
255251
surrogateescape,
252+
surrogatepass,
253+
update,
256254
utf_8: "utf-8",
255+
values,
256+
version,
257+
WarningMessage,
258+
xmlcharrefreplace,
257259
}
258260

259261
// Basic objects:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
main_module = sys.modules["__main__"]
4+
assert main_module.__file__.endswith("builtin___main__.py")
5+
assert main_module.__cached__ is None

‎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)