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());

View file

@ -226,20 +226,6 @@ impl Compiler {
Ok(CompleteArtifact::new(last, arti.warns))
}
pub fn exec_compile(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact<ExitStatus>, ErrorArtifact> {
let arti = self.compile(src, mode)?;
let stat = arti
.object
.exec(self.cfg.py_magic_num, self.cfg.output.clone())
.expect("failed to dump a .pyc file (maybe permission denied)");
let stat = ExitStatus::new(stat.code().unwrap_or(0), arti.warns.len(), 0);
Ok(CompleteArtifact::new(stat, arti.warns))
}
pub fn compile(
&mut self,
src: String,

View file

@ -8,7 +8,7 @@ use std::time::Duration;
use erg_common::config::ErgConfig;
use erg_common::error::MultiErrorDisplay;
use erg_common::python_util::spawn_py;
use erg_common::traits::{ExitStatus, Runnable};
use erg_common::traits::{ExitStatus, Runnable, Stream};
use erg_compiler::hir::Expr;
use erg_compiler::ty::HasType;
@ -284,12 +284,17 @@ impl Runnable for DummyVM {
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
let src = self.cfg_mut().input.read();
let art = self.compiler.exec_compile(src, "exec").map_err(|eart| {
let art = self.compiler.compile(src, "exec").map_err(|eart| {
eart.warns.write_all_to(&mut self.cfg_mut().output);
eart.errors
})?;
art.warns.write_all_to(&mut self.cfg_mut().output);
Ok(art.object)
let stat = art
.object
.exec(self.cfg().py_magic_num, self.cfg().output.clone())
.expect("failed to execute");
let stat = ExitStatus::new(stat.code().unwrap_or(0), art.warns.len(), 0);
Ok(stat)
}
fn eval(&mut self, src: String) -> Result<String, EvalErrors> {