Skip to content

Commit a215502

Browse files
committed
fixing line numbers and sys.path import
1 parent 0204451 commit a215502

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/error.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ impl From<rustpython_vm::PyRef<rustpython_vm::builtins::PyBaseException>> for Er
4343
if let Some(tb) = error.traceback() {
4444
println!("Traceback (most recent call last):");
4545
for trace in tb.iter() {
46+
let file = trace.frame.code.source_path.as_str();
47+
let original_line = trace.lineno.to_usize();
48+
let line = if file == "main.py" {
49+
original_line - 2 // sys.path import has 2 additional lines
50+
} else {
51+
original_line
52+
};
4653
println!(
47-
" File \"{}\", line {}, in {}",
48-
trace.frame.code.source_path,
49-
trace.lineno.to_usize(),
54+
" File \"{file}\", line {line}, in {}",
5055
trace.frame.code.obj_name
5156
);
5257
}

src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
8181

8282
fn init_python(code: String, dir: PathBuf) {
8383
#[allow(unused_mut)]
84-
let mut sys_pyth_dir = format!("sys.path.append('{}')", dir.to_str().unwrap());
84+
let mut sys_pyth_dir = vec![format!("\"{}\"", dir.to_string_lossy())];
8585
#[cfg(feature = "venv")]
8686
{
8787
let venv_dir = dir.join(".venv").join("lib");
@@ -91,26 +91,23 @@ fn init_python(code: String, dir: PathBuf) {
9191
let site_packages = entry.path().join("site-packages");
9292
// use first folder with site-packages for venv, ignore venv version
9393
if Path::exists(site_packages.as_path()) {
94-
sys_pyth_dir += "\n";
95-
sys_pyth_dir +=
96-
&format!("sys.path.append('{}')", site_packages.to_str().unwrap());
94+
sys_pyth_dir.push(format!("\"{}\"", site_packages.to_string_lossy()));
9795
break;
9896
}
9997
}
10098
}
10199
}
102100
}
103101
let path_import = format!(
104-
r#"
105-
import sys
102+
r#"import sys
103+
sys.path = sys.path + [{}]
106104
{}
107105
"#,
108-
sys_pyth_dir
106+
sys_pyth_dir.join(", "),
107+
code
109108
);
110-
py_lib::run_python_internal(path_import, "<tauri_init>".into())
111-
.unwrap_or_else(|e| panic!("Error '{e}' initializing sys_import"));
112-
py_lib::run_python_internal(code, "main.py".into())
113-
.unwrap_or_else(|e| panic!("Error '{e}' initializing main.py."));
109+
py_lib::run_python_internal(path_import, "main.py".into())
110+
.unwrap_or_else(|e| panic!("Error '{e}' initializing main.py"));
114111
}
115112

116113
/// Initializes the plugin.

0 commit comments

Comments
 (0)