Skip to content

Commit f7a0c5f

Browse files
committed
--install-pip default to ensurepip
1 parent aec155f commit f7a0c5f

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ jobs:
256256
name: check that --install-pip succeeds
257257
run: |
258258
mkdir site-packages
259-
target/release/rustpython --install-pip -t site-packages
259+
target/release/rustpython --install-pip ensurepip --user
260260
261261
lint:
262262
name: Check Rust code with rustfmt and clippy

src/lib.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,18 @@ fn create_settings(matches: &ArgMatches) -> Settings {
366366
.chain(module.skip(1).map(ToOwned::to_owned))
367367
.collect()
368368
} else if let Some(get_pip_args) = matches.values_of("install_pip") {
369-
std::iter::once("get-pip.py".to_owned())
370-
.chain(get_pip_args.map(ToOwned::to_owned))
371-
.collect()
369+
settings.isolated = true;
370+
let mut args: Vec<_> = get_pip_args.map(ToOwned::to_owned).collect();
371+
if args.is_empty() {
372+
args.push("ensurepip".to_owned());
373+
args.push("--upgrade".to_owned());
374+
args.push("--default-pip".to_owned());
375+
}
376+
match args.first().map(String::as_str) {
377+
Some("ensurepip") | Some("get-pip") => (),
378+
_ => panic!("--install-pip takes ensurepip or get-pip as first argument"),
379+
}
380+
args
372381
} else if let Some(cmd) = matches.values_of("c") {
373382
std::iter::once("-c".to_owned())
374383
.chain(cmd.skip(1).map(ToOwned::to_owned))
@@ -497,7 +506,7 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
497506
}
498507

499508
#[cfg(feature = "ssl")]
500-
fn install_pip(scope: Scope, vm: &VirtualMachine) -> PyResult {
509+
fn get_pip(scope: Scope, vm: &VirtualMachine) -> PyResult<()> {
501510
let get_getpip = rustpython_vm::py_compile!(
502511
source = r#"\
503512
__import__("io").TextIOWrapper(
@@ -512,14 +521,29 @@ __import__("io").TextIOWrapper(
512521
.downcast()
513522
.expect("TextIOWrapper.read() should return str");
514523
eprintln!("running get-pip.py...");
515-
vm.run_code_string(scope, getpip_code.as_str(), "get-pip.py".to_owned())
524+
vm.run_code_string(scope, getpip_code.as_str(), "get-pip.py".to_owned())?;
525+
Ok(())
516526
}
517527

518-
#[cfg(not(feature = "ssl"))]
519-
fn install_pip(_: Scope, vm: &VirtualMachine) -> PyResult {
528+
#[cfg(feature = "ssl")]
529+
fn ensurepip(_: Scope, vm: &VirtualMachine) -> PyResult<()> {
530+
vm.run_module("ensurepip")
531+
}
532+
533+
fn install_pip(_scope: Scope, vm: &VirtualMachine) -> PyResult<()> {
534+
#[cfg(feature = "ssl")]
535+
{
536+
match vm.state.settings.argv[0].as_str() {
537+
"ensurepip" => ensurepip(_scope, vm),
538+
"get-pip" => get_pip(_scope, vm),
539+
_ => unreachable!(),
540+
}
541+
}
542+
543+
#[cfg(not(feature = "ssl"))]
520544
Err(vm.new_exception_msg(
521545
vm.ctx.exceptions.system_error.to_owned(),
522-
"install-pip requires rustpython be build with the 'ssl' feature enabled.".to_owned(),
546+
"install-pip requires rustpython be build with '--features=ssl'".to_owned(),
523547
))
524548
}
525549

0 commit comments

Comments
 (0)