Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python Path for Windows #533

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions crates/erg_common/python_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,33 @@ fn get_poetry_virtualenv_path() -> Option<String> {
let out = if cfg!(windows) {
Command::new("cmd")
.arg("/C")
.arg("poetry env info -p")
.args(["poetry", "env", "info", "-p"])
.output()
.ok()?
} else {
Command::new("sh")
.arg("-c")
.arg("poetry env info -p")
.args(["poetry", "env", "info", "-p"])
.output()
.ok()?
};
let path = String::from_utf8(out.stdout).ok()?;
Path::new(path.trim())
.exists()
.then_some(path.trim().to_string())
}

fn get_uv_python_venv_path() -> Option<String> {
let out = if cfg!(windows) {
Command::new("cmd")
.arg("/C")
.args(["uv", "python", "find"])
.output()
.ok()?
} else {
Command::new("sh")
.arg("-c")
.args(["uv", "python", "find"])
.output()
.ok()?
};
Expand All @@ -613,19 +633,32 @@ pub fn _opt_which_python() -> Result<String, String> {
if let Some(path) = which_python_from_toml() {
return Ok(path);
}
if Path::new("./.venv/bin/python").is_file() {
let path = canonicalize("./.venv/bin/python").unwrap();
return Ok(path.to_string_lossy().to_string());
} else if let Some(path) = get_poetry_virtualenv_path() {
if let Some(path) = get_poetry_virtualenv_path() {
return Ok(format!("{path}/bin/python"));
} else if let Some(path) = get_uv_python_venv_path() {
return Ok(path);
}

let path = if cfg!(windows) {
r".venv\Scripts\python.exe"
} else {
".venv/bin/python"
};
if Path::new(&path).is_file() {
let path = canonicalize(path).unwrap();
return Ok(path.to_string_lossy().to_string());
}
let (cmd, python) = if cfg!(windows) {
("where", "python")
let out = if cfg!(windows) {
Command::new("cmd")
.arg("/C")
.arg("where")
.arg("python")
.output()
} else {
("which", "python3")
Command::new("sh").arg("-c").arg("which python3").output()
};
let Ok(out) = Command::new(cmd).arg(python).output() else {
return Err(format!("{}: {python} not found", fn_name_full!()));
let Ok(out) = out else {
return Err(format!("{}: python not found", fn_name_full!()));
};
let Ok(res) = String::from_utf8(out.stdout) else {
return Err(format!(
Expand All @@ -635,7 +668,7 @@ pub fn _opt_which_python() -> Result<String, String> {
};
let res = res.split('\n').next().unwrap_or("").replace('\r', "");
if res.is_empty() {
return Err(format!("{}: {python} not found", fn_name_full!()));
return Err(format!("{}: python not found", fn_name_full!()));
} else if res.contains("pyenv") && cfg!(windows) {
// because pyenv-win does not support `-c` option
return Err("cannot use pyenv-win".into());
Expand Down
3 changes: 3 additions & 0 deletions examples/helloworld.er
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
sys = pyimport "sys"
sys.stdout.reconfigure!(encoding:="utf-8")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is inconvenient that multilingual support is only available if the user sets it up using sys.stdout.reconfigure!, so I think it would be better to include a process to embed this code when generating the code.
For example, Erg embeds a code in the bytecode that passes its own standard API path and loads it. Using this as a reference, it is possible to call sys.stdout.reconfigure first.

fn load_prelude_py(&mut self) {
self.emit_global_import_items(
Identifier::static_public("sys"),
vec![(
Identifier::static_public("path"),
Some(Identifier::private("#path")),
)],
);
self.emit_load_name_instr(Identifier::private("#path"));
self.emit_load_method_instr(Identifier::static_public("append"), BoundAttr);
self.emit_load_const(erg_core_path().to_str().unwrap());
self.emit_call_instr(1, BoundAttr);
self.stack_dec();
self.emit_pop_top();
let erg_std_mod = Identifier::static_public("_erg_std_prelude");
// escaping
self.emit_global_import_items(
erg_std_mod.clone(),
vec![(
Identifier::static_public("contains_operator"),
Some(Identifier::private("#contains_operator")),
)],
);
self.emit_import_all_instr(erg_std_mod);
}


print! "Hello, world!"
print! "こんにちは、世界!"
print! "Γειά σου Κόσμε!"
Expand Down
7 changes: 1 addition & 6 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,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().unwrap().minor >= Some(8) {
expect_end_with("examples/helloworld.er", 0, 1)
} else {
expect_success("examples/helloworld.er", 0)
}
expect_success("examples/helloworld.er", 0)
}

#[test]
Expand Down
Loading