π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Be more fault-tolerant of misbehaving file-like objects.
Peter Sobot33137e852e66
diff --git a/pedalboard/io/PythonOutputStream.h b/pedalboard/io/PythonOutputStream.hindex 075e0a9..6d81fa8 100644--- a/pedalboard/io/PythonOutputStream.h+++ b/pedalboard/io/PythonOutputStream.h@@ -79,9 +79,25 @@ public:return false;try {- int bytesWritten =- fileLike.attr("write")(py::bytes((const char *)ptr, numBytes))- .cast<int>();+ py::object writeResponse =+ fileLike.attr("write")(py::bytes((const char *)ptr, numBytes));++ int bytesWritten;+ if (writeResponse.is_none()) {+ // Assume bytesWritten is numBytes if `write` returned 0.+ // This shouldn't happen, but sometimes does if the file-like+ // object is not fully compliant with io.RawIOBase.+ bytesWritten = numBytes;+ } else {+ try {+ bytesWritten = writeResponse.cast<int>();+ } catch (const py::cast_error &e) {+ throw py::type_error(+ py::repr(fileLike.attr("write")).cast<std::string>() ++ " was expected to return an integer, but got " ++ py::repr(writeResponse).cast<std::string>());+ }+ }if (bytesWritten < numBytes) {return false;@@ -110,10 +126,25 @@ public:for (size_t i = 0; i < numTimesToRepeat; i += buffer.size()) {const size_t chunkSize = std::min(numTimesToRepeat - i, buffer.size());- int bytesWritten = fileLike- .attr("write")(py::bytes(- (const char *)buffer.data(), chunkSize))- .cast<int>();+ py::object writeResponse = fileLike.attr("write")(+ py::bytes((const char *)buffer.data(), chunkSize));++ int bytesWritten;+ if (writeResponse.is_none()) {+ // Assume bytesWritten is numBytes if `write` returned 0.+ // This shouldn't happen, but sometimes does if the file-like+ // object is not fully compliant with io.RawIOBase.+ bytesWritten = chunkSize;+ } else {+ try {+ bytesWritten = writeResponse.cast<int>();+ } catch (const py::cast_error &e) {+ throw py::type_error(+ py::repr(fileLike.attr("write")).cast<std::string>() ++ " was expected to return an integer, but got " ++ py::repr(writeResponse).cast<std::string>());+ }+ }if (bytesWritten != chunkSize) {return false;