Skip to content

Commit

Permalink
Merge pull request #1600 from messense/unittest-find-interpreter
Browse files Browse the repository at this point in the history
Add a unit test for `PythonInterpreter::find_by_target`
  • Loading branch information
messense authored May 7, 2023
2 parents bcb3c29 + f731a3a commit 7421443
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl BuildContext {
let interp_names: HashSet<_> = non_abi3_interps
.iter()
.map(|interp| match interp.interpreter_kind {
InterpreterKind::CPython => interp.implmentation_name.to_string(),
InterpreterKind::CPython => interp.implementation_name.to_string(),
InterpreterKind::PyPy => "PyPy".to_string(),
})
.collect();
Expand Down
8 changes: 4 additions & 4 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl BuildOptions {
executable: PathBuf::new(),
platform: None,
runnable: false,
implmentation_name: interpreter_kind.to_string().to_ascii_lowercase(),
implementation_name: interpreter_kind.to_string().to_ascii_lowercase(),
soabi: soabi.cloned(),
});
} else {
Expand Down Expand Up @@ -360,7 +360,7 @@ impl BuildOptions {
executable: PathBuf::new(),
platform: None,
runnable: false,
implmentation_name: "cpython".to_string(),
implementation_name: "cpython".to_string(),
soabi: None,
}])
} else if let Some(config_file) = env::var_os("PYO3_CONFIG_FILE") {
Expand All @@ -387,7 +387,7 @@ impl BuildOptions {
executable: PathBuf::new(),
platform: None,
runnable: false,
implmentation_name: "cpython".to_string(),
implementation_name: "cpython".to_string(),
soabi: None,
}])
} else {
Expand Down Expand Up @@ -427,7 +427,7 @@ impl BuildOptions {
executable: PathBuf::new(),
platform: None,
runnable: false,
implmentation_name: "cpython".to_string(),
implementation_name: "cpython".to_string(),
soabi: None,
}])
} else if target.cross_compiling() {
Expand Down
41 changes: 33 additions & 8 deletions src/python_interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub struct PythonInterpreter {
/// and it's `executable` is empty
pub runnable: bool,
/// Comes from `sys.platform.name`
pub implmentation_name: String,
pub implementation_name: String,
/// Comes from sysconfig var `SOABI`
pub soabi: Option<String>,
}
Expand Down Expand Up @@ -433,7 +433,7 @@ fn fun_with_abiflags(
impl PythonInterpreter {
/// Does this interpreter have PEP 384 stable api aka. abi3 support?
pub fn has_stable_api(&self) -> bool {
if self.implmentation_name.parse::<InterpreterKind>().is_err() {
if self.implementation_name.parse::<InterpreterKind>().is_err() {
false
} else {
match self.interpreter_kind {
Expand Down Expand Up @@ -465,12 +465,12 @@ impl PythonInterpreter {
} else {
context.get_platform_tag(platform_tags)?
};
let tag = if self.implmentation_name.parse::<InterpreterKind>().is_err() {
let tag = if self.implementation_name.parse::<InterpreterKind>().is_err() {
// Use generic tags when `sys.implementation.name` != `platform.python_implementation()`, for example Pyston
// See also https://github.com/pypa/packaging/blob/0031046f7fad649580bc3127d1cef9157da0dd79/packaging/tags.py#L234-L261
format!(
"{interpreter}{major}{minor}-{soabi}-{platform}",
interpreter = self.implmentation_name,
interpreter = self.implementation_name,
major = self.major,
minor = self.minor,
soabi = self
Expand Down Expand Up @@ -676,20 +676,20 @@ impl PythonInterpreter {
executable,
platform,
runnable: true,
implmentation_name: message.implementation_name,
implementation_name: message.implementation_name,
soabi: message.soabi,
}))
}

/// Construct a `PythonInterpreter` from a sysconfig and target
pub fn from_config(config: InterpreterConfig) -> Self {
let implmentation_name = config.interpreter_kind.to_string().to_ascii_lowercase();
let implementation_name = config.interpreter_kind.to_string().to_ascii_lowercase();
PythonInterpreter {
config,
executable: PathBuf::new(),
platform: None,
runnable: false,
implmentation_name,
implementation_name,
soabi: None,
}
}
Expand Down Expand Up @@ -879,7 +879,7 @@ impl PythonInterpreter {
let pointer_width = self.pointer_width.unwrap_or(64);
format!(
"{}-{}.{}-{}bit",
self.implmentation_name, self.major, self.minor, pointer_width
self.implementation_name, self.major, self.minor, pointer_width
)
}

Expand Down Expand Up @@ -928,3 +928,28 @@ impl fmt::Display for PythonInterpreter {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_find_interpreter_by_target() {
let target =
Target::from_target_triple(Some("x86_64-unknown-linux-gnu".to_string())).unwrap();
let pythons = PythonInterpreter::find_by_target(&target, None);
assert_eq!(pythons.len(), 10);

let pythons = PythonInterpreter::find_by_target(
&target,
Some(&VersionSpecifiers::from_str(">=3.7").unwrap()),
);
assert_eq!(pythons.len(), 9);

let pythons = PythonInterpreter::find_by_target(
&target,
Some(&VersionSpecifiers::from_str(">=3.10").unwrap()),
);
assert_eq!(pythons.len(), 3);
}
}

0 comments on commit 7421443

Please sign in to comment.