-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Align contextvars, CLI startup, and stat validation with CPython #8657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,19 +236,32 @@ fn run_file(vm: &VirtualMachine, scope: Scope, argv0: &str) -> PyResult<()> { | |
| } | ||
|
|
||
| cfg_select! { | ||
| feature = "host_env" => vm.run_any_file(scope, path), | ||
| feature = "host_env" => { | ||
| match rustpython_vm::host_env::fs::metadata(path) { | ||
| Ok(_) => vm.run_any_file(scope, path), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🤖 get_repo_knowledge executed:
Length of output: 14168 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- changed hunk ---'
git diff -- src/lib.rs | sed -n '1,220p'
printf '%s\n' '--- relevant source ---'
sed -n '180,285p' src/lib.rs
printf '%s\n' '--- bindings and callers ---'
rg -n -C 3 'run_any_file|run_simple_file|cant_open_file|host_env|read_to_string|metadata' srcRepository: RustPython/RustPython Length of output: 6535 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- run_any_file definitions and callers ---'
rg -n -C 8 'fn run_any_file|run_any_file\(' crates src --glob '*.rs'
printf '%s\n' '--- host_env filesystem bindings ---'
rg -n -C 8 'pub mod fs|mod fs|fn metadata|read_to_string|run_simple_file' crates src --glob '*.rs'Repository: RustPython/RustPython Length of output: 23394 🏁 Script executed: #!/bin/bash
set -eu
rg -n -C 8 'fn run_any_file|run_any_file\(' crates src --glob '*.rs'
rg -n -C 8 'pub mod fs|mod fs|fn metadata|read_to_string|run_simple_file' crates src --glob '*.rs'Repository: RustPython/RustPython Length of output: 23312 🏁 Script executed: #!/bin/bash
set -eu
sed -n '80,155p' crates/vm/src/vm/python_run.rs
rg -n -C 6 'read_to_string|fs::|host_env::fs|compile_file|compile\(' crates/vm/src/vm/python_run.rs crates/vm/src/compiler --glob '*.rs'Repository: RustPython/RustPython Length of output: 9134 Map host-side read failures to
🤖 Prompt for AI Agents |
||
| Err(err) => cant_open_file(vm, path, &err), | ||
| } | ||
| } | ||
| _ => { | ||
| // In sandbox mode, the binary reads the file and feeds source to the VM. | ||
| // The VM itself has no filesystem access. | ||
| let path = if path.is_empty() { "???" } else { path }; | ||
| match std::fs::read_to_string(path) { | ||
| Ok(source) => vm.run_string(scope, &source, path).map(drop), | ||
| Err(err) => Err(vm.new_os_error(err.to_string())), | ||
| Err(err) => cant_open_file(vm, path, &err), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- src/lib.rs:220-275 ---'
sed -n '220,275p' src/lib.rs
printf '%s\n' '--- cant_open_file definitions and callers ---'
rg -n -C 8 'cant_open_file|read_to_string|run_any_file|run_simple_file' src/lib.rs srcRepository: RustPython/RustPython Length of output: 6378 🤖 get_repo_knowledge executed:
Length of output: 12711 🌐 Web query:
💡 Result: In Rust, the std::fs::read_to_string function is designed to read the entire contents of a file into a String [1]. Because a Rust String must be valid UTF-8, this function will return an error if the file's contents are not valid UTF-8 [1]. When this occurs during the conversion process, the resulting std::io::Error will typically have an ErrorKind of InvalidData, which indicates that the data provided does not conform to the expected format (in this case, UTF-8 encoding). If you encounter this error, it signifies that the file you are attempting to read contains non-UTF-8 bytes [1]. Citations: Handle UTF-8 decoding errors separately from file-open errors. If 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn cant_open_file(vm: &VirtualMachine, path: &str, err: &std::io::Error) -> PyResult<()> { | ||
| let program = &vm.state.config.paths.executable; | ||
| let filename_repr = vm.ctx.new_str(path).as_object().repr(vm)?; | ||
| let errno = err.raw_os_error().unwrap_or(2); | ||
| eprintln!("{program}: can't open file {filename_repr}: [Errno {errno}] {err}"); | ||
| Err(vm.new_system_exit(vec![vm.ctx.new_int(2).into()].into())) | ||
| } | ||
|
|
||
| fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> { | ||
| use rustpython_vm::builtins::PyDictRef; | ||
| use rustpython_vm::convert::TryFromObject; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge RustPython/RustPython /tmp/coderabbit-repo-knowledge/rustpython-rustpython-3b4170e8/learningsLength of output: 15289
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 16885
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 18034
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 7998
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 487
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 8735
Preserve all Python
strcode points inContextVar.name.ContextVar.nameis stored as a RustString, and line 527 converts the validatedPyStrwithto_string(). RustStringcannot represent lone surrogates, soContextVar("\ud800").namecannot preserve the input. Store the name as a WTF-8-compatible value and add a regression test forContextVar("\ud800").name == "\ud800".🤖 Prompt for AI Agents