@@ -232,11 +232,12 @@ def test_read_mono_into_2d_out(file_mono_r):
232
232
233
233
234
234
def test_read_non_existing_file ():
235
- with pytest .raises (RuntimeError ) as excinfo :
235
+ with pytest .raises (sf . SoundFileError ) as excinfo :
236
236
sf .read ("i_do_not_exist.wav" )
237
237
assert "Error opening 'i_do_not_exist.wav'" in str (excinfo .value )
238
238
239
239
240
+
240
241
# -----------------------------------------------------------------------------
241
242
# Test write() function
242
243
# -----------------------------------------------------------------------------
@@ -418,7 +419,7 @@ def test_blocks_wplus(sf_stereo_wplus):
418
419
419
420
420
421
def test_blocks_write (sf_stereo_w ):
421
- with pytest .raises (RuntimeError ):
422
+ with pytest .raises (sf . SoundFileError ):
422
423
list (sf_stereo_w .blocks (blocksize = 2 ))
423
424
424
425
@@ -644,9 +645,9 @@ def test_seek_in_read_mode(sf_stereo_r):
644
645
assert sf_stereo_r .tell () == 2
645
646
assert sf_stereo_r .seek (2 , sf .SEEK_CUR ) == 4
646
647
assert sf_stereo_r .seek (- 2 , sf .SEEK_END ) == len (data_stereo ) - 2
647
- with pytest .raises (RuntimeError ):
648
+ with pytest .raises (sf . SoundFileError ):
648
649
sf_stereo_r .seek (666 )
649
- with pytest .raises (RuntimeError ):
650
+ with pytest .raises (sf . SoundFileError ):
650
651
sf_stereo_r .seek (- 666 )
651
652
652
653
@@ -685,8 +686,9 @@ def test_truncate(file_stereo_rplus, use_default):
685
686
else :
686
687
# file objects don't support truncate()
687
688
with sf .SoundFile (file_stereo_rplus , 'r+' , closefd = False ) as f :
688
- with pytest .raises (RuntimeError ) as excinfo :
689
+ with pytest .raises (sf . SoundFileError ) as excinfo :
689
690
f .truncate ()
691
+ assert isinstance (excinfo .value , RuntimeError )
690
692
assert "Error truncating" in str (excinfo .value )
691
693
692
694
@@ -696,7 +698,7 @@ def test_truncate(file_stereo_rplus, use_default):
696
698
697
699
698
700
def test_read_write_only (sf_stereo_w ):
699
- with pytest .raises (RuntimeError ):
701
+ with pytest .raises (sf . SoundFileError ):
700
702
sf_stereo_w .read (2 )
701
703
702
704
@@ -796,7 +798,7 @@ def test_buffer_read_into(sf_stereo_r):
796
798
797
799
798
800
def test_write_to_read_only_file_should_fail (sf_stereo_r ):
799
- with pytest .raises (RuntimeError ):
801
+ with pytest .raises (sf . SoundFileError ):
800
802
sf_stereo_r .write (data_stereo )
801
803
802
804
@@ -887,8 +889,9 @@ def test_closing_should_close_file(file_stereo_r):
887
889
def test_anything_on_closed_file (file_stereo_r ):
888
890
with sf .SoundFile (file_stereo_r ) as f :
889
891
pass
890
- with pytest .raises (RuntimeError ) as excinfo :
892
+ with pytest .raises (sf . SoundFileError ) as excinfo :
891
893
f .seek (0 )
894
+ assert isinstance (excinfo .value , RuntimeError )
892
895
assert "closed" in str (excinfo .value )
893
896
894
897
@@ -1014,8 +1017,9 @@ def test_write_non_seekable_file(file_w):
1014
1017
f .write (data_mono )
1015
1018
assert f .frames == len (data_mono )
1016
1019
1017
- with pytest .raises (RuntimeError ) as excinfo :
1020
+ with pytest .raises (sf . SoundFileError ) as excinfo :
1018
1021
f .seek (2 )
1022
+ assert isinstance (excinfo .value , RuntimeError )
1019
1023
assert "unseekable" in str (excinfo .value )
1020
1024
1021
1025
with sf .SoundFile (filename_new ) as f :
@@ -1026,8 +1030,9 @@ def test_write_non_seekable_file(file_w):
1026
1030
data = f .read (666 , dtype = 'int16' )
1027
1031
assert np .all (data == data_mono [3 :])
1028
1032
1029
- with pytest .raises (RuntimeError ) as excinfo :
1033
+ with pytest .raises (sf . SoundFileError ) as excinfo :
1030
1034
f .seek (2 )
1035
+ assert isinstance (excinfo .value , RuntimeError )
1031
1036
assert "unseekable" in str (excinfo .value )
1032
1037
1033
1038
with pytest .raises (ValueError ) as excinfo :
@@ -1041,3 +1046,32 @@ def test_write_non_seekable_file(file_w):
1041
1046
with pytest .raises (ValueError ) as excinfo :
1042
1047
sf .read (filename_new , start = 3 )
1043
1048
assert "start is only allowed for seekable files" in str (excinfo .value )
1049
+
1050
+
1051
+ # -----------------------------------------------------------------------------
1052
+ # Test LibsndfileError
1053
+ # -----------------------------------------------------------------------------
1054
+
1055
+ def test_libsndfile_error ():
1056
+ err = sf .LibsndfileError (2 )
1057
+ assert isinstance (err , sf .SoundFileError )
1058
+ assert err .error_string # cannot assume exact message generated by libsndfile
1059
+ assert str (err ) == err .error_string
1060
+
1061
+ def test_libsndfile_error_with_prefix ():
1062
+ err = sf .LibsndfileError (2 , prefix = "XX " )
1063
+ assert isinstance (err , sf .SoundFileError )
1064
+ assert err .error_string # cannot assume exact message generated by libsndfile
1065
+ assert str (err ) == "XX " + err .error_string
1066
+
1067
+ def test_rasing_libsndfile_error ():
1068
+ with pytest .raises (sf .LibsndfileError ) as excinfo :
1069
+ sf .read ("i_do_not_exist.wav" )
1070
+ assert isinstance (excinfo .value , sf .SoundFileError )
1071
+ assert isinstance (excinfo .value , RuntimeError )
1072
+ assert excinfo .value .code == 2 # SF_ERR_SYSTEM
1073
+ if sys .version_info [0 ] == 2 :
1074
+ assert isinstance (excinfo .value .error_string , unicode )
1075
+ else :
1076
+ assert isinstance (excinfo .value .error_string , str )
1077
+ assert excinfo .value .error_string in str (excinfo .value )
0 commit comments