Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent accidental renaming when using with_suffix #1192

Merged
merged 4 commits into from
Oct 25, 2023

Conversation

chiiyeh
Copy link
Contributor

@chiiyeh chiiyeh commented Oct 18, 2023

Fixes #1183

The use of .with_suffix replaces the strings after the ., which leads to issues when the key/path contains . in the name.

Copy link
Collaborator

@desh2608 desh2608 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test for this change?

@@ -280,7 +280,8 @@ def write(self, key: str, value: np.ndarray) -> str:
# too many files in a single directory.
subdir = self.storage_path_ / key[:3]
subdir.mkdir(exist_ok=True)
output_features_path = (subdir / key).with_suffix(".llc")
p = subdir / key
output_features_path = p.with_suffix(p.suffix + ".llc" if p.suffix != ".llc" else ".llc")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the if/else needed here? (Same for all below changes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if/else is to prevent adding of suffix if p contains the suffix for some reason say "test.llc". So it will not become "test.llc.llc".
Which is the expected behavior mentioned here
https://github.com/lhotse-speech/lhotse/blob/master/lhotse/features/io.py#L443

@chiiyeh
Copy link
Contributor Author

chiiyeh commented Oct 19, 2023

I am not too sure what sort of test is needed, is it those similar to the one in the main testing folder like this:
https://github.com/lhotse-speech/lhotse/blob/master/test/features/test_chunky_writer.py#L31-L35
Also for the code referred in this test, not sure if there is a bug, as the writer variable is used at the get_reader outside of the with that it is created.

@pzelasko
Copy link
Collaborator

A test similar to that one would do. The goal is to cover a scenario in which the test would have failed before the change, but passes after it. You might want to use either tmpdir pytest fixture or python's NamedTemporaryFile to create a tmp file that has a problematic name.

Also for the code referred in this test, not sure if there is a bug, as the writer variable is used at the get_reader outside of the with that it is created.

That test is correct, writer still exists after with (at which point writer.__exit__() is being called), and we're only fetching the .name property to know which type of reader to create.

@chiiyeh
Copy link
Contributor Author

chiiyeh commented Oct 23, 2023

Thanks for the clarification. Have added in the test. Tested it on my side, should be working.

pzelasko
pzelasko previously approved these changes Oct 25, 2023
Copy link
Collaborator

@pzelasko pzelasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks!

@pzelasko pzelasko added this to the v1.18 milestone Oct 25, 2023
@pzelasko pzelasko enabled auto-merge (squash) October 25, 2023 01:49
auto-merge was automatically disabled October 25, 2023 02:24

Head branch was pushed to by a user without write access

pzelasko
pzelasko previously approved these changes Oct 25, 2023
@pzelasko pzelasko enabled auto-merge (squash) October 25, 2023 04:13
auto-merge was automatically disabled October 25, 2023 04:33

Head branch was pushed to by a user without write access

@chiiyeh
Copy link
Contributor Author

chiiyeh commented Oct 25, 2023

Gosh.. so sorry.. had trouble using the isort on my vscode.. and missed one of the changes.

@pzelasko pzelasko enabled auto-merge (squash) October 25, 2023 11:29
@pzelasko pzelasko merged commit 523b653 into lhotse-speech:master Oct 25, 2023
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LilcomChunkyWriter replaces storage_path if it contains "."
3 participants