Skip to content

Commit

Permalink
Merge pull request PyO3#1203 from messense/path-strip-prefix
Browse files Browse the repository at this point in the history
Use `unwrap` instead of `?` operator for some `strip_prefix` calls
  • Loading branch information
messense authored Oct 18, 2022
2 parents 5f2c219 + 1b8ccf6 commit c63bad4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ pub fn write_bindings_module(

let relative = project_layout
.rust_module
.strip_prefix(python_module.parent().unwrap())?;
.strip_prefix(python_module.parent().unwrap())
.unwrap();
writer.add_file_with_permissions(relative.join(&so_filename), &artifact, 0o755)?;
}
} else {
Expand Down Expand Up @@ -729,7 +730,8 @@ pub fn write_cffi_module(

let relative = project_layout
.rust_module
.strip_prefix(python_module.parent().unwrap())?;
.strip_prefix(python_module.parent().unwrap())
.unwrap();
module = relative.join(&project_layout.extension_name);
if !editable {
writer.add_directory(&module)?;
Expand Down Expand Up @@ -839,7 +841,9 @@ pub fn write_python_part(
) -> Result<()> {
for absolute in WalkBuilder::new(&python_module).hidden(false).build() {
let absolute = absolute?.into_path();
let relative = absolute.strip_prefix(python_module.as_ref().parent().unwrap())?;
let relative = absolute
.strip_prefix(python_module.as_ref().parent().unwrap())
.unwrap();
if absolute.is_dir() {
writer.add_directory(relative)?;
} else {
Expand Down Expand Up @@ -936,7 +940,7 @@ pub fn add_data(writer: &mut impl ModuleWriter, data: Option<&Path>) -> Result<(
.build()
{
let file = file?;
let relative = file.path().strip_prefix(data.parent().unwrap())?;
let relative = file.path().strip_prefix(data.parent().unwrap()).unwrap();

if file.path_is_symlink() {
// Copy the actual file contents, not the link, so that you can create a
Expand Down
13 changes: 7 additions & 6 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ fn add_crate_to_source_distribution(

let local_deps_folder = if cargo_toml_in_subdir {
let level = abs_manifest_dir
.strip_prefix(pyproject_dir)?
.strip_prefix(pyproject_dir)
.unwrap()
.components()
.count();
format!("{}{}", "../".repeat(level), LOCAL_DEPENDENCIES_FOLDER)
Expand All @@ -342,7 +343,7 @@ fn add_crate_to_source_distribution(
writer.add_directory(prefix)?;

let cargo_toml = if cargo_toml_in_subdir {
let relative_manifest_path = abs_manifest_path.strip_prefix(pyproject_dir)?;
let relative_manifest_path = abs_manifest_path.strip_prefix(pyproject_dir).unwrap();
prefix.join(relative_manifest_path)
} else {
prefix.join(manifest_path.file_name().unwrap())
Expand Down Expand Up @@ -482,9 +483,9 @@ pub fn source_distribution(
if cargo_lock_required || cargo_lock_path.exists() {
let project_root = pyproject_toml_path.parent().unwrap();
let relative_cargo_lock = if cargo_lock_path.starts_with(project_root) {
cargo_lock_path.strip_prefix(project_root)?
cargo_lock_path.strip_prefix(project_root).unwrap()
} else {
cargo_lock_path.strip_prefix(&abs_manifest_dir)?
cargo_lock_path.strip_prefix(&abs_manifest_dir).unwrap()
};
writer.add_file(root_dir.join(relative_cargo_lock), &cargo_lock_path)?;
} else {
Expand Down Expand Up @@ -512,7 +513,7 @@ pub fn source_distribution(
continue;
}
let source = fs::canonicalize(source)?;
let target = root_dir.join(source.strip_prefix(&pyproject_dir)?);
let target = root_dir.join(source.strip_prefix(&pyproject_dir).unwrap());
if source.is_dir() {
writer.add_directory(target)?;
} else {
Expand Down Expand Up @@ -542,7 +543,7 @@ pub fn source_distribution(
.expect("No files found for pattern")
.filter_map(Result::ok)
{
let target = root_dir.join(&source.strip_prefix(pyproject_dir)?);
let target = root_dir.join(&source.strip_prefix(pyproject_dir).unwrap());
if source.is_dir() {
writer.add_directory(target)?;
} else {
Expand Down

0 comments on commit c63bad4

Please sign in to comment.