python-soundfile icon indicating copy to clipboard operation
python-soundfile copied to clipboard

SoundFile.__setattr__ raises error if value is empty string

Open sammlapp opened this issue 2 years ago • 2 comments
trafficstars

We can create a SoundFile object in 'r+' mode and update metadata fields. However, if the value of the field is an empty string, it raises an error.

I think the behavior should be to set the field to an empty string rather than to raise an error.

For example,

with soundfile.SoundFile(path, "r+") as s:
    s.__setattr__('date', '')

raises an error (that is not very helpful or easy to understand):

---------------------------------------------------------------------------
LibsndfileError                           Traceback (most recent call last)
Cell In[19], line 2
      1 with soundfile.SoundFile(path, "r+") as s:
----> 2     s.__setattr__('date','')

File ~/miniconda3/envs/opso090/lib/python3.9/site-packages/soundfile.py:731, in SoundFile.__setattr__(self, name, value)
    728     self._check_if_closed()
    729     err = _snd.sf_set_string(self._file, _str_types[name],
    730                              value.encode())
--> 731     _error_check(err)
    732 else:
    733     object.__setattr__(self, name, value)

File ~/miniconda3/envs/opso090/lib/python3.9/site-packages/soundfile.py:1407, in _error_check(err, prefix)
   1405 """Raise LibsndfileError if there is an error."""
   1406 if err != 0:
-> 1407     raise LibsndfileError(err, prefix=prefix)

LibsndfileError: Error : bad string.

sammlapp avatar May 05 '23 15:05 sammlapp

Thank you for raising the issue with libsndfile. They will have to define what a correct behavior should be here.

bastibe avatar May 06 '23 06:05 bastibe

I have not opened an issue on linbsndfile. I can't tell where the underlying libsndfile issue occurs, because I cannot trace the error back beyond this line: https://github.com/bastibe/python-soundfile/blob/0a8071d1b04c8e45b2465d05866994ec2df8b1f8/soundfile.py#L729

Specifically, the error occurs with _snd.sf_set_string, and _snd is defined with _snd = _ffi.dlopen(_full_path), and _fii is imported from _soundfile, a module which I cannot find anywhere.

This is why I asked about what the _soundfile import is.

sammlapp avatar May 06 '23 21:05 sammlapp