From 306a4d7ce3bed9019e8ab894d656149d7a5ddbcb Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 23 May 2024 11:00:45 -0400 Subject: [PATCH] Improve logging of interpreter implementation (#3791) ``` Found Python interpreter CPython 3.12.3 at... ``` instead of ``` Found Python interpreter cpython 3.12.3 at ``` --- crates/uv-interpreter/src/discovery.rs | 4 ++-- crates/uv-interpreter/src/implementation.rs | 24 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/uv-interpreter/src/discovery.rs b/crates/uv-interpreter/src/discovery.rs index 814705721..dc8963491 100644 --- a/crates/uv-interpreter/src/discovery.rs +++ b/crates/uv-interpreter/src/discovery.rs @@ -6,7 +6,7 @@ use uv_fs::Simplified; use uv_warnings::warn_user_once; use which::which; -use crate::implementation::ImplementationName; +use crate::implementation::{ImplementationName, LenientImplementationName}; use crate::interpreter::Error as InterpreterError; use crate::managed::toolchains_for_current_platform; use crate::py_launcher::py_list_paths; @@ -366,7 +366,7 @@ fn python_interpreters<'a>( .inspect(|(source, interpreter)| { trace!( "Found Python interpreter {} {} at {} from {source}", - interpreter.implementation_name(), + LenientImplementationName::from(interpreter.implementation_name()), interpreter.python_full_version(), path.display() ); diff --git a/crates/uv-interpreter/src/implementation.rs b/crates/uv-interpreter/src/implementation.rs index d63f06d21..69b08c6f4 100644 --- a/crates/uv-interpreter/src/implementation.rs +++ b/crates/uv-interpreter/src/implementation.rs @@ -17,6 +17,12 @@ pub enum ImplementationName { PyPy, } +#[derive(Debug, Eq, PartialEq, Clone)] +pub(crate) enum LenientImplementationName { + Known(ImplementationName), + Unknown(String), +} + impl ImplementationName { pub(crate) fn iter() -> impl Iterator { static NAMES: &[ImplementationName] = @@ -52,3 +58,21 @@ impl Display for ImplementationName { } } } + +impl From<&str> for LenientImplementationName { + fn from(s: &str) -> Self { + match ImplementationName::from_str(s) { + Ok(implementation) => Self::Known(implementation), + Err(_) => Self::Unknown(s.to_string()), + } + } +} + +impl Display for LenientImplementationName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Known(implementation) => implementation.fmt(f), + Self::Unknown(name) => f.write_str(name), + } + } +}