Skip to content

Commit 35e9473

Browse files
committed
fixing rustpython
1 parent 25f070c commit 35e9473

File tree

4 files changed

+40
-50
lines changed

4 files changed

+40
-50
lines changed

Cargo2.toml

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tokio::sync::{mpsc, oneshot};
1818

1919
#[derive(Debug)]
2020
pub(crate) enum CmdType {
21+
RunFile(PathBuf),
2122
RunCode(String),
2223
EvalCode(String),
2324
ReadVariable(String),
@@ -131,16 +132,9 @@ impl PyRunner {
131132
/// * `file`: Absolute path to a python file to execute.
132133
/// Also loads the path of the file to sys.path for imports.
133134
pub async fn run_file(&self, file: &Path) -> Result<(), PyRunnerError> {
134-
let sys_import = format!(
135-
r#"
136-
import sys
137-
sys.path = sys.path + [{}]
138-
"#,
139-
print_path_for_python(&file.parent().unwrap().to_path_buf()),
140-
);
141-
self.run(&sys_import).await?;
142-
let code = tokio::fs::read_to_string(file).await.map_err(|e| PyRunnerError::PyError(e.to_string()))?;
143-
self.run(&code).await
135+
self.send_command(CmdType::RunFile(file.to_path_buf()))
136+
.await
137+
.map(|_| ())
144138
}
145139

146140
/// Asynchronously evaluates a single Python expression.

src/pyo3_runner.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/async_py
55

6-
use crate::{CmdType, PyCommand};
6+
use crate::{print_path_for_python, CmdType, PyCommand};
77
use pyo3::{
88
exceptions::PyKeyError,
99
prelude::*,
@@ -31,6 +31,9 @@ pub(crate) fn python_thread_main(mut receiver: mpsc::Receiver<PyCommand>) {
3131
py.eval(&c_code, Some(&globals), None)
3232
.and_then(|obj| py_any_to_json(py, &obj))
3333
}
34+
CmdType::RunFile(file) => {
35+
handle_run_file(py, &globals, file)
36+
}
3437
CmdType::ReadVariable(var_name) => {
3538
get_py_object(&globals, &var_name).and_then(|obj| py_any_to_json(py, &obj))
3639
}
@@ -71,6 +74,25 @@ fn get_py_object<'py>(
7174
Ok(obj)
7275
}
7376

77+
fn handle_run_file(
78+
py: Python,
79+
globals: &pyo3::Bound<'_, PyDict>,
80+
file: std::path::PathBuf,
81+
) -> PyResult<Value> {
82+
let code = format!(
83+
r#"
84+
import sys
85+
sys.path.insert(0, {})
86+
with open({}, 'r') as f:
87+
exec(f.read())
88+
"#,
89+
print_path_for_python(&file.parent().unwrap().to_path_buf()),
90+
print_path_for_python(&file.to_path_buf())
91+
);
92+
let c_code = CString::new(code).expect("CString::new failed");
93+
py.run(&c_code, Some(&globals), None).map(|_| Value::Null)
94+
}
95+
7496
/// Handles the `CallFunction` command.
7597
fn handle_call_function(
7698
py: Python,

src/rustpython_runner.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub(crate) fn python_thread_main(mut receiver: mpsc::Receiver<PyCommand>) {
3131
.map(|_| Value::Null),
3232
CmdType::EvalCode(code) => eval::eval(vm, code, scope.clone(), "<string, eval>")
3333
.and_then(|obj| py_to_json(vm, &obj)),
34+
CmdType::RunFile(file) => {
35+
handle_run_file(vm, scope.clone(), file).map(|_| Value::Null)
36+
}
3437
CmdType::ReadVariable(var_name) => {
3538
read_variable(vm, scope.clone(), var_name).and_then(|obj| py_to_json(vm, &obj))
3639
}
@@ -52,6 +55,16 @@ pub(crate) fn python_thread_main(mut receiver: mpsc::Receiver<PyCommand>) {
5255
});
5356
}
5457

58+
fn handle_run_file(
59+
vm: &VirtualMachine,
60+
scope: Scope,
61+
file: &std::path::PathBuf,
62+
) -> PyResult<()> {
63+
let dir = file.parent().unwrap().to_str().unwrap();
64+
vm.insert_sys_path(vm.new_pyobj(dir))?;
65+
vm.run_script(scope, file.to_str().unwrap())
66+
}
67+
5568
fn read_variable(vm: &VirtualMachine, scope: Scope, var_name: &str) -> PyResult<PyObjectRef> {
5669
let parts: Vec<String> = var_name.split('.').map(|s| s.to_owned()).collect();
5770
let mut obj = scope.globals.get_item(&parts[0], vm)?;

0 commit comments

Comments
 (0)