π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Fix for reading certain Ogg files on Linux via file-like objects. (#436)
Peter Sobotab36dbb72814
diff --git a/pedalboard/io/PythonFileLike.h b/pedalboard/io/PythonFileLike.hindex 39c2258..cdb4748 100644--- a/pedalboard/io/PythonFileLike.h+++ b/pedalboard/io/PythonFileLike.h@@ -17,6 +17,7 @@#pragma once+#include <cerrno>#include <mutex>#include <optional>@@ -45,6 +46,31 @@ inline void raise() {}}; // namespace PythonException+/**+ * @brief A helper class that clears the thread's errno when destructed.+ *+ * This is used to avoid failure when the following sequence of events occurs:+ *+ * 1. A Python file-like object is passed to Pedalboard, which could call any+ * other native code in its methods+ * 2. Pedalboard calls a Python file-like object's methods (e.g. read(), seek(),+ * tell(), seekable())+ * 3. The native code sets errno to a non-zero value, but does not clear it+ * 4. Pedalboard's codecs (Ogg Vorbis, etc) check errno and fail to decode+ * 5. The user is presented with a cryptic error message+ *+ * This makes it seem like we're ignoring errno, but we're not; the Python-level+ * code should throw an exception if the file-like object has an error, and we+ * handle errors at that level correctly.+ *+ * See also: https://en.wikipedia.org/wiki/Errno.h+ */+class ClearErrnoBeforeReturn {+public:+ ClearErrnoBeforeReturn() {}+ ~ClearErrnoBeforeReturn() { errno = 0; }+};+/*** A tiny helper class that downgrades a write lock* to a read lock for the given scope. If we can'tdiff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.hindex aa46751..c3d9c0f 100644--- a/pedalboard/io/PythonInputStream.h+++ b/pedalboard/io/PythonInputStream.h@@ -63,6 +63,7 @@ public:juce::int64 getTotalLength() noexcept override {ScopedDowngradeToReadLockWithGIL lock(objectLock);+ ClearErrnoBeforeReturn clearErrnoBeforeReturn;py::gil_scoped_acquire acquire;if (PythonException::isPending())@@ -99,6 +100,7 @@ public:// sign that something is broken!jassert(buffer != nullptr && bytesToRead >= 0);ScopedDowngradeToReadLockWithGIL lock(objectLock);+ ClearErrnoBeforeReturn clearErrnoBeforeReturn;py::gil_scoped_acquire acquire;if (PythonException::isPending())@@ -161,6 +163,7 @@ public:juce::int64 totalLength = getTotalLength();ScopedDowngradeToReadLockWithGIL lock(objectLock);+ ClearErrnoBeforeReturn clearErrnoBeforeReturn;py::gil_scoped_acquire acquire;if (PythonException::isPending())@@ -183,6 +186,7 @@ public:juce::int64 getPosition() noexcept override {ScopedDowngradeToReadLockWithGIL lock(objectLock);+ ClearErrnoBeforeReturn clearErrnoBeforeReturn;py::gil_scoped_acquire acquire;if (PythonException::isPending())@@ -201,6 +205,7 @@ public:bool setPosition(juce::int64 pos) noexcept override {ScopedDowngradeToReadLockWithGIL lock(objectLock);+ ClearErrnoBeforeReturn clearErrnoBeforeReturn;py::gil_scoped_acquire acquire;if (PythonException::isPending())diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.hindex 2c4c478..8a1597d 100644--- a/pedalboard/io/ReadableAudioFile.h+++ b/pedalboard/io/ReadableAudioFile.h@@ -155,7 +155,7 @@ public:} else {ss << " has its stream position set to the end of the stream ("<< originalStreamPosition;- ss << "bytes).";+ ss << " bytes).";}ss << " Try seeking this file-like object back to its start before ""passing it to AudioFile";diff --git a/tests/test_io.py b/tests/test_io.pyindex 4055838..fa59df1 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -1592,3 +1592,67 @@ def test_write_two_by_two_buffer_with_hint():f.write(np.zeros((2, 2), dtype=np.float32))assert f.tell() == 2+++class ErrnoTriggeringFileLike(BinaryIO):+ """A file-like object that triggers errno to be set through real failed operations."""++ def __init__(self, wrapped_file):+ self._file = wrapped_file+ # Create a path that definitely doesn't exist for triggering ENOENT+ self._nonexistent_path = str(pathlib.Path("definitely_does_not_exist_12345.txt"))++ def _trigger_errno(self):+ # Try to open a non-existent file, which sets errno to ENOENT (2)+ # We catch the exception but errno remains set+ try:+ open(self._nonexistent_path, "rb")+ except FileNotFoundError:+ pass # Expected - errno is now set to ENOENT++ def read(self, *args, **kwargs):+ result = self._file.read(*args, **kwargs)+ self._trigger_errno()+ return result++ def seek(self, *args, **kwargs):+ result = self._file.seek(*args, **kwargs)+ self._trigger_errno()+ return result++ def tell(self):+ result = self._file.tell()+ self._trigger_errno()+ return result++ def seekable(self):+ result = self._file.seekable()+ self._trigger_errno()+ return result++[email protected]("format_name", ["wav", "ogg"])+def test_errno_cleared_after_python_file_operations(format_name: str):+ """+ Regression test to ensure that errno values set by Python file-like objects+ don't leak through and cause codec failures. Before the fix (ClearErrnoBeforeReturn),+ errno values set during Python file operations would cause mysterious failures in+ codecs like Ogg Vorbis that check errno.++ This test creates a file-like wrapper that intentionally sets errno to a non-zero+ value after each operation, simulating what might happen with certain third-party+ libraries or file systems. The fix ensures these errno values are cleared before+ returning to codec code.+ """++ buf = io.BytesIO()+ buf.name = f"test.{format_name}"+ with pedalboard.io.AudioFile(buf, "w", 44100, 2, format=format_name) as af:+ af.write(np.random.rand(2, 44100))++ # Add some random garbage past the end of the file to trigger the bug:+ buf.write(b"\x00\x00")+ buf.seek(0)++ with pedalboard.io.AudioFile(ErrnoTriggeringFileLike(buf)) as af:+ assert af.samplerate == 44100