Skip to content

Commit

Permalink
Fix sysconfig.get_platform parsing for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Nov 20, 2021
1 parent 075eb1e commit a5f1912
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/python_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ fn fun_with_abiflags(
}
}

/// macOS contains system version in platform name so need special handle
///
/// See https://github.com/pypa/wheel/blob/0acd203cd896afec7f715aa2ff5980a403459a3b/src/wheel/macosx_libfile.py#L359-L428
fn calculate_macosx_platform_tag(platform: &str) -> Result<String> {
let mut parts = platform.splitn(3, '-');
let err_ctx = "Invalid platform string from `sysconfig.get_platform()`";
let prefix = parts.next().context(err_ctx)?;
let base_version = parts.next().context(err_ctx)?;
let suffix = parts.next().context(err_ctx)?;
let (major_ver, minor_ver) = base_version.split_once('.').unwrap_or((base_version, "0"));
let major_ver: u64 = major_ver.parse()?;
let minor_ver = if major_ver > 10 { "0" } else { minor_ver };
// FIXME: We should also take MACOSX_DEPLOYMENT_TARGET env var into account
let new_platform = format!("{}_{}_{}_{}", prefix, major_ver, minor_ver, suffix);
Ok(new_platform)
}

impl PythonInterpreter {
/// Returns the supported python environment in the PEP 425 format used for the wheel filename:
/// {python tag}-{abi tag}-{platform tag}
Expand Down Expand Up @@ -496,17 +513,7 @@ impl PythonInterpreter {
))?;

let platform = if message.platform.starts_with("macosx") {
// macOS contains system version in platform name so need special handle
let mut parts = message.platform.splitn(3, '-');
let err_ctx = "Invalid platform string from `sysconfig.get_platform()`";
let prefix = parts.next().context(err_ctx)?;
let base_version = parts.next().context(err_ctx)?;
let suffix = parts.next().context(err_ctx)?;
let (major_ver, minor_ver) = base_version.split_once('.').context(err_ctx)?;
let major_ver: u64 = major_ver.parse()?;
let minor_ver = if major_ver > 10 { "0" } else { minor_ver };
// FIXME: We should also take MACOSX_DEPLOYMENT_TARGET env var into account
format!("{}_{}_{}_{}", prefix, major_ver, minor_ver, suffix)
calculate_macosx_platform_tag(&message.platform)?
} else {
message.platform
};
Expand Down Expand Up @@ -641,3 +648,28 @@ impl fmt::Display for PythonInterpreter {
)
}
}

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

#[test]
fn test_calculate_macosx_platform_tag() {
assert_eq!(
calculate_macosx_platform_tag("macosx-10.7-x86_64").unwrap(),
"macosx_10_7_x86_64"
);
assert_eq!(
calculate_macosx_platform_tag("macosx-11-x86_64").unwrap(),
"macosx_11_0_x86_64"
);
assert_eq!(
calculate_macosx_platform_tag("macosx-11-arm64").unwrap(),
"macosx_11_0_arm64"
);
assert_eq!(
calculate_macosx_platform_tag("macosx-11.1-arm64").unwrap(),
"macosx_11_0_arm64"
);
}
}

0 comments on commit a5f1912

Please sign in to comment.