Skip to content

Commit

Permalink
Raise any Python exceptions thrown during seek operations on file-lik…
Browse files Browse the repository at this point in the history
…e objects. (#262)
  • Loading branch information
psobot authored Oct 13, 2023
1 parent 9a92fae commit 2c35d8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 1 addition & 4 deletions pedalboard/io/PythonFileLike.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ class PythonFileLike {

if (!PythonException::isPending()) {
try {
if (fileLike.attr("seekable")().cast<bool>()) {
fileLike.attr("seek")(pos);
}

fileLike.attr("seek")(pos);
return fileLike.attr("tell")().cast<juce::int64>() == pos;
} catch (py::error_already_set e) {
e.restore();
Expand Down
13 changes: 13 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,19 @@ def test_no_crash_if_type_error_on_file_like():
assert "bool" in str(e)


def test_file_like_must_be_seekable_for_write():
stream = io.BytesIO()
stream.seek = lambda x: (_ for _ in ()).throw(
ValueError(f"Failed to seek from {stream.tell():,} to {x:,} because I don't wanna")
)

with pytest.raises(ValueError) as e:
with pedalboard.io.AudioFile(stream, "w", 44100, 2, format="flac"):
pass

assert "I don't wanna" in str(e)


def test_write_fails_without_extension(tmp_path: pathlib.Path):
dest_path = str(tmp_path / "no_extension")
with pytest.raises(ValueError):
Expand Down

0 comments on commit 2c35d8f

Please sign in to comment.