Skip to content

Commit a4ea814

Browse files
Avoid panic for invalid Python versions (#13077)
## Summary We unwrap these further on, so we should validate them ahead of time. Closes #13075.
1 parent a6a0087 commit a4ea814

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

crates/uv-python/src/python_version.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ impl FromStr for PythonVersion {
3131
if version.epoch() != 0 {
3232
return Err(format!("Python version `{s}` has a non-zero epoch"));
3333
}
34+
if let Some(major) = version.release().first() {
35+
if u8::try_from(*major).is_err() {
36+
return Err(format!(
37+
"Python version `{s}` has an invalid major version ({major})"
38+
));
39+
}
40+
}
41+
if let Some(minor) = version.release().get(1) {
42+
if u8::try_from(*minor).is_err() {
43+
return Err(format!(
44+
"Python version `{s}` has an invalid minor version ({minor})"
45+
));
46+
}
47+
}
48+
if let Some(patch) = version.release().get(2) {
49+
if u8::try_from(*patch).is_err() {
50+
return Err(format!(
51+
"Python version `{s}` has an invalid patch version ({patch})"
52+
));
53+
}
54+
}
3455

3556
Ok(Self(version))
3657
}

crates/uv/tests/it/pip_install.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,26 @@ dependencies = ["flask==1.0.x"]
364364
Ok(())
365365
}
366366

367+
#[test]
368+
fn invalid_python_version() {
369+
let context = TestContext::new("3.12");
370+
371+
uv_snapshot!(context.filters(), context.pip_install()
372+
.arg("flask")
373+
.arg("--python-version")
374+
.arg("311"), @r"
375+
success: false
376+
exit_code: 2
377+
----- stdout -----
378+
379+
----- stderr -----
380+
error: invalid value '311' for '--python-version <PYTHON_VERSION>': Python version `311` has an invalid major version (311)
381+
382+
For more information, try '--help'.
383+
"
384+
);
385+
}
386+
367387
#[test]
368388
fn missing_pip() {
369389
uv_snapshot!(Command::new(get_bin()).arg("install"), @r###"

0 commit comments

Comments
 (0)