π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Fix Python 3.10 crash when reading bytes from Python.
Peter Sobote4c8b663dacd
diff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.hindex 5d77dbc..1c9a787 100644--- a/pedalboard/io/PythonInputStream.h+++ b/pedalboard/io/PythonInputStream.h@@ -102,49 +102,57 @@ public:if (PythonException::isPending())return 0;- py::bytes resultBytes;- {- py::gil_scoped_acquire acquire;- try {- auto readResult = fileLike.attr("read")(bytesToRead);-- if (!py::isinstance<py::bytes>(readResult)) {- std::string message =- "File-like object passed to AudioFile was expected to return "- "bytes from its read(...) method, but "- "returned " +- py::str(readResult.get_type().attr("__name__"))- .cast<std::string>() +- ".";-- if (py::hasattr(fileLike, "mode") &&- fileLike.attr("mode").cast<std::string>() == "r") {- message += " (Try opening the stream in \"rb\" mode instead of "- "\"r\" mode if possible.)";- }-- throw py::type_error(message);- return 0;+ py::gil_scoped_acquire acquire;+ try {+ auto readResult = fileLike.attr("read")(bytesToRead);++ if (!py::isinstance<py::bytes>(readResult)) {+ std::string message =+ "File-like object passed to AudioFile was expected to return "+ "bytes from its read(...) method, but "+ "returned " ++ py::str(readResult.get_type().attr("__name__"))+ .cast<std::string>() ++ ".";++ if (py::hasattr(fileLike, "mode") &&+ py::str(fileLike.attr("mode")).cast<std::string>() == "r") {+ message += " (Try opening the stream in \"rb\" mode instead of "+ "\"r\" mode if possible.)";}- resultBytes = readResult.cast<py::bytes>();- } catch (py::error_already_set e) {- e.restore();- return 0;- } catch (const py::builtin_exception &e) {- e.set_error();+ throw py::type_error(message);return 0;}- }- std::string resultAsString = resultBytes;- std::memcpy((char *)buffer, (char *)resultAsString.data(),- resultAsString.size());+ py::bytes bytesObject = readResult.cast<py::bytes>();+ char *pythonBuffer = nullptr;+ ssize_t pythonLength = 0;++ if (PYBIND11_BYTES_AS_STRING_AND_SIZE(bytesObject.ptr(), &pythonBuffer,+ &pythonLength)) {+ throw py::buffer_error(+ "Internal error: failed to read bytes from bytes object!");+ }++ if (!buffer && pythonLength > 0) {+ throw py::buffer_error("Internal error: bytes pointer is null, but a "+ "non-zero number of bytes were returned!");+ }- int bytesCopied = resultAsString.size();+ if (buffer && pythonLength) {+ std::memcpy(buffer, pythonBuffer, pythonLength);+ }- lastReadWasSmallerThanExpected = bytesToRead > bytesCopied;- return bytesCopied;+ lastReadWasSmallerThanExpected = bytesToRead > pythonLength;+ return pythonLength;+ } catch (py::error_already_set e) {+ e.restore();+ return 0;+ } catch (const py::builtin_exception &e) {+ e.set_error();+ return 0;+ }}bool isExhausted() noexcept {diff --git a/pedalboard/io/PythonOutputStream.h b/pedalboard/io/PythonOutputStream.hindex 2316490..035f8ff 100644--- a/pedalboard/io/PythonOutputStream.h+++ b/pedalboard/io/PythonOutputStream.h@@ -95,7 +95,7 @@ public:py::gil_scoped_acquire acquire;try {- const size_t maxEffectiveSize = std::min(numTimesToRepeat, 8192UL);+ const size_t maxEffectiveSize = std::min(numTimesToRepeat, (size_t)8192);std::vector<char> buffer(maxEffectiveSize, byte);for (size_t i = 0; i < numTimesToRepeat; i += buffer.size()) {diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.hindex 35d7e4e..fe9fe53 100644--- a/pedalboard/io/ReadableAudioFile.h+++ b/pedalboard/io/ReadableAudioFile.h@@ -572,7 +572,7 @@ inline void init_readable_audio_file(py::module &m) {.def_property_readonly("samplerate", &ReadableAudioFile::getSampleRate,"The sample rate of this file in samples ""(per channel) per second (Hz).")- .def_property_readonly("channels", &ReadableAudioFile::getNumChannels,+ .def_property_readonly("num_channels", &ReadableAudioFile::getNumChannels,"The number of channels in this file.").def_property_readonly("frames", &ReadableAudioFile::getLengthInSamples,"The total number of frames (samples per "diff --git a/pedalboard/io/WriteableAudioFile.h b/pedalboard/io/WriteableAudioFile.hindex 97148e4..b678497 100644--- a/pedalboard/io/WriteableAudioFile.h+++ b/pedalboard/io/WriteableAudioFile.h@@ -225,16 +225,15 @@ public:// Use the pythonOutputStream's filename if possible, falling back to the// provided filename string (which should contain an extension) if// necessary.- std::string filenameOrFormat;if (!filename.empty()) {- filenameOrFormat = ("." + filename);+ extension = filename;} else if (auto streamName = pythonOutputStream->getFilename()) {- filenameOrFormat = streamName.value();+ // Dummy-stream-filename added here to avoid a JUCE assertion+ // if the stream name doesn't start with a slash.+ juce::File file(File::getSeparatorString().toStdString() + "dummy-stream-filename-" + streamName.value());+ extension = file.getFileExtension().toStdString();}- juce::File file(filenameOrFormat);- extension = file.getFileExtension().toStdString();-format = formatManager.findFormatForFileExtension(extension);if (!format) {@@ -869,7 +868,8 @@ inline void init_writeable_audio_file(py::module &m) {.def_property_readonly("samplerate", &WriteableAudioFile::getSampleRate,"The sample rate of this file in samples ""(per channel) per second (Hz).")- .def_property_readonly("channels", &WriteableAudioFile::getNumChannels,+ .def_property_readonly("num_channels",+ &WriteableAudioFile::getNumChannels,"The number of channels in this file.").def_property_readonly("frames", &WriteableAudioFile::getFramesWritten,"The total number of frames (samples per "diff --git a/tests/test_io.py b/tests/test_io.pyindex 5003d2e..ade33c3 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -148,7 +148,7 @@ def test_basic_formats_available_on_all_platforms(extension: str):def test_basic_read(audio_filename: str, samplerate: float):af = pedalboard.io.AudioFile(audio_filename)assert af.samplerate == samplerate- assert af.channels == 1+ assert af.num_channels == 1if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)else:@@ -172,12 +172,14 @@ def test_basic_read(audio_filename: str, samplerate: float):assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))assert f"samplerate={int(af.samplerate)}" in repr(af)- assert f"num_channels={af.channels}" in repr(af)+ assert f"num_channels={af.num_channels}" in repr(af)assert f"file_dtype={af.file_dtype}" in repr(af)af.seek(0)actual = af.read(af.frames)- expected = generate_sine_at(af.samplerate, num_channels=af.channels, num_seconds=af.duration)+ expected = generate_sine_at(+ af.samplerate, num_channels=af.num_channels, num_seconds=af.duration+ )# Crop the ends of the file, as lossy formats sometimes don't encode the whole file:actual = actual[:, : len(expected)]tolerance = get_tolerance_for_format_and_bit_depth(@@ -188,7 +190,7 @@ def test_basic_read(audio_filename: str, samplerate: float):af.close()with pytest.raises(RuntimeError):- af.channels+ af.num_channelswith pytest.raises(RuntimeError):af.read(1)@@ -207,7 +209,7 @@ def test_read_raw(audio_filename: str, samplerate: float):def test_use_reader_as_context_manager(audio_filename: str, samplerate: float):with pedalboard.io.AudioFile(audio_filename) as af:assert af.samplerate == samplerate- assert af.channels == 1+ assert af.num_channels == 1if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)else:@@ -231,10 +233,10 @@ def test_use_reader_as_context_manager(audio_filename: str, samplerate: float):assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))assert f"samplerate={int(af.samplerate)}" in repr(af)- assert f"num_channels={af.channels}" in repr(af)+ assert f"num_channels={af.num_channels}" in repr(af)with pytest.raises(RuntimeError):- af.channels+ af.num_channelswith pytest.raises(RuntimeError):af.read(1)@@ -257,7 +259,7 @@ def test_read_okay_without_extension(try:with pedalboard.io.AudioFile(dest_path) as af:assert af.samplerate == samplerate- assert af.channels == 1+ assert af.num_channels == 1except Exception:if ".mp3" in audio_filename:# Skip this test - due to a bug in JUCE's MP3 reader on Linux/Windows,@@ -286,7 +288,7 @@ def test_read_from_seekable_stream(audio_filename: str, samplerate: float):with af:assert af.samplerate == samplerate- assert af.channels == 1+ assert af.num_channels == 1if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)else:@@ -310,11 +312,11 @@ def test_read_from_seekable_stream(audio_filename: str, samplerate: float):assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))assert f"samplerate={int(af.samplerate)}" in repr(af)- assert f"num_channels={af.channels}" in repr(af)+ assert f"num_channels={af.num_channels}" in repr(af)assert repr(stream) in repr(af)with pytest.raises(RuntimeError):- af.channels+ af.num_channelswith pytest.raises(RuntimeError):af.read(1)@@ -521,7 +523,7 @@ def test_basic_write(assert os.path.getsize(filename) > 0with pedalboard.io.ReadableAudioFile(filename) as af:assert af.samplerate == samplerate- assert af.channels == num_channels+ assert af.num_channels == num_channelstolerance = get_tolerance_for_format_and_bit_depth(extension, input_format, af.file_dtype)as_written = af.read(num_samples)np.testing.assert_allclose(original_audio, np.squeeze(as_written), atol=tolerance)@@ -642,7 +644,7 @@ def test_write_to_seekable_stream(with pedalboard.io.AudioFile(stream) as af:assert af.samplerate == samplerate- assert af.channels == num_channels+ assert af.num_channels == num_channelstolerance = get_tolerance_for_format_and_bit_depth(extension, input_format, af.file_dtype)as_written = af.read(num_samples)np.testing.assert_allclose(original_audio, np.squeeze(as_written), atol=tolerance)