fix: windows specific bug

This commit is contained in:
Shunsuke Shibayama 2023-07-26 20:07:06 +09:00
parent 3eb2d439ca
commit ebfc314e0b
3 changed files with 31 additions and 19 deletions

View file

@ -1,7 +1,9 @@
//! utilities for calling CPython.
//!
//! CPythonを呼び出すためのユーティリティー
use std::fs;
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
@ -853,11 +855,30 @@ pub fn spawn_py(py_command: Option<&str>, code: &str) {
pub fn exec_py_code(code: &str, output: Output) -> std::io::Result<ExitStatus> {
let mut out = if cfg!(windows) {
let fallback = |err: std::io::Error| {
// if the filename or extension is too long
// create a temporary file and execute it
if err.raw_os_error() == Some(206) {
let tmp_dir = env::temp_dir();
let tmp_file = tmp_dir.join("tmp.py");
File::create(&tmp_file)
.unwrap()
.write_all(code.as_bytes())
.unwrap();
Command::new(which_python())
.arg(tmp_file)
.stdout(output.clone())
.spawn()
} else {
Err(err)
}
};
Command::new(which_python())
.arg("-c")
.arg(code)
.stdout(output)
.stdout(output.clone())
.spawn()
.or_else(fallback)
.expect("cannot execute python")
} else {
let exec_command = format!("{} -c \"{code}\"", which_python());