Skip to content

Commit

Permalink
cargo fix --edition to prepare for Rust edition 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Feb 21, 2025
1 parent 865f965 commit 6f18099
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 162 deletions.
17 changes: 10 additions & 7 deletions src/auditwheel/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,19 @@ pub fn auditwheel_rs(
}
Err(err) => Err(err),
}
} else if let Some(policy) = highest_policy {
Ok(policy)
} else {
eprintln!(
"⚠️ Warning: No compatible platform tag found, using the linux tag instead. \
match highest_policy {
Some(policy) => Ok(policy),
_ => {
eprintln!(
"⚠️ Warning: No compatible platform tag found, using the linux tag instead. \
You won't be able to upload those wheels to PyPI."
);
);

// Fallback to linux
Ok(Policy::default())
// Fallback to linux
Ok(Policy::default())
}
}
}?;
Ok((policy, should_repair))
}
Expand Down
11 changes: 4 additions & 7 deletions src/auditwheel/platform_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ impl PlatformTag {
/// manylinux aliases
pub fn aliases(&self) -> Vec<String> {
match self {
PlatformTag::Manylinux { .. } => {
if let Some(policy) = Policy::from_name(&self.to_string()) {
policy.aliases
} else {
Vec::new()
}
}
PlatformTag::Manylinux { .. } => match Policy::from_name(&self.to_string()) {
Some(policy) => policy.aliases,
_ => Vec::new(),
},
PlatformTag::Musllinux { .. } => Vec::new(),
PlatformTag::Linux => Vec::new(),
}
Expand Down
12 changes: 6 additions & 6 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,16 @@ impl BuildContext {
// macOS
(Os::Macos, Arch::X86_64) | (Os::Macos, Arch::Aarch64) => {
let ((x86_64_major, x86_64_minor), (arm64_major, arm64_minor)) = macosx_deployment_target(env::var("MACOSX_DEPLOYMENT_TARGET").ok().as_deref(), self.universal2)?;
let x86_64_tag = if let Some(deployment_target) = self.pyproject_toml.as_ref().and_then(|x| x.target_config("x86_64-apple-darwin")).and_then(|config| config.macos_deployment_target.as_ref()) {
let x86_64_tag = match self.pyproject_toml.as_ref().and_then(|x| x.target_config("x86_64-apple-darwin")).and_then(|config| config.macos_deployment_target.as_ref()) { Some(deployment_target) => {
deployment_target.replace('.', "_")
} else {
} _ => {
format!("{x86_64_major}_{x86_64_minor}")
};
let arm64_tag = if let Some(deployment_target) = self.pyproject_toml.as_ref().and_then(|x| x.target_config("aarch64-apple-darwin")).and_then(|config| config.macos_deployment_target.as_ref()) {
}};
let arm64_tag = match self.pyproject_toml.as_ref().and_then(|x| x.target_config("aarch64-apple-darwin")).and_then(|config| config.macos_deployment_target.as_ref()) { Some(deployment_target) => {
deployment_target.replace('.', "_")
} else {
} _ => {
format!("{arm64_major}_{arm64_minor}")
};
}};
if self.universal2 {
format!(
"macosx_{x86_64_tag}_x86_64.macosx_{arm64_tag}_arm64.macosx_{x86_64_tag}_universal2"
Expand Down
14 changes: 8 additions & 6 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@ impl BuildOptions {
let host_python = &host_interpreters[0];
eprintln!("🐍 Using host {host_python} for cross-compiling preparation");
// pyo3
env::set_var("PYO3_PYTHON", &host_python.executable);
unsafe { env::set_var("PYO3_PYTHON", &host_python.executable) };
// rust-cpython, and legacy pyo3 versions
env::set_var("PYTHON_SYS_EXECUTABLE", &host_python.executable);
unsafe { env::set_var("PYTHON_SYS_EXECUTABLE", &host_python.executable) };

let sysconfig_path = find_sysconfigdata(cross_lib_dir.as_ref(), target)?;
env::set_var(
"MATURIN_PYTHON_SYSCONFIGDATA_DIR",
sysconfig_path.parent().unwrap(),
);
unsafe {
env::set_var(
"MATURIN_PYTHON_SYSCONFIGDATA_DIR",
sysconfig_path.parent().unwrap(),
)
};

let sysconfig_data = parse_sysconfigdata(host_python, sysconfig_path)?;
let major = sysconfig_data
Expand Down
4 changes: 2 additions & 2 deletions src/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,12 +1286,12 @@ mod tests {

#[test]
fn test_generate_github_zig_pytest() {
let gen = GenerateCI {
let r#gen = GenerateCI {
zig: true,
pytest: true,
..Default::default()
};
let conf = gen
let conf = r#gen
.generate_github(
"example",
&BridgeModel::Bindings(Bindings {
Expand Down
94 changes: 49 additions & 45 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,52 +255,56 @@ impl ProjectResolver {
pyproject_file
);
let pyproject = PyProjectToml::new(&pyproject_file)?;
if let Some(path) = pyproject.manifest_path() {
debug!("Using cargo manifest path from pyproject.toml {:?}", path);
return Ok((
path.normalize()
.with_context(|| {
format!("failed to normalize manifest path `{}`", path.display())
})?
.into_path_buf(),
pyproject_file,
));
} else {
// Detect src layout:
//
// my-project
// ├── README.md
// ├── pyproject.toml
// ├── src
// │ └── my_project
// │ ├── __init__.py
// │ └── bar.py
// └── rust
// ├── Cargo.toml
// └── src
// └── lib.rs
let path = current_dir.join("rust").join("Cargo.toml");
if path.is_file() {
debug!("Python first src-layout detected");
if pyproject.python_source().is_some() {
// python source directory is specified in pyproject.toml
return Ok((path, pyproject_file));
} else if let Some(project_name) = pyproject.project_name() {
// Check if python source directory in `src/<project_name>`
let import_name = project_name.replace('-', "_");
let mut package_init = HashSet::new();
package_init.insert(
current_dir
.join("src")
.join(import_name)
.join("__init__.py"),
);
for package in pyproject.python_packages().unwrap_or_default() {
package_init
.insert(current_dir.join("src").join(package).join("__init__.py"));
}
if package_init.iter().any(|x| x.is_file()) {
match pyproject.manifest_path() {
Some(path) => {
debug!("Using cargo manifest path from pyproject.toml {:?}", path);
return Ok((
path.normalize()
.with_context(|| {
format!("failed to normalize manifest path `{}`", path.display())
})?
.into_path_buf(),
pyproject_file,
));
}
_ => {
// Detect src layout:
//
// my-project
// ├── README.md
// ├── pyproject.toml
// ├── src
// │ └── my_project
// │ ├── __init__.py
// │ └── bar.py
// └── rust
// ├── Cargo.toml
// └── src
// └── lib.rs
let path = current_dir.join("rust").join("Cargo.toml");
if path.is_file() {
debug!("Python first src-layout detected");
if pyproject.python_source().is_some() {
// python source directory is specified in pyproject.toml
return Ok((path, pyproject_file));
} else if let Some(project_name) = pyproject.project_name() {
// Check if python source directory in `src/<project_name>`
let import_name = project_name.replace('-', "_");
let mut package_init = HashSet::new();
package_init.insert(
current_dir
.join("src")
.join(import_name)
.join("__init__.py"),
);
for package in pyproject.python_packages().unwrap_or_default() {
package_init.insert(
current_dir.join("src").join(package).join("__init__.py"),
);
}
if package_init.iter().any(|x| x.is_file()) {
return Ok((path, pyproject_file));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl GlobPattern {
pub fn targets(&self, format: Format) -> Option<&str> {
match self {
// Not specified defaults to both
Self::Path(ref glob) => Some(glob),
Self::Path(glob) => Some(glob),
Self::WithFormat {
path,
format: formats,
Expand Down
103 changes: 54 additions & 49 deletions src/python_interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ fn find_all_windows(
Ok(output) => output,
Err(err) => {
eprintln!(
"⚠️ Warning: failed to determine the path to python for `{executable}`: {err}"
);
"⚠️ Warning: failed to determine the path to python for `{executable}`: {err}"
);
continue;
}
};
Expand Down Expand Up @@ -275,21 +275,22 @@ fn windows_python_info(executable: &Path) -> Result<Option<WindowsPythonInfo>> {

let version_info = str::from_utf8(&python_info.stdout).unwrap();
let expr = Regex::new(r"(\d).(\d).(\d+)").unwrap();
if let Some(capture) = expr.captures(version_info) {
let major = capture.get(1).unwrap().as_str().parse::<usize>().unwrap();
let minor = capture.get(2).unwrap().as_str().parse::<usize>().unwrap();
let pointer_width = if version_info.contains("64 bit (AMD64)") {
64
} else {
32
};
Ok(Some(WindowsPythonInfo {
major,
minor,
pointer_width: Some(pointer_width),
}))
} else {
Ok(None)
match expr.captures(version_info) {
Some(capture) => {
let major = capture.get(1).unwrap().as_str().parse::<usize>().unwrap();
let minor = capture.get(2).unwrap().as_str().parse::<usize>().unwrap();
let pointer_width = if version_info.contains("64 bit (AMD64)") {
64
} else {
32
};
Ok(Some(WindowsPythonInfo {
major,
minor,
pointer_width: Some(pointer_width),
}))
}
_ => Ok(None),
}
}

Expand Down Expand Up @@ -620,28 +621,31 @@ impl PythonInterpreter {
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
if cfg!(windows) {
if let Some(python) = executable.as_ref().to_str() {
let ver = if python.starts_with("python") {
python.strip_prefix("python").unwrap_or(python)
} else {
python
};
// Try py -x.y on Windows
let mut metadata_py = tempfile::NamedTempFile::new()?;
write!(metadata_py, "{GET_INTERPRETER_METADATA}")?;
let mut cmd = Command::new("cmd");
cmd.arg("/c")
.arg("py")
.arg(format!("-{}-{}", ver, target.pointer_width()))
.arg(metadata_py.path())
.env("PYTHONNOUSERSITE", "1");
let output = cmd.output();
match output {
Ok(output) if output.status.success() => output,
_ => return Ok(None),
match executable.as_ref().to_str() {
Some(python) => {
let ver = if python.starts_with("python") {
python.strip_prefix("python").unwrap_or(python)
} else {
python
};
// Try py -x.y on Windows
let mut metadata_py = tempfile::NamedTempFile::new()?;
write!(metadata_py, "{GET_INTERPRETER_METADATA}")?;
let mut cmd = Command::new("cmd");
cmd.arg("/c")
.arg("py")
.arg(format!("-{}-{}", ver, target.pointer_width()))
.arg(metadata_py.path())
.env("PYTHONNOUSERSITE", "1");
let output = cmd.output();
match output {
Ok(output) if output.status.success() => output,
_ => return Ok(None),
}
}
_ => {
return Ok(None);
}
} else {
return Ok(None);
}
} else {
return Ok(None);
Expand Down Expand Up @@ -849,17 +853,18 @@ impl PythonInterpreter {
) -> Result<Vec<PythonInterpreter>> {
let mut available_versions = Vec::new();
for executable in executables {
if let Some(version) = PythonInterpreter::check_executable(executable, target, bridge)
.context(format!(
"{} is not a valid python interpreter",
executable.display()
))? {
available_versions.push(version);
} else {
bail!(
"Python interpreter `{}` doesn't exist",
executable.display()
);
match PythonInterpreter::check_executable(executable, target, bridge).context(
format!("{} is not a valid python interpreter", executable.display()),
)? {
Some(version) => {
available_versions.push(version);
}
_ => {
bail!(
"Python interpreter `{}` doesn't exist",
executable.display()
);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn rewrite_cargo_toml(
} else {
let mut new_members = toml_edit::Array::new();
for member in members {
if let toml_edit::Value::String(ref s) = member {
if let toml_edit::Value::String(s) = member {
let member_path = s.value();
// See https://github.com/rust-lang/cargo/blob/0de91c89e6479016d0ed8719fdc2947044335b36/src/cargo/util/restricted_names.rs#L119-L122
let is_glob_pattern = member_path.contains(['*', '?', '[', ']']);
Expand Down
Loading

0 comments on commit 6f18099

Please sign in to comment.