mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
fix: windows specific bug
This commit is contained in:
parent
3eb2d439ca
commit
ebfc314e0b
3 changed files with 31 additions and 19 deletions
|
@ -1,7 +1,9 @@
|
||||||
//! utilities for calling CPython.
|
//! utilities for calling CPython.
|
||||||
//!
|
//!
|
||||||
//! CPythonを呼び出すためのユーティリティー
|
//! CPythonを呼び出すためのユーティリティー
|
||||||
use std::fs;
|
use std::env;
|
||||||
|
use std::fs::{self, File};
|
||||||
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::{Command, ExitStatus, Stdio};
|
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> {
|
pub fn exec_py_code(code: &str, output: Output) -> std::io::Result<ExitStatus> {
|
||||||
let mut out = if cfg!(windows) {
|
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())
|
Command::new(which_python())
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(code)
|
.arg(code)
|
||||||
.stdout(output)
|
.stdout(output.clone())
|
||||||
.spawn()
|
.spawn()
|
||||||
|
.or_else(fallback)
|
||||||
.expect("cannot execute python")
|
.expect("cannot execute python")
|
||||||
} else {
|
} else {
|
||||||
let exec_command = format!("{} -c \"{code}\"", which_python());
|
let exec_command = format!("{} -c \"{code}\"", which_python());
|
||||||
|
|
|
@ -226,20 +226,6 @@ impl Compiler {
|
||||||
Ok(CompleteArtifact::new(last, arti.warns))
|
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(
|
pub fn compile(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: String,
|
src: String,
|
||||||
|
|
11
src/dummy.rs
11
src/dummy.rs
|
@ -8,7 +8,7 @@ use std::time::Duration;
|
||||||
use erg_common::config::ErgConfig;
|
use erg_common::config::ErgConfig;
|
||||||
use erg_common::error::MultiErrorDisplay;
|
use erg_common::error::MultiErrorDisplay;
|
||||||
use erg_common::python_util::spawn_py;
|
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::hir::Expr;
|
||||||
use erg_compiler::ty::HasType;
|
use erg_compiler::ty::HasType;
|
||||||
|
@ -284,12 +284,17 @@ impl Runnable for DummyVM {
|
||||||
|
|
||||||
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
|
fn exec(&mut self) -> Result<ExitStatus, Self::Errs> {
|
||||||
let src = self.cfg_mut().input.read();
|
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.warns.write_all_to(&mut self.cfg_mut().output);
|
||||||
eart.errors
|
eart.errors
|
||||||
})?;
|
})?;
|
||||||
art.warns.write_all_to(&mut self.cfg_mut().output);
|
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> {
|
fn eval(&mut self, src: String) -> Result<String, EvalErrors> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue