Skip to content

Commit 5674670

Browse files
committed
Fix dot notation for RustPython
1 parent 6dac76a commit 5674670

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
7979
init_and_register(vec![])
8080
}
8181

82+
fn init_python(code: String, dir: PathBuf) {
83+
let path_import_and_code = format!(
84+
r#"
85+
import sys
86+
sys.path.append('{}')
87+
{}
88+
"#,
89+
dir.to_str().unwrap(),
90+
code
91+
);
92+
py_lib::run_python(StringRequest {
93+
value: path_import_and_code,
94+
})
95+
.unwrap_or_else(|e| panic!("Error '{e}' initializing main.py."));
96+
}
97+
8298
/// Initializes the plugin.
8399
pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> TauriPlugin<R> {
84100
Builder::new("python")
@@ -107,7 +123,7 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
107123
if code.is_empty() {
108124
println!("ERROR: Error reading 'src-tauri/main.py'");
109125
}
110-
py_lib::init_python(code, dir).unwrap();
126+
init_python(code, dir);
111127
for function_name in python_functions {
112128
py_lib::register_function_str(function_name.into(), None).unwrap();
113129
}
@@ -117,7 +133,6 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
117133
.unwrap_or_default()
118134
.replace("'", "\""); // python arrays are serialized usings ' instead of "
119135

120-
// dbg!(&functions);
121136
if let Ok(python_functions) = serde_json::from_str::<Vec<String>>(&functions) {
122137
for function_name in python_functions {
123138
py_lib::register_function_str(function_name, None).unwrap();

src/py_lib.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

6-
use std::path::PathBuf;
76
use std::sync::atomic::AtomicBool;
87
use std::{collections::HashSet, sync::Mutex};
98

@@ -24,26 +23,6 @@ lazy_static! {
2423
static ref GLOBALS: rustpython_vm::scope::Scope = create_globals();
2524
}
2625

27-
pub fn init_python(code: String, dir: PathBuf) -> crate::Result<()> {
28-
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
29-
vm.import("sys", 0).unwrap();
30-
let path_import = format!("sys.path.append('{}')", dir.to_str().unwrap());
31-
let exec_python = |code: String| {
32-
let code_obj = vm
33-
.compile(
34-
&code,
35-
rustpython_vm::compiler::Mode::Exec,
36-
"<embedded>".to_owned(),
37-
)
38-
.map_err(|err| vm.new_syntax_error(&err, Some(&code)))?;
39-
vm.run_code_obj(code_obj, GLOBALS.clone())
40-
};
41-
exec_python(path_import)?;
42-
exec_python(code)
43-
})?;
44-
Ok(())
45-
}
46-
4726
pub fn run_python(payload: StringRequest) -> crate::Result<()> {
4827
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
4928
let code_obj = vm
@@ -70,9 +49,13 @@ pub fn register_function_str(
7049
}
7150
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
7251
let var_dot_split: Vec<&str> = function_name.split(".").collect();
73-
let func = GLOBALS.globals.get_item(var_dot_split[0], vm)?;
52+
let func = GLOBALS.globals.get_item(var_dot_split[0], vm).unwrap_or_else(|_| {
53+
panic!("Cannot find '{}' in globals", var_dot_split[0]);
54+
});
7455
if var_dot_split.len() > 1 {
75-
func.get_item(var_dot_split[1], vm)?;
56+
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm).unwrap_or_else(|_| {
57+
panic!("Cannot find sub function '{}' in '{}'", var_dot_split[1], var_dot_split[0]);
58+
});
7659
}
7760

7861
if let Some(num_args) = number_of_args {
@@ -119,7 +102,7 @@ pub fn call_function(payload: RunRequest) -> crate::Result<String> {
119102
let var_dot_split: Vec<&str> = function_name.split(".").collect();
120103
let func = GLOBALS.globals.get_item(var_dot_split[0], vm)?;
121104
Ok(if var_dot_split.len() > 1 {
122-
func.get_item(var_dot_split[1], vm)?
105+
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
123106
} else {
124107
func
125108
}
@@ -134,7 +117,7 @@ pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
134117
let var_dot_split: Vec<&str> = payload.value.split(".").collect();
135118
let var = GLOBALS.globals.get_item(var_dot_split[0], vm)?;
136119
Ok(if var_dot_split.len() > 1 {
137-
var.get_item(var_dot_split[1], vm)?
120+
var.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
138121
} else {
139122
var
140123
}

src/py_lib_pyo3.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

6-
use std::path::PathBuf;
76
use std::sync::atomic::AtomicBool;
87
use std::{collections::HashMap, ffi::CString, sync::Mutex};
98

@@ -22,19 +21,6 @@ lazy_static! {
2221
Mutex::new(marker::Python::with_gil(|py| { PyDict::new(py).into() }));
2322
}
2423

25-
pub fn init_python(code: String, dir: PathBuf) -> crate::Result<()> {
26-
let c_code = CString::new(code).expect("error creating cstring from code");
27-
Ok(marker::Python::with_gil(|py| -> PyResult<()> {
28-
let syspath = py
29-
.import("sys")?
30-
.getattr("path")?
31-
.downcast_into::<PyList>()?;
32-
syspath.insert(0, dir.to_str())?;
33-
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);
34-
py.run(&c_code, Some(&globals), None)
35-
})?)
36-
}
37-
3824
pub fn run_python(payload: StringRequest) -> crate::Result<()> {
3925
marker::Python::with_gil(|py| -> crate::Result<()> {
4026
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);

0 commit comments

Comments
 (0)