mirror of
https://github.com/erg-lang/erg.git
synced 2025-07-07 13:15:21 +00:00
refactor: wrap and handle the py ver retrieval
This commit is contained in:
parent
e0f4bf569c
commit
edd193a119
6 changed files with 20 additions and 13 deletions
4
build.rs
4
build.rs
|
@ -1,7 +1,9 @@
|
|||
use erg_common::python_util::{env_magic_number, env_python_version};
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let version = env_python_version();
|
||||
let Some(version) = env_python_version() else {
|
||||
panic!("Failed to get python version");
|
||||
};
|
||||
if version.major != 3 {
|
||||
panic!("Python 3 is required");
|
||||
}
|
||||
|
|
|
@ -376,7 +376,7 @@ impl ErgConfig {
|
|||
.parse::<String>()
|
||||
.expect("the value of `-py-command` is not a valid Python command");
|
||||
cfg.py_magic_num = Some(detect_magic_number(&py_command));
|
||||
cfg.target_version = Some(get_python_version(&py_command));
|
||||
cfg.target_version = get_python_version(&py_command);
|
||||
cfg.py_command = Some(Box::leak(py_command.into_boxed_str()));
|
||||
}
|
||||
"--hex-py-magic-num" | "--hex-python-magic-number" => {
|
||||
|
|
|
@ -693,7 +693,7 @@ impl std::str::FromStr for PythonVersion {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_python_version(py_command: &str) -> PythonVersion {
|
||||
pub fn get_python_version(py_command: &str) -> Option<PythonVersion> {
|
||||
let out = if cfg!(windows) {
|
||||
Command::new("cmd")
|
||||
.arg("/C")
|
||||
|
@ -710,19 +710,19 @@ pub fn get_python_version(py_command: &str) -> PythonVersion {
|
|||
.expect("cannot get the python version")
|
||||
};
|
||||
let s_version = String::from_utf8(out.stdout).unwrap();
|
||||
let mut iter = s_version.split(' ');
|
||||
let mut iter = iter.nth(1).unwrap().split('.');
|
||||
let iter = s_version.split(' ').nth(1)?;
|
||||
let mut iter = iter.split('.');
|
||||
let major = iter.next().and_then(|i| i.parse().ok()).unwrap_or(3);
|
||||
let minor = iter.next().and_then(|i| i.parse().ok());
|
||||
let micro = iter.next().and_then(|i| i.trim_end().parse().ok());
|
||||
PythonVersion {
|
||||
Some(PythonVersion {
|
||||
major,
|
||||
minor,
|
||||
micro,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn env_python_version() -> PythonVersion {
|
||||
pub fn env_python_version() -> Option<PythonVersion> {
|
||||
get_python_version(&which_python())
|
||||
}
|
||||
|
||||
|
|
|
@ -228,7 +228,12 @@ pub struct PyCodeGenerator {
|
|||
impl PyCodeGenerator {
|
||||
pub fn new(cfg: ErgConfig) -> Self {
|
||||
Self {
|
||||
py_version: cfg.target_version.unwrap_or_else(env_python_version),
|
||||
py_version: cfg.target_version.unwrap_or_else(|| {
|
||||
let Some(version) = env_python_version() else {
|
||||
panic!("Failed to get python version");
|
||||
};
|
||||
version
|
||||
}),
|
||||
cfg,
|
||||
str_cache: CacheSet::new(),
|
||||
prelude_loaded: false,
|
||||
|
|
|
@ -69,7 +69,7 @@ while! do! c < 7, do!:
|
|||
|
||||
#[test]
|
||||
fn test_transpiler_embedding3() -> Result<(), ()> {
|
||||
if env_python_version().minor < Some(10) {
|
||||
if env_python_version().unwrap().minor < Some(10) {
|
||||
println!("skipped: {}", fn_name!());
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ print!(i, end:=\"\")
|
|||
|
||||
#[test]
|
||||
fn test_transpiler_embedding4() -> Result<(), ()> {
|
||||
if env_python_version().minor < Some(10) {
|
||||
if env_python_version().unwrap().minor < Some(10) {
|
||||
println!("skipped: {}", fn_name!());
|
||||
return Ok(());
|
||||
}
|
||||
|
|
|
@ -172,7 +172,7 @@ fn exec_fib() -> Result<(), ()> {
|
|||
#[test]
|
||||
fn exec_helloworld() -> Result<(), ()> {
|
||||
// HACK: When running the test with Windows, the exit code is 1 (the cause is unknown)
|
||||
if cfg!(windows) && env_python_version().minor >= Some(8) {
|
||||
if cfg!(windows) && env_python_version().unwrap().minor >= Some(8) {
|
||||
expect_end_with("examples/helloworld.er", 0, 1)
|
||||
} else {
|
||||
expect_success("examples/helloworld.er", 0)
|
||||
|
@ -323,7 +323,7 @@ fn exec_pattern() -> Result<(), ()> {
|
|||
#[test]
|
||||
fn exec_pyimport_test() -> Result<(), ()> {
|
||||
// HACK: When running the test with Windows, the exit code is 1 (the cause is unknown)
|
||||
if cfg!(windows) && env_python_version().minor < Some(8) {
|
||||
if cfg!(windows) && env_python_version().unwrap().minor < Some(8) {
|
||||
expect_end_with("tests/should_ok/pyimport.er", 2, 1)
|
||||
} else {
|
||||
expect_success("tests/should_ok/pyimport.er", 2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue