Remove all unwrap calls from the resolver (#5426)

This commit is contained in:
Charlie Marsh 2023-06-28 14:06:17 -04:00 committed by GitHub
parent 4d90a5a9bc
commit 864f50a3a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 50 deletions

View file

@ -1,7 +1,7 @@
//! Support for native Python extension modules.
use std::ffi::OsStr;
use std::path::Path;
use std::path::{Path, PathBuf};
/// Returns `true` if the given file extension is that of a native module.
pub(crate) fn is_native_module_file_extension(file_extension: &OsStr) -> bool {
@ -36,6 +36,18 @@ pub(crate) fn is_native_module_file_name(module_name: &str, file_name: &Path) ->
native_module_name(file_name) == Some(module_name)
}
/// Find the native module within the namespace package at the given path.
pub(crate) fn find_native_module(dir_path: &Path) -> Option<PathBuf> {
let module_name = dir_path.file_name()?.to_str()?;
dir_path
.read_dir()
.ok()?
.flatten()
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
.map(|entry| entry.path())
.find(|path| is_native_module_file_name(module_name, path))
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;