Skip to content

Commit

Permalink
refactor: wrap and handle the py ver retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
GreasySlug committed Feb 4, 2024
1 parent e0f4bf5 commit edd193a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand Down
12 changes: 6 additions & 6 deletions crates/erg_common/python_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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())
}

Expand Down
7 changes: 6 additions & 1 deletion crates/erg_compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand All @@ -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(());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit edd193a

Please sign in to comment.