Working example
This commit is contained in:
@@ -19,3 +19,8 @@ target
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "flux-rt"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "flux-rt"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,13 @@
|
||||
fn main() {
|
||||
let lib_dir = std::process::Command::new("python3")
|
||||
.arg("-c")
|
||||
.arg("import sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
|
||||
.output()
|
||||
.expect("failed to run python3")
|
||||
.stdout;
|
||||
|
||||
let lib_dir = String::from_utf8(lib_dir).unwrap();
|
||||
println!("cargo:rustc-link-search=native={}", lib_dir.trim());
|
||||
|
||||
println!("cargo:rustc-link-lib=python3.11");
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
use std::{ffi::CString, fs, path::Path};
|
||||
|
||||
#[link(name = "python3.13")]
|
||||
extern "C" {
|
||||
fn Py_Initialize();
|
||||
fn PyRun_SimpleString(command: *const i8) -> i32;
|
||||
fn Py_Finalize();
|
||||
}
|
||||
|
||||
pub fn run_py(path: &Path) {
|
||||
let code = fs::read_to_string(path).expect("Failed to read python script");
|
||||
|
||||
unsafe {
|
||||
Py_Initialize();
|
||||
|
||||
let code = CString::new(code.as_str()).unwrap();
|
||||
PyRun_SimpleString(code.as_ptr());
|
||||
|
||||
Py_Finalize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use std::path::Path;
|
||||
use flux_rt::run_py;
|
||||
|
||||
fn main() {
|
||||
run_py(Path::new("bro.py"));
|
||||
}
|
||||
Reference in New Issue
Block a user