Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearer error messages when write() is called with incorrectly formatted channels #411

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,11 @@ def _check_dtype(self, dtype):

def _array_io(self, action, array, frames):
"""Check array and call low-level IO function."""
if (array.ndim not in (1, 2) or
array.ndim == 1 and self.channels != 1 or
array.ndim == 2 and array.shape[1] != self.channels):
raise ValueError("Invalid shape: {0!r}".format(array.shape))
if self.channels not in (1,2):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong. Previously, we were checking if the input array had one or two dimensions. Now we're checking whether we have one or two channels. Those are different things, right?

We support arbitrary numbers of channels (depending on the file type), but only one- or two-dimensional data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood what the old code was doing but I see the problem now. Will fix

raise ValueError("Cannot convert: file has {0} channels, but only 1 or 2 is supported".format(self.channels))
array_channels = 1 if array.ndim == 1 else array.shape[1]
if array_channels != self.channels:
raise ValueError("Invalid shape: {0!r} (Expected {1} channels, got {2})".format(array.shape, self.channels, array_channels))
if not array.flags.c_contiguous:
raise ValueError("Data must be C-contiguous")
ctype = self._check_dtype(array.dtype.name)
Expand Down