This commit is contained in:
Shunsuke Shibayama 2023-07-26 01:19:48 +09:00
parent ada421cd5c
commit 3fc42f65e8
4 changed files with 59 additions and 19 deletions

View file

@ -3,9 +3,10 @@
//! CPythonを呼び出すためのユーティリティー
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::process::{Command, ExitStatus, Stdio};
use crate::fn_name_full;
use crate::io::Output;
use crate::pathutil::remove_verbatim;
use crate::serialize::get_magic_num_from_bytes;
@ -849,3 +850,23 @@ pub fn spawn_py(py_command: Option<&str>, code: &str) {
.expect("cannot execute python");
}
}
pub fn exec_py_code(code: &str, output: Output) -> std::io::Result<ExitStatus> {
let mut out = if cfg!(windows) {
Command::new(which_python())
.arg("-c")
.arg(code)
.stdout(output)
.spawn()
.expect("cannot execute python")
} else {
let exec_command = format!("{} -c \"{code}\"", which_python());
Command::new("sh")
.arg("-c")
.arg(exec_command)
.stdout(output)
.spawn()
.expect("cannot execute python")
};
out.wait()
}