Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Fix missing GIL around re-raised exceptions.

Peter Sobot53b91b850df8
diff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.h
index 9bfc541..5d77dbc 100644
--- a/pedalboard/io/PythonInputStream.h
+++ b/pedalboard/io/PythonInputStream.h
@@ -46,11 +46,12 @@ public:
}
bool isSeekable() noexcept {
+ py::gil_scoped_acquire acquire;
+
if (PythonException::isPending())
return false;
try {
- py::gil_scoped_acquire acquire;
return fileLike.attr("seekable")().cast<bool>();
} catch (py::error_already_set e) {
e.restore();
@@ -62,11 +63,11 @@ public:
}
juce::int64 getTotalLength() noexcept {
+ py::gil_scoped_acquire acquire;
+
if (PythonException::isPending())
return -1;
- py::gil_scoped_acquire acquire;
-
// TODO: Try reading a couple of Python properties that may contain the
// total length: urllib3.response.HTTPResponse provides `length_remaining`,
// for instance
@@ -147,6 +148,8 @@ public:
}
bool isExhausted() noexcept {
+ py::gil_scoped_acquire acquire;
+
if (PythonException::isPending())
return true;
@@ -155,7 +158,6 @@ public:
}
try {
- py::gil_scoped_acquire acquire;
return fileLike.attr("tell")().cast<juce::int64>() == getTotalLength();
} catch (py::error_already_set e) {
e.restore();
@@ -167,11 +169,12 @@ public:
}
juce::int64 getPosition() noexcept {
+ py::gil_scoped_acquire acquire;
+
if (PythonException::isPending())
return -1;
try {
- py::gil_scoped_acquire acquire;
return fileLike.attr("tell")().cast<juce::int64>();
} catch (py::error_already_set e) {
e.restore();
@@ -183,12 +186,12 @@ public:
}
bool setPosition(juce::int64 pos) noexcept {
+ py::gil_scoped_acquire acquire;
+
if (PythonException::isPending())
return false;
try {
- py::gil_scoped_acquire acquire;
-
if (fileLike.attr("seekable")().cast<bool>()) {
fileLike.attr("seek")(pos);
}
diff --git a/pedalboard/io/PythonOutputStream.h b/pedalboard/io/PythonOutputStream.h
index a4eb150..4bf86da 100644
--- a/pedalboard/io/PythonOutputStream.h
+++ b/pedalboard/io/PythonOutputStream.h
@@ -47,6 +47,7 @@ public:
virtual void flush() noexcept override {
py::gil_scoped_acquire acquire;
+
try {
if (py::hasattr(fileLike, "flush")) {
fileLike.attr("flush")();
@@ -91,11 +92,31 @@ public:
virtual bool writeRepeatedByte(juce::uint8 byte,
size_t numTimesToRepeat) noexcept override {
- // TODO: This could be made faster, but is it ever actually called?
- for (int i = 0; i < numTimesToRepeat; i++) {
- if (!write(&byte, 1)) {
- return false;
+ py::gil_scoped_acquire acquire;
+
+ try {
+ const size_t maxEffectiveSize =
+ std::min(numTimesToRepeat, 8192UL);
+ std::vector<char> buffer(maxEffectiveSize, byte);
+
+ 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>();
+
+ if (bytesWritten != chunkSize) {
+ return false;
+ }
}
+ } catch (py::error_already_set e) {
+ e.restore();
+ return false;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return false;
}
return true;
diff --git a/pedalboard/io/WriteableAudioFile.h b/pedalboard/io/WriteableAudioFile.h
index de745b3..97148e4 100644
--- a/pedalboard/io/WriteableAudioFile.h
+++ b/pedalboard/io/WriteableAudioFile.h
@@ -688,9 +688,9 @@ inline void init_writeable_audio_file(py::module &m) {
std::shared_ptr<WriteableAudioFile>>(
m, "WriteableAudioFile",
"An audio file writer interface, with native support for Ogg Vorbis, "
- "MP3, WAV, FLAC, and AIFF files on all operating systems. (Use "
- "pedalboard.io.get_supported_write_formats() to see which formats are "
- "supported on the current platform.)")
+ "WAV, FLAC, and AIFF files on all operating systems. (Use "
+ "pedalboard.io.get_supported_write_formats() to see which additional "
+ "formats are supported on the current platform.)")
.def(py::init([](std::string filename, double sampleRate, int numChannels,
int bitDepth,
std::optional<std::variant<std::string, float>> quality)